[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH v6 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>

diff --git a/lib/ukalloc/alloc.c b/lib/ukalloc/alloc.c
index 0e8c8d5..07d0553 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;
 
@@ -277,6 +278,49 @@ 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 != 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 f87feb3..9d5581a 100644
--- a/lib/ukalloc/include/uk/alloc_impl.h
+++ b/lib/ukalloc/include/uk/alloc_impl.h
@@ -62,9 +62,29 @@ 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
+ * 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)
 
 /* Shortcut for doing a registration of an allocator that only
  * implements palloc(), pfree(), addmem() */
-- 
2.7.4


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.