xref: /haiku/src/tests/add-ons/kernel/kernelland_emu/slab.cpp (revision adb0d19d561947362090081e81d90dde59142026)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <slab/Slab.h>
7 
8 #include <stdlib.h>
9 
10 #include <new>
11 
12 
13 struct object_cache {
14 	object_cache(const char *name, size_t objectSize,
15 		size_t alignment, size_t maxByteUsage, uint32 flags, void *cookie,
16 		object_cache_constructor constructor,
17 		object_cache_destructor destructor, object_cache_reclaimer reclaimer)
18 		:
19 		objectSize(objectSize),
20 		cookie(cookie),
21 		objectConstructor(constructor),
22 		objectDestructor(destructor)
23 	{
24 	}
25 
26 	size_t						objectSize;
27 	void*						cookie;
28 	object_cache_constructor	objectConstructor;
29 	object_cache_destructor		objectDestructor;
30 };
31 
32 
33 object_cache *
34 create_object_cache(const char *name, size_t objectSize,
35 	size_t alignment, void *cookie, object_cache_constructor constructor,
36 	object_cache_destructor destructor)
37 {
38 	return new(std::nothrow) object_cache(name, objectSize, alignment,
39 		0, 0, cookie, constructor, destructor, NULL);
40 }
41 
42 
43 object_cache *
44 create_object_cache_etc(const char *name, size_t objectSize,
45 	size_t alignment, size_t maxByteUsage, uint32 flags, void *cookie,
46 	object_cache_constructor constructor, object_cache_destructor destructor,
47 	object_cache_reclaimer reclaimer)
48 {
49 	return new(std::nothrow) object_cache(name, objectSize, alignment,
50 		maxByteUsage, flags, cookie, constructor, destructor, reclaimer);
51 }
52 
53 
54 void
55 delete_object_cache(object_cache *cache)
56 {
57 	delete cache;
58 }
59 
60 
61 status_t
62 object_cache_set_minimum_reserve(object_cache *cache, size_t objectCount)
63 {
64 	return B_OK;
65 }
66 
67 
68 void *
69 object_cache_alloc(object_cache *cache, uint32 flags)
70 {
71 	void* object = cache != NULL ? malloc(cache->objectSize) : NULL;
72 	if (object == NULL)
73 		return NULL;
74 
75 	if (cache->objectConstructor != NULL)
76 		cache->objectConstructor(cache->cookie, object);
77 
78 	return object;
79 }
80 
81 
82 void
83 object_cache_free(object_cache *cache, void *object)
84 {
85 	if (object != NULL) {
86 		if (cache != NULL && cache->objectDestructor != NULL)
87 			cache->objectDestructor(cache->cookie, object);
88 
89 		free(object);
90 	}
91 }
92 
93 
94 status_t
95 object_cache_reserve(object_cache *cache, size_t object_count, uint32 flags)
96 {
97 	return B_OK;
98 }
99 
100 
101 void object_cache_get_usage(object_cache *cache, size_t *_allocatedMemory)
102 {
103 	*_allocatedMemory = 0;
104 }
105