1 /*
2  * Copyright 2010 Marek Olšák <maraeo@gmail.com>
3  * Copyright 2016 Advanced Micro Devices, Inc.
4  *
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:
11  *
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
14  * Software.
15  *
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. */
23 
24 /**
25  * Slab allocator for equally sized memory allocations.
26  *
27  * Objects are allocated from "child" pools that are connected to a "parent"
28  * pool.
29  *
30  * Calls to slab_alloc/slab_free for the same child pool must not occur from
31  * multiple threads simultaneously.
32  *
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.
37  *
38  * For convenience and to ease the transition, there is also a set of wrapper
39  * functions around a single parent-child pair.
40  */
41 
42 #ifndef SLAB_H
43 #define SLAB_H
44 
45 #include "simple_mtx.h"
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 struct slab_element_header;
52 struct slab_page_header;
53 
54 struct slab_parent_pool {
55    simple_mtx_t mutex;
56    unsigned element_size;
57    unsigned num_elements;
58 };
59 
60 struct slab_child_pool {
61    struct slab_parent_pool *parent;
62 
63    struct slab_page_header *pages;
64 
65    /* Free elements. */
66    struct slab_element_header *free;
67 
68    /* Elements that are owned by this pool but were freed with a different
69     * pool as the argument to slab_free.
70     *
71     * This list is protected by the parent mutex.
72     */
73    struct slab_element_header *migrated;
74 };
75 
76 void slab_create_parent(struct slab_parent_pool *parent,
77                         unsigned item_size,
78                         unsigned num_items);
79 void slab_destroy_parent(struct slab_parent_pool *parent);
80 void slab_create_child(struct slab_child_pool *pool,
81                        struct slab_parent_pool *parent);
82 void slab_destroy_child(struct slab_child_pool *pool);
83 void *slab_alloc(struct slab_child_pool *pool);
84 void slab_free(struct slab_child_pool *pool, void *ptr);
85 
86 struct slab_mempool {
87    struct slab_parent_pool parent;
88    struct slab_child_pool child;
89 };
90 
91 void slab_create(struct slab_mempool *mempool,
92                  unsigned item_size,
93                  unsigned num_items);
94 void slab_destroy(struct slab_mempool *mempool);
95 void *slab_alloc_st(struct slab_mempool *mempool);
96 void slab_free_st(struct slab_mempool *mempool, void *ptr);
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif
103