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?
Heap Management
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Heap Management
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
Giovanni
Re: Heap Management
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.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Heap Management
Hi,
__malloc and __free are names I invented, I meant to redirect all calls to other functions that you would provide.
Giovanni
__malloc and __free are names I invented, I meant to redirect all calls to other functions that you would provide.
Giovanni
Re: Heap Management
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.