2 * Copyright 2008, Axel Dörfler. All Rights Reserved.
3 * Copyright 2007, Hugo Santos. All Rights Reserved.
5 * Distributed under the terms of the MIT License.
9 #include "SmallObjectCache.h"
11 #include "MemoryManager.h"
12 #include "slab_private.h"
15 RANGE_MARKER_FUNCTION_BEGIN(SlabSmallObjectCache
)
19 slab_in_pages(const void *pages
, size_t slab_size
)
21 return (slab
*)(((uint8
*)pages
) + slab_size
- sizeof(slab
));
25 /*static*/ SmallObjectCache
*
26 SmallObjectCache::Create(const char* name
, size_t object_size
,
27 size_t alignment
, size_t maximum
, size_t magazineCapacity
,
28 size_t maxMagazineCount
, uint32 flags
, void* cookie
,
29 object_cache_constructor constructor
, object_cache_destructor destructor
,
30 object_cache_reclaimer reclaimer
)
32 void* buffer
= slab_internal_alloc(sizeof(SmallObjectCache
), flags
);
36 SmallObjectCache
* cache
= new(buffer
) SmallObjectCache();
38 if (cache
->Init(name
, object_size
, alignment
, maximum
, magazineCapacity
,
39 maxMagazineCount
, flags
, cookie
, constructor
, destructor
,
45 if ((flags
& CACHE_LARGE_SLAB
) != 0)
46 cache
->slab_size
= 1024 * object_size
;
48 cache
->slab_size
= SLAB_CHUNK_SIZE_SMALL
;
50 cache
->slab_size
= MemoryManager::AcceptableChunkSize(cache
->slab_size
);
57 SmallObjectCache::Delete()
59 this->~SmallObjectCache();
60 slab_internal_free(this, 0);
65 SmallObjectCache::CreateSlab(uint32 flags
)
67 if (!check_cache_quota(this))
73 status_t error
= MemoryManager::Allocate(this, flags
, pages
);
79 slab
* newSlab
= slab_in_pages(pages
, slab_size
);
80 size_t byteCount
= slab_size
- sizeof(slab
);
81 if (AllocateTrackingInfos(newSlab
, byteCount
, flags
) != B_OK
) {
82 MemoryManager::Free(pages
, flags
);
86 return InitSlab(newSlab
, pages
, byteCount
, flags
);
91 SmallObjectCache::ReturnSlab(slab
* slab
, uint32 flags
)
96 FreeTrackingInfos(slab
, flags
);
97 MemoryManager::Free(slab
->pages
, flags
);
103 SmallObjectCache::ObjectSlab(void* object
) const
105 return slab_in_pages(lower_boundary(object
, slab_size
), slab_size
);
109 RANGE_MARKER_FUNCTION_END(SlabSmallObjectCache
)