1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (C) 2019-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/utilities/heap_allocator.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/utilities/logger.h"
#include <algorithm>
namespace NEO {
bool operator<(const HeapChunk &hc1, const HeapChunk &hc2) {
return hc1.ptr < hc2.ptr;
}
uint64_t HeapAllocator::allocateWithCustomAlignmentWithStartAddressHint(const uint64_t requiredStartAddress, size_t &sizeToAllocate, size_t alignment) {
if (alignment < this->allocationAlignment) {
alignment = this->allocationAlignment;
}
UNRECOVERABLE_IF(alignment % allocationAlignment != 0); // custom alignment have to be a multiple of allocator alignment
sizeToAllocate = alignUp(sizeToAllocate, allocationAlignment);
uint64_t ptrReturn = 0llu;
{
std::lock_guard<std::mutex> lock(mtx);
DBG_LOG(LogAllocationMemoryPool, __FUNCTION__, "Allocator usage == ", this->getUsage());
if (availableSize < sizeToAllocate) {
return 0llu;
}
if (requiredStartAddress >= pLeftBound && requiredStartAddress <= pRightBound) {
const uint64_t misalignment = requiredStartAddress - pLeftBound;
if (pLeftBound + misalignment + sizeToAllocate <= pRightBound) {
if (misalignment) {
storeInFreedChunks(pLeftBound, static_cast<size_t>(misalignment), freedChunksBig);
pLeftBound += misalignment;
}
ptrReturn = pLeftBound;
pLeftBound += sizeToAllocate;
availableSize -= sizeToAllocate;
}
} else { // Try to find in freed chunks
defragment();
if (requiredStartAddress >= this->baseAddress && requiredStartAddress < this->pLeftBound) {
// If between baseAddress and pLeftBound, get from freedChunksBig
ptrReturn = getFromFreedChunksWithStartAddressHint(requiredStartAddress, sizeToAllocate, freedChunksBig);
} else {
// If between pRightBound and (baseAddress + size), get from freedChunksSmall
ptrReturn = getFromFreedChunksWithStartAddressHint(requiredStartAddress, sizeToAllocate, freedChunksSmall);
}
if (ptrReturn != 0llu) {
availableSize -= sizeToAllocate;
}
}
}
if (ptrReturn == 0llu) {
return allocateWithCustomAlignment(sizeToAllocate, alignment);
}
UNRECOVERABLE_IF(!isAligned(ptrReturn, alignment));
return ptrReturn;
}
uint64_t HeapAllocator::allocateWithCustomAlignment(size_t &sizeToAllocate, size_t alignment) {
if (alignment < this->allocationAlignment) {
alignment = this->allocationAlignment;
}
UNRECOVERABLE_IF(alignment % allocationAlignment != 0); // custom alignment have to be a multiple of allocator alignment
sizeToAllocate = alignUp(sizeToAllocate, allocationAlignment);
std::lock_guard<std::mutex> lock(mtx);
DBG_LOG(LogAllocationMemoryPool, __FUNCTION__, "Allocator usage == ", this->getUsage());
if (availableSize < sizeToAllocate) {
return 0llu;
}
std::vector<HeapChunk> &freedChunks = (sizeToAllocate > sizeThreshold) ? freedChunksBig : freedChunksSmall;
uint32_t defragmentCount = 0;
for (;;) {
uint64_t ptrReturn = 0llu;
if (sizeToAllocate > sizeThreshold) {
const uint64_t misalignment = alignUp(pLeftBound, alignment) - pLeftBound;
if (pLeftBound + misalignment + sizeToAllocate <= pRightBound) {
if (misalignment) {
storeInFreedChunks(pLeftBound, static_cast<size_t>(misalignment), freedChunks);
pLeftBound += misalignment;
}
ptrReturn = pLeftBound;
pLeftBound += sizeToAllocate;
}
} else {
const uint64_t pStart = pRightBound - sizeToAllocate;
const uint64_t misalignment = pStart - alignDown(pStart, alignment);
if (pLeftBound + sizeToAllocate + misalignment <= pRightBound) {
if (misalignment) {
pRightBound -= misalignment;
storeInFreedChunks(pRightBound, static_cast<size_t>(misalignment), freedChunks);
}
pRightBound -= sizeToAllocate;
ptrReturn = pRightBound;
}
}
size_t sizeOfFreedChunk = 0;
if (ptrReturn == 0llu) {
ptrReturn = getFromFreedChunks(sizeToAllocate, freedChunks, sizeOfFreedChunk, alignment);
}
if (ptrReturn != 0llu) {
if (sizeOfFreedChunk > 0) {
availableSize -= sizeOfFreedChunk;
sizeToAllocate = sizeOfFreedChunk;
} else {
availableSize -= sizeToAllocate;
}
UNRECOVERABLE_IF(!isAligned(ptrReturn, alignment));
return ptrReturn;
}
if (defragmentCount == 0) {
defragment();
defragmentCount++;
} else if (alignment > 2 * MemoryConstants::megaByte && pRightBound - pLeftBound >= sizeToAllocate) {
alignment = Math::prevPowerOfTwo(static_cast<size_t>(pRightBound - pLeftBound - 1 - sizeToAllocate + 2 * MemoryConstants::pageSize64k));
} else {
return 0llu;
}
}
}
void HeapAllocator::free(uint64_t ptr, size_t size) {
if (ptr == 0llu) {
return;
}
std::lock_guard<std::mutex> lock(mtx);
DBG_LOG(LogAllocationMemoryPool, __FUNCTION__, "Allocator usage == ", this->getUsage());
if (ptr == pRightBound) {
pRightBound = ptr + size;
mergeLastFreedSmall();
} else if (ptr == pLeftBound - size) {
pLeftBound = ptr;
mergeLastFreedBig();
} else if (ptr < pLeftBound) {
DEBUG_BREAK_IF(size <= sizeThreshold);
storeInFreedChunks(ptr, size, freedChunksBig);
} else {
storeInFreedChunks(ptr, size, freedChunksSmall);
}
availableSize += size;
}
NO_SANITIZE
double HeapAllocator::getUsage() const {
return static_cast<double>(size - availableSize) / size;
}
uint64_t HeapAllocator::getFromFreedChunksWithStartAddressHint(const uint64_t requiredStartAddress, size_t size, std::vector<HeapChunk> &freedChunks) {
for (size_t i = 0; i < freedChunks.size(); i++) {
uint64_t chunkStart = freedChunks[i].ptr;
uint64_t chunkEnd = chunkStart + freedChunks[i].size;
if (requiredStartAddress >= chunkStart && requiredStartAddress + size <= chunkEnd) {
size_t leadingSize = static_cast<size_t>(requiredStartAddress - chunkStart);
size_t trailingSize = static_cast<size_t>(chunkEnd - (requiredStartAddress + size));
// Chunk splitting
if (leadingSize > 0) {
freedChunks[i].size = leadingSize;
if (trailingSize > 0) {
freedChunks.emplace_back(requiredStartAddress + size, trailingSize);
}
} else {
if (trailingSize > 0) {
freedChunks[i].ptr = requiredStartAddress + size;
freedChunks[i].size = trailingSize;
} else {
freedChunks.erase(freedChunks.begin() + i);
}
}
return requiredStartAddress;
}
}
return 0llu;
}
uint64_t HeapAllocator::getFromFreedChunks(size_t size, std::vector<HeapChunk> &freedChunks, size_t &sizeOfFreedChunk, size_t requiredAlignment) {
size_t elements = freedChunks.size();
size_t bestFitIndex = -1;
size_t bestFitSize = 0;
sizeOfFreedChunk = 0;
for (size_t i = 0; i < elements; i++) {
const bool chunkAligned = isAligned(freedChunks[i].ptr, requiredAlignment);
if (!chunkAligned) {
continue;
}
if (freedChunks[i].size == size) {
auto ptr = freedChunks[i].ptr;
freedChunks.erase(freedChunks.begin() + i);
return ptr;
}
if (freedChunks[i].size > size) {
if (freedChunks[i].size < bestFitSize || bestFitSize == 0) {
bestFitIndex = i;
bestFitSize = freedChunks[i].size;
}
}
}
if (bestFitSize != 0) {
if (bestFitSize < (size << 1)) {
auto ptr = freedChunks[bestFitIndex].ptr;
sizeOfFreedChunk = freedChunks[bestFitIndex].size;
freedChunks.erase(freedChunks.begin() + bestFitIndex);
return ptr;
} else {
size_t sizeDelta = freedChunks[bestFitIndex].size - size;
DEBUG_BREAK_IF(!(size <= sizeThreshold || (size > sizeThreshold && sizeDelta > sizeThreshold)));
auto ptr = freedChunks[bestFitIndex].ptr + sizeDelta;
if (!isAligned(ptr, requiredAlignment)) {
auto alignedPtr = alignDown(ptr, requiredAlignment);
auto alignedDelta = ptr - alignedPtr;
sizeOfFreedChunk = size + static_cast<size_t>(alignedDelta);
freedChunks[bestFitIndex].size = sizeDelta - static_cast<size_t>(alignedDelta);
if (freedChunks[bestFitIndex].size == 0) {
freedChunks.erase(freedChunks.begin() + bestFitIndex);
}
return alignedPtr;
}
freedChunks[bestFitIndex].size = sizeDelta;
return ptr;
}
}
return 0llu;
}
void HeapAllocator::defragment() {
if (freedChunksSmall.size() > 1) {
std::sort(freedChunksSmall.rbegin(), freedChunksSmall.rend());
size_t maxSize = freedChunksSmall.size();
for (size_t i = maxSize - 1; i > 0; --i) {
auto ptr = freedChunksSmall[i].ptr;
size_t chunkSize = freedChunksSmall[i].size;
if (freedChunksSmall[i - 1].ptr == ptr + chunkSize) {
freedChunksSmall[i - 1].ptr = ptr;
freedChunksSmall[i - 1].size += chunkSize;
freedChunksSmall.erase(freedChunksSmall.begin() + i);
}
}
}
mergeLastFreedSmall();
if (freedChunksBig.size() > 1) {
std::sort(freedChunksBig.begin(), freedChunksBig.end());
size_t maxSize = freedChunksBig.size();
for (size_t i = maxSize - 1; i > 0; --i) {
auto ptr = freedChunksBig[i].ptr;
size_t chunkSize = freedChunksBig[i].size;
if ((freedChunksBig[i - 1].ptr + freedChunksBig[i - 1].size) == ptr) {
freedChunksBig[i - 1].size += chunkSize;
freedChunksBig.erase(freedChunksBig.begin() + i);
}
}
}
mergeLastFreedBig();
DBG_LOG(LogAllocationMemoryPool, __FUNCTION__, "Allocator usage == ", this->getUsage());
}
} // namespace NEO
|