7 assert(blockSize > 0 && blocks >0);
8 const std::size_t totalSize= blockSize*blocks;
9 assert( totalSize / blockSize == blocks);
10 fData =
static_cast<unsigned char*
> (
operator new(totalSize) );
11 Reset( blockSize, blocks );
16 assert(blockSize > 0 && blocks > 0);
20 for (
unsigned char * p = fData; i != blocks; p += blockSize )
25 assert( fData != NULL );
26 ::operator
delete ( fData );
31 if ( IsFilled() )
return NULL;
33 assert((fFirstAvailable * blockSize) / blockSize == fFirstAvailable);
35 unsigned char* result = fData + (fFirstAvailable * blockSize);
36 fFirstAvailable = *result;
46 unsigned char* toRelease =
static_cast<unsigned char*
>(p);
48 assert((toRelease - fData) % blockSize == 0);
49 unsigned char index =
static_cast< unsigned char >(
50 ( toRelease - fData ) / blockSize);
53 *toRelease = fFirstAvailable;
54 fFirstAvailable = index;
56 assert(fFirstAvailable == (toRelease - fData) / blockSize);
63 if ( numBlocks < fNAvail )
73 unsigned char index = fFirstAvailable;
74 if ( numBlocks <= index )
88 std::bitset< 256 > foundBlocks;
89 unsigned char * nextBlock = NULL;
117 for (
unsigned char cc = 0; ; )
119 nextBlock = fData + ( index * blockSize );
120 foundBlocks.set( index,
true );
126 if ( numBlocks <= index )
135 if ( foundBlocks.test( index ) )
146 if ( foundBlocks.count() != fNAvail )
161 std::size_t blockSize )
const
168 unsigned char * place =
static_cast< unsigned char *
>( p );
170 assert( ( place - fData ) % blockSize == 0 );
171 unsigned char blockIndex =
static_cast< unsigned char >(
172 ( place - fData ) / blockSize );
174 unsigned char index = fFirstAvailable;
175 assert( numBlocks > index );
176 if ( index == blockIndex )
182 std::bitset< 256 > foundBlocks;
183 unsigned char * nextBlock = NULL;
184 for (
unsigned char cc = 0; ; )
186 nextBlock = fData + ( index * blockSize );
187 foundBlocks.set( index,
true );
193 if ( index == blockIndex )
195 assert( numBlocks > index );
196 assert( !foundBlocks.test( index ) );
bool IsCorrupt(unsigned char numBlocks, std::size_t blockSize, bool checkIndexes) const
void * Allocate(std::size_t blocks)
Allocates out a number of additional blocks.
void Reset(std::size_t blockSize, unsigned char blocks)
Each time a chunk is reused, it needs to be reset.
bool Init(std::size_t size, unsigned char n)
void Deallocate(void *p, std::size_t blocks)
DeAllocates a number of blocks starting at pointer.
void Release()
Deallocates all blocks in chunck.
bool IsBlockAvailable(void *p, unsigned char numBlocks, std::size_t blockSize) const
Checks the block starting at p and for p+numBlocks*blockSize checks to see if they're used...