关于 loki 的内存分配 SmallObj.cpp,该如何解决
关于 loki 的内存分配 SmallObj.cpp
void Chunk::Deallocate(void* p, std::size_t blockSize)
00372 {
00373 assert(p > = pData_);
00374
00375 unsigned char* toRelease = static_cast <unsigned char*> (p);
00376 // Alignment check
00377 assert((toRelease - pData_) % blockSize == 0);
00378 unsigned char index = static_cast < unsigned char > (
00379 ( toRelease - pData_ ) / blockSize);
00380
00381 #if defined(DEBUG) || defined(_DEBUG)
00382 // Check if block was already deleted. Attempting to delete the same
00383 // block more than once causes Chunk 's linked-list of stealth indexes to
00384 // become corrupt. And causes count of blocksAvailable_ to be wrong.
00385 if ( 0 < blocksAvailable_ )
00386 assert( firstAvailableBlock_ != index );
00387 #endif
00388
00389 *toRelease = firstAvailableBlock_;
00390 firstAvailableBlock_ = index;
00391 // Truncation check
00392 assert(firstAvailableBlock_ == (toRelease - pData_) / blockSize);
00393
00394 ++blocksAvailable_;
00395 }
====================我觉得这段程序不能起作用。index 其实同 firstAvaulableBlock是同一个值,没有效果。
------解决方案--------------------
我有个版本的Loki中关于这段的代码中没有index。这儿的index是归还内存后链表头的索引,而firstAvailableBlock_是未归还内存前链表头的索引。
------解决方案--------------------
知道思路就可以了。
void Chunk::Deallocate(void* p, std::size_t blockSize)
00372 {
00373 assert(p > = pData_);
00374
00375 unsigned char* toRelease = static_cast <unsigned char*> (p);
00376 // Alignment check
00377 assert((toRelease - pData_) % blockSize == 0);
00378 unsigned char index = static_cast < unsigned char > (
00379 ( toRelease - pData_ ) / blockSize);
00380
00381 #if defined(DEBUG) || defined(_DEBUG)
00382 // Check if block was already deleted. Attempting to delete the same
00383 // block more than once causes Chunk 's linked-list of stealth indexes to
00384 // become corrupt. And causes count of blocksAvailable_ to be wrong.
00385 if ( 0 < blocksAvailable_ )
00386 assert( firstAvailableBlock_ != index );
00387 #endif
00388
00389 *toRelease = firstAvailableBlock_;
00390 firstAvailableBlock_ = index;
00391 // Truncation check
00392 assert(firstAvailableBlock_ == (toRelease - pData_) / blockSize);
00393
00394 ++blocksAvailable_;
00395 }
====================我觉得这段程序不能起作用。index 其实同 firstAvaulableBlock是同一个值,没有效果。
------解决方案--------------------
我有个版本的Loki中关于这段的代码中没有index。这儿的index是归还内存后链表头的索引,而firstAvailableBlock_是未归还内存前链表头的索引。
------解决方案--------------------
知道思路就可以了。