forked from vk2tds/libosdp_arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slab.h
52 lines (44 loc) · 1.18 KB
/
slab.h
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
45
46
47
48
49
50
51
52
/*
* Copyright (c) 2020-2021 Siddharth Chandrasekaran <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#ifndef _UTILS_SLAB_H_
#define _UTILS_SLAB_H_
#include <stddef.h>
#include <stdint.h>
#include "utils.h"
typedef struct {
uint8_t *blob;
size_t size;
size_t count;
} slab_t;
/**
* @brief Initializes a resource pool of slabs (out of `blob`) that are
* at-least `slab_size` bytes long.
*
* @return -1 on errors
* @return number of slabs issuable from `blob` on success
*/
int slab_init(slab_t *slab, size_t slab_size,
uint8_t *blob, size_t blob_size);
/**
* @brief Allocates a slab of memory from the resource pool held at
* slab_t. The allocated block is at least `size` bytes large is
* guaranteed to be aligned to a power the nearest power of 2.
*
* @return -1 on errors
* @return 0 on success
*/
int slab_alloc(slab_t *slab, void **block);
/**
* @brief Releases a slab that was previously allocated by a call
* to slab_alloc(). This method can fail if the pointer passed did
* not belong to the slab pool of slab_t.
*
* @return -1 on errors
* @return 0 on success
*/
int slab_free(slab_t *slab, void *block);
#endif /* _UTILS_SLAB_H_ */