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. */
25 * Slab allocator for equally sized memory allocations.
27 * Objects are allocated from "child" pools that are connected to a "parent"
30 * Calls to slab_alloc/slab_free for the same child pool must not occur from
31 * multiple threads simultaneously.
33 * Allocations obtained from one child pool should usually be freed in the
34 * same child pool. Freeing an allocation in a different child pool associated
35 * to the same parent is allowed (and requires no locking by the caller), but
36 * it is discouraged because it implies a performance penalty.
38 * For convenience and to ease the transition, there is also a set of wrapper
39 * functions around a single parent-child pair.
45 #include "c11/threads.h"
47 struct slab_element_header
;
48 struct slab_page_header
;
50 struct slab_parent_pool
{
52 unsigned element_size
;
53 unsigned num_elements
;
56 struct slab_child_pool
{
57 struct slab_parent_pool
*parent
;
59 struct slab_page_header
*pages
;
62 struct slab_element_header
*free
;
64 /* Elements that are owned by this pool but were freed with a different
65 * pool as the argument to slab_free.
67 * This list is protected by the parent mutex.
69 struct slab_element_header
*migrated
;
72 void slab_create_parent(struct slab_parent_pool
*parent
,
75 void slab_destroy_parent(struct slab_parent_pool
*parent
);
76 void slab_create_child(struct slab_child_pool
*pool
,
77 struct slab_parent_pool
*parent
);
78 void slab_destroy_child(struct slab_child_pool
*pool
);
79 void *slab_alloc(struct slab_child_pool
*pool
);
80 void slab_free(struct slab_child_pool
*pool
, void *ptr
);
83 struct slab_parent_pool parent
;
84 struct slab_child_pool child
;
87 void slab_create(struct slab_mempool
*mempool
,
90 void slab_destroy(struct slab_mempool
*mempool
);
91 void *slab_alloc_st(struct slab_mempool
*mempool
);
92 void slab_free_st(struct slab_mempool
*mempool
, void *ptr
);