Replaced the broken BBufferCache implementation.
According to the BeBook, it is NOT allowed to allocate one large pool, instead the memory blocks must be allocated individually. To achieve O(1) for both Save() and Get() function, only one list of free blocks is maintained. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4392 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+129
-149
@@ -1,173 +1,153 @@
|
|||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2002, OpenBeOS
|
* Copyright (c) 2003 Marcus Overhagen
|
||||||
//
|
*
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
// to deal in the Software without restriction, including without limitation
|
* to deal in the Software without restriction, including without limitation
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
//
|
*
|
||||||
// The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
// all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
//
|
*
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
* DEALINGS IN THE SOFTWARE.
|
||||||
//
|
*/
|
||||||
// File Name: BlockCache.cpp
|
|
||||||
// Author(s): Massimiliano Origgi
|
|
||||||
//
|
|
||||||
// Description: Handles a cache of memory blocks of the same size
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Standard Includes -----------------------------------------------------------
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
// System Includes -------------------------------------------------------------
|
|
||||||
#include <Autolock.h>
|
|
||||||
#include <BlockCache.h>
|
#include <BlockCache.h>
|
||||||
|
#include <Debug.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
#define MAGIC1 0x9183f4d9
|
||||||
|
#define MAGIC2 0xa6b3c87d
|
||||||
|
|
||||||
// Private functions
|
// The requirements set by the BeBook's description of the destructor,
|
||||||
static void *
|
// as well as Get() function, allowing the caller to dispose of the
|
||||||
object_alloc(size_t Size)
|
// 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
|
||||||
|
|
||||||
|
// XXX the BeBook describes the first parameter of the constructor as
|
||||||
|
// XXX "size_t count", while the original BeOS header uses "size_t cache_size"
|
||||||
|
// XXX this are very different meanings
|
||||||
|
|
||||||
|
BBlockCache::BBlockCache(size_t cacheSize,
|
||||||
|
size_t blockSize,
|
||||||
|
uint32 allocationType)
|
||||||
|
: fFreeList(0),
|
||||||
|
fBlockSize(blockSize),
|
||||||
|
fExcessBlocks(0),
|
||||||
|
fLocker(),
|
||||||
|
fAlloc(0),
|
||||||
|
fFree(0)
|
||||||
{
|
{
|
||||||
return (void *)(new char[Size]);
|
if (cacheSize < blockSize)
|
||||||
}
|
debugger("Error, you can't create a BBlockCache with cacheSize < blockSize\n");
|
||||||
|
|
||||||
|
switch (allocationType) {
|
||||||
static void
|
case B_OBJECT_CACHE:
|
||||||
object_free(void *Data)
|
fAlloc = &operator new[];
|
||||||
{
|
fFree = &operator delete[];
|
||||||
delete[] (int8*)Data;
|
break;
|
||||||
}
|
case B_MALLOC_CACHE:
|
||||||
|
fAlloc = &malloc;
|
||||||
|
fFree = &free;
|
||||||
static void*
|
break;
|
||||||
malloc_alloc(size_t Size)
|
default:
|
||||||
{
|
debugger("Error, you can't create a BBlockCache with undefined allocationType\n");
|
||||||
return malloc(Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
malloc_free(void *Data)
|
|
||||||
{
|
|
||||||
free(Data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Private structure for cache
|
|
||||||
struct _Block
|
|
||||||
{
|
|
||||||
_Block(void);
|
|
||||||
~_Block(void);
|
|
||||||
void *Data;
|
|
||||||
bool InUse;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
_Block::_Block(void) :
|
|
||||||
Data(NULL),
|
|
||||||
InUse(false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_Block::~_Block(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
BBlockCache::BBlockCache(size_t CacheSize, size_t BlockSize, uint32 Type)
|
|
||||||
: fCacheSize(CacheSize),
|
|
||||||
fBlockSize(BlockSize),
|
|
||||||
fMark(0)
|
|
||||||
{
|
|
||||||
// Setup function pointers based on Type
|
|
||||||
if(Type == B_OBJECT_CACHE) {
|
|
||||||
fAlloc = &object_alloc;
|
|
||||||
fFree = &object_free;
|
|
||||||
} else {
|
|
||||||
fAlloc = &malloc_alloc;
|
|
||||||
fFree = &malloc_free;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate cache
|
|
||||||
fCache = (void *)new _Block[CacheSize];
|
|
||||||
|
|
||||||
for(size_t i = 0; i < CacheSize; i++)
|
|
||||||
((_Block *)fCache)[i].Data = fAlloc(BlockSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// To properly maintain a list of free buffers, a buffer must be
|
||||||
|
// large enough to contain the _FreeBlock struct that is used.
|
||||||
|
if (blockSize < sizeof(_FreeBlock))
|
||||||
|
blockSize = sizeof(_FreeBlock);
|
||||||
|
|
||||||
|
// create blocks and put them into the free list
|
||||||
|
int count = cacheSize / blockSize;
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
_FreeBlock *block = reinterpret_cast<_FreeBlock *>(fAlloc(blockSize));
|
||||||
|
if (!block)
|
||||||
|
break;
|
||||||
|
block->next = fFreeList;
|
||||||
|
fFreeList = block;
|
||||||
|
DEBUG_ONLY(block->magic1 = MAGIC1);
|
||||||
|
DEBUG_ONLY(block->magic2 = MAGIC2 + (uint32)block->next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BBlockCache::~BBlockCache()
|
BBlockCache::~BBlockCache()
|
||||||
{
|
{
|
||||||
delete[] (_Block*)fCache;
|
// walk the free list and deallocate all blocks
|
||||||
|
fLocker.Lock();
|
||||||
|
while (fFreeList) {
|
||||||
|
ASSERT(fFreeList->magic1 == MAGIC1);
|
||||||
|
ASSERT(fFreeList->magic2 == MAGIC2 + (uint32)fFreeList->next);
|
||||||
|
void *pointer = fFreeList;
|
||||||
|
fFreeList = fFreeList->next;
|
||||||
|
DEBUG_ONLY(memset(pointer, 0xCC, sizeof(_FreeBlock)));
|
||||||
|
fFree(pointer);
|
||||||
|
}
|
||||||
|
fLocker.Unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
BBlockCache::Get(size_t BlockSize)
|
BBlockCache::Get(size_t blockSize)
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
if (!fLocker.Lock())
|
||||||
|
return 0;
|
||||||
if(BlockSize != fBlockSize)
|
void *pointer;
|
||||||
return fAlloc(BlockSize);
|
if (blockSize == fBlockSize && fFreeList != 0) {
|
||||||
|
// we can take a block from the list
|
||||||
_Block *block;
|
ASSERT(fFreeList->magic1 == MAGIC1);
|
||||||
for(size_t i = fMark; i < fCacheSize; i++) {
|
ASSERT(fFreeList->magic2 == MAGIC2 + (uint32)fFreeList->next);
|
||||||
block = &((_Block *)fCache)[i];
|
pointer = fFreeList;
|
||||||
if(block->InUse == false) {
|
fFreeList = fFreeList->next;
|
||||||
block->InUse = true;
|
DEBUG_ONLY(memset(pointer, 0xCC, sizeof(_FreeBlock)));
|
||||||
++fMark;
|
} else {
|
||||||
if(fMark == fCacheSize)
|
// we need to allocate a new block
|
||||||
fMark = 0;
|
if (blockSize == fBlockSize) {
|
||||||
return block->Data;
|
// we now have one more block than wanted
|
||||||
|
fExcessBlocks++;
|
||||||
}
|
}
|
||||||
|
if (blockSize < sizeof(_FreeBlock))
|
||||||
|
blockSize = sizeof(_FreeBlock);
|
||||||
|
pointer = fAlloc(blockSize);
|
||||||
|
DEBUG_ONLY(if (pointer) memset(pointer, 0xCC, sizeof(_FreeBlock)));
|
||||||
}
|
}
|
||||||
|
fLocker.Unlock();
|
||||||
if(fMark == 0)
|
return pointer;
|
||||||
return fAlloc(BlockSize);
|
|
||||||
|
|
||||||
for(size_t i = 0; i < fMark; i++) {
|
|
||||||
block = &((_Block *)fCache)[i];
|
|
||||||
if(block->InUse == false) {
|
|
||||||
block->InUse = true;
|
|
||||||
++fMark;
|
|
||||||
if(fMark == fCacheSize)
|
|
||||||
fMark = 0;
|
|
||||||
return block->Data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fAlloc(BlockSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
void
|
BBlockCache::Save(void *pointer, size_t blockSize)
|
||||||
BBlockCache::Save(void *Data, size_t BlockSize)
|
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
if (!fLocker.Lock())
|
||||||
|
|
||||||
if(BlockSize != fBlockSize) {
|
|
||||||
fFree(Data);
|
|
||||||
return;
|
return;
|
||||||
}
|
if (blockSize == fBlockSize && fExcessBlocks <= 0) {
|
||||||
|
// the block needs to be returned to the cache
|
||||||
_Block *block;
|
_FreeBlock *block = reinterpret_cast<_FreeBlock *>(pointer);
|
||||||
for(size_t i = 0; i < fCacheSize; i++) {
|
block->next = fFreeList;
|
||||||
block = &((_Block *)fCache)[i];
|
fFreeList = block;
|
||||||
if(block->Data == Data) {
|
DEBUG_ONLY(block->magic1 = MAGIC1);
|
||||||
block->InUse = false;
|
DEBUG_ONLY(block->magic2 = MAGIC2 + (uint32)block->next);
|
||||||
fMark = i;
|
} else {
|
||||||
return;
|
// the block needs to be deallocated
|
||||||
|
if (blockSize == fBlockSize) {
|
||||||
|
fExcessBlocks--;
|
||||||
|
ASSERT(fExcessBlocks >= 0);
|
||||||
}
|
}
|
||||||
|
DEBUG_ONLY(memset(pointer, 0xCC, sizeof(_FreeBlock)));
|
||||||
|
fFree(pointer);
|
||||||
}
|
}
|
||||||
fFree(Data);
|
fLocker.Unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user