diff options
Diffstat (limited to 'mem/include')
-rw-r--r-- | mem/include/mem.h | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/mem/include/mem.h b/mem/include/mem.h index 69f426f..b3d9157 100644 --- a/mem/include/mem.h +++ b/mem/include/mem.h | |||
@@ -19,19 +19,24 @@ | |||
19 | #include <stdint.h> | 19 | #include <stdint.h> |
20 | 20 | ||
21 | /// Define a typed memory allocator backed by a statically-allocated array. | 21 | /// Define a typed memory allocator backed by a statically-allocated array. |
22 | #define DEF_MEM(MEM, TYPE, NUM_BLOCKS) \ | 22 | #define DEF_MEM(MEM, TYPE, NUM_BLOCKS) \ |
23 | typedef struct MEM { \ | 23 | typedef struct MEM { \ |
24 | Memory mem; \ | 24 | Memory mem; \ |
25 | Chunk chunks[NUM_BLOCKS]; \ | 25 | Chunk chunks[NUM_BLOCKS]; \ |
26 | TYPE blocks[NUM_BLOCKS]; \ | 26 | TYPE blocks[NUM_BLOCKS]; \ |
27 | /* For uniformity with the dynamically-allocated pool. */ \ | ||
28 | TYPE* object; \ | ||
27 | } MEM; | 29 | } MEM; |
28 | 30 | ||
29 | /// Define a typed memory allocator backed by a dynamically-allocated array. | 31 | /// Define a typed memory allocator backed by a dynamically-allocated array. |
30 | #define DEF_MEM_DYN(MEM, TYPE) \ | 32 | #define DEF_MEM_DYN(MEM, TYPE) \ |
31 | typedef struct MEM { \ | 33 | typedef struct MEM { \ |
32 | Memory mem; \ | 34 | Memory mem; \ |
33 | Chunk* chunks; \ | 35 | Chunk* chunks; \ |
34 | TYPE* blocks; \ | 36 | TYPE* blocks; \ |
37 | /* Does not point anywhere. Storing a pointer here so that we can recall \ | ||
38 | * the type. */ \ | ||
39 | TYPE* object; \ | ||
35 | } MEM; | 40 | } MEM; |
36 | 41 | ||
37 | /// Initialize a statically-backed memory allocator. | 42 | /// Initialize a statically-backed memory allocator. |
@@ -73,7 +78,7 @@ | |||
73 | /// Return a pointer to a chunk given the chunk's handle. | 78 | /// Return a pointer to a chunk given the chunk's handle. |
74 | /// The chunk must have been allocated. | 79 | /// The chunk must have been allocated. |
75 | #define mem_get_chunk(MEM, HANDLE) \ | 80 | #define mem_get_chunk(MEM, HANDLE) \ |
76 | ((__typeof__((MEM)->blocks[0])*)mem_get_chunk_(&(MEM)->mem, HANDLE)) | 81 | ((__typeof__((MEM)->object[0])*)mem_get_chunk_(&(MEM)->mem, HANDLE)) |
77 | 82 | ||
78 | /// Get the handle to the given chunk. | 83 | /// Get the handle to the given chunk. |
79 | #define mem_get_chunk_handle(MEM, CHUNK_PTR) \ | 84 | #define mem_get_chunk_handle(MEM, CHUNK_PTR) \ |
@@ -87,15 +92,16 @@ | |||
87 | /// The caller can use 'i' as the index of the current chunk. | 92 | /// The caller can use 'i' as the index of the current chunk. |
88 | /// | 93 | /// |
89 | /// It is valid to mem_free() the chunk at each step of the iteration. | 94 | /// It is valid to mem_free() the chunk at each step of the iteration. |
90 | #define mem_foreach(MEM, ITER, BODY) \ | 95 | #define mem_foreach(MEM, ITER, BODY) \ |
91 | size_t i = 0; \ | 96 | size_t i = 0; \ |
92 | do { \ | 97 | do { \ |
93 | if ((MEM)->chunks[i].used) { \ | 98 | if ((MEM)->mem.chunks[i].used) { \ |
94 | __typeof__((MEM)->blocks[0])* ITER = &(MEM)->blocks[i]; \ | 99 | __typeof__((MEM)->object[0])* ITER = \ |
95 | (void)ITER; \ | 100 | &(((__typeof__((MEM)->object[0])*)(MEM)->mem.blocks))[i]; \ |
96 | BODY; \ | 101 | (void)ITER; \ |
97 | } \ | 102 | BODY; \ |
98 | i = (MEM)->chunks[i].next; \ | 103 | } \ |
104 | i = (MEM)->chunks[i].next; \ | ||
99 | } while (i); | 105 | } while (i); |
100 | 106 | ||
101 | // ----------------------------------------------------------------------------- | 107 | // ----------------------------------------------------------------------------- |