malloc vs scalable_malloc

malloc vs scalable_malloc

HejI have seen "malloc" as kind of replacement for "new" to create some heap memory and return a pointer to it (though I still wonder why would one need to use malloc instead of new), but I wonder what TBB "scalable_malloc" brings different from "malloc". And what situation it best fits to use "scalable_malloc" ?

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The principal difference between malloc and new, or between scalable malloc and overloaded new with scalable new is the "new" methodcan include an object constructor (your code to be executed upon allocation). Similar with the return of memory using free and delete, or scalable_free and scalable delete (overloade delete) and its ability to have an object destructor (your code).

The principal differences between the non-scalable variants and the scalable variants are

non-scalable multi-threaded heap (malloc/new, free/delete) uses critical sections.
whereas scalable methods reduce the frequency of entering critical sections. In some cases thereduction (ratio of with critical section to without critical section) aproacheszero.

malloc often has less overhead than new (although optimizing compilers may make this a moot point).
The advantage for new/delete even for objects that do not require a constructor is the ability to use anencapsulation object which holds the POD (Plane Old Data) and then when the encaptulation object exits scope that your dtor can delete the encapsulated object. What this means is should a function using such an encapsulation object contain multiple exit points (returns), then you do not have to maintain multiple clean-up sections of code (a common place for error of omission). Note, the encapsulate object can use either malloc/new and free/delete.

Jim Dempsey

www.quickthreadprogramming.com

...I still wonder why would one need to use malloc instead of new...

To improveperformance! Becauseconstructor and destructor arenot called,memory allocated anywayand all object attributes could beinitialized later. Icalled it Delayed ObjectInitialization ( DOI ).

So, I use that trick with a calloc CRT-function and a C++ template, like:

...
m_ppvRs = ( RTvoid ** )( TStrassenHBCResultSet< RTint > ** )calloc(
m_uiNumOfPartitions,
sizeof( TStrassenHBCResultSet< RTint > * ) );
if( m_ppvRs == RTnull )
return ( RTbool )bOk;
...

and, by default a block ofmemory is initialized to 0x0s by calloc and I don't need to do anything!

Best regards,
Sergey

Leave a Comment

Please sign in to add a comment. Not a member? Join today