File size: 4,842 Bytes
0c51b93 |
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 |
/* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited. */
#include "NvCoFreeList.h"
#include <stdlib.h>
#include <string.h>
#define DEFAULT_ALIGNMENT 16
namespace nvidia {
namespace Common {
FreeList::~FreeList()
{
_deallocateBlocks(m_activeBlocks);
_deallocateBlocks(m_freeBlocks);
}
void FreeList::_init()
{
m_top = nullptr;
m_end = nullptr;
m_activeBlocks = nullptr;
m_freeBlocks = nullptr;
m_freeElements = nullptr;
m_elementSize = 0;
m_alignment = 1;
m_blockSize = 0;
m_blockAllocationSize = 0;
//m_allocator = nullptr;
}
void FreeList::_init(size_t elementSize, size_t alignment, size_t elemsPerBlock)
{
//allocator = allocator ? allocator : MemoryAllocator::getInstance();
//assert(allocator);
//m_allocator = allocator;
alignment = (alignment < sizeof(void*)) ? sizeof(void*) : alignment;
// Alignment must be a power of 2
assert(((alignment - 1) & alignment) == 0);
// The elementSize must at least be
elementSize = (elementSize >= alignment) ? elementSize : alignment;
m_blockSize = elementSize * elemsPerBlock;
m_elementSize = elementSize;
m_alignment = alignment;
// Calculate the block size neeed, correcting for alignment
const size_t alignedBlockSize = (alignment <= DEFAULT_ALIGNMENT) ?
_calcAlignedBlockSize(DEFAULT_ALIGNMENT) :
_calcAlignedBlockSize(alignment);
// Make the block struct size aligned
m_blockAllocationSize = m_blockSize + alignedBlockSize;
m_top = nullptr;
m_end = nullptr;
m_activeBlocks = nullptr;
m_freeBlocks = nullptr; ///< Blocks that there are no allocations in
m_freeElements = nullptr;
}
void FreeList::init(size_t elementSize, size_t alignment, size_t elemsPerBlock)
{
_deallocateBlocks(m_activeBlocks);
_deallocateBlocks(m_freeBlocks);
_init(elementSize, alignment, elemsPerBlock);
}
void FreeList::_deallocateBlocks(Block* block)
{
while (block)
{
Block* next = block->m_next;
#ifdef NV_CO_FREE_LIST_INIT_MEM
Memory::set(block, 0xfd, m_blockAllocationSize);
#endif
free(block);
block = next;
}
}
bool FreeList::isValidAllocation(const void* dataIn) const
{
uint8_t* data = (uint8_t*)dataIn;
Block* block = m_activeBlocks;
while (block)
{
uint8_t* start = block->m_data;
uint8_t* end = start + m_blockSize;
if (data >= start && data < end)
{
// Check it's aligned correctly
if ((data - start) % m_elementSize)
{
return false;
}
// Non allocated data is between top and end
if (data >= m_top && data < m_end)
{
return false;
}
// It can't be in the free list
Element* ele = m_freeElements;
while (ele)
{
if (ele == (Element*)data)
{
return false;
}
ele = ele->m_next;
}
return true;
}
block = block->m_next;
}
// It's not in an active block -> it cannot be a valid allocation
return false;
}
void* FreeList::_allocate()
{
Block* block = m_freeBlocks;
if (block)
{
/// Remove from the free blocks
m_freeBlocks = block->m_next;
}
else
{
block = (Block*)malloc(m_blockAllocationSize);
if (!block)
{
// Allocation failed... doh
return nullptr;
}
// Do the alignment
{
size_t fix = (size_t(block) + sizeof(Block) + m_alignment - 1) & ~(m_alignment - 1);
block->m_data = (uint8_t*)fix;
}
}
// Attach to the active blocks
block->m_next = m_activeBlocks;
m_activeBlocks = block;
// Set up top and end
m_end = block->m_data + m_blockSize;
// Return the first element
uint8_t* element = block->m_data;
m_top = element + m_elementSize;
NV_CO_FREE_LIST_INIT_ALLOCATE(element)
return element;
}
void FreeList::deallocateAll()
{
Block* block = m_activeBlocks;
if (block)
{
// Find the end block
while (block->m_next)
{
#ifdef NV_CO_FREE_LIST_INIT_MEM
Memory::set(block->m_data, 0xfd, m_blockSize);
#endif
block = block->m_next;
}
// Attach to the freeblocks
block->m_next = m_freeBlocks;
// The list is now all freelists
m_freeBlocks = m_activeBlocks;
// There are no active blocks
m_activeBlocks = nullptr;
}
m_top = nullptr;
m_end = nullptr;
}
void FreeList::reset()
{
_deallocateBlocks(m_activeBlocks);
_deallocateBlocks(m_freeBlocks);
m_top = nullptr;
m_end = nullptr;
m_activeBlocks = nullptr;
m_freeBlocks = nullptr;
m_freeElements = nullptr;
}
void FreeList::_initAllocate(void* mem)
{
memset(mem, 0xcd, m_elementSize);
}
void FreeList::_initDeallocate(void* mem)
{
memset(mem, 0xfd, m_elementSize);
}
} // namespace Common
} // namespace nvidia
|