blob: 5a25adaf7f4aafff47d420285fe38f1691f48c70 [file] [log] [blame]
Marek Olšák761ff402016-08-27 19:52:31 +02001/*
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.
Nicolai Hähnled8cff812016-09-27 18:30:18 +020026 *
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.
Marek Olšák761ff402016-08-27 19:52:31 +020040 */
41
42#ifndef SLAB_H
43#define SLAB_H
44
45#include "c11/threads.h"
46
Nicolai Hähnled8cff812016-09-27 18:30:18 +020047struct slab_element_header;
48struct slab_page_header;
Marek Olšák761ff402016-08-27 19:52:31 +020049
Nicolai Hähnled8cff812016-09-27 18:30:18 +020050struct slab_parent_pool {
Marek Olšák761ff402016-08-27 19:52:31 +020051 mtx_t mutex;
52 unsigned element_size;
53 unsigned num_elements;
Nicolai Hähnled8cff812016-09-27 18:30:18 +020054};
55
56struct slab_child_pool {
57 struct slab_parent_pool *parent;
58
59 struct slab_page_header *pages;
60
61 /* Free elements. */
62 struct slab_element_header *free;
63
64 /* Elements that are owned by this pool but were freed with a different
65 * pool as the argument to slab_free.
66 *
67 * This list is protected by the parent mutex.
68 */
69 struct slab_element_header *migrated;
70};
71
72void slab_create_parent(struct slab_parent_pool *parent,
73 unsigned item_size,
74 unsigned num_items);
75void slab_destroy_parent(struct slab_parent_pool *parent);
76void slab_create_child(struct slab_child_pool *pool,
77 struct slab_parent_pool *parent);
78void slab_destroy_child(struct slab_child_pool *pool);
79void *slab_alloc(struct slab_child_pool *pool);
80void slab_free(struct slab_child_pool *pool, void *ptr);
81
82struct slab_mempool {
83 struct slab_parent_pool parent;
84 struct slab_child_pool child;
Marek Olšák761ff402016-08-27 19:52:31 +020085};
86
Ian Romanicka6b7d112018-11-26 10:28:02 -080087void slab_create(struct slab_mempool *mempool,
Marek Olšák761ff402016-08-27 19:52:31 +020088 unsigned item_size,
89 unsigned num_items);
Ian Romanicka6b7d112018-11-26 10:28:02 -080090void slab_destroy(struct slab_mempool *mempool);
91void *slab_alloc_st(struct slab_mempool *mempool);
92void slab_free_st(struct slab_mempool *mempool, void *ptr);
Marek Olšák761ff402016-08-27 19:52:31 +020093
94#endif