test_slab: moved the Backend independent HashCacheStrategy operations to BaseHashCacheStrategy so we don't end up with multiple instantiations of the same code.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20826 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -312,24 +312,21 @@ Fls(size_t value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Backend>
|
struct BaseHashCacheStrategy {
|
||||||
struct HashCacheStrategy : BaseCacheStrategy<Backend> {
|
|
||||||
typedef typename BaseCacheStrategy<Backend>::Slab Slab;
|
|
||||||
typedef HashCacheStrategy<Backend> Strategy;
|
|
||||||
typedef BaseCache::ObjectLink ObjectLink;
|
typedef BaseCache::ObjectLink ObjectLink;
|
||||||
typedef BaseCache::ObjectInfo ObjectInfo;
|
typedef BaseCache::ObjectInfo ObjectInfo;
|
||||||
|
|
||||||
struct Link : ObjectLink, HashTableLink<Link> {
|
struct Link : ObjectLink, HashTableLink<Link> {
|
||||||
Slab *slab;
|
BaseCache::Slab *slab;
|
||||||
void *buffer;
|
void *buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HashTableDefinition {
|
struct HashTableDefinition {
|
||||||
typedef Strategy ParentType;
|
typedef BaseHashCacheStrategy ParentType;
|
||||||
typedef void * KeyType;
|
typedef void * KeyType;
|
||||||
typedef Link ValueType;
|
typedef Link ValueType;
|
||||||
|
|
||||||
HashTableDefinition(Strategy *_parent) : parent(_parent) {}
|
HashTableDefinition(BaseHashCacheStrategy *_parent) : parent(_parent) {}
|
||||||
|
|
||||||
size_t HashKey(void *key) const
|
size_t HashKey(void *key) const
|
||||||
{
|
{
|
||||||
@@ -345,7 +342,7 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend> {
|
|||||||
|
|
||||||
HashTableLink<Link> *GetLink(Link *value) const { return value; }
|
HashTableLink<Link> *GetLink(Link *value) const { return value; }
|
||||||
|
|
||||||
Strategy *parent;
|
BaseHashCacheStrategy *parent;
|
||||||
};
|
};
|
||||||
|
|
||||||
// for g++ 2.95
|
// for g++ 2.95
|
||||||
@@ -353,15 +350,8 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend> {
|
|||||||
|
|
||||||
typedef OpenHashTable<HashTableDefinition> HashTable;
|
typedef OpenHashTable<HashTableDefinition> HashTable;
|
||||||
|
|
||||||
HashCacheStrategy(BaseCache *parent)
|
BaseHashCacheStrategy(BaseCache *parent)
|
||||||
: BaseCacheStrategy<Backend>(parent), fHashTable(this),
|
: fHashTable(this), fLowerBoundary(Fls(parent->ObjectSize()) - 1) {}
|
||||||
fSlabCache("slab cache", 0), fLinkCache("link cache", 0),
|
|
||||||
fLowerBoundary(Fls(parent->ObjectSize()) - 1) {}
|
|
||||||
|
|
||||||
static size_t RequiredSpace(size_t objectSize)
|
|
||||||
{
|
|
||||||
return objectSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *Object(ObjectLink *link) const
|
void *Object(ObjectLink *link) const
|
||||||
{
|
{
|
||||||
@@ -374,6 +364,36 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend> {
|
|||||||
return ObjectInfo(link->slab, link);
|
return ObjectInfo(link->slab, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Link *_Linkage(void *object) const
|
||||||
|
{
|
||||||
|
return fHashTable.Lookup(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ObjectLink *_Linkage(void *_this, void *object)
|
||||||
|
{
|
||||||
|
return ((BaseHashCacheStrategy *)_this)->_Linkage(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
HashTable fHashTable;
|
||||||
|
const size_t fLowerBoundary;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Backend>
|
||||||
|
struct HashCacheStrategy : BaseCacheStrategy<Backend>, BaseHashCacheStrategy {
|
||||||
|
typedef typename BaseCacheStrategy<Backend>::Slab Slab;
|
||||||
|
typedef HashCacheStrategy<Backend> Strategy;
|
||||||
|
|
||||||
|
HashCacheStrategy(BaseCache *parent)
|
||||||
|
: BaseCacheStrategy<Backend>(parent), BaseHashCacheStrategy(parent),
|
||||||
|
fSlabCache("slab cache", 0), fLinkCache("link cache", 0) {}
|
||||||
|
|
||||||
|
static size_t RequiredSpace(size_t objectSize)
|
||||||
|
{
|
||||||
|
return objectSize;
|
||||||
|
}
|
||||||
|
|
||||||
BaseCache::Slab *NewSlab(uint32_t flags)
|
BaseCache::Slab *NewSlab(uint32_t flags)
|
||||||
{
|
{
|
||||||
size_t byteCount = _SlabSize();
|
size_t byteCount = _SlabSize();
|
||||||
@@ -389,31 +409,21 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend> {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *data = (uint8_t *)pages;
|
if (_PrepareSlab(fParent, slab, pages, byteCount, flags) < B_OK) {
|
||||||
for (uint8_t *it = data;
|
Backend::FreePages(fParent, slab->id);
|
||||||
it < (data + byteCount); it += fParent->ObjectSize()) {
|
fSlabCache.Free(slab);
|
||||||
Link *link = fLinkCache.Alloc(flags);
|
return NULL;
|
||||||
link->slab = slab;
|
|
||||||
link->buffer = it;
|
|
||||||
fHashTable.Insert(link);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it's very important that we cast this to BaseHashCacheStrategy
|
||||||
|
// so we get the proper instance offset through void *
|
||||||
return BaseCacheStrategy<Backend>::_ConstructSlab(slab, pages, 0,
|
return BaseCacheStrategy<Backend>::_ConstructSlab(slab, pages, 0,
|
||||||
_Linkage, this);
|
_Linkage, (BaseHashCacheStrategy *)this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReturnSlab(BaseCache::Slab *slab)
|
void ReturnSlab(BaseCache::Slab *slab)
|
||||||
{
|
{
|
||||||
uint8_t *data = (uint8_t *)slab->pages;
|
_ClearSlab(fParent, slab->pages, _SlabSize());
|
||||||
size_t byteCount = _SlabSize();
|
|
||||||
|
|
||||||
for (uint8_t *it = data;
|
|
||||||
it < (data + byteCount); it += fParent->ObjectSize()) {
|
|
||||||
Link *link = fHashTable.Lookup(it);
|
|
||||||
fHashTable.Remove(link);
|
|
||||||
fLinkCache.Free(link);
|
|
||||||
}
|
|
||||||
|
|
||||||
BaseCacheStrategy<Backend>::_DestructSlab(slab);
|
BaseCacheStrategy<Backend>::_DestructSlab(slab);
|
||||||
fSlabCache.Free((Slab *)slab);
|
fSlabCache.Free((Slab *)slab);
|
||||||
}
|
}
|
||||||
@@ -424,20 +434,44 @@ private:
|
|||||||
return BaseCacheStrategy<Backend>::SlabSize(0);
|
return BaseCacheStrategy<Backend>::SlabSize(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Link *_Linkage(void *object) const
|
status_t _PrepareSlab(BaseCache *parent, Slab *slab, void *pages,
|
||||||
|
size_t byteCount, uint32_t flags)
|
||||||
{
|
{
|
||||||
return fHashTable.Lookup(object);
|
uint8_t *data = (uint8_t *)pages;
|
||||||
|
for (uint8_t *it = data;
|
||||||
|
it < (data + byteCount); it += parent->ObjectSize()) {
|
||||||
|
Link *link = fLinkCache.Alloc(flags);
|
||||||
|
|
||||||
|
if (link == NULL) {
|
||||||
|
_ClearSlabRange(parent, data, it);
|
||||||
|
return B_NO_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
link->slab = slab;
|
||||||
|
link->buffer = it;
|
||||||
|
fHashTable.Insert(link);
|
||||||
|
}
|
||||||
|
|
||||||
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ObjectLink *_Linkage(void *_this, void *object)
|
void _ClearSlab(BaseCache *parent, void *pages, size_t byteCount)
|
||||||
{
|
{
|
||||||
return ((Strategy *)_this)->_Linkage(object);
|
_ClearSlabRange(parent, (uint8_t *)pages,
|
||||||
|
((uint8_t *)pages) + byteCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _ClearSlabRange(BaseCache *parent, uint8_t *data, uint8_t *end)
|
||||||
|
{
|
||||||
|
for (uint8_t *it = data; it < end; it += parent->ObjectSize()) {
|
||||||
|
Link *link = fHashTable.Lookup(it);
|
||||||
|
fHashTable.Remove(link);
|
||||||
|
fLinkCache.Free(link);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HashTable fHashTable;
|
|
||||||
TypedCache<Slab, Backend> fSlabCache;
|
TypedCache<Slab, Backend> fSlabCache;
|
||||||
TypedCache<Link, Backend> fLinkCache;
|
TypedCache<Link, Backend> fLinkCache;
|
||||||
const size_t fLowerBoundary;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user