| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
// A memory arena class implementation. |
| 10 |
|
| 11 |
// NOTE: Object requiring dword alignment is NOT supported. |
| 12 |
|
| 13 |
#ifndef _ARENA_HPP_ |
| 14 |
#define _ARENA_HPP_ |
| 15 |
|
| 16 |
#include <assert.h> |
| 17 |
#include <iostream> |
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
// #include "Option.h" |
| 21 |
|
| 22 |
// #define COLLECT_ALLOCATION_STATS |
| 23 |
|
| 24 |
#ifdef COLLECT_ALLOCATION_STATS |
| 25 |
extern int numAllocations; |
| 26 |
extern int numMallocCalls; |
| 27 |
extern int totalAllocSize; |
| 28 |
extern int totalMallocSize; |
| 29 |
extern int numMemManagers; |
| 30 |
extern int maxArenaLength; |
| 31 |
#endif |
| 32 |
|
| 33 |
namespace iga { |
| 34 |
class ArenaHeader { |
| 35 |
friend class ArenaManager; |
| 36 |
|
| 37 |
public: |
| 38 |
static size_t WordAlign(size_t addr) { return (addr + 0x3) & ~0x3; } |
| 39 |
|
| 40 |
static size_t GetArenaSize(size_t dataSize) { |
| 41 |
return WordAlign(sizeof(ArenaHeader)) + dataSize; |
| 42 |
} |
| 43 |
|
| 44 |
unsigned char *GetArenaData() const { |
| 45 |
assert(WordAlign(size_t(this)) == size_t(this)); |
| 46 |
return (unsigned char *)(WordAlign(size_t(this) + sizeof(ArenaHeader))); |
| 47 |
} |
| 48 |
|
| 49 |
void *operator new(size_t, unsigned char *memory) { return memory; } |
| 50 |
|
| 51 |
void operator delete(void *, unsigned char *) { |
| 52 |
// Do nothing |
| 53 |
} |
| 54 |
|
| 55 |
private: |
| 56 |
ArenaHeader(size_t dataSize, ArenaHeader * /* nextArena */) |
| 57 |
: _nextArena(nullptr) { |
| 58 |
_nextByte = GetArenaData(); |
| 59 |
_lastByte = _nextByte + dataSize; |
| 60 |
assert(((unsigned char *)(this) + GetArenaSize(dataSize)) == _lastByte); |
| 61 |
} |
| 62 |
|
| 63 |
~ArenaHeader() { |
| 64 |
_nextByte = _lastByte = 0; |
| 65 |
_nextArena = 0; |
| 66 |
} |
| 67 |
|
| 68 |
ArenaHeader(const ArenaHeader&) = delete; |
| 69 |
ArenaHeader& operator=(const ArenaHeader&) = delete; |
| 70 |
|
| 71 |
void *AllocSpace(size_t size); |
| 72 |
|
| 73 |
ArenaHeader *_nextArena; // Word aligned |
| 74 |
unsigned char *_nextByte; // Char aligned |
| 75 |
unsigned char *_lastByte; // Char aligned |
| 76 |
}; |
| 77 |
|
| 78 |
class ArenaManager { |
| 79 |
friend class MemManager; |
| 80 |
|
| 81 |
private: |
| 82 |
ArenaManager(size_t defaultArenaSize) |
| 83 |
: _arenas(0), _defaultArenaSize(defaultArenaSize) { |
| 84 |
CreateArena(_defaultArenaSize); |
| 85 |
} |
| 86 |
|
| 87 |
~ArenaManager() { FreeArenas(); } |
| 88 |
ArenaManager(const ArenaManager&) = delete; |
| 89 |
ArenaManager& operator=(const ArenaManager&) = delete; |
| 90 |
|
| 91 |
void *AllocDataSpace(size_t size) { |
| 92 |
// Do separate memory allocations of debugMemAlloc is set, to allow |
| 93 |
// valgrind/drmemory to find more buffer over-reads/writes |
| 94 |
#if !defined(NDEBUG) && defined(IGA_DEBUG_MEM_ALLOC) |
| 95 |
return size == 0 ? 0 : malloc(size); |
| 96 |
#endif |
| 97 |
void *space = nullptr; |
| 98 |
|
| 99 |
if (size) { |
| 100 |
space = _arenas->AllocSpace(size); |
| 101 |
|
| 102 |
if (space == 0) { |
| 103 |
CreateArena(size); |
| 104 |
space = _arenas->AllocSpace(size); |
| 105 |
} |
| 106 |
|
| 107 |
assert(space); |
| 108 |
} |
| 109 |
|
| 110 |
#ifdef COLLECT_ALLOCATION_STATS |
| 111 |
numAllocations++; |
| 112 |
totalAllocSize += size; |
| 113 |
#endif |
| 114 |
|
| 115 |
return space; |
| 116 |
} |
| 117 |
|
| 118 |
ArenaHeader *CreateArena(size_t size) { |
| 119 |
size_t arenaDataSize = |
| 120 |
(size > _defaultArenaSize) ? size : _defaultArenaSize; |
| 121 |
arenaDataSize = ArenaHeader::WordAlign(arenaDataSize); |
| 122 |
unsigned char *arena = |
| 123 |
new unsigned char[ArenaHeader::GetArenaSize(arenaDataSize)]; |
| 124 |
ArenaHeader *newArena = new (arena) ArenaHeader(arenaDataSize, _arenas); |
| 125 |
// Add new arena to the head of queue |
| 126 |
if (_arenas != NULL) { |
| 127 |
newArena->_nextArena = _arenas; |
| 128 |
} |
| 129 |
|
| 130 |
// std::cout << "Create new Buffer: " << (arenaDataSize / 1024) << " KB" << |
| 131 |
// std::endl; |
| 132 |
_arenas = newArena; |
| 133 |
|
| 134 |
#ifdef COLLECT_ALLOCATION_STATS |
| 135 |
numMallocCalls++; |
| 136 |
totalMallocSize += arenaDataSize; |
| 137 |
int numArenas = 0; |
| 138 |
for (ArenaHeader *tmpArena = _arenas; tmpArena != NULL; |
| 139 |
tmpArena = tmpArena->_nextArena) { |
| 140 |
numArenas++; |
| 141 |
} |
| 142 |
if (numArenas > maxArenaLength) { |
| 143 |
maxArenaLength = numArenas; |
| 144 |
} |
| 145 |
if (numArenas == 1) { |
| 146 |
numMemManagers++; |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
return _arenas; |
| 151 |
} |
| 152 |
|
| 153 |
void FreeArenas(); |
| 154 |
|
| 155 |
// Data |
| 156 |
|
| 157 |
ArenaHeader *_arenas; |
| 158 |
const size_t _defaultArenaSize; |
| 159 |
}; |
| 160 |
} // namespace iga |
| 161 |
#endif |