[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH v2 3/3] 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 v2: - define uk_alloc_order_to_size as a macro instead of static inline - use __unused instead of __attribute__((unused)) - define _compat helpers outside of #if CONFIG_LIBUKALLOC_IFPAGES block diff --git a/lib/ukalloc/alloc.c b/lib/ukalloc/alloc.c index aa87274..fc416dc 100644 --- a/lib/ukalloc/alloc.c +++ b/lib/ukalloc/alloc.c @@ -53,6 +53,9 @@ #include <uk/assert.h> #include <uk/arch/limits.h> +#define page_off(x) ((unsigned long)(x) & (__PAGE_SIZE - 1)) +#define uk_alloc_order_to_size(order) ((__PAGE_SIZE) << (size_t)(order)) + static struct uk_alloc *uk_alloc_head; int uk_alloc_register(struct uk_alloc *a) @@ -267,6 +270,53 @@ int uk_posix_memalign_ifpages(struct uk_alloc *a, #endif +void uk_pfree_compat(struct uk_alloc *a, void *ptr, size_t order __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, size_t order) { + void *ptr; + size_t size; + + UK_ASSERT(a); + if (!order) + return NULL; + + size = uk_alloc_order_to_size(order); + if (uk_posix_memalign(a, &ptr, __PAGE_SIZE, 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..e8afd60 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, size_t order); +void uk_pfree_compat(struct uk_alloc *a, void *ptr, size_t order); + +/* 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 |