xref: /haiku/src/tests/add-ons/kernel/kernelland_emu/slab.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
1 /*
2  * Copyright 2009-2010, 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 ObjectCache {
14 	ObjectCache(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) ObjectCache(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, size_t magazineCapacity,
46 	size_t maxMagazineCount, uint32 flags, void *cookie,
47 	object_cache_constructor constructor, object_cache_destructor destructor,
48 	object_cache_reclaimer reclaimer)
49 {
50 	return new(std::nothrow) ObjectCache(name, objectSize, alignment,
51 		maxByteUsage, flags, cookie, constructor, destructor, reclaimer);
52 }
53 
54 
55 void
56 delete_object_cache(object_cache *cache)
57 {
58 	delete cache;
59 }
60 
61 
62 status_t
63 object_cache_set_minimum_reserve(object_cache *cache, size_t objectCount)
64 {
65 	return B_OK;
66 }
67 
68 
69 void *
70 object_cache_alloc(object_cache *cache, uint32 flags)
71 {
72 	void* object = cache != NULL ? malloc(cache->objectSize) : NULL;
73 	if (object == NULL)
74 		return NULL;
75 
76 	if (cache->objectConstructor != NULL)
77 		cache->objectConstructor(cache->cookie, object);
78 
79 	return object;
80 }
81 
82 
83 void
84 object_cache_free(object_cache *cache, void *object, uint32 flags)
85 {
86 	if (object != NULL) {
87 		if (cache != NULL && cache->objectDestructor != NULL)
88 			cache->objectDestructor(cache->cookie, object);
89 
90 		free(object);
91 	}
92 }
93 
94 
95 status_t
96 object_cache_reserve(object_cache *cache, size_t object_count, uint32 flags)
97 {
98 	return B_OK;
99 }
100 
101 
102 void
103 object_cache_get_usage(object_cache *cache, size_t *_allocatedMemory)
104 {
105 	*_allocatedMemory = 0;
106 }
107