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 */
17void *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 */
26void *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 */
35void *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 */
42void slab_free(const void *addr);
43
44typedef 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
54slab_t *kmemcache_create(const char *name, size_t ent_size);
55void *kmemcache_alloc(slab_t *slab);
56
57__END_DECLS
58