summaryrefslogtreecommitdiffstats
path: root/include/slab.h
blob: 04d42e44987ddf7a316968c0cf207cb3ff94dfec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef JOVE_MEMORY_SLAB_H
#define JOVE_MEMORY_SLAB_H 1

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#define SLABCACHE_NAME_LIMIT 32
typedef struct slab_cache
{
    char name[SLABCACHE_NAME_LIMIT];

    struct slab *list_free;
    struct slab *list_partial;
    struct slab *list_full;

    size_t obj_count;
    size_t obj_size;
    size_t obj_capacity;

    size_t slab_pages;
} slab_cache_t;

typedef struct slab
{
    struct slab *prev;
    struct slab *next;
    void *slab_base;
    void *obj_base;

    size_t free_count;
    int free_index;
    uintptr_t free[];
} slab_t;

void slabcache_new(slab_cache_t *cache, char *name, size_t objsize);

void* slab_alloc(slab_cache_t *cache);
void slab_free(slab_cache_t *cache, void *ptr);

void* slab_get(slab_cache_t *cache, size_t index);
void slab_set(slab_cache_t *cache, size_t index, void *data);

#endif