Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

Dynamic allocator replacement on OS X* with Intel® TBB

$
0
0

The Intel® Threading Building Blocks (Intel® TBB) library provides an alternative way to dynamically allocate memory - Intel TBB scalable allocator (tbbmalloc). Its purpose is to provide better performance and scalability for memory allocation/deallocation operations in multithreaded applications, compared to the default allocator.

There are two general ways to employ Intel TBB scalable allocator in your application:

  1. Explicitly specifying TBB scalable allocator in source code, either by using memory allocation routines (like “scalable_malloc”) or by specifying Intel TBB scalable allocator for containers:
#include <tbb/scalable_allocator.h>
std::vector<int, tbb::scalable_allocator <int> >;
  1. Automatic replacing of all calls to standard functions for dynamic memory allocation (such as malloc) with the Intel TBB scalable equivalents. This option was introduced in Intel TBB 4.3

One way to do the automatic replacement is to link the main executable file with the Intel TBB malloc proxy library:

clang++ foo.o bar.o -ltbbmalloc_proxy -o a.out

Another way does not even require re-building, so you can provide a new memory allocator to the same binary. This is done by loading the malloc proxy library at an application start time using the DYLD_INSERT_LIBRARIES environment variable:

DYLD_INSERT_LIBRARIES=libtbbmalloc_proxy.dylib

In OS X, simple loading libraries with DYLD_INSERT_LIBRARIESrequires using flat namespaces in order to access the shared library symbols. If an application was built with two-level namespaces, this will not work, and forcing usage of flat namespaces may lead to a crash.

Intel TBB overcomes this problem in a smart way. When libtbbmalloc_proxy library is loaded into the process, its static constructor is called and registers a “malloc zone” for TBB memory allocation routines. This allows redirecting memory allocation routine calls from a standard C++ library into TBB scalable allocator routines. So the application doesn’t need to use TBB malloc library symbols, it continues to call standard “libc” routines, thus there are no problems with name spaces. Also, OS X “malloc zones” mechanism allows applications to have several memory allocators (e.g. used by different libraries) and manage memory correctly. It guarantees that Intel TBB will use the same allocator for allocations and deallocations. It is a safeguard against crashes due to calling a deallocation routine for a memory object allocated from another allocator.

Additional links:

Intel TBB: Memory Allocation
Intel TBB documentation: dynamic memory interface replacement on OS X
Intel TBB documentation: Memory Allocation reference

 


Viewing all articles
Browse latest Browse all 3384

Trending Articles