AandD - 2011-12-05

Greetings,
I have qustion about chunk class in SmallObj.

Chunk::Allocate/Deallocate funtions.

void* Chunk::Allocate(std::size_t blockSize)
{
    if ( IsFilled() ) return NULL;
    assert((firstAvailableBlock_ * blockSize) / blockSize == 
        firstAvailableBlock_);
    [b]unsigned char * pResult = pData_ + (firstAvailableBlock_ * blockSize);
    firstAvailableBlock_ = *pResult; //and if next block was allready allocated?!!![/b]
    --blocksAvailable_;
    return pResult;
}
// 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 );
#endif
    *toRelease = firstAvailableBlock_;
   [b] firstAvailableBlock_ = index; //OK AND What about free blocks before this???[/b]
    // Truncation check
    assert(firstAvailableBlock_ == (toRelease - pData_) / blockSize);
    ++blocksAvailable_;
}

Problem 1)
We dont check if next block is already allocated, we can allocate block more tha once without deallocating it.
Example allocating 2 blocks:

Problem 2)
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
loki 0.1.7