Heap Management

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Heap Management

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Heap Management

Post 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
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Heap Management

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Heap Management

Post by Giovanni »

Hi,

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

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Heap Management

Post 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.
Post Reply