]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/nolibc: Provide symbols for `malloc`, `free`, `calloc`, `realloc`
authorEduard Mihailescu <mihailescu.eduard@gmail.com>
Fri, 11 Aug 2023 08:27:01 +0000 (11:27 +0300)
committerRazvan Deaconescu <razvand@unikraft.io>
Fri, 20 Oct 2023 16:35:55 +0000 (19:35 +0300)
`posix_memalign` and `memalign`

To ensure that symbols such as malloc, calloc, and free are accessible
during link time for internal gcc objects, such as those related to gcov,
these functions must be designated as public.

As a side-effect of this change, unikraft loses the fine-grained stats
introduced in #229 when using the before-mentioned calls,
and instead each operation via the libc interface will be accounted
to `nolibc` instead to the individual libraries that made the calls.

Signed-off-by: Eduard-Florin Mihailescu <mihailescu.eduard@gmail.com>
iReviewed-by: Andra Paraschiv <andra@unikraft.org>
Reviewed-by: Radu Nichita <radunichita99@gmail.com>
Approved-by: Simon Kuenzer <simon@unikraft.io>
GitHub-Closes: #1042

lib/nolibc/include/stdlib.h
lib/nolibc/stdlib.c

index 5d75eaef12829c1a13f897d18f19270f0dc93162..6f79780969e738372b3992cc2a4b5410a3a39313 100644 (file)
@@ -63,58 +63,15 @@ int atoi(const char *s);
 long atol(const char *s);
 
 #if CONFIG_LIBUKALLOC
-/* Allocate size bytes of memory. Returns pointer to start of allocated memory,
- * or NULL on failure.
- */
-static inline void *malloc(size_t size)
-{
-       return uk_malloc(uk_alloc_get_default(), size);
-}
-
-/* Release memory previously allocated by malloc(). ptr must be a pointer
- * previously returned by malloc(), otherwise undefined behavior will occur.
- */
-static inline void free(void *ptr)
-{
-       return uk_free(uk_alloc_get_default(), ptr);
-}
-
-/* Allocate memory for an array of nmemb elements of size bytes. Returns
- * pointer to start of allocated memory, or NULL on failure.
- */
-static inline void *calloc(size_t nmemb, size_t size)
-{
-       return uk_calloc(uk_alloc_get_default(), nmemb, size);
-}
 
-/* Change the size of the memory block pointed to by ptr to size bytes.
- * Returns a pointer to the resized memory area. If the area pointed to was
- * moved, a free(ptr) is done.
- */
-static inline void *realloc(void *ptr, size_t size)
-{
-       return uk_realloc(uk_alloc_get_default(), ptr, size);
-}
-
-/* Allocate size bytes of memory, aligned to align bytes, and return the
- * pointer to that area in *memptr. Returns 0 on success, and a non-zero error
- * value on failure.
- */
-static inline int posix_memalign(void **memptr, size_t align, size_t size)
-{
-       return uk_posix_memalign(uk_alloc_get_default(),
-                                memptr, align, size);
-}
+void *malloc(size_t size);
+void free(void *ptr);
+void *calloc(size_t nmemb, size_t size);
+void *realloc(void *ptr, size_t size);
+int posix_memalign(void **memptr, size_t align, size_t size);
+void *memalign(size_t align, size_t size);
 
-/* Allocate size bytes of memory, aligned to align bytes. Returns pointer to
- * start of allocated memory, or NULL on failure.
- */
-static inline void *memalign(size_t align, size_t size)
-{
-       return uk_memalign(uk_alloc_get_default(), align, size);
-}
 #endif /* CONFIG_LIBUKALLOC */
-
 void abort(void) __noreturn;
 
 void exit(int status) __noreturn;
index 30963f659eb64157b6c069ce214ceee0faf14f10..5c26eb4b9504f4ffce0161061c413c0f6443447f 100644 (file)
 
 #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
 
+
+#if CONFIG_LIBUKALLOC
+/* Allocate size bytes of memory. Returns pointer to start of allocated memory,
+ * or NULL on failure.
+ */
+void *malloc(size_t size)
+{
+       return uk_malloc(uk_alloc_get_default(), size);
+}
+
+/* Release memory previously allocated by malloc(). ptr must be a pointer
+ * previously returned by malloc(), otherwise undefined behavior will occur.
+ */
+void free(void *ptr)
+{
+       return uk_free(uk_alloc_get_default(), ptr);
+}
+
+/* Allocate memory for an array of nmemb elements of size bytes. Returns
+ * pointer to start of allocated memory, or NULL on failure.
+ */
+void *calloc(size_t nmemb, size_t size)
+{
+       return uk_calloc(uk_alloc_get_default(), nmemb, size);
+}
+
+/* Change the size of the memory block pointed to by ptr to size bytes.
+ * Returns a pointer to the resized memory area. If the area pointed to was
+ * moved, a free(ptr) is done. If the call fails, the memory remains unchanged
+ */
+void *realloc(void *ptr, size_t size)
+{
+       return uk_realloc(uk_alloc_get_default(), ptr, size);
+}
+
+/* Allocate size bytes of memory, aligned to align bytes, and return the
+ * pointer to that area in *memptr. Returns 0 on success, and a non-zero error
+ * value on failure.
+ */
+int posix_memalign(void **memptr, size_t align, size_t size)
+{
+       return uk_posix_memalign(uk_alloc_get_default(),
+                                memptr, align, size);
+}
+
+/* Allocate size bytes of memory, aligned to align bytes. Returns pointer to
+ * start of allocated memory, or NULL on failure.
+ */
+void *memalign(size_t align, size_t size)
+{
+       return uk_memalign(uk_alloc_get_default(), align, size);
+}
+#endif /* CONFIG_LIBUKALLOC */
+
 /*
  * Convert a string to an unsigned long integer.
  *