added object_cache_reserve to allow cache users to pre-allocate slabs.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20912 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Hugo Santos
2007-04-29 21:45:23 +00:00
parent 11debaf6e4
commit 39af445966
2 changed files with 40 additions and 5 deletions
+3
View File
@@ -46,6 +46,9 @@ void delete_object_cache(object_cache *cache);
void *object_cache_alloc(object_cache *cache, uint32 flags); void *object_cache_alloc(object_cache *cache, uint32 flags);
void object_cache_free(object_cache *cache, void *object); void object_cache_free(object_cache *cache, void *object);
status_t object_cache_reserve(object_cache *cache, size_t object_count,
uint32 flags);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+37 -5
View File
@@ -158,6 +158,8 @@ static benaphore sObjectCacheListLock;
static uint8 *sInitialBegin, *sInitialLimit, *sInitialPointer; static uint8 *sInitialBegin, *sInitialLimit, *sInitialPointer;
static status_t object_cache_reserve_internal(object_cache *cache,
size_t object_count, uint32 flags);
static status_t object_depot_init_locks(object_depot *depot); static status_t object_depot_init_locks(object_depot *depot);
static depot_magazine *alloc_magazine(); static depot_magazine *alloc_magazine();
static void free_magazine(depot_magazine *magazine); static void free_magazine(depot_magazine *magazine);
@@ -558,16 +560,14 @@ object_cache_alloc(object_cache *cache, uint32 flags)
if (cache->partial.IsEmpty()) { if (cache->partial.IsEmpty()) {
if (cache->empty.IsEmpty()) { if (cache->empty.IsEmpty()) {
source = cache->CreateSlab(flags); if (object_cache_reserve_internal(cache, 1, flags) < B_OK)
if (source == NULL)
return NULL; return NULL;
cache->pressure++; cache->pressure++;
} else {
source = cache->empty.RemoveHead();
cache->empty_count--;
} }
source = cache->empty.RemoveHead();
cache->empty_count--;
cache->partial.Add(source); cache->partial.Add(source);
} else { } else {
source = cache->partial.Head(); source = cache->partial.Head();
@@ -635,6 +635,38 @@ object_cache_free(object_cache *cache, void *object)
} }
static status_t
object_cache_reserve_internal(object_cache *cache, size_t object_count,
uint32 flags)
{
size_t numBytes = object_count * cache->object_size;
size_t slabCount = ((numBytes - 1) / cache->slab_size) + 1;
while (slabCount > 0) {
slab *newSlab = cache->CreateSlab(flags);
if (newSlab == NULL)
return B_NO_MEMORY;
cache->empty.Add(newSlab);
cache->empty_count++;
slabCount--;
}
return B_OK;
}
status_t
object_cache_reserve(object_cache *cache, size_t object_count, uint32 flags)
{
if (object_count == 0)
return B_OK;
BenaphoreLocker _(cache->lock);
return object_cache_reserve_internal(cache, object_count, flags);
}
slab * slab *
object_cache::InitSlab(slab *slab, void *pages, size_t byteCount) object_cache::InitSlab(slab *slab, void *pages, size_t byteCount)
{ {