[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH v5 4/4] 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> diff --git a/lib/ukalloc/alloc.c b/lib/ukalloc/alloc.c index 124f411..192613f 100644 --- a/lib/ukalloc/alloc.c +++ b/lib/ukalloc/alloc.c @@ -54,6 +54,7 @@ #include <uk/arch/limits.h> #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; @@ -254,6 +255,49 @@ int uk_posix_memalign_ifpages(struct uk_alloc *a, #endif +void uk_pfree_compat(struct uk_alloc *a, void *ptr, unsigned long num_pages __unused) { + UK_ASSERT(a != NULL); + + /* 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 379c17b..5611c05 100644 --- a/lib/ukalloc/include/uk/alloc_impl.h +++ b/lib/ukalloc/include/uk/alloc_impl.h @@ -65,9 +65,29 @@ void *uk_memalign_ifpages(struct uk_alloc *a, size_t align, size_t size); void uk_free_ifpages(struct uk_alloc *a, void *ptr); #endif /* CONFIG_LIBUKALLOC_IFPAGES */ -/* 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 + * realloc, memalign, palloc or pfree */ +#define uk_alloc_init_malloc(a, malloc_f, calloc_f, free_f, posix_memalign_f, addmem_f) \ + do { \ + (a)->malloc = (malloc_f); \ + (a)->calloc = (calloc_f); \ + (a)->realloc = uk_realloc_compat; \ + (a)->posix_memalign = (posix_memalign_f); \ + (a)->memalign = uk_memalign_compat; \ + (a)->free = (free_f); \ + (a)->palloc = uk_palloc_compat; \ + (a)->pfree = uk_pfree_compat; \ + (a)->addmem = (addmem_f); \ + \ + uk_alloc_register((a)); \ + } while (0) #if CONFIG_LIBUKALLOC_IFPAGES /* Shortcut for doing a registration of an allocator that only -- 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 |