I have qustion about chunk class in SmallObj.
void* Chunk::Allocate(std::size_t blockSize)
if ( IsFilled() ) return NULL;
assert((firstAvailableBlock_ * blockSize) / blockSize ==
[b]unsigned char * pResult = pData_ + (firstAvailableBlock_ * blockSize);
firstAvailableBlock_ = *pResult; //and if next block was allready allocated?!!![/b]
// Chunk::Deallocate ----------------------------------------------------------
void Chunk::Deallocate(void* p, std::size_t blockSize)
assert(p >= pData_);
unsigned char* toRelease = static_cast<unsigned char*>(p);
// Alignment check
assert((toRelease - pData_) % blockSize == 0);
unsigned char index = static_cast< unsigned char >(
( toRelease - pData_ ) / blockSize);
#if defined(DEBUG) || defined(_DEBUG)
// Check if block was already deleted. Attempting to delete the same
// block more than once causes Chunk's linked-list of stealth indexes to
// become corrupt. And causes count of blocksAvailable_ to be wrong.
if ( 0 < blocksAvailable_ )
assert( firstAvailableBlock_ != index );
*toRelease = firstAvailableBlock_;
[b] firstAvailableBlock_ = index; //OK AND What about free blocks before this???[/b]
// Truncation check
assert(firstAvailableBlock_ == (toRelease - pData_) / blockSize);
We dont check if next block is already allocated, we can allocate block more tha once without deallocating it.
Example allocating 2 blocks:
For example we have allocated 8 blocks of char(it's not mater)
Then we deallocated blocks with index 6, 2, 4, 8
last index will be 8! (as last deallocate)
More than while we reach end of blocks we have 3 blocks available and will have errors of allocating from this stage.
ps Maybe i some thing dont understand so can you help me
Log in to post a comment.