1.. _Memory_Allocation: 2 3Memory Allocation 4================= 5 6 7|full_name| provides several memory 8allocator templates that are similar to the STL template class 9std::allocator. Two templates, ``scalable_allocator<T>`` and 10``cache_aligned_allocator<T>``, address critical issues in parallel 11programming as follows: 12 13 14- **Scalability**. Problems of scalability arise when using memory 15 allocators originally designed for serial programs, on threads that 16 might have to compete for a single shared pool in a way that allows 17 only one thread to allocate at a time. 18 19 20 Use the ``scalable_allocator<T>`` template to avoid scalability 21 bottlenecks. This template can improve the performance of programs 22 that rapidly allocate and free memory. 23 24 25- **False sharing**. Problems of sharing arise when two threads access 26 different words that share the same cache line. The problem is that a 27 cache line is the unit of information interchange between processor 28 caches. If one processor modifies a cache line and another processor 29 reads the same cache line, the line must be moved from one processor 30 to the other, even if the two processors are dealing with different 31 words within the line. False sharing can hurt performance because 32 cache lines can take hundreds of clocks to move. 33 34 35 Use the ``cache_aligned_allocator<T>`` template to always allocate on 36 a separate cache line. Two objects allocated by 37 ``cache_aligned_allocator`` are guaranteed to not have false sharing. 38 However, if an object is allocated by ``cache_aligned_allocator`` and 39 another object is allocated some other way, there is no guarantee. 40 41 42You can use these allocator templates as the *allocator* argument to STL 43template classes.The following code shows how to declare an STL vector 44that uses ``cache_aligned_allocator``\ for allocation: 45 46 47:: 48 49 50 std::vector<int,cache_aligned_allocator<int> >; 51 52 53.. tip:: 54 The functionality of ``cache_aligned_allocator<T>`` comes at some 55 cost in space, because it must allocate at least one cache line’s 56 worth of memory, even for a small object. So use 57 ``cache_aligned_allocator<T>`` only if false sharing is likely to be 58 a real problem. 59 60 61The scalable memory allocator also provides a set of functions 62equivalent to the C standard library memory management routines but has 63the ``scalable_`` prefix in their names, as well as the way to easily 64redirect the standard routines to these functions. 65 66.. toctree:: 67 :maxdepth: 4 68 69 ../tbb_userguide/Which_Dynamic_Libraries_to_Use 70 ../tbb_userguide/Allocator_Configuration 71 ../tbb_userguide/automatically-replacing-malloc 72