[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH v7 5/5] lib/ukalloc: implement new allocator helpers
Add new allocator helpers uk_pfree_compat, uk_palloc_compat, uk_realloc_compat and uk_alloc_init_malloc allowing for proper initialization of allocators based on malloc, calloc, free and posix_memalign. Signed-off-by: Hugo Lefeuvre <hugo.lefeuvre@xxxxxxxxx> --- Changes v7: - fix style issues reported by checkpatch - update a few comments for clarity - change uk_alloc_init_malloc to rather be a helper that initializes the struct for allocators that just don't support palloc() and pfree(). diff --git a/lib/ukalloc/alloc.c b/lib/ukalloc/alloc.c index 13c71d5..1f22583 100644 --- a/lib/ukalloc/alloc.c +++ b/lib/ukalloc/alloc.c @@ -55,6 +55,7 @@ #define size_to_num_pages(size) \ (ALIGN_UP((unsigned long)(size), __PAGE_SIZE) / __PAGE_SIZE) +#define page_off(x) ((unsigned long)(x) & (__PAGE_SIZE - 1)) static struct uk_alloc *uk_alloc_head; @@ -286,6 +287,52 @@ int uk_posix_memalign_ifpages(struct uk_alloc *a, return 0; } +void uk_pfree_compat(struct uk_alloc *a, void *ptr, + unsigned long num_pages __unused) +{ + UK_ASSERT(a); + + /* if the object is not page aligned it was clearly not from us */ + UK_ASSERT(page_off(ptr) == 0); + + uk_free(a, ptr); +} + +void *uk_palloc_compat(struct uk_alloc *a, unsigned long num_pages) +{ + void *ptr; + + UK_ASSERT(a); + + if (uk_posix_memalign(a, &ptr, __PAGE_SIZE, num_pages * __PAGE_SIZE)) + return NULL; + + return ptr; +} + +void *uk_realloc_compat(struct uk_alloc *a, void *ptr, size_t size) +{ + void *retptr; + + UK_ASSERT(a); + if (!ptr) + return uk_malloc(a, size); + + if (ptr && !size) { + uk_free(a, ptr); + return NULL; + } + + retptr = uk_malloc(a, size); + if (!retptr) + return NULL; + + memcpy(retptr, ptr, size); + + uk_free(a, ptr); + return retptr; +} + void *uk_calloc_compat(struct uk_alloc *a, size_t nmemb, size_t size) { void *ptr; diff --git a/lib/ukalloc/exportsyms.uk b/lib/ukalloc/exportsyms.uk index c044705..2357501 100644 --- a/lib/ukalloc/exportsyms.uk +++ b/lib/ukalloc/exportsyms.uk @@ -7,3 +7,6 @@ uk_realloc_ifpages uk_posix_memalign_ifpages uk_calloc_compat uk_memalign_compat +uk_realloc_compat +uk_palloc_compat +uk_pfree_compat diff --git a/lib/ukalloc/include/uk/alloc_impl.h b/lib/ukalloc/include/uk/alloc_impl.h index f87feb3..8bcca94 100644 --- a/lib/ukalloc/include/uk/alloc_impl.h +++ b/lib/ukalloc/include/uk/alloc_impl.h @@ -62,12 +62,35 @@ int uk_posix_memalign_ifpages(struct uk_alloc *a, void **memptr, size_t align, size_t size); void uk_free_ifpages(struct uk_alloc *a, void *ptr); -/* Functionality that is provided based on malloc() */ +/* Functionality that is provided based on malloc() and posix_memalign() */ void *uk_calloc_compat(struct uk_alloc *a, size_t num, size_t len); +void *uk_realloc_compat(struct uk_alloc *a, void *ptr, size_t size); void *uk_memalign_compat(struct uk_alloc *a, size_t align, size_t len); +void *uk_palloc_compat(struct uk_alloc *a, unsigned long num_pages); +void uk_pfree_compat(struct uk_alloc *a, void *ptr, unsigned long num_pages); + +/* Shortcut for doing a registration of an allocator that does not implement + * palloc() or pfree() + */ +#define uk_alloc_init_malloc(a, malloc_f, calloc_f, realloc_f, free_f, \ + posix_memalign_f, memalign_f, addmem_f) \ + do { \ + (a)->malloc = (malloc_f); \ + (a)->calloc = (calloc_f); \ + (a)->realloc = (realloc_f); \ + (a)->posix_memalign = (posix_memalign_f); \ + (a)->memalign = (memalign_f); \ + (a)->free = (free_f); \ + (a)->palloc = uk_palloc_compat; \ + (a)->pfree = uk_pfree_compat; \ + (a)->addmem = (addmem_f); \ + \ + uk_alloc_register((a)); \ + } while (0) /* Shortcut for doing a registration of an allocator that only - * implements palloc(), pfree(), addmem() */ + * implements palloc(), pfree(), addmem() + */ #define uk_alloc_init_palloc(a, palloc_func, pfree_func, addmem_func) \ do { \ (a)->malloc = uk_malloc_ifpages; \ -- 2.7.4 _______________________________________________ Minios-devel mailing list Minios-devel@xxxxxxxxxxxxxxxxxxxx https://lists.xenproject.org/mailman/listinfo/minios-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |