Heap Management

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Heap Management

Postby steved » Wed Jul 19, 2017 11:37 am

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: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Heap Management

Postby Giovanni » Wed Jul 19, 2017 3:43 pm

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: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Heap Management

Postby steved » Wed Jul 19, 2017 5:41 pm

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: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Heap Management

Postby Giovanni » Wed Jul 19, 2017 6:14 pm

Hi,

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

Giovanni

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Heap Management

Postby steved » Thu Jul 20, 2017 1:23 pm

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.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 19 guests