ChunkAllocatorBase (struct)¶
This is a low-level, base allocator class that doesn’t contain the allocated values, but rather manages the indices. It provides memory-stable storage: an allocated value won’t move in memory. Allocated values are identified with a ChA_ID structure, which can convert relatively efficiently from and to an absolute UInt32 index.
This allocator is intended to be used when many items are expected to be allocated, as it takes some memory even when empty (about 400 bytes). Item IDs are encoded as chunk/index pairs and take 32 bits (ChA_ID).
Allocated chunk sizes are 64, then 128, then 256, until 2^27 for the last 12 chunks. The maximum number of stored entries is 1610612672.
Since there is no template in KL, to allocate values for a specific type, you need to copy the following and replace “MyType” with the type you want to allocate with:
struct MyTypeAllocator : ChunkAllocatorBase { MyType[32][] values;//NOTE: important that this is exactly [32]. };
inline ChA_ID MyTypeAllocator.allocate!() { Boolean resizeNewChunkThenCallPostChunkAllocate; ChA_ID ID = this.allocate_internal( resizeNewChunkThenCallPostChunkAllocate ); if( resizeNewChunkThenCallPostChunkAllocate ) { this.values[this.nextChunk].resize(this.nextChunkSize); this.postChunkAllocate(); } return ID; }
Then, use the following to allocate and access a value:
MyTypeAllocator alloc; // allocate a value ChA_ID id = alloc.allocate();
// use the allocated value: alloc.values[id.chunk][id.index] = MyType();
// release the value alloc.release( id );
Members¶
ChA_ID[] | freeIds | |
UInt32 | lastFreeID | |
UInt8 | nextChunk | |
UInt32 | nextChunkSize | |
UInt32 | nextChunkSubIndex | |
LightLock | lock |
Methods¶
ChunkAllocatorBase ( in ChunkAllocatorBase other ) | |
ChunkAllocatorBase () | |
ChA_ID | allocate_internal ! ( io Boolean resizeNewChunkThenCallPostChunkAllocate ) |
ChunkAllocatorBase | clone ? () |
Boolean | isInRange ? ( in ChA_ID ID ) |
postChunkAllocate ! () | |
release ! ( in ChA_ID ID ) | |
UInt32 | totalAllocated ? () |
UInt32 | totalUsed ? () |
Methods in detail¶
ChunkAllocatorBase ( in ChunkAllocatorBase other )
copy constructor
default constructor
ChA_ID ChunkAllocatorBase.allocate_internal! ( io Boolean resizeNewChunkThenCallPostChunkAllocate )
ChunkAllocatorBase ChunkAllocatorBase.clone? ()
clone method
Boolean ChunkAllocatorBase.isInRange? ( in ChA_ID ID )
ChunkAllocatorBase.postChunkAllocate! ()
ChunkAllocatorBase.release! ( in ChA_ID ID )
UInt32 ChunkAllocatorBase.totalAllocated? ()
UInt32 ChunkAllocatorBase.totalUsed? ()