-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.h
73 lines (62 loc) · 1.66 KB
/
memory.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @file memory.h
* @author Corentin Banier
* @brief Memory Pool implementation for CSCHC.
* @version 1.0
* @date 2024-08-26
*
* @copyright Copyright (c) Orange 2024. This project is released under the MIT
* License.
*
*/
#ifndef _MEMORY_H_
#define _MEMORY_H_
#include <stddef.h>
#include <stdint.h>
#define POOL_SIZE (1024 * 1024) // 1MB
/**
* @brief Struct that defines a memory pool.
*
* @details This memory pool implementation is not fragmentation-friendly.
* Indeed, no realignment is performed. Therefore, allocation/deallocation from
* the pool must be done in the correct order to prevent data fragmentation. As
* the pool is mainly used internally, we should avoid this issue.
*/
typedef struct {
uint8_t *memory; // Dynamically allocated space
size_t used; // Current memory used
size_t size; // Total amount of memory allocated
} memory_pool_t;
/**
* @brief Creates a memory pool object.
*
* @return A pointer to a dynamically allocated memory_pool_t of size POOL_SIZE.
*/
memory_pool_t *create_memory_pool(void);
/**
* @brief Pointer to track the memory_pool_t.
*/
extern memory_pool_t *pool;
/**
* @brief Frees the pool.
*/
void destroy_memory_pool(void);
/**
* @brief Initializes the pool.
*/
void init_memory_pool(void);
/**
* @brief Allocates an object from the pool according to its size.
*
* @param size The size of the object to allocate.
* @return Pointer to the allocated object.
*/
void *pool_alloc(size_t size);
/**
* @brief Deallocates a pointer given its size.
*
* @param ptr Pointer to deallocate.
* @param size The size of the pointer.
*/
void pool_dealloc(void *ptr, size_t size);
#endif // _MEMORY_H_