1 /*
2 * Copyright 2008-2010, Axel Dörfler. All Rights Reserved.
3 * Copyright 2007, Hugo Santos. All Rights Reserved.
4 *
5 * Distributed under the terms of the MIT License.
6 */
7 #ifndef _SLAB_SLAB_H_
8 #define _SLAB_SLAB_H_
9
10
11 #include <heap.h>
12
13
14 enum {
15 /* object_cache_{alloc,free}() flags */
16 CACHE_DONT_WAIT_FOR_MEMORY = HEAP_DONT_WAIT_FOR_MEMORY,
17 CACHE_DONT_LOCK_KERNEL_SPACE = HEAP_DONT_LOCK_KERNEL_SPACE,
18 CACHE_PRIORITY_VIP = HEAP_PRIORITY_VIP,
19 CACHE_ALLOC_FLAGS = CACHE_DONT_WAIT_FOR_MEMORY
20 | CACHE_DONT_LOCK_KERNEL_SPACE
21 | CACHE_PRIORITY_VIP,
22
23 /* create_object_cache_etc flags */
24 CACHE_NO_DEPOT = 0x08000000,
25 CACHE_UNLOCKED_PAGES = 0x10000000, // unsupported
26 CACHE_LARGE_SLAB = 0x20000000,
27
28 /* internal */
29 CACHE_ALIGN_ON_SIZE = 0x40000000,
30 CACHE_DURING_BOOT = 0x80000000
31 };
32
33 struct ObjectCache;
34 typedef struct ObjectCache object_cache;
35
36 typedef status_t (*object_cache_constructor)(void* cookie, void* object);
37 typedef void (*object_cache_destructor)(void* cookie, void* object);
38 typedef void (*object_cache_reclaimer)(void* cookie, int32 level);
39
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 object_cache* create_object_cache(const char* name, size_t objectSize,
46 size_t alignment, void* cookie, object_cache_constructor constructor,
47 object_cache_destructor destructor);
48 object_cache* create_object_cache_etc(const char* name, size_t objectSize,
49 size_t alignment, size_t maxByteUsage, size_t magazineCapacity,
50 size_t maxMagazineCount, uint32 flags, void* cookie,
51 object_cache_constructor constructor, object_cache_destructor destructor,
52 object_cache_reclaimer reclaimer);
53
54 void delete_object_cache(object_cache* cache);
55
56 status_t object_cache_set_minimum_reserve(object_cache* cache,
57 size_t objectCount);
58
59 void* object_cache_alloc(object_cache* cache, uint32 flags);
60 void object_cache_free(object_cache* cache, void* object, uint32 flags);
61
62 status_t object_cache_reserve(object_cache* cache, size_t object_count,
63 uint32 flags);
64
65 void object_cache_get_usage(object_cache* cache, size_t* _allocatedMemory);
66
67 #ifdef __cplusplus
68 }
69 #endif
70
71
72 #ifdef __cplusplus
73
74 #include <new>
75
76
77 inline void*
new(size_t size,ObjectCache * objectCache,uint32 flags)78 operator new(size_t size, ObjectCache* objectCache, uint32 flags) throw()
79 {
80 return object_cache_alloc(objectCache, flags);
81 }
82
83
84 template<typename Type>
85 inline void
86 object_cache_delete(ObjectCache* objectCache, Type* object, uint32 flags = 0)
87 {
88 if (object != NULL) {
89 object->~Type();
90 object_cache_free(objectCache, object, flags);
91 }
92 }
93
94 #endif // __cplusplus
95
96
97 #endif /* _SLAB_SLAB_H_ */
98