Question

Threading Building Blocks includes a thread local allocator in the form of libtbbmalloc (and scalable_allocator). For Linux and Windows they also offer a libtbbmalloc_proxy that can replace malloc and new with scalable versions simply by linking against it.

The official docs omit any mention of libtbbmalloc_proxy on OSX, and it also does not appear in the lib directory of the distribution. What is the canonical way to get a scalable new on OSX?

Was it helpful?

Solution

Updated: Global replacement of memory allocation routines for OS X is added into Intel Threading Building Blocks v4.3. You can do the replacement either by loading libtbbmalloc_proxy.dylib at program load time using the DYLD_INSERT_LIBRARIES environment variable, or by linking the main executable file with the proxy library. See more details in the TBB User Guide. Prior to 4.3, this functionality was unavailable for OS X.


The canonical way to replace new is to define your own version of it. By the C++ standard, the following routines declared in <new> should be defined in the global namespace:

void* operator new(std::size_t size);
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
void operator delete(void* ptr) noexcept;
void operator delete(void* ptr, const std::nothrow_t&) noexcept;
void* operator new[](std::size_t size);
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
void operator delete[](void* ptr) noexcept;
void operator delete[](void* ptr, const std::nothrow_t&) noexcept;

By default, these functions are defined (implemented) in the language support library; but a program is allowed to redefine these functions. So you might implement these functions to use the TBB memory allocator, e.g.

void* operator new( std::size_t object_size )
{
    void* mem = scalable_malloc( object_size );
    if (mem==NULL) throw std::bad_alloc();
    return mem;
}
void* operator new(std::size_t object_size, const std::nothrow_t&) noexcept
{
    return scalable_malloc( object_size );
}

and etc.

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top