xref: /third_party/mesa3d/src/util/slab.h (revision bf215546)
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
48extern "C" {
49#endif
50
51struct slab_element_header;
52struct slab_page_header;
53
54struct slab_parent_pool {
55   simple_mtx_t mutex;
56   unsigned element_size;
57   unsigned num_elements;
58   unsigned item_size;
59};
60
61struct slab_child_pool {
62   struct slab_parent_pool *parent;
63
64   struct slab_page_header *pages;
65
66   /* Free elements. */
67   struct slab_element_header *free;
68
69   /* Elements that are owned by this pool but were freed with a different
70    * pool as the argument to slab_free.
71    *
72    * This list is protected by the parent mutex.
73    */
74   struct slab_element_header *migrated;
75};
76
77void slab_create_parent(struct slab_parent_pool *parent,
78                        unsigned item_size,
79                        unsigned num_items);
80void slab_destroy_parent(struct slab_parent_pool *parent);
81void slab_create_child(struct slab_child_pool *pool,
82                       struct slab_parent_pool *parent);
83void slab_destroy_child(struct slab_child_pool *pool);
84void *slab_alloc(struct slab_child_pool *pool);
85void *slab_zalloc(struct slab_child_pool *pool);
86void slab_free(struct slab_child_pool *pool, void *ptr);
87
88struct slab_mempool {
89   struct slab_parent_pool parent;
90   struct slab_child_pool child;
91};
92
93void slab_create(struct slab_mempool *mempool,
94                 unsigned item_size,
95                 unsigned num_items);
96void slab_destroy(struct slab_mempool *mempool);
97void *slab_alloc_st(struct slab_mempool *mempool);
98void slab_free_st(struct slab_mempool *mempool, void *ptr);
99
100#ifdef __cplusplus
101}
102#endif
103
104#endif
105