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"
30 #define ALIGN(value, align) (((value) + (align) - 1) & ~((align) - 1))
33 #define SLAB_MAGIC 0xcafe4321
34 #define SET_MAGIC(element) (element)->magic = SLAB_MAGIC
35 #define CHECK_MAGIC(element) assert((element)->magic == SLAB_MAGIC)
37 #define SET_MAGIC(element)
38 #define CHECK_MAGIC(element)
41 /* One array element within a big buffer. */
42 struct slab_element_header
{
43 /* The next free element. */
44 struct slab_element_header
*next_free
;
47 /* Use intptr_t to keep the header aligned to a pointer size. */
52 static struct slab_element_header
*
53 slab_get_element(struct slab_mempool
*pool
,
54 struct slab_page_header
*page
, unsigned index
)
56 return (struct slab_element_header
*)
57 ((uint8_t*)&page
[1] + (pool
->element_size
* index
));
61 slab_add_new_page(struct slab_mempool
*pool
)
63 struct slab_page_header
*page
;
64 struct slab_element_header
*element
;
67 page
= malloc(sizeof(struct slab_page_header
) +
68 pool
->num_elements
* pool
->element_size
);
73 make_empty_list(&pool
->list
);
75 insert_at_tail(&pool
->list
, page
);
77 /* Mark all elements as free. */
78 for (i
= 0; i
< pool
->num_elements
-1; i
++) {
79 element
= slab_get_element(pool
, page
, i
);
80 element
->next_free
= slab_get_element(pool
, page
, i
+ 1);
84 element
= slab_get_element(pool
, page
, pool
->num_elements
- 1);
85 element
->next_free
= pool
->first_free
;
87 pool
->first_free
= slab_get_element(pool
, page
, 0);
92 * Allocate an object from the slab. Single-threaded (no mutex).
95 slab_alloc_st(struct slab_mempool
*pool
)
97 struct slab_element_header
*element
;
99 /* Allocate a new page. */
100 if (!pool
->first_free
&&
101 !slab_add_new_page(pool
))
104 element
= pool
->first_free
;
105 CHECK_MAGIC(element
);
106 pool
->first_free
= element
->next_free
;
111 * Free an object allocated from the slab. Single-threaded (no mutex).
114 slab_free_st(struct slab_mempool
*pool
, void *ptr
)
116 struct slab_element_header
*element
=
117 ((struct slab_element_header
*)ptr
- 1);
119 CHECK_MAGIC(element
);
120 element
->next_free
= pool
->first_free
;
121 pool
->first_free
= element
;
125 * Allocate an object from the slab. Thread-safe.
128 slab_alloc_mt(struct slab_mempool
*pool
)
132 mtx_lock(&pool
->mutex
);
133 mem
= slab_alloc_st(pool
);
134 mtx_unlock(&pool
->mutex
);
139 * Free an object allocated from the slab. Thread-safe.
142 slab_free_mt(struct slab_mempool
*pool
, void *ptr
)
144 mtx_lock(&pool
->mutex
);
145 slab_free_st(pool
, ptr
);
146 mtx_unlock(&pool
->mutex
);
150 slab_destroy(struct slab_mempool
*pool
)
152 struct slab_page_header
*page
, *temp
;
154 if (pool
->list
.next
) {
155 foreach_s(page
, temp
, &pool
->list
) {
156 remove_from_list(page
);
161 mtx_destroy(&pool
->mutex
);
165 * Create an allocator for same-sized objects.
167 * \param item_size Size of one object.
168 * \param num_items Number of objects to allocate at once.
171 slab_create(struct slab_mempool
*pool
,
175 mtx_init(&pool
->mutex
, mtx_plain
);
176 pool
->element_size
= ALIGN(sizeof(struct slab_element_header
) + item_size
,
178 pool
->num_elements
= num_items
;
179 pool
->first_free
= NULL
;