1 /*****************************************************************************/
3 * \file slab/include/slab.h
4 * \brief Slab allocator API.
7 * \author Lars Reuther <reuther@os.inf.tu-dresden.de>
8 * \author Christian Helmuth <ch12@os.inf.tu-dresden.de>
10 /*****************************************************************************/
13 * (c) 2006-2009 Author(s)
14 * economic rights: Technische Universität Dresden (Germany)
16 * This file is part of TUD:OS and distributed under the terms of the
17 * GNU General Public License 2.
18 * Please see the COPYING-GPL-2 file for details.
21 /* This version supports multi-page slabs (slab size stored in cache.slab_size)
22 * and tries to follow Bonwicks original suggestion to limit internal
23 * fragmentation to 1/8 (12.5 %).
27 #ifndef _L4SLAB_SLAB_H
28 #define _L4SLAB_SLAB_H
31 #include <l4/sys/linkage.h>
32 #include <l4/sys/l4int.h>
33 #include <l4/sys/compiler.h>
35 /*****************************************************************************
37 *****************************************************************************/
40 * Slab descriptor type
42 typedef struct l4slab_slab l4slab_slab_t;
45 * \brief Slab cache descriptor type
48 typedef struct l4slab_cache l4slab_cache_t;
51 * \brief Cache grow callback function
54 * \param cache Descriptor of the slab cache which requests
56 * \retval data Slab user data pointer, the contents is returned
57 * with the slab to the release callback function.
59 * \return Pointer to the allocated slab.
61 * This function is called by a slab cache to allocate a new slab for the
62 * cache. It must return a pointer to a memory area with the size of
63 * cache->slab_size and which is slab_size aligned.
65 typedef L4_CV void * (* l4slab_grow_fn_t) (l4slab_cache_t * cache,
69 * \brief Cache release callback function
72 * \param cache Slab cache descriptor
73 * \param buffer Slab address
74 * \param data Slab user data pointer
76 * Cache release callback function. It is called by a slab cache to release
77 * slabs which are no longer needed by the cache. Slab have a size of
80 typedef L4_CV void (* l4slab_release_fn_t) (l4slab_cache_t * cache,
81 void * buffer, void * data);
84 * Slab cache descriptor
88 l4_size_t obj_size; ///< size of cache objects
89 l4_size_t slab_size; ///< size of one slab
90 int num_objs; ///< number of objects per slab
91 int num_slabs; ///< number of slabs in cache
92 int num_free; ///< number of unused slabs
93 int max_free; ///< max. allowed unused slabs
95 l4slab_slab_t * slabs_full; ///< list of completely used slabs
96 l4slab_slab_t * slabs_part; ///< list of partially used slabs
97 l4slab_slab_t * slabs_free; ///< list of unused slabs
99 l4slab_grow_fn_t grow_fn; ///< cache grow callback
100 l4slab_release_fn_t release_fn; ///< slab release callback
102 void * data; ///< application data pointer
105 #define L4SLAB_LOG_CACHE(cp) do { \
106 LOG("\033[34;1mcache at %p\033[0m", cp); \
107 LOG("\033[34;1m obj_size %u\033[0m", cp->obj_size); \
108 LOG("\033[34;1m slab_size %u\033[0m", cp->slab_size); \
109 LOG("\033[34;1m objs per slab %d\033[0m", cp->num_objs); \
110 LOG("\033[34;1m num_free %d\033[0m", cp->num_free); \
111 LOG("\033[34;1m data ptr %p\033[0m", cp->data); \
115 /*****************************************************************************
117 *****************************************************************************/
121 /*****************************************************************************/
123 * \brief Initialize slab cache.
126 * \param cache Slab cache descriptor
127 * \param size Size of the cache objects
128 * \param max_free Maximum number of free slabs allowed in the cache.
129 * If more slabs in the slab cache are freed, they are
130 * released (if a release callback function is
132 * \param grow_fn Cache grow callback function, called by the slab cache
133 * to allocate new buffers for the cache. If no function
134 * is specified the cache cannot allocate buffers on
136 * \param release_fn Slab release callback function, called by the cache to
137 * release unused buffers. If no function is specified
138 * unused buffers are not released.
140 * \return 0 on success (initialized cache descriptor), error code otherwise:
141 * - -L4_EINVAL size too big / invalid cache descriptor
143 * Setup cache descriptor. The function initializes the internal cache
144 * descriptor structures, but does not allocate any memory. Memory can be
145 * added using the l4slab_add_slab() function or memory is allocated
146 * on demand by the cache if the grow callback function is specified.
148 /*****************************************************************************/
150 l4slab_cache_init(l4slab_cache_t * cache, l4_size_t size,
151 unsigned int max_free, l4slab_grow_fn_t grow_fn,
152 l4slab_release_fn_t release_fn);
154 /*****************************************************************************/
156 * \brief Destroy slab cache
159 * \param cache Cache descriptor
161 * Release slab descriptor and free all allocated memory. This function is
162 * only useful if a release callback function is specified for the cache,
163 * otherwise it has no effect.
165 /*****************************************************************************/
167 l4slab_destroy(l4slab_cache_t * cache);
169 /*****************************************************************************/
171 * \brief Allocate object
174 * \param cache Cache descriptor
176 * \return pointer to object, NULL if allocation failed.
178 /*****************************************************************************/
180 l4slab_alloc(l4slab_cache_t * cache);
182 /*****************************************************************************/
184 * \brief Release object
187 * \param cache Cache descriptor
188 * \param objp Pointer to object
190 /*****************************************************************************/
192 l4slab_free(l4slab_cache_t * cache, void * objp);
194 /*****************************************************************************/
196 * \brief Add a slab to the slab cache
199 * \param cache Cache descriptor
200 * \param buffer Pointer to new slab
201 * \param data Application data
203 * Add the slab (buffer) to the slab cache. Buffer must be
204 * cache->slab_size-sized and slab_size aligned in memory.
206 /*****************************************************************************/
208 l4slab_add_slab(l4slab_cache_t * cache, void * buffer, void * data);
210 /*****************************************************************************/
212 * \brief Set cache application data pointer.
215 * \param cache Cache descriptor
216 * \param data Application data pointer
218 /*****************************************************************************/
220 l4slab_set_data(l4slab_cache_t * cache, void * data);
222 /*****************************************************************************/
224 * \brief Get cache application data.
227 * \param cache Cache descriptor
229 * \return Application data pointer, NULL if invalid cache descriptor or no
232 /*****************************************************************************/
234 l4slab_get_data(l4slab_cache_t * cache);
236 /*****************************************************************************/
238 * \brief Dump cache slab list
241 * \param cache Cache descriptor
242 * \param dump_free Dump free list of slabs
244 /*****************************************************************************/
246 l4slab_dump_cache(l4slab_cache_t * cache, int dump_free);
248 /*****************************************************************************/
250 * \brief Dump cache free slab list
253 * \param cache Cache descriptor
255 /*****************************************************************************/
257 l4slab_dump_cache_free(l4slab_cache_t * cache);
261 #endif /* !_L4SLAB_SLAB_H */