haiku/headers/private/kernel/slab/Slab.h
Hugo Santos 81423c91c7 added initial slab code to the kernel. It is still unused, and there is still no VM interaction.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20832 a95241bf-73f2-0310-859d-f6bbb57e9c96
2007-04-26 03:41:24 +00:00

104 lines
2.2 KiB
C++

/*
* Copyright 2007, Hugo Santos. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Hugo Santos, hugosantos@gmail.com
*/
#ifndef _SLAB_SLAB_H_
#define _SLAB_SLAB_H_
#ifdef __cplusplus
#include <slab/Backend.h>
#include <slab/Base.h>
#include <slab/Depot.h>
#include <slab/MergedStrategy.h>
#include <slab/HashStrategy.h>
#include <slab/Utilities.h>
extern "C" {
#endif
enum {
CACHE_DONT_SLEEP = 1 << 0,
CACHE_ALIGN_TO_TOTAL = 1 << 16,
};
typedef void *object_cache_t;
object_cache_t
object_cache_create(const char *name, size_t object_size, size_t alignment,
void (*_constructor)(void *, void *), void (*_destructor)(void *, void *),
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