Check in per Jeremy Rand: Adjusted implementation to behave like that in R5 -- no more tracking of excess blocks.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4523 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2003-09-07 11:55:25 +00:00
parent bfdb37cc03
commit 369eef1343
2 changed files with 9 additions and 17 deletions

View File

@ -67,11 +67,12 @@ private:
_FreeBlock *fFreeList;
size_t fBlockSize;
int32 fExcessBlocks;
int32 fFreeBlocks;
int32 fBlockCount;
BLocker fLocker;
void * (*fAlloc)(size_t size);
void (*fFree)(void *pointer);
uint32 _reserved[3];
uint32 _reserved[2];
};
#endif /* _BLOCK_CACHE_H */

View File

@ -33,16 +33,15 @@
// as well as Get() function, allowing the caller to dispose of the
// memory, do not allow to allocate one large block to be used as pool.
// Thus we need to create multiple small ones.
// We maintain a list of free blocks. If more blocks with a size of blockSize
// are allocated than available, the variable fExcessBlocks is used to avoid
// growing the pool of free buffers
// We maintain a list of free blocks.
BBlockCache::BBlockCache(uint32 blockCount,
size_t blockSize,
uint32 allocationType)
: fFreeList(0),
fBlockSize(blockSize),
fExcessBlocks(0),
fFreeBlocks(blockCount),
fBlockCount(blockCount),
fLocker("some BBlockCache lock"),
fAlloc(0),
fFree(0)
@ -107,13 +106,9 @@ BBlockCache::Get(size_t blockSize)
ASSERT(fFreeList->magic2 == MAGIC2 + (uint32)fFreeList->next);
pointer = fFreeList;
fFreeList = fFreeList->next;
fFreeBlocks--;
DEBUG_ONLY(memset(pointer, 0xCC, sizeof(_FreeBlock)));
} else {
// we need to allocate a new block
if (blockSize == fBlockSize) {
// we now have one more block than wanted
fExcessBlocks++;
}
if (blockSize < sizeof(_FreeBlock))
blockSize = sizeof(_FreeBlock);
pointer = fAlloc(blockSize);
@ -128,19 +123,15 @@ BBlockCache::Save(void *pointer, size_t blockSize)
{
if (!fLocker.Lock())
return;
if (blockSize == fBlockSize && fExcessBlocks <= 0) {
if (blockSize == fBlockSize && fFreeBlocks < fBlockCount) {
// the block needs to be returned to the cache
_FreeBlock *block = reinterpret_cast<_FreeBlock *>(pointer);
block->next = fFreeList;
fFreeList = block;
fFreeBlocks++;
DEBUG_ONLY(block->magic1 = MAGIC1);
DEBUG_ONLY(block->magic2 = MAGIC2 + (uint32)block->next);
} else {
// the block needs to be deallocated
if (blockSize == fBlockSize) {
fExcessBlocks--;
ASSERT(fExcessBlocks >= 0);
}
DEBUG_ONLY(memset(pointer, 0xCC, sizeof(_FreeBlock)));
fFree(pointer);
}