|
1 | 1 | Memory Pool
|
2 | 2 | ==========
|
3 | 3 |
|
4 |
| -This is a template class implementation of a memory pool allocator that is very simple to use and extremely fast with minimal overhead for each allocation/deallocation. The provided class is mostly compliant with the C++ Standard Library with a few exceptions (see C++ Compliance for details). |
| 4 | +This is a template class implementation of a memory pool allocator that is very simple to use and extremely fast with minimal overhead for each allocation/deallocation. The provided class is mostly compliant with the C++ Standard Library with a few exceptions (see [C++ Compliance](#c-compliance) for details). |
5 | 5 |
|
6 | 6 | This library uses variadic templates for perfect argument forwarding and some other optimizations, and thus requires C++11 features. I may be willing to implement a C++98 version if somebody really needs it ( [send me an email ](mailto:[email protected]) if you are interested).
|
7 | 7 |
|
@@ -31,7 +31,7 @@ Put `MemoryPool.h` and `MemoryPool.tcc` into your project folder and include `Me
|
31 | 31 | template <typename T, size_t BlockSize = 4096>
|
32 | 32 | ```
|
33 | 33 |
|
34 |
| -Here, `T` is the type of the objects you want to allocate and `BlockSize` is the size of the chunks in bytes the allocator will ask from the system (see Picking BlockSize for more information). `T` can be any object, while `BlockSize` needs to be at least twice the size of `T`. After that, you create an instance of `MemoryPool` class and use it just like a standard allocator object. Here is an example: |
| 34 | +Here, `T` is the type of the objects you want to allocate and `BlockSize` is the size of the chunks MemoryPool allocates (see [Picking BlockSize] (#picking-blocksize) for more information). `T` can be any object, while `BlockSize` needs to be at least twice the size of `T`. After that, you create an instance of `MemoryPool` class and use it just like a standard allocator object. Here is an example: |
35 | 35 | ```C++
|
36 | 36 | #include <iostream>
|
37 | 37 | #include "MemoryPool.h"
|
@@ -75,7 +75,11 @@ More examples are provided with the code.
|
75 | 75 |
|
76 | 76 | Picking BlockSize
|
77 | 77 | -------------------------
|
78 |
| -`BlockSize` is the size of the chunks in bytes the allocator will ask from the system. Picking the correct `BlockSize` (the second, optional, template parameter) is essential for good performance. I suggest you pick a power of two, which may decrease memory fragmentation depending on your system. Also, make sure that `BlockSize` is at least several hundred times larger than the size of `T` for maximum performance. The idea is, the greater the `BlockSize`, the less calls to `malloc` the library will make. However, picking a size too big might increase memory usage unnecessarily and actually decrease the performance because `malloc` may need to make many system calls. For objects that contain several pointers, the default size of 4096 bytes should be good. If you need bigger object, you may need to time your code with larger sizes for `BlockSize`. |
| 78 | +`BlockSize` is the size of the chunks in bytes the allocator will ask from the system. It has to be large enough to contain at least two pointers or two `T` objects, depending on which is bigger. |
| 79 | + |
| 80 | +Picking the correct `BlockSize` is essential for good performance. I suggest you pick a power of two, which may decrease memory fragmentation depending on your system. Also, make sure that `BlockSize` is at least several hundred times larger than the size of `T` for maximum performance. The idea is, the greater the `BlockSize`, the less calls to `malloc` the library will make. However, picking a size too big might increase memory usage unnecessarily and actually decrease the performance because `malloc` may need to make many system calls. |
| 81 | + |
| 82 | +For objects that contain several pointers, the default size of 4096 bytes should be good. If you need bigger object, you may need to time your code with larger sizes and see what works best. Unless you will be maintaining many MemoryPool objects, I do not think you need to go smaller than 4096 bytes. Though if you are working on a more limited platform (that has a copiler with C++11 support), you may need to go for smaller values. |
79 | 83 |
|
80 | 84 | About the Code
|
81 | 85 | -------------------------
|
|
0 commit comments