rewrote the object cache (slab) implementation a bit, preparing for further integration.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20887 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Hugo Santos, [email protected]
|
||||
*/
|
||||
|
||||
#ifndef _SLAB_BACKEND_H_
|
||||
#define _SLAB_BACKEND_H_
|
||||
|
||||
#include <slab/Base.h>
|
||||
|
||||
extern "C" {
|
||||
status_t slab_area_backend_allocate(base_cache *cache, area_id *id,
|
||||
void **pages, size_t byte_count, uint32_t flags);
|
||||
void slab_area_backend_free(base_cache *cache, area_id id);
|
||||
}
|
||||
|
||||
struct AreaBackend {
|
||||
typedef area_id AllocationID;
|
||||
|
||||
static const size_t kPageSize = B_PAGE_SIZE;
|
||||
static const size_t kMaximumAlignedLength = B_PAGE_SIZE;
|
||||
|
||||
static status_t AllocatePages(base_cache *cache, area_id *id, void **pages,
|
||||
size_t byteCount, uint32_t flags)
|
||||
{
|
||||
return slab_area_backend_allocate(cache, id, pages, byteCount, flags);
|
||||
}
|
||||
|
||||
static void FreePages(base_cache *cache, area_id id)
|
||||
{
|
||||
return slab_area_backend_free(cache, id);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,170 +9,44 @@
|
||||
#ifndef _SLAB_BASE_SLAB_H_
|
||||
#define _SLAB_BASE_SLAB_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <KernelExport.h>
|
||||
#include <OS.h>
|
||||
|
||||
#include <lock.h>
|
||||
#include <vm_low_memory.h>
|
||||
#include <util/AutoLock.h>
|
||||
#include <util/list.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <utility> // pair<>
|
||||
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* create_object_cache_etc flags */
|
||||
enum {
|
||||
CACHE_DONT_SLEEP = 1 << 0,
|
||||
CACHE_ALIGN_TO_TOTAL = 1 << 16,
|
||||
CACHE_NO_DEPOT = 1 << 0,
|
||||
};
|
||||
|
||||
static const int kMinimumSlabItems = 32;
|
||||
/* object_cache_alloc flags */
|
||||
enum {
|
||||
CACHE_DONT_SLEEP = 1 << 0,
|
||||
};
|
||||
|
||||
typedef status_t (*base_cache_constructor)(void *cookie, void *object);
|
||||
typedef void (*base_cache_destructor)(void *cookie, void *object);
|
||||
typedef struct object_cache object_cache;
|
||||
|
||||
/* base Slab implementation, opaque to the backend used.
|
||||
*
|
||||
* NOTE: the caller is responsible for the Cache's locking.
|
||||
* Cache<> below handles it as well. */
|
||||
typedef status_t (*object_cache_constructor)(void *cookie, void *object);
|
||||
typedef void (*object_cache_destructor)(void *cookie, void *object);
|
||||
typedef void (*object_cache_reclaimer)(void *cookie, void *object);
|
||||
|
||||
typedef struct base_cache {
|
||||
char name[32];
|
||||
size_t object_size;
|
||||
size_t cache_color_cycle;
|
||||
struct list empty, partial, full;
|
||||
size_t empty_count, pressure;
|
||||
base_cache_constructor constructor;
|
||||
base_cache_destructor destructor;
|
||||
void *cookie;
|
||||
} base_cache;
|
||||
object_cache *create_object_cache(const char *name, size_t object_size,
|
||||
size_t alignment, void *cookie, object_cache_constructor constructor,
|
||||
object_cache_destructor);
|
||||
object_cache *create_object_cache_etc(const char *name, size_t object_size,
|
||||
size_t alignment, size_t max_byte_usage, uint32 flags, void *cookie,
|
||||
object_cache_constructor constructor, object_cache_destructor destructor,
|
||||
object_cache_reclaimer reclaimer);
|
||||
|
||||
typedef struct cache_slab {
|
||||
void *pages;
|
||||
size_t count, size;
|
||||
size_t offset;
|
||||
struct cache_object_link *free;
|
||||
struct list_link link;
|
||||
} cache_slab;
|
||||
void delete_object_cache(object_cache *cache);
|
||||
|
||||
// TODO add reclaim method to base_cache to be called under severe memory
|
||||
// pressure so the slab owner can free as much buffers as possible.
|
||||
|
||||
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 *));
|
||||
void base_cache_low_memory(base_cache *cache, int32 level,
|
||||
void (*return_slab)(base_cache *, cache_slab *));
|
||||
|
||||
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,
|
||||
void *object);
|
||||
|
||||
typedef status_t (*base_cache_owner_prepare)(void *parent,
|
||||
cache_slab *slab, void *object);
|
||||
typedef void (*base_cache_owner_unprepare)(void *parent, cache_slab *slab,
|
||||
void *object);
|
||||
|
||||
cache_slab *base_cache_construct_slab(base_cache *cache, cache_slab *slab,
|
||||
void *pages, size_t byte_count, void *parent,
|
||||
base_cache_owner_prepare prepare, base_cache_owner_unprepare unprepare);
|
||||
void base_cache_destruct_slab(base_cache *cache, cache_slab *slab,
|
||||
void *parent, base_cache_owner_unprepare unprepare);
|
||||
void *object_cache_alloc(object_cache *cache, uint32 flags);
|
||||
void object_cache_free(object_cache *cache, void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
// Slab implementation, glues together the frontend, backend as
|
||||
// well as the Slab strategy used.
|
||||
template<typename Strategy>
|
||||
class Cache : protected base_cache {
|
||||
public:
|
||||
typedef Cache<Strategy> ThisCache;
|
||||
typedef base_cache_constructor Constructor;
|
||||
typedef base_cache_destructor Destructor;
|
||||
|
||||
Cache(const char *name, size_t objectSize, size_t alignment,
|
||||
Constructor constructor, Destructor destructor, void *cookie)
|
||||
: fStrategy(this)
|
||||
{
|
||||
if (benaphore_init(&fLock, name) >= B_OK) {
|
||||
base_cache_init(this, name, objectSize, alignment, constructor,
|
||||
destructor, cookie);
|
||||
register_low_memory_handler(_LowMemory, this, 0);
|
||||
}
|
||||
}
|
||||
|
||||
~Cache()
|
||||
{
|
||||
if (fLock.sem >= B_OK) {
|
||||
benaphore_lock(&fLock);
|
||||
unregister_low_memory_handler(_LowMemory, this);
|
||||
base_cache_destroy(this, _ReturnSlab);
|
||||
benaphore_destroy(&fLock);
|
||||
}
|
||||
}
|
||||
|
||||
status_t InitCheck() const { return fLock.sem; }
|
||||
|
||||
void *AllocateObject(uint32_t flags)
|
||||
{
|
||||
BenaphoreLocker _(fLock);
|
||||
|
||||
void *object = base_cache_allocate_object(this);
|
||||
|
||||
// if the cache is returning NULL it is because it ran out of slabs
|
||||
if (object == NULL) {
|
||||
cache_slab *newSlab = fStrategy.NewSlab(flags);
|
||||
if (newSlab == NULL)
|
||||
return NULL;
|
||||
object = base_cache_allocate_object_with_new_slab(this, newSlab);
|
||||
if (object == NULL)
|
||||
panic("cache: failed to allocate with an empty slab");
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
void ReturnObject(void *object)
|
||||
{
|
||||
BenaphoreLocker _(fLock);
|
||||
|
||||
cache_slab *slab = fStrategy.ObjectSlab(object);
|
||||
|
||||
if (base_cache_return_object(this, slab, object))
|
||||
fStrategy.ReturnSlab(slab);
|
||||
}
|
||||
|
||||
private:
|
||||
static void _ReturnSlab(base_cache *self, cache_slab *slab)
|
||||
{
|
||||
// Already locked, ~Cache() -> base_cache_destroy -> _ReturnSlab
|
||||
((ThisCache *)self)->fStrategy.ReturnSlab(slab);
|
||||
}
|
||||
|
||||
static void _LowMemory(void *_self, int32 level)
|
||||
{
|
||||
if (level == B_NO_LOW_MEMORY)
|
||||
return;
|
||||
|
||||
ThisCache *self = (ThisCache *)_self;
|
||||
|
||||
BenaphoreLocker _(self->fLock);
|
||||
base_cache_low_memory(self, level, _ReturnSlab);
|
||||
}
|
||||
|
||||
benaphore fLock;
|
||||
Strategy fStrategy;
|
||||
};
|
||||
|
||||
#endif /* __cplusplus */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,29 +18,22 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct depot_magazine {
|
||||
struct depot_magazine *next;
|
||||
uint16_t current_round, round_count;
|
||||
void *rounds[0];
|
||||
} depot_magazine;
|
||||
typedef struct base_depot {
|
||||
benaphore lock;
|
||||
struct depot_magazine *full, *empty;
|
||||
size_t full_count, empty_count;
|
||||
struct depot_cpu_store *stores;
|
||||
|
||||
void (*return_object)(struct base_depot *depot, void *object);
|
||||
} base_depot;
|
||||
|
||||
|
||||
typedef struct depot_cpu_store {
|
||||
benaphore lock;
|
||||
depot_magazine *loaded, *previous;
|
||||
struct depot_magazine *loaded, *previous;
|
||||
} depot_cpu_store;
|
||||
|
||||
|
||||
typedef struct base_depot {
|
||||
benaphore lock;
|
||||
depot_magazine *full, *empty;
|
||||
size_t full_count, empty_count;
|
||||
depot_cpu_store *stores;
|
||||
|
||||
void (*return_object)(base_depot *depot, void *object);
|
||||
} base_depot;
|
||||
|
||||
|
||||
static inline depot_cpu_store *
|
||||
base_depot_cpu(base_depot *depot)
|
||||
{
|
||||
|
||||
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Hugo Santos, [email protected]
|
||||
*/
|
||||
|
||||
#ifndef _SLAB_HASH_STRATEGY_H_
|
||||
#define _SLAB_HASH_STRATEGY_H_
|
||||
|
||||
#include <slab/Strategy.h>
|
||||
#include <slab/Utilities.h> // for TypedCache
|
||||
|
||||
#include <KernelExport.h>
|
||||
#include <util/OpenHashTable.h>
|
||||
|
||||
|
||||
struct BaseHashCacheStrategy {
|
||||
struct Link : HashTableLink<Link> {
|
||||
const void *buffer;
|
||||
cache_slab *slab;
|
||||
};
|
||||
|
||||
struct HashTableDefinition {
|
||||
typedef BaseHashCacheStrategy ParentType;
|
||||
typedef const void * KeyType;
|
||||
typedef Link ValueType;
|
||||
|
||||
HashTableDefinition(BaseHashCacheStrategy *_parent) : parent(_parent) {}
|
||||
|
||||
size_t HashKey(const void *key) const
|
||||
{
|
||||
return (((const uint8_t *)key)
|
||||
- ((const uint8_t *)0)) >> parent->fLowerBoundary;
|
||||
}
|
||||
|
||||
size_t Hash(Link *value) const { return HashKey(value->buffer); }
|
||||
|
||||
bool Compare(const void *key, Link *value) const
|
||||
{
|
||||
return value->buffer == key;
|
||||
}
|
||||
|
||||
HashTableLink<Link> *GetLink(Link *value) const { return value; }
|
||||
|
||||
BaseHashCacheStrategy *parent;
|
||||
};
|
||||
|
||||
// for g++ 2.95
|
||||
friend class HashTableDefinition;
|
||||
|
||||
typedef OpenHashTable<HashTableDefinition> HashTable;
|
||||
|
||||
static inline int
|
||||
__Fls0(size_t value)
|
||||
{
|
||||
if (value == 0)
|
||||
return -1;
|
||||
|
||||
int bit;
|
||||
for (bit = 0; value != 1; bit++)
|
||||
value >>= 1;
|
||||
return bit;
|
||||
}
|
||||
|
||||
BaseHashCacheStrategy(base_cache *parent)
|
||||
: fHashTable(this), fLowerBoundary(__Fls0(parent->object_size)) {}
|
||||
|
||||
cache_slab *ObjectSlab(void *object) const
|
||||
{
|
||||
return _Linkage(object)->slab;
|
||||
}
|
||||
|
||||
protected:
|
||||
Link *_Linkage(void *object) const
|
||||
{
|
||||
Link *link = fHashTable.Lookup(object);
|
||||
if (link == NULL)
|
||||
panic("slab: missing buffer link from hash table.");
|
||||
return link;
|
||||
}
|
||||
|
||||
HashTable fHashTable;
|
||||
const size_t fLowerBoundary;
|
||||
};
|
||||
|
||||
|
||||
template<typename Backend>
|
||||
struct HashCacheStrategy : BaseCacheStrategy<Backend>, BaseHashCacheStrategy {
|
||||
typedef typename BaseCacheStrategy<Backend>::BaseSlab BaseSlab;
|
||||
typedef typename BaseCacheStrategy<Backend>::Slab Slab;
|
||||
typedef HashCacheStrategy<Backend> Strategy;
|
||||
|
||||
HashCacheStrategy(base_cache *parent)
|
||||
: BaseCacheStrategy<Backend>(parent), BaseHashCacheStrategy(parent),
|
||||
fSlabCache("slab cache", 0), fLinkCache("link cache", 0) {}
|
||||
|
||||
BaseSlab *NewSlab(uint32_t flags)
|
||||
{
|
||||
size_t byteCount = _SlabSize();
|
||||
|
||||
Slab *slab = fSlabCache.Alloc(flags);
|
||||
if (slab == NULL)
|
||||
return NULL;
|
||||
|
||||
void *pages;
|
||||
if (Backend::AllocatePages(Parent(), &slab->id, &pages, byteCount,
|
||||
flags) < B_OK) {
|
||||
fSlabCache.Free(slab);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 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(), this, _PrepareObject, _UnprepareObject);
|
||||
if (result == NULL) {
|
||||
Backend::FreePages(Parent(), slab->id);
|
||||
fSlabCache.Free(slab);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ReturnSlab(BaseSlab *slab)
|
||||
{
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab, this, _UnprepareObject);
|
||||
fSlabCache.Free((Slab *)slab);
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _SlabSize() const
|
||||
{
|
||||
return BaseCacheStrategy<Backend>::SlabSize(0);
|
||||
}
|
||||
|
||||
base_cache *Parent() const { return BaseCacheStrategy<Backend>::Parent(); }
|
||||
|
||||
static status_t _PrepareObject(void *_self, cache_slab *slab, void *object)
|
||||
{
|
||||
Strategy *self = (Strategy *)_self;
|
||||
|
||||
Link *link = self->fLinkCache.Alloc(CACHE_DONT_SLEEP);
|
||||
if (link == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
link->slab = slab;
|
||||
link->buffer = object;
|
||||
|
||||
self->fHashTable.Insert(link);
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
static void _UnprepareObject(void *_self, cache_slab *slab, void *object)
|
||||
{
|
||||
((Strategy *)_self)->_UnprepareObject(object);
|
||||
}
|
||||
|
||||
void _UnprepareObject(void *object)
|
||||
{
|
||||
Link *link = _Linkage(object);
|
||||
fHashTable.Remove(link);
|
||||
fLinkCache.Free(link);
|
||||
}
|
||||
|
||||
TypedCache<Slab, Backend> fSlabCache;
|
||||
TypedCache<Link, Backend> fLinkCache;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Hugo Santos, [email protected]
|
||||
*/
|
||||
|
||||
#ifndef _SLAB_MERGED_STRATEGY_H_
|
||||
#define _SLAB_MERGED_STRATEGY_H_
|
||||
|
||||
#include <slab/Strategy.h>
|
||||
|
||||
|
||||
// This slab strategy includes the ObjectLink at the end of each object and the
|
||||
// slab at the end of the allocated pages. It uses aligned allocations to
|
||||
// provide object to slab mapping with zero storage, thus there is only one
|
||||
// word of overhead per object. This is optimized for small objects.
|
||||
template<typename Backend>
|
||||
class MergedLinkCacheStrategy : public BaseCacheStrategy<Backend> {
|
||||
public:
|
||||
typedef typename BaseCacheStrategy<Backend>::BaseSlab BaseSlab;
|
||||
typedef typename BaseCacheStrategy<Backend>::Slab Slab;
|
||||
|
||||
MergedLinkCacheStrategy(base_cache *parent)
|
||||
: BaseCacheStrategy<Backend>(parent) {}
|
||||
|
||||
static inline const void *
|
||||
LowerBoundary(void *object, size_t byteCount)
|
||||
{
|
||||
const uint8_t *null = (uint8_t *)NULL;
|
||||
return null + ((((uint8_t *)object) - null) & ~(byteCount - 1));
|
||||
}
|
||||
|
||||
BaseSlab *ObjectSlab(void *object) const
|
||||
{
|
||||
return _SlabInPages(LowerBoundary(object, _SlabSize()));
|
||||
}
|
||||
|
||||
BaseSlab *NewSlab(uint32_t flags)
|
||||
{
|
||||
typename Backend::AllocationID id;
|
||||
void *pages;
|
||||
|
||||
size_t byteCount = _SlabSize();
|
||||
// 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.
|
||||
if (Backend::AllocatePages(Parent(), &id, &pages, byteCount,
|
||||
CACHE_ALIGN_TO_TOTAL | flags) < B_OK)
|
||||
return NULL;
|
||||
|
||||
_SlabInPages(pages)->id = id;
|
||||
|
||||
return BaseCacheStrategy<Backend>::_ConstructSlab(_SlabInPages(pages),
|
||||
pages, byteCount - sizeof(Slab), this, NULL, NULL);
|
||||
}
|
||||
|
||||
void ReturnSlab(BaseSlab *slab)
|
||||
{
|
||||
BaseCacheStrategy<Backend>::_DestructSlab(slab, NULL, NULL);
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _SlabSize() const
|
||||
{
|
||||
size_t byteCount = BaseCacheStrategy<Backend>::SlabSize(sizeof(Slab));
|
||||
if (byteCount > Backend::kMaximumAlignedLength)
|
||||
byteCount = Backend::kMaximumAlignedLength;
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
base_cache *Parent() const { return BaseCacheStrategy<Backend>::Parent(); }
|
||||
|
||||
Slab *_SlabInPages(const void *pages) const
|
||||
{
|
||||
return (Slab *)(((uint8_t *)pages) + _SlabSize() - sizeof(Slab));
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,90 +9,7 @@
|
||||
#ifndef _SLAB_SLAB_H_
|
||||
#define _SLAB_SLAB_H_
|
||||
|
||||
#include <slab/Base.h>
|
||||
#include <slab/Depot.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <slab/Backend.h>
|
||||
#include <slab/Base.h>
|
||||
#include <slab/MergedStrategy.h>
|
||||
#include <slab/HashStrategy.h>
|
||||
#include <slab/Utilities.h>
|
||||
|
||||
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void *object_cache_t;
|
||||
|
||||
|
||||
object_cache_t
|
||||
object_cache_create(const char *name, size_t object_size, size_t alignment,
|
||||
base_cache_constructor constructor, base_cache_destructor destructor,
|
||||
void *cookie);
|
||||
void *object_cache_alloc(object_cache_t cache);
|
||||
void *object_cache_alloc_etc(object_cache_t cache, uint32_t flags);
|
||||
void object_cache_free(object_cache_t cache, void *object);
|
||||
void object_cache_destroy(object_cache_t cache);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
|
||||
template<typename CacheType>
|
||||
class LocalCache : public CacheType, protected base_depot {
|
||||
public:
|
||||
typedef LocalCache<CacheType> ThisType;
|
||||
typedef typename CacheType::Constructor Constructor;
|
||||
typedef typename CacheType::Destructor Destructor;
|
||||
|
||||
LocalCache(const char *name, size_t objectSize, size_t alignment,
|
||||
Constructor _constructor, Destructor _destructor, void *_cookie)
|
||||
: CacheType(name, objectSize, alignment, _constructor, _destructor,
|
||||
_cookie)
|
||||
{
|
||||
fStatus = base_depot_init(this, _ReturnObject);
|
||||
}
|
||||
|
||||
~LocalCache()
|
||||
{
|
||||
base_depot_destroy(this);
|
||||
}
|
||||
|
||||
status_t InitCheck() const { return fStatus; }
|
||||
|
||||
void *Alloc(uint32_t flags)
|
||||
{
|
||||
void *object = base_depot_obtain_from_store(this, base_depot_cpu(this));
|
||||
if (object == NULL)
|
||||
object = CacheType::AllocateObject(flags);
|
||||
return object;
|
||||
}
|
||||
|
||||
void Free(void *object)
|
||||
{
|
||||
if (!base_depot_return_to_store(this, base_depot_cpu(this), object))
|
||||
CacheType::ReturnObject(object);
|
||||
}
|
||||
|
||||
void Destroy()
|
||||
{
|
||||
base_depot_make_empty(this);
|
||||
}
|
||||
|
||||
private:
|
||||
void ReturnObject(void *object)
|
||||
{
|
||||
CacheType::ReturnObject(object);
|
||||
}
|
||||
|
||||
static void _ReturnObject(base_depot *self, void *object)
|
||||
{
|
||||
static_cast<ThisType *>(self)->ReturnObject(object);
|
||||
}
|
||||
|
||||
status_t fStatus;
|
||||
};
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Hugo Santos, [email protected]
|
||||
*/
|
||||
|
||||
#ifndef _SLAB_STRATEGY_H_
|
||||
#define _SLAB_STRATEGY_H_
|
||||
|
||||
#include <slab/Base.h>
|
||||
|
||||
|
||||
template<typename Backend>
|
||||
class BaseCacheStrategy {
|
||||
protected:
|
||||
typedef cache_slab BaseSlab;
|
||||
|
||||
BaseCacheStrategy(base_cache *parent)
|
||||
: fParent(parent) {}
|
||||
|
||||
size_t SlabSize(size_t tailSpace) const
|
||||
{
|
||||
size_t pageCount = ((kMinimumSlabItems * fParent->object_size
|
||||
+ tailSpace) + Backend::kPageSize / 2) / Backend::kPageSize;
|
||||
if (pageCount < 1)
|
||||
pageCount = 1;
|
||||
return pageCount * Backend::kPageSize;
|
||||
}
|
||||
|
||||
struct Slab : BaseSlab {
|
||||
typename Backend::AllocationID id;
|
||||
};
|
||||
|
||||
BaseSlab *_ConstructSlab(Slab *slab, void *pages, size_t byteCount,
|
||||
void *parent, base_cache_owner_prepare prepare,
|
||||
base_cache_owner_unprepare unprepare)
|
||||
{
|
||||
return base_cache_construct_slab(fParent, slab, pages, byteCount,
|
||||
parent, prepare, unprepare);
|
||||
}
|
||||
|
||||
void _DestructSlab(BaseSlab *slab, void *parent,
|
||||
base_cache_owner_unprepare unprepare)
|
||||
{
|
||||
base_cache_destruct_slab(fParent, slab, parent, unprepare);
|
||||
Backend::FreePages(fParent, ((Slab *)slab)->id);
|
||||
}
|
||||
|
||||
base_cache *Parent() const { return fParent; }
|
||||
|
||||
base_cache *fParent;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Hugo Santos, [email protected]
|
||||
*/
|
||||
|
||||
#ifndef _SLAB_UTILITIES_H_
|
||||
#define _SLAB_UTILITIES_H_
|
||||
|
||||
#include <slab/Base.h>
|
||||
#include <slab/MergedStrategy.h>
|
||||
|
||||
|
||||
template<typename Type, typename Backend>
|
||||
class TypedCache : public Cache< MergedLinkCacheStrategy<Backend> > {
|
||||
public:
|
||||
typedef MergedLinkCacheStrategy<Backend> Strategy;
|
||||
typedef Cache<Strategy> BaseType;
|
||||
|
||||
TypedCache(const char *name, size_t alignment)
|
||||
: BaseType(name, sizeof(Type), alignment, _ConstructObject,
|
||||
_DestructObject, this) {}
|
||||
virtual ~TypedCache() {}
|
||||
|
||||
Type *Alloc(uint32_t flags) { return (Type *)BaseType::AllocateObject(flags); }
|
||||
void Free(Type *object) { BaseType::ReturnObject(object); }
|
||||
|
||||
private:
|
||||
static status_t _ConstructObject(void *cookie, void *object)
|
||||
{
|
||||
return ((TypedCache *)cookie)->ConstructObject((Type *)object);
|
||||
}
|
||||
|
||||
static void _DestructObject(void *cookie, void *object)
|
||||
{
|
||||
((TypedCache *)cookie)->DestructObject((Type *)object);
|
||||
}
|
||||
|
||||
virtual status_t ConstructObject(Type *object) { return B_OK; }
|
||||
virtual void DestructObject(Type *object) {}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user