2 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
3 * Copyright 2016 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
26 #include "simple_list.h"
31 #define ALIGN(value, align) (((value) + (align) - 1) & ~((align) - 1))
34 #define SLAB_MAGIC 0xcafe4321
35 #define SET_MAGIC(element) (element)->magic = SLAB_MAGIC
36 #define CHECK_MAGIC(element) assert((element)->magic == SLAB_MAGIC)
38 #define SET_MAGIC(element)
39 #define CHECK_MAGIC(element)
42 /* One array element within a big buffer. */
43 struct slab_element_header
{
44 /* The next free element. */
45 struct slab_element_header
*next_free
;
48 /* Use intptr_t to keep the header aligned to a pointer size. */
53 static struct slab_element_header
*
54 slab_get_element(struct slab_mempool
*pool
,
55 struct slab_page_header
*page
, unsigned index
)
57 return (struct slab_element_header
*)
58 ((uint8_t*)&page
[1] + (pool
->element_size
* index
));
62 slab_add_new_page(struct slab_mempool
*pool
)
64 struct slab_page_header
*page
;
65 struct slab_element_header
*element
;
68 page
= malloc(sizeof(struct slab_page_header
) +
69 pool
->num_elements
* pool
->element_size
);
74 make_empty_list(&pool
->list
);
76 insert_at_tail(&pool
->list
, page
);
78 /* Mark all elements as free. */
79 for (i
= 0; i
< pool
->num_elements
-1; i
++) {
80 element
= slab_get_element(pool
, page
, i
);
81 element
->next_free
= slab_get_element(pool
, page
, i
+ 1);
85 element
= slab_get_element(pool
, page
, pool
->num_elements
- 1);
86 element
->next_free
= pool
->first_free
;
88 pool
->first_free
= slab_get_element(pool
, page
, 0);
93 * Allocate an object from the slab. Single-threaded (no mutex).
96 slab_alloc_st(struct slab_mempool
*pool
)
98 struct slab_element_header
*element
;
100 /* Allocate a new page. */
101 if (!pool
->first_free
&&
102 !slab_add_new_page(pool
))
105 element
= pool
->first_free
;
106 CHECK_MAGIC(element
);
107 pool
->first_free
= element
->next_free
;
112 * Free an object allocated from the slab. Single-threaded (no mutex).
115 slab_free_st(struct slab_mempool
*pool
, void *ptr
)
117 struct slab_element_header
*element
=
118 ((struct slab_element_header
*)ptr
- 1);
120 CHECK_MAGIC(element
);
121 element
->next_free
= pool
->first_free
;
122 pool
->first_free
= element
;
126 * Allocate an object from the slab. Thread-safe.
129 slab_alloc_mt(struct slab_mempool
*pool
)
133 mtx_lock(&pool
->mutex
);
134 mem
= slab_alloc_st(pool
);
135 mtx_unlock(&pool
->mutex
);
140 * Free an object allocated from the slab. Thread-safe.
143 slab_free_mt(struct slab_mempool
*pool
, void *ptr
)
145 mtx_lock(&pool
->mutex
);
146 slab_free_st(pool
, ptr
);
147 mtx_unlock(&pool
->mutex
);
151 slab_destroy(struct slab_mempool
*pool
)
153 struct slab_page_header
*page
, *temp
;
155 if (pool
->list
.next
) {
156 foreach_s(page
, temp
, &pool
->list
) {
157 remove_from_list(page
);
162 mtx_destroy(&pool
->mutex
);
166 * Create an allocator for same-sized objects.
168 * \param item_size Size of one object.
169 * \param num_items Number of objects to allocate at once.
172 slab_create(struct slab_mempool
*pool
,
176 mtx_init(&pool
->mutex
, mtx_plain
);
177 pool
->element_size
= ALIGN(sizeof(struct slab_element_header
) + item_size
,
179 pool
->num_elements
= num_items
;
180 pool
->first_free
= NULL
;