Page 1 of 1

Heap Management

Posted: Wed Jul 19, 2017 11:37 am
by steved
I'm using some third-party code which uses malloc() and free() a lot, and would very much prefer not to modify the source.
From here, the default malloc() and free() are NOT thread-safe (I've not managed to find how they integrate with Chibi's heap management yet, although they appear to work!)
I've tried overriding both the re-entrant versions of the heap management (_malloc_r() etc) and the standard ones, but get 'duplicate definition' errors at link-time - this appears to be a 'well known' problem of newlib.
Has anyone else managed to achieve thread-safe heap management?

Re: Heap Management

Posted: Wed Jul 19, 2017 3:43 pm
by Giovanni
Is it possible to rework the code to use __malloc() and __free() instead? you could provide wrappers around the kernel heap allocator which is thread safe.

Giovanni

Re: Heap Management

Posted: Wed Jul 19, 2017 5:41 pm
by steved
Giovanni wrote:Is it possible to rework the code to use __malloc() and __free() instead?

That's exactly what I'm trying to avoid! As well as a large number of malloc() and free() calls, the code also uses strdup() and similar functions a lot; uncertain whether they'd also need rework.

I haven't found any definitive reference on __malloc() and __free() yet; from the few references I've found so far, they appear to usually resolve to malloc() and free(). Can you point me to any definition?

Giovanni wrote:you could provide wrappers around the kernel heap allocator which is thread safe.

I've done the wrappers; its linking them in that's causing issues. I'll probably pursue this route a bit further first.

Incidentally, a small point concerning free()/chHeapFree(). According to this, free(0) is legitimate coding, and does nothing. chHeapFree(0) gives an error if debugging is enabled. A difference which could catch people out.

Re: Heap Management

Posted: Wed Jul 19, 2017 6:14 pm
by Giovanni
Hi,

__malloc and __free are names I invented, I meant to redirect all calls to other functions that you would provide.

Giovanni

Re: Heap Management

Posted: Thu Jul 20, 2017 1:23 pm
by steved
Giovanni wrote:__malloc and __free are names I invented, I meant to redirect all calls to other functions that you would provide.

That explains it! From my searches, you're not the first to use those names.