1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* Copyright 2010 Marek Olšák <maraeo@gmail.com>
* Copyright 2016 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "slab.h"
#include "macros.h"
#include "u_atomic.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#define ALIGN(value, align) (((value) + (align) - 1) & ~((align) - 1))
#define SLAB_MAGIC_ALLOCATED 0xcafe4321
#define SLAB_MAGIC_FREE 0x7ee01234
#ifdef DEBUG
#define SET_MAGIC(element, value) (element)->magic = (value)
#define CHECK_MAGIC(element, value) assert((element)->magic == (value))
#else
#define SET_MAGIC(element, value)
#define CHECK_MAGIC(element, value)
#endif
/* One array element within a big buffer. */
struct slab_element_header {
/* The next element in the free or migrated list. */
struct slab_element_header *next;
/* This is either
* - a pointer to the child pool to which this element belongs, or
* - a pointer to the orphaned page of the element, with the least
* significant bit set to 1.
*/
intptr_t owner;
#ifdef DEBUG
intptr_t magic;
#endif
};
/* The page is an array of allocations in one block. */
struct slab_page_header {
union {
/* Next page in the same child pool. */
struct slab_page_header *next;
/* Number of remaining, non-freed elements (for orphaned pages). */
unsigned num_remaining;
} u;
/* Memory after the last member is dedicated to the page itself.
* The allocated size is always larger than this structure.
*/
};
static struct slab_element_header *
slab_get_element(struct slab_parent_pool *parent,
struct slab_page_header *page, unsigned index)
{
return (struct slab_element_header*)
((uint8_t*)&page[1] + (parent->element_size * index));
}
/* The given object/element belongs to an orphaned page (i.e. the owning child
* pool has been destroyed). Mark the element as freed and free the whole page
* when no elements are left in it.
*/
static void
slab_free_orphaned(struct slab_element_header *elt)
{
struct slab_page_header *page;
assert(elt->owner & 1);
page = (struct slab_page_header *)(elt->owner & ~(intptr_t)1);
if (!p_atomic_dec_return(&page->u.num_remaining))
free(page);
}
/**
* Create a parent pool for the allocation of same-sized objects.
*
* \param item_size Size of one object.
* \param num_items Number of objects to allocate at once.
*/
void
slab_create_parent(struct slab_parent_pool *parent,
unsigned item_size,
unsigned num_items)
{
mtx_init(&parent->mutex, mtx_plain);
parent->element_size = ALIGN(sizeof(struct slab_element_header) + item_size,
sizeof(intptr_t));
parent->num_elements = num_items;
}
void
slab_destroy_parent(struct slab_parent_pool *parent)
{
mtx_destroy(&parent->mutex);
}
/**
* Create a child pool linked to the given parent.
*/
void slab_create_child(struct slab_child_pool *pool,
struct slab_parent_pool *parent)
{
pool->parent = parent;
pool->pages = NULL;
pool->free = NULL;
pool->migrated = NULL;
}
/**
* Destroy the child pool.
*
* Pages associated to the pool will be orphaned. They are eventually freed
* when all objects in them are freed.
*/
void slab_destroy_child(struct slab_child_pool *pool)
{
mtx_lock(&pool->parent->mutex);
while (pool->pages) {
struct slab_page_header *page = pool->pages;
pool->pages = page->u.next;
p_atomic_set(&page->u.num_remaining, pool->parent->num_elements);
for (unsigned i = 0; i < pool->parent->num_elements; ++i) {
struct slab_element_header *elt = slab_get_element(pool->parent, page, i);
p_atomic_set(&elt->owner, (intptr_t)page | 1);
}
}
while (pool->migrated) {
struct slab_element_header *elt = pool->migrated;
pool->migrated = elt->next;
slab_free_orphaned(elt);
}
mtx_unlock(&pool->parent->mutex);
while (pool->free) {
struct slab_element_header *elt = pool->free;
pool->free = elt->next;
slab_free_orphaned(elt);
}
/* Guard against use-after-free. */
pool->parent = NULL;
}
static bool
slab_add_new_page(struct slab_child_pool *pool)
{
struct slab_page_header *page;
unsigned i;
page = malloc(sizeof(struct slab_page_header) +
pool->parent->num_elements * pool->parent->element_size);
if (!page)
return false;
for (unsigned i = 0; i < pool->parent->num_elements; ++i) {
struct slab_element_header *elt = slab_get_element(pool->parent, page, i);
elt->owner = (intptr_t)pool;
assert(!(elt->owner & 1));
elt->next = pool->free;
pool->free = elt;
SET_MAGIC(elt, SLAB_MAGIC_FREE);
}
page->u.next = pool->pages;
pool->pages = page;
return true;
}
/**
* Allocate an object from the child pool. Single-threaded (i.e. the caller
* must ensure that no operation happens on the same child pool in another
* thread).
*/
void *
slab_alloc(struct slab_child_pool *pool)
{
struct slab_element_header *elt;
if (!pool->free) {
/* First, collect elements that belong to us but were freed from a
* different child pool.
*/
mtx_lock(&pool->parent->mutex);
pool->free = pool->migrated;
pool->migrated = NULL;
mtx_unlock(&pool->parent->mutex);
/* Now allocate a new page. */
if (!pool->free && !slab_add_new_page(pool))
return NULL;
}
elt = pool->free;
pool->free = elt->next;
CHECK_MAGIC(elt, SLAB_MAGIC_FREE);
SET_MAGIC(elt, SLAB_MAGIC_ALLOCATED);
return &elt[1];
}
/**
* Free an object allocated from the slab. Single-threaded (i.e. the caller
* must ensure that no operation happens on the same child pool in another
* thread).
*
* Freeing an object in a different child pool from the one where it was
* allocated is allowed, as long the pool belong to the same parent. No
* additional locking is required in this case.
*/
void slab_free(struct slab_child_pool *pool, void *ptr)
{
struct slab_element_header *elt = ((struct slab_element_header*)ptr - 1);
intptr_t owner_int;
CHECK_MAGIC(elt, SLAB_MAGIC_ALLOCATED);
SET_MAGIC(elt, SLAB_MAGIC_FREE);
if (p_atomic_read(&elt->owner) == (intptr_t)pool) {
/* This is the simple case: The caller guarantees that we can safely
* access the free list.
*/
elt->next = pool->free;
pool->free = elt;
return;
}
/* The slow case: migration or an orphaned page. */
mtx_lock(&pool->parent->mutex);
/* Note: we _must_ re-read elt->owner here because the owning child pool
* may have been destroyed by another thread in the meantime.
*/
owner_int = p_atomic_read(&elt->owner);
if (!(owner_int & 1)) {
struct slab_child_pool *owner = (struct slab_child_pool *)owner_int;
elt->next = owner->migrated;
owner->migrated = elt;
mtx_unlock(&pool->parent->mutex);
} else {
mtx_unlock(&pool->parent->mutex);
slab_free_orphaned(elt);
}
}
/**
* Allocate an object from the slab. Single-threaded (no mutex).
*/
void *
slab_alloc_st(struct slab_mempool *pool)
{
return slab_alloc(&pool->child);
}
/**
* Free an object allocated from the slab. Single-threaded (no mutex).
*/
void
slab_free_st(struct slab_mempool *pool, void *ptr)
{
slab_free(&pool->child, ptr);
}
void
slab_destroy(struct slab_mempool *pool)
{
slab_destroy_child(&pool->child);
slab_destroy_parent(&pool->parent);
}
/**
* Create an allocator for same-sized objects.
*
* \param item_size Size of one object.
* \param num_items Number of objects to allocate at once.
*/
void
slab_create(struct slab_mempool *pool,
unsigned item_size,
unsigned num_items)
{
slab_create_parent(&pool->parent, item_size, num_items);
slab_create_child(&pool->child, &pool->parent);
}
|