removed some of the slab's initial heavy debugging. we now merge the links into the slab itself resulting in zero overhead per buffer with MergedLink strategy.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20841 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -58,6 +58,7 @@ typedef struct cache_object_link {
|
||||
typedef struct cache_slab {
|
||||
void *pages;
|
||||
size_t count, size;
|
||||
size_t offset;
|
||||
cache_object_link *free;
|
||||
struct list_link link;
|
||||
} cache_slab;
|
||||
@@ -73,11 +74,11 @@ void base_cache_destroy(base_cache *cache,
|
||||
void base_cache_low_memory(base_cache *cache, int32 level,
|
||||
void (*return_slab)(base_cache *, cache_slab *));
|
||||
|
||||
cache_object_link *base_cache_allocate_object(base_cache *cache);
|
||||
cache_object_link *base_cache_allocate_object_with_new_slab(base_cache *cache,
|
||||
void *base_cache_allocate_object(base_cache *cache);
|
||||
void *base_cache_allocate_object_with_new_slab(base_cache *cache,
|
||||
cache_slab *slab);
|
||||
int base_cache_return_object(base_cache *cache, cache_slab *slab,
|
||||
cache_object_link *link);
|
||||
void *object);
|
||||
|
||||
typedef status_t (*base_cache_owner_prepare)(void *parent,
|
||||
cache_slab *slab, void *object);
|
||||
@@ -86,15 +87,13 @@ typedef void (*base_cache_owner_unprepare)(void *parent, cache_slab *slab,
|
||||
|
||||
cache_slab *base_cache_construct_slab(base_cache *cache, cache_slab *slab,
|
||||
void *pages, size_t byte_count, void *parent,
|
||||
cache_object_link *(*get_link)(void *parent, void *object),
|
||||
base_cache_owner_prepare prepare, base_cache_owner_unprepare unprepare);
|
||||
void base_cache_destruct_slab(base_cache *cache, cache_slab *slab);
|
||||
void base_cache_destruct_slab(base_cache *cache, cache_slab *slab,
|
||||
void *parent, base_cache_owner_unprepare unprepare);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
typedef std::pair<cache_slab *, cache_object_link *> CacheObjectInfo;
|
||||
|
||||
// Slab implementation, glues together the frontend, backend as
|
||||
// well as the Slab strategy used.
|
||||
template<typename Strategy>
|
||||
@@ -109,8 +108,8 @@ public:
|
||||
: fStrategy(this)
|
||||
{
|
||||
if (benaphore_init(&fLock, name) >= B_OK) {
|
||||
base_cache_init(this, name, Strategy::RequiredSpace(objectSize),
|
||||
alignment, constructor, destructor, cookie);
|
||||
base_cache_init(this, name, objectSize, alignment, constructor,
|
||||
destructor, cookie);
|
||||
register_low_memory_handler(_LowMemory, this, 0);
|
||||
}
|
||||
}
|
||||
@@ -131,29 +130,29 @@ public:
|
||||
{
|
||||
BenaphoreLocker _(fLock);
|
||||
|
||||
cache_object_link *link = base_cache_allocate_object(this);
|
||||
void *object = base_cache_allocate_object(this);
|
||||
|
||||
// if the cache is returning NULL it is because it ran out of slabs
|
||||
if (link == NULL) {
|
||||
if (object == NULL) {
|
||||
cache_slab *newSlab = fStrategy.NewSlab(flags);
|
||||
if (newSlab == NULL)
|
||||
return NULL;
|
||||
link = base_cache_allocate_object_with_new_slab(this, newSlab);
|
||||
if (link == NULL)
|
||||
object = base_cache_allocate_object_with_new_slab(this, newSlab);
|
||||
if (object == NULL)
|
||||
panic("cache: failed to allocate with an empty slab");
|
||||
}
|
||||
|
||||
return fStrategy.Object(link);
|
||||
return object;
|
||||
}
|
||||
|
||||
void ReturnObject(void *object)
|
||||
{
|
||||
BenaphoreLocker _(fLock);
|
||||
|
||||
CacheObjectInfo location = fStrategy.ObjectInformation(object);
|
||||
cache_slab *slab = fStrategy.ObjectSlab(object);
|
||||
|
||||
if (base_cache_return_object(this, location.first, location.second))
|
||||
fStrategy.ReturnSlab(location.first);
|
||||
if (base_cache_return_object(this, slab, object))
|
||||
fStrategy.ReturnSlab(slab);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -17,26 +17,27 @@
|
||||
|
||||
|
||||
struct BaseHashCacheStrategy {
|
||||
struct Link : cache_object_link, HashTableLink<Link> {
|
||||
struct Link : HashTableLink<Link> {
|
||||
const void *buffer;
|
||||
cache_slab *slab;
|
||||
void *buffer;
|
||||
};
|
||||
|
||||
struct HashTableDefinition {
|
||||
typedef BaseHashCacheStrategy ParentType;
|
||||
typedef void * KeyType;
|
||||
typedef const void * KeyType;
|
||||
typedef Link ValueType;
|
||||
|
||||
HashTableDefinition(BaseHashCacheStrategy *_parent) : parent(_parent) {}
|
||||
|
||||
size_t HashKey(void *key) const
|
||||
size_t HashKey(const void *key) const
|
||||
{
|
||||
return (((uint8_t *)key) - ((uint8_t *)0)) >> parent->fLowerBoundary;
|
||||
return (((const uint8_t *)key)
|
||||
- ((const uint8_t *)0)) >> parent->fLowerBoundary;
|
||||
}
|
||||
|
||||
size_t Hash(Link *value) const { return HashKey(value->buffer); }
|
||||
|
||||
bool Compare(void *key, Link *value) const
|
||||
bool Compare(const void *key, Link *value) const
|
||||
{
|
||||
return value->buffer == key;
|
||||
}
|
||||
@@ -66,15 +67,9 @@ struct BaseHashCacheStrategy {
|
||||
BaseHashCacheStrategy(base_cache *parent)
|
||||
: fHashTable(this), fLowerBoundary(__Fls0(parent->object_size)) {}
|
||||
|
||||
void *Object(cache_object_link *link) const
|
||||
cache_slab *ObjectSlab(void *object) const
|
||||
{
|
||||
return ((Link *)link)->buffer;
|
||||
}
|
||||
|
||||
CacheObjectInfo ObjectInformation(void *object) const
|
||||
{
|
||||
Link *link = _Linkage(object);
|
||||
return CacheObjectInfo(link->slab, link);
|
||||
return _Linkage(object)->slab;
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -86,11 +81,6 @@ protected:
|
||||
return link;
|
||||
}
|
||||
|
||||
static cache_object_link *_Linkage(void *_this, void *object)
|
||||
{
|
||||
return ((BaseHashCacheStrategy *)_this)->_Linkage(object);
|
||||
}
|
||||
|
||||
HashTable fHashTable;
|
||||
const size_t fLowerBoundary;
|
||||
};
|
||||
@@ -106,11 +96,6 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend>, BaseHashCacheStrategy {
|
||||
: BaseCacheStrategy<Backend>(parent), BaseHashCacheStrategy(parent),
|
||||
fSlabCache("slab cache", 0), fLinkCache("link cache", 0) {}
|
||||
|
||||
static size_t RequiredSpace(size_t objectSize)
|
||||
{
|
||||
return objectSize;
|
||||
}
|
||||
|
||||
BaseSlab *NewSlab(uint32_t flags)
|
||||
{
|
||||
size_t byteCount = _SlabSize();
|
||||
@@ -129,8 +114,7 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend>, BaseHashCacheStrategy {
|
||||
// it's very important that we cast this to BaseHashCacheStrategy
|
||||
// so we get the proper instance offset through void *
|
||||
cache_slab *result = BaseCacheStrategy<Backend>::_ConstructSlab(slab,
|
||||
pages, _SlabSize(), (BaseHashCacheStrategy *)this, _Linkage,
|
||||
_PrepareObject, _UnprepareObject);
|
||||
pages, _SlabSize(), this, _PrepareObject, _UnprepareObject);
|
||||
if (result == NULL) {
|
||||
Backend::FreePages(Parent(), slab->id);
|
||||
fSlabCache.Free(slab);
|
||||
@@ -141,8 +125,7 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend>, BaseHashCacheStrategy {
|
||||
|
||||
void ReturnSlab(BaseSlab *slab)
|
||||
{
|
||||
_ClearSlab(slab->pages, _SlabSize());
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab);
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab, this, _UnprepareObject);
|
||||
fSlabCache.Free((Slab *)slab);
|
||||
}
|
||||
|
||||
@@ -156,8 +139,7 @@ private:
|
||||
|
||||
static status_t _PrepareObject(void *_self, cache_slab *slab, void *object)
|
||||
{
|
||||
BaseHashCacheStrategy *base = (BaseHashCacheStrategy *)_self;
|
||||
Strategy *self = (Strategy *)base;
|
||||
Strategy *self = (Strategy *)_self;
|
||||
|
||||
Link *link = self->fLinkCache.Alloc(CACHE_DONT_SLEEP);
|
||||
if (link == NULL)
|
||||
@@ -173,8 +155,7 @@ private:
|
||||
|
||||
static void _UnprepareObject(void *_self, cache_slab *slab, void *object)
|
||||
{
|
||||
BaseHashCacheStrategy *base = (BaseHashCacheStrategy *)_self;
|
||||
((Strategy *)base)->_UnprepareObject(object);
|
||||
((Strategy *)_self)->_UnprepareObject(object);
|
||||
}
|
||||
|
||||
void _UnprepareObject(void *object)
|
||||
@@ -184,15 +165,6 @@ private:
|
||||
fLinkCache.Free(link);
|
||||
}
|
||||
|
||||
void _ClearSlab(void *pages, size_t size)
|
||||
{
|
||||
uint8_t *data = (uint8_t *)pages;
|
||||
uint8_t *end = data + size;
|
||||
|
||||
for (uint8_t *it = data; it < end; it += Parent()->object_size)
|
||||
_UnprepareObject(it);
|
||||
}
|
||||
|
||||
TypedCache<Slab, Backend> fSlabCache;
|
||||
TypedCache<Link, Backend> fLinkCache;
|
||||
};
|
||||
|
||||
@@ -21,21 +21,10 @@ class MergedLinkCacheStrategy : public BaseCacheStrategy<Backend> {
|
||||
public:
|
||||
typedef typename BaseCacheStrategy<Backend>::BaseSlab BaseSlab;
|
||||
typedef typename BaseCacheStrategy<Backend>::Slab Slab;
|
||||
typedef cache_object_link Link;
|
||||
|
||||
MergedLinkCacheStrategy(base_cache *parent)
|
||||
: BaseCacheStrategy<Backend>(parent) {}
|
||||
|
||||
static size_t RequiredSpace(size_t objectSize)
|
||||
{
|
||||
return objectSize + sizeof(Link);
|
||||
}
|
||||
|
||||
void *Object(Link *link) const
|
||||
{
|
||||
return ((uint8_t *)link) - (Parent()->object_size - sizeof(Link));
|
||||
}
|
||||
|
||||
static inline const void *
|
||||
LowerBoundary(void *object, size_t byteCount)
|
||||
{
|
||||
@@ -43,10 +32,9 @@ public:
|
||||
return null + ((((uint8_t *)object) - null) & ~(byteCount - 1));
|
||||
}
|
||||
|
||||
CacheObjectInfo ObjectInformation(void *object) const
|
||||
BaseSlab *ObjectSlab(void *object) const
|
||||
{
|
||||
Slab *slab = _SlabInPages(LowerBoundary(object, _SlabSize()));
|
||||
return CacheObjectInfo(slab, _Linkage(object));
|
||||
return _SlabInPages(LowerBoundary(object, _SlabSize()));
|
||||
}
|
||||
|
||||
BaseSlab *NewSlab(uint32_t flags)
|
||||
@@ -65,12 +53,12 @@ public:
|
||||
_SlabInPages(pages)->id = id;
|
||||
|
||||
return BaseCacheStrategy<Backend>::_ConstructSlab(_SlabInPages(pages),
|
||||
pages, byteCount - sizeof(Slab), this, _Linkage, NULL, NULL);
|
||||
pages, byteCount - sizeof(Slab), this, NULL, NULL);
|
||||
}
|
||||
|
||||
void ReturnSlab(BaseSlab *slab)
|
||||
{
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab);
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab, NULL, NULL);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -84,108 +72,10 @@ private:
|
||||
|
||||
base_cache *Parent() const { return BaseCacheStrategy<Backend>::Parent(); }
|
||||
|
||||
Link *_Linkage(void *object) const
|
||||
{
|
||||
return (Link *)(((uint8_t *)object)
|
||||
+ (Parent()->object_size - sizeof(Link)));
|
||||
}
|
||||
|
||||
Slab *_SlabInPages(const void *pages) const
|
||||
{
|
||||
return (Slab *)(((uint8_t *)pages) + _SlabSize() - sizeof(Slab));
|
||||
}
|
||||
|
||||
static Link *_Linkage(void *_this, void *object)
|
||||
{
|
||||
return ((MergedLinkCacheStrategy *)_this)->_Linkage(object);
|
||||
}
|
||||
};
|
||||
|
||||
// This slab strategy includes the ObjectLink at the end of each object and the
|
||||
// slab at the end of the allocated pages. It maintains a pointer to the owning
|
||||
// slab in the ObjectLink. This is optimized for medium sized objects whose
|
||||
// length is not a power of 2.
|
||||
template<typename Backend>
|
||||
class MergedLinkAndSlabCacheStrategy : public BaseCacheStrategy<Backend> {
|
||||
public:
|
||||
typedef MergedLinkAndSlabCacheStrategy<Backend> Strategy;
|
||||
typedef typename BaseCacheStrategy<Backend>::BaseSlab BaseSlab;
|
||||
typedef typename BaseCacheStrategy<Backend>::Slab Slab;
|
||||
|
||||
struct Link : cache_object_link {
|
||||
cache_slab *slab;
|
||||
};
|
||||
|
||||
MergedLinkAndSlabCacheStrategy(base_cache *parent)
|
||||
: BaseCacheStrategy<Backend>(parent) {}
|
||||
|
||||
static size_t RequiredSpace(size_t objectSize)
|
||||
{
|
||||
return objectSize + sizeof(Link);
|
||||
}
|
||||
|
||||
void *Object(cache_object_link *_link) const
|
||||
{
|
||||
Link *link = static_cast<Link *>(_link);
|
||||
|
||||
return ((uint8_t *)link) - (Parent()->object_size - sizeof(Link));
|
||||
}
|
||||
|
||||
CacheObjectInfo ObjectInformation(void *object) const
|
||||
{
|
||||
Link *link = _Linkage(object);
|
||||
return CacheObjectInfo(link->slab, link);
|
||||
}
|
||||
|
||||
BaseSlab *NewSlab(uint32_t flags)
|
||||
{
|
||||
typename Backend::AllocationID id;
|
||||
void *pages;
|
||||
|
||||
size_t size = _SlabSize();
|
||||
if (Backend::AllocatePages(Parent(), &id, &pages, size, flags) < B_OK)
|
||||
return NULL;
|
||||
|
||||
_SlabInPages(pages)->id = id;
|
||||
|
||||
return BaseCacheStrategy<Backend>::_ConstructSlab(_SlabInPages(pages),
|
||||
pages, size - sizeof(Slab), this, _Linkage, _PrepareObject, NULL);
|
||||
}
|
||||
|
||||
void ReturnSlab(BaseSlab *slab)
|
||||
{
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab);
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _SlabSize() const
|
||||
{
|
||||
return BaseCacheStrategy<Backend>::SlabSize(sizeof(Slab));
|
||||
}
|
||||
|
||||
base_cache *Parent() const { return BaseCacheStrategy<Backend>::Parent(); }
|
||||
|
||||
Link *_Linkage(void *_object) const
|
||||
{
|
||||
uint8_t *object = (uint8_t *)_object;
|
||||
return (Link *)(object + (Parent()->object_size - sizeof(Link)));
|
||||
}
|
||||
|
||||
Slab *_SlabInPages(const void *pages) const
|
||||
{
|
||||
return (Slab *)(((uint8_t *)pages) + _SlabSize() - sizeof(Slab));
|
||||
}
|
||||
|
||||
static cache_object_link *_Linkage(void *_this, void *object)
|
||||
{
|
||||
return static_cast<Strategy *>(_this)->_Linkage(object);
|
||||
}
|
||||
|
||||
static status_t _PrepareObject(void *_this, cache_slab *slab, void *object)
|
||||
{
|
||||
static_cast<Strategy *>(_this)->_Linkage(object)->slab = slab;
|
||||
return B_OK;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
template<typename Backend>
|
||||
class BaseCacheStrategy {
|
||||
protected:
|
||||
typedef cache_object_link ObjectLink;
|
||||
typedef cache_slab BaseSlab;
|
||||
|
||||
BaseCacheStrategy(base_cache *parent)
|
||||
@@ -35,16 +34,17 @@ protected:
|
||||
};
|
||||
|
||||
BaseSlab *_ConstructSlab(Slab *slab, void *pages, size_t byteCount,
|
||||
void *parent, ObjectLink *(*getLink)(void *parent, void *object),
|
||||
base_cache_owner_prepare prepare, base_cache_owner_unprepare unprepare)
|
||||
void *parent, base_cache_owner_prepare prepare,
|
||||
base_cache_owner_unprepare unprepare)
|
||||
{
|
||||
return base_cache_construct_slab(fParent, slab, pages, byteCount,
|
||||
parent, getLink, prepare, unprepare);
|
||||
parent, prepare, unprepare);
|
||||
}
|
||||
|
||||
void _DestructSlab(BaseSlab *slab)
|
||||
void _DestructSlab(BaseSlab *slab, void *parent,
|
||||
base_cache_owner_unprepare unprepare)
|
||||
{
|
||||
base_cache_destruct_slab(fParent, slab);
|
||||
base_cache_destruct_slab(fParent, slab, parent, unprepare);
|
||||
Backend::FreePages(fParent, ((Slab *)slab)->id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user