assorted slab fixes.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20833 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Hugo Santos
2007-04-26 06:05:08 +00:00
parent 81423c91c7
commit 81bc570922
6 changed files with 116 additions and 26 deletions
+28 -6
View File
@@ -25,13 +25,16 @@ static const int kMinimumSlabItems = 32;
typedef void (*base_cache_constructor)(void *cookie, void *object);
typedef void (*base_cache_destructor)(void *cookie, void *object);
/* base Slab implementation, opaque to the backend used. */
/* base Slab implementation, opaque to the backend used.
*
* NOTE: the caller is responsible for the Cache's locking. */
typedef struct base_cache {
char name[32];
size_t object_size;
size_t cache_color_cycle;
struct list partial, full;
struct list empty, partial, full;
size_t empty_count;
base_cache_constructor constructor;
base_cache_destructor destructor;
void *cookie;
@@ -51,8 +54,12 @@ typedef struct cache_slab {
void base_cache_init(base_cache *cache, const char *name, size_t object_size,
size_t alignment, base_cache_constructor constructor,
base_cache_destructor destructor, void *cookie);
void base_cache_destroy(base_cache *cache,
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,
cache_slab *slab);
int base_cache_return_object(base_cache *cache, cache_slab *slab,
cache_object_link *link);
@@ -82,16 +89,26 @@ public:
destructor, cookie);
}
~Cache()
{
base_cache_destroy(this, _ReturnSlab);
}
void *AllocateObject(uint32_t flags)
{
if (list_is_empty(&partial)) {
cache_object_link *link = base_cache_allocate_object(this);
// if the cache is returning NULL it is because it ran out of slabs
if (link == NULL) {
cache_slab *newSlab = fStrategy.NewSlab(flags);
if (newSlab == NULL)
return NULL;
list_add_item(&partial, newSlab);
link = base_cache_allocate_object_with_new_slab(this, newSlab);
if (link == NULL)
panic("cache: failed to allocate with an empty slab");
}
return fStrategy.Object(base_cache_allocate_object(this));
return fStrategy.Object(link);
}
void ReturnObject(void *object)
@@ -103,9 +120,14 @@ public:
}
private:
static void _ReturnSlab(base_cache *self, cache_slab *slab)
{
((Cache<Strategy> *)self)->fStrategy.ReturnSlab(slab);
}
Strategy fStrategy;
};
#endif
#endif /* __cplusplus */
#endif
+2 -2
View File
@@ -133,8 +133,8 @@ struct HashCacheStrategy : BaseCacheStrategy<Backend>, BaseHashCacheStrategy {
// 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,
_Linkage, (BaseHashCacheStrategy *)this);
return BaseCacheStrategy<Backend>::_ConstructSlab(slab, pages,
_SlabSize(), _Linkage, (BaseHashCacheStrategy *)this);
}
void ReturnSlab(BaseSlab *slab)
+5 -5
View File
@@ -57,9 +57,6 @@ public:
void *pages;
size_t byteCount = _SlabSize();
if (byteCount > Backend::kMaximumAlignedLength)
byteCount = Backend::kMaximumAlignedLength;
// in order to save a pointer per object or a hash table to
// map objects to slabs we required this set of pages to be
// aligned in a (pageCount * PAGE_SIZE) boundary.
@@ -70,7 +67,7 @@ public:
_SlabInPages(pages)->id = id;
return BaseCacheStrategy<Backend>::_ConstructSlab(_SlabInPages(pages),
pages, sizeof(Slab), _Linkage, this);
pages, _SlabSize() - sizeof(Slab), _Linkage, this);
}
void ReturnSlab(BaseSlab *slab)
@@ -81,7 +78,10 @@ public:
private:
size_t _SlabSize() const
{
return BaseCacheStrategy<Backend>::SlabSize(sizeof(Slab));
size_t byteCount = BaseCacheStrategy<Backend>::SlabSize(sizeof(Slab));
if (byteCount > Backend::kMaximumAlignedLength)
byteCount = Backend::kMaximumAlignedLength;
return byteCount;
}
Link *_Linkage(void *object) const
+3 -3
View File
@@ -34,11 +34,11 @@ protected:
typename Backend::AllocationID id;
};
BaseSlab *_ConstructSlab(Slab *slab, void *pages, size_t tailSpace,
BaseSlab *_ConstructSlab(Slab *slab, void *pages, size_t byteCount,
ObjectLink *(*getLink)(void *parent, void *object), void *parent)
{
return base_cache_construct_slab(fParent, slab, pages,
SlabSize(tailSpace) - tailSpace, getLink, parent);
return base_cache_construct_slab(fParent, slab, pages, byteCount,
getLink, parent);
}
void _DestructSlab(BaseSlab *slab)