Constructor
| 38 | |
| 39 | // Constructor |
| 40 | PoolAllocator::PoolAllocator(MemoryAllocator& baseAllocator) : mBaseAllocator(baseAllocator) { |
| 41 | |
| 42 | // Allocate some memory to manage the blocks |
| 43 | mNbAllocatedMemoryBlocks = 64; |
| 44 | mNbCurrentMemoryBlocks = 0; |
| 45 | const size_t sizeToAllocate = mNbAllocatedMemoryBlocks * sizeof(MemoryBlock); |
| 46 | mMemoryBlocks = static_cast<MemoryBlock*>(baseAllocator.allocate(sizeToAllocate)); |
| 47 | memset(mMemoryBlocks, 0, sizeToAllocate); |
| 48 | memset(mFreeMemoryUnits, 0, sizeof(mFreeMemoryUnits)); |
| 49 | |
| 50 | #ifndef NDEBUG |
| 51 | mNbTimesAllocateMethodCalled = 0; |
| 52 | #endif |
| 53 | |
| 54 | // If the mMapSizeToHeapIndex has not been initialized yet |
| 55 | if (!isMapSizeToHeadIndexInitialized) { |
| 56 | |
| 57 | // Initialize the array that contains the sizes of the memory units that will |
| 58 | // be allocated in each different heap |
| 59 | for (uint i=0; i < NB_HEAPS; i++) { |
| 60 | mUnitSizes[i] = (i+1) * MIN_UNIT_SIZE; |
| 61 | } |
| 62 | |
| 63 | // Initialize the lookup table that maps the size to allocated to the |
| 64 | // corresponding heap we will use for the allocation |
| 65 | uint j = 0; |
| 66 | mMapSizeToHeapIndex[0] = -1; // This element should not be used |
| 67 | for (uint i=1; i <= MAX_UNIT_SIZE; i++) { |
| 68 | if (i <= mUnitSizes[j]) { |
| 69 | mMapSizeToHeapIndex[i] = j; |
| 70 | } |
| 71 | else { |
| 72 | j++; |
| 73 | mMapSizeToHeapIndex[i] = j; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | isMapSizeToHeadIndexInitialized = true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Destructor |
| 82 | PoolAllocator::~PoolAllocator() { |