-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory_management_lazy.h
85 lines (70 loc) · 2.28 KB
/
memory_management_lazy.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
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef PQ_MEMORY_MANAGEMENT
#define PQ_MEMORY_MANAGEMENT
//==============================================================================
// DEFINES, INCLUDES, and STRUCTS
//==============================================================================
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define PQ_MEM_WIDTH 32
/**
* Basic memory pool to use for node allocation. Memory maps can be shared
* between multiple queues for the purpose of melding. The size of the pool is
* doubled when the current capacity is exceeded.
*/
typedef struct mem_map_t
{
//! number of different node types
uint32_t types;
//! sizes of single nodes
uint32_t *sizes;
uint8_t ***data;
uint8_t ****free;
uint32_t *chunk_data;
uint32_t *chunk_free;
uint32_t *index_data;
uint32_t *index_free;
} mem_map;
//==============================================================================
// PUBLIC DECLARATIONS
//==============================================================================
/**
* Creates a new memory map for the specified node sizes
*
* @param types The number of different types of nodes to manage
* @param size Sizes of a single node of each type
* @return Pointer to the new memory map
*/
mem_map* mm_create( uint32_t types, uint32_t *sizes );
/**
* Releases all allocated memory associated with the map.
*
* @param map Map to deallocate
*/
void mm_destroy( mem_map *map );
/**
* Resets map to initial state. Does not deallocate memory.
*
* @param map Map to reset
*/
void mm_clear( mem_map *map );
/**
* Allocates a single node from the memory pool. First attempts to recycle old
* data from the free list. If there is nothing to recycle, then it takes a new
* node off the allocated list. Zeroes the memory of the allocated node.
*
* @param map Map from which to allocate
* @param type Type of node to allocate
* @return Pointer to allocated node
*/
void* pq_alloc_node( mem_map *map, uint32_t type );
/**
* Takes a previously allocated node and adds it to the free list to be
* recycled with further allocation requests.
*
* @param map Map to which the node belongs
* @param type Type of node to free
* @param node Node to free
*/
void pq_free_node( mem_map *map, uint32_t type, void *node );
#endif