1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2010 Marek Olšák <maraeo@gmail.com>
3b8e80941Smrg * Copyright 2016 Advanced Micro Devices, Inc.
4b8e80941Smrg *
5b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
7b8e80941Smrg * to deal in the Software without restriction, including without limitation
8b8e80941Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
9b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom
10b8e80941Smrg * the Software is furnished to do so, subject to the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * The above copyright notice and this permission notice (including the next
13b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
14b8e80941Smrg * Software.
15b8e80941Smrg *
16b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23b8e80941Smrg
24b8e80941Smrg/**
25b8e80941Smrg * Slab allocator for equally sized memory allocations.
26b8e80941Smrg *
27b8e80941Smrg * Objects are allocated from "child" pools that are connected to a "parent"
28b8e80941Smrg * pool.
29b8e80941Smrg *
30b8e80941Smrg * Calls to slab_alloc/slab_free for the same child pool must not occur from
31b8e80941Smrg * multiple threads simultaneously.
32b8e80941Smrg *
33b8e80941Smrg * Allocations obtained from one child pool should usually be freed in the
34b8e80941Smrg * same child pool. Freeing an allocation in a different child pool associated
35b8e80941Smrg * to the same parent is allowed (and requires no locking by the caller), but
36b8e80941Smrg * it is discouraged because it implies a performance penalty.
37b8e80941Smrg *
38b8e80941Smrg * For convenience and to ease the transition, there is also a set of wrapper
39b8e80941Smrg * functions around a single parent-child pair.
40b8e80941Smrg */
41b8e80941Smrg
42b8e80941Smrg#ifndef SLAB_H
43b8e80941Smrg#define SLAB_H
44b8e80941Smrg
45b8e80941Smrg#include "c11/threads.h"
46b8e80941Smrg
47b8e80941Smrgstruct slab_element_header;
48b8e80941Smrgstruct slab_page_header;
49b8e80941Smrg
50b8e80941Smrgstruct slab_parent_pool {
51b8e80941Smrg   mtx_t mutex;
52b8e80941Smrg   unsigned element_size;
53b8e80941Smrg   unsigned num_elements;
54b8e80941Smrg};
55b8e80941Smrg
56b8e80941Smrgstruct slab_child_pool {
57b8e80941Smrg   struct slab_parent_pool *parent;
58b8e80941Smrg
59b8e80941Smrg   struct slab_page_header *pages;
60b8e80941Smrg
61b8e80941Smrg   /* Free elements. */
62b8e80941Smrg   struct slab_element_header *free;
63b8e80941Smrg
64b8e80941Smrg   /* Elements that are owned by this pool but were freed with a different
65b8e80941Smrg    * pool as the argument to slab_free.
66b8e80941Smrg    *
67b8e80941Smrg    * This list is protected by the parent mutex.
68b8e80941Smrg    */
69b8e80941Smrg   struct slab_element_header *migrated;
70b8e80941Smrg};
71b8e80941Smrg
72b8e80941Smrgvoid slab_create_parent(struct slab_parent_pool *parent,
73b8e80941Smrg                        unsigned item_size,
74b8e80941Smrg                        unsigned num_items);
75b8e80941Smrgvoid slab_destroy_parent(struct slab_parent_pool *parent);
76b8e80941Smrgvoid slab_create_child(struct slab_child_pool *pool,
77b8e80941Smrg                       struct slab_parent_pool *parent);
78b8e80941Smrgvoid slab_destroy_child(struct slab_child_pool *pool);
79b8e80941Smrgvoid *slab_alloc(struct slab_child_pool *pool);
80b8e80941Smrgvoid slab_free(struct slab_child_pool *pool, void *ptr);
81b8e80941Smrg
82b8e80941Smrgstruct slab_mempool {
83b8e80941Smrg   struct slab_parent_pool parent;
84b8e80941Smrg   struct slab_child_pool child;
85b8e80941Smrg};
86b8e80941Smrg
87b8e80941Smrgvoid slab_create(struct slab_mempool *mempool,
88b8e80941Smrg                 unsigned item_size,
89b8e80941Smrg                 unsigned num_items);
90b8e80941Smrgvoid slab_destroy(struct slab_mempool *mempool);
91b8e80941Smrgvoid *slab_alloc_st(struct slab_mempool *mempool);
92b8e80941Smrgvoid slab_free_st(struct slab_mempool *mempool, void *ptr);
93b8e80941Smrg
94b8e80941Smrg#endif
95