1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | |
3 | #pragma once |
4 | |
5 | #include <mos/lib/structures/list.h> |
6 | #include <mos/lib/sync/spinlock.h> |
7 | #include <stddef.h> |
8 | |
9 | __BEGIN_DECLS |
10 | |
11 | /** |
12 | * @brief Allocate a block of memory from the slab allocator. |
13 | * |
14 | * @param size |
15 | * @return void* |
16 | */ |
17 | void *slab_alloc(size_t size); |
18 | |
19 | /** |
20 | * @brief Allocate a block of memory from the slab allocator and zero it. |
21 | * |
22 | * @param nmemb |
23 | * @param size |
24 | * @return void* |
25 | */ |
26 | void *slab_calloc(size_t nmemb, size_t size); |
27 | |
28 | /** |
29 | * @brief Reallocate a block of memory from the slab allocator. |
30 | * |
31 | * @param addr |
32 | * @param size |
33 | * @return void* |
34 | */ |
35 | void *slab_realloc(void *addr, size_t size); |
36 | |
37 | /** |
38 | * @brief Free a block of memory from the slab allocator. |
39 | * |
40 | * @param addr |
41 | */ |
42 | void slab_free(const void *addr); |
43 | |
44 | typedef struct |
45 | { |
46 | as_linked_list; |
47 | spinlock_t lock; |
48 | ptr_t first_free; |
49 | size_t ent_size; |
50 | const char *name; |
51 | size_t nobjs; |
52 | } slab_t; |
53 | |
54 | slab_t *kmemcache_create(const char *name, size_t ent_size); |
55 | void *kmemcache_alloc(slab_t *slab); |
56 | |
57 | __END_DECLS |
58 | |