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