embeddedartistry / libmemory

Embedded systems memory management library. Implementations for malloc(), free(), and other useful memory management functions
https://embeddedartistry.com
MIT License
216 stars 44 forks source link

Provide a malloc_addblock() implementation for the libmemory hosted variant #73

Closed phillipjohnston closed 2 years ago

phillipjohnston commented 2 years ago

Split from #71

There is no malloc_addblock() in libmemory.a and thus one gets the missing reference error when following the current documentation, which talks a lot about that function but only of linking with libmemory.a. Of course in the case of libmemory.a in a hosted environment one does not strictly need that function and calling it makes not really sense - however, having an implementation anyway would make it possible to share the same code in the user application (and documentation examples, tests etc.) no matter which variant is linked eventually. One easy way to accomplish that would be to add something like:

diff --git a/src/posix_memalign.c b/src/posix_memalign.c
index 996a85e..fa30924 100644
--- a/src/posix_memalign.c
+++ b/src/posix_memalign.c
@@ -1,3 +1,4 @@
+#include <malloc.h>
 #include <aligned_malloc.h>
 #include <assert.h>
 #include <errno.h>
@@ -5,6 +6,10 @@

 #define IS_POWER_2(x) (!((x) & ((x)-1)))

+__attribute__((weak)) void malloc_addblock(__attribute__((unused)) void* addr, __attribute__((unused)) size_t size)
+{
+}
+
 int posix_memalign(void** __memptr, size_t __alignment, size_t __size)
 {
  int r = ENOMEM;

As I wrote initially, I would add a run-time warning there too to make sure the user understands what is going on (else this is easily missed I guess).

phillipjohnston commented 2 years ago

My current thought is to make this assert so that it fails if someone calls it.