Код: Выделить всё
const size_t remainingBytes = allocatedBlock->rawBlockSize - (bestFitLayout.rawExclusiveEndAddress - bestFitLayout.rawStartAddress);
Ниже приведен код, и я отметил проблемную строку с комментарием. Также я включил изображения с точными значениями, которые появились в отладчике, которые вызвали недостаток и также некоторые дополнительные изображения, которые показывают точные используемые структуры данных.
Код: Выделить всё
void* TlsfAllocator::allocate(size_t size)
{
TlsfBlockHeader* allocatedBlock = getFreeBlock(size);
if (allocatedBlock == nullptr) return nullptr;// allocation failed
const size_t rawStartAddress =( reinterpret_cast(allocatedBlock)-(allocatedBlock->rawOffset));
const size_t rawEndAddress = rawStartAddress + allocatedBlock->rawBlockSize;
const Layout bestFitLayout = calculateLayout(rawStartAddress, size);
/** The calculation done below is the one causing the problem!!! **/
const size_t remainingBytes = allocatedBlock->rawBlockSize - (bestFitLayout.rawExclusiveEndAddress - bestFitLayout.rawStartAddress);
< /code>
Структура данных tlsfblockheader: < /p>
struct TlsfBlockHeader
{
/*******WARNING:
The header is offset from the raw block start address to ensure proper alignment of Header if raw block start address is not a Header aligned address.
This offset is indicated by the rawOffset field in the header.
ie. The address of Header may not be same as the start address of the raw block.
*******/
TlsfBlockHeader* nextFreeBlock = nullptr; // Pointer to the next block in the free list
TlsfBlockHeader* prevFreeBlock = nullptr; // Pointer to the previous block in the free list
size_t rawOffset = 0; // Offset from the start of the raw block to the header,this offset is there because of the Tlsf header alignment requirements.
size_t rawBlockSize = 0; // Size of the block, including header and footer and all padding
size_t UserAreaSize = 0; //Size of the area usable by the user
bool isFree = false;
};
< /code>
Структура данных макета: < /p>
struct Layout
{
size_t rawStartAddress = 0;
size_t paddingHeader = 0;
size_t HeaderStartAddress = 0; // Address of the TlsfBlockHeader(INCLUSIVE)
size_t userAreaStartAddress = 0; // Start address of the user area(INCLUSIVE)
size_t userAreaExclusiveEnd = 0;
size_t paddingFooter = 0; // Padding after the user area, if any
size_t footerStartAddress = 0; // Address of the TlsfBlockFooter(INCLUSIVE)
size_t rawExclusiveEndAddress = 0;
};
Tlsf_header Структура данных
Структура данных макета
Подробнее здесь: https://stackoverflow.com/questions/797 ... ead-of-zer