demake 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/demake +199 -6
- data/lib/apps/example/Makefile +0 -0
- data/lib/apps/example/demake/auto_bits.rb +59 -0
- data/lib/apps/example/demake/auto_list.rb +60 -0
- data/lib/apps/example/src/Makefile +6 -0
- data/lib/apps/example/src/goodbye.c +0 -0
- data/lib/apps/example/src/hello.c +0 -0
- data/lib/apps/example/src/string/string.c +0 -0
- data/lib/apps/example/src/string/string.h +0 -0
- data/lib/apps/oreo/Makefile +0 -0
- data/lib/apps/oreo/src/Makefile +6 -0
- data/lib/apps/oreo/src/defines.h +0 -0
- data/lib/apps/oreo/src/fast_read_file.h +0 -0
- data/lib/apps/oreo/src/oreo.c +0 -0
- data/lib/apps/oreo/src/typedefs.h +0 -0
- data/lib/data/libsrc/auto_bits.c +41 -0
- data/lib/data/libsrc/auto_bits.h +212 -4
- data/lib/data/libsrc/auto_bits.rb +9 -0
- data/lib/data/libsrc/auto_list.rb +9 -0
- data/lib/data/libsrc/auto_string.c +300 -0
- data/lib/data/libsrc/auto_string.h +1023 -0
- data/lib/data/libsrc/auto_string_clamp_test.c +52 -0
- data/lib/data/libsrc/base.h +0 -0
- data/lib/data/libsrc/cpu_mark_check.h +0 -0
- data/lib/data/libsrc/defines.h +29 -3
- data/lib/data/libsrc/fast_read_file.h +0 -0
- data/lib/data/libsrc/fast_sha2.h +0 -0
- data/lib/data/libsrc/memory_arena.c +32 -0
- data/lib/data/libsrc/memory_arena.h +345 -0
- data/lib/data/libsrc/memory_arena_test.c +173 -0
- data/lib/data/libsrc/parse_arguments.h +0 -0
- data/lib/data/libsrc/rb_library.c +0 -0
- data/lib/data/libsrc/simple_linked_list.c +42 -22
- data/lib/data/libsrc/simple_linked_list.h +140 -56
- data/lib/data/libsrc/typedefs.h +5 -3
- data/lib/to_plural.rb +126 -0
- metadata +24 -13
- data/lib/data/libsrc/simple_linked_list +0 -0
- data/lib/data/libsrc/smart_alloc.h +0 -45
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/*
|
|
2
|
+
!!! IMPORTANT NOTE ON CLAMP !!!
|
|
3
|
+
|
|
4
|
+
This works:
|
|
5
|
+
|
|
6
|
+
c8 my_string[] = "This will work. Pretty cool, right?";
|
|
7
|
+
|
|
8
|
+
!!! Can't clamp on: *char literals !!!
|
|
9
|
+
!!! !!!
|
|
10
|
+
!!! Can only clamp on char[] and heap !!!
|
|
11
|
+
!!! !!!
|
|
12
|
+
!!! This will segfault: !!!
|
|
13
|
+
|
|
14
|
+
c8 *my_string2 = "This will segfault if you try to clamp it. Unfortunately.";
|
|
15
|
+
|
|
16
|
+
view2 = auto_view(my_string2);
|
|
17
|
+
auto_view_chop_right(&view2, 15);
|
|
18
|
+
|
|
19
|
+
This will segfault:
|
|
20
|
+
|
|
21
|
+
auto_view_clamp(&view2));
|
|
22
|
+
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#define AUTO_STRING_IMPLEMENTATION
|
|
26
|
+
#include "auto_string.h"
|
|
27
|
+
|
|
28
|
+
i32 main(i32 argc, c8 *argv[])
|
|
29
|
+
{
|
|
30
|
+
AutoView view = { 0 }, view2 = { 0 };
|
|
31
|
+
c8 my_string[] = "This will work. Pretty cool, right?";
|
|
32
|
+
c8 *my_string2 = "This will segfault if you try to clamp it. Unfortunately.";
|
|
33
|
+
|
|
34
|
+
view = auto_view(my_string);
|
|
35
|
+
auto_view_chop_right(&view, 20);
|
|
36
|
+
printf("view: ");
|
|
37
|
+
auto_view_print(&view);
|
|
38
|
+
printf("\n");
|
|
39
|
+
|
|
40
|
+
printf("view clamped: %s\n", auto_view_clamp(&view));
|
|
41
|
+
printf("my_string clamped: %s\n", my_string);
|
|
42
|
+
auto_view_unclamp(&view);
|
|
43
|
+
printf("my_string unclamped: %s\n", my_string);
|
|
44
|
+
|
|
45
|
+
view2 = auto_view(my_string2);
|
|
46
|
+
auto_view_chop_right(&view2, 15);
|
|
47
|
+
printf("view2: ");
|
|
48
|
+
auto_view_print(&view2);
|
|
49
|
+
printf("\n");
|
|
50
|
+
|
|
51
|
+
exit(0);
|
|
52
|
+
}
|
data/lib/data/libsrc/base.h
CHANGED
|
File without changes
|
|
File without changes
|
data/lib/data/libsrc/defines.h
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
defines.h -- Basic C preprocessor macro definitions
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
#ifndef DEFINES_H_Minaswan /*
|
|
5
|
+
#ifndef DEFINES_H_Minaswan /* Prevent multiple inclusions */
|
|
6
6
|
#define DEFINES_H_Minaswan
|
|
7
|
-
#define DEFINES_VERSION "0.1.
|
|
7
|
+
#define DEFINES_VERSION "0.1.1"
|
|
8
8
|
#ifndef NULL
|
|
9
9
|
#define NULL ((void *) 0)
|
|
10
10
|
#endif
|
|
@@ -26,4 +26,30 @@
|
|
|
26
26
|
#ifndef ON
|
|
27
27
|
#define ON 1
|
|
28
28
|
#endif
|
|
29
|
-
#
|
|
29
|
+
#ifndef MIN
|
|
30
|
+
#define MIN(x, y) (x) < (y) ? (x) : (y)
|
|
31
|
+
#endif
|
|
32
|
+
#ifndef MAX
|
|
33
|
+
#define MAX(x, y) (x) > (y) ? (x) : (y)
|
|
34
|
+
#endif
|
|
35
|
+
#ifndef KB
|
|
36
|
+
#define KB(x) ((u64)(x) * 1024)
|
|
37
|
+
#endif
|
|
38
|
+
#ifndef MB
|
|
39
|
+
#define MB(x) ((u64)(x) * 1024 * 1024)
|
|
40
|
+
#endif
|
|
41
|
+
#ifndef GB
|
|
42
|
+
#define GB(x) ((u64)(x) * 1024 * 1024 * 1024)
|
|
43
|
+
#endif
|
|
44
|
+
#ifndef TO_UPPER_CASE
|
|
45
|
+
#define TO_UPPER_CASE(x) (((uc8)(x) >= (uc8)'a' && (uc8)(x) <= (uc8)'z') ? \
|
|
46
|
+
(uc8)((uc8)(x) - (uc8)'a' + (uc8)'A') : (uc8)(x))
|
|
47
|
+
#endif
|
|
48
|
+
#ifndef TO_LOWER_CASE
|
|
49
|
+
#define TO_LOWER_CASE(x) (((uc8)(x) >= (uc8)'A' && (uc8)(x) <= (uc8)'Z') ? \
|
|
50
|
+
(uc8)((uc8)(x) - (uc8)'A' + (uc8)'a') : (uc8)(x))
|
|
51
|
+
#endif
|
|
52
|
+
#ifndef IS_WHITE_SPACE
|
|
53
|
+
#define IS_WHITE_SPACE(x) ((uc8)(x) == ' ' || (uc8)(x) - (uc8)'\t' < 5)
|
|
54
|
+
#endif
|
|
55
|
+
#endif /* DEFINES_H_Minaswan */
|
|
File without changes
|
data/lib/data/libsrc/fast_sha2.h
CHANGED
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#define MEMORY_ARENA_IMPLEMENTATION
|
|
2
|
+
#include "memory_arena.h"
|
|
3
|
+
|
|
4
|
+
i32 main(i32 argc, c8 *argv[])
|
|
5
|
+
{
|
|
6
|
+
i32 memory_arena_identifier = 0;
|
|
7
|
+
c8 *data1 = NULL, *data2 = NULL, *data3 = NULL;
|
|
8
|
+
|
|
9
|
+
printf("Direct allocate 4096 bytes in literal memory arena 1\n");
|
|
10
|
+
data1 = memory_arena_allocate(1, 4096);
|
|
11
|
+
printf("Direct allocate 16 KB in literal memory arena 2\n");
|
|
12
|
+
data2 = memory_arena_allocate(2, KB(16));
|
|
13
|
+
printf("Initialize a 4 MB block in a new (3) memory arena\n");
|
|
14
|
+
memory_arena_identifier = memory_arena_init(MB(4));
|
|
15
|
+
printf("Allocate 1 MB block from existing memory arena (3)\n");
|
|
16
|
+
data3 = memory_arena_allocate(memory_arena_identifier, MB(1));
|
|
17
|
+
memory_arena_show();
|
|
18
|
+
printf("Reset all memory in existing arena (3)\n");
|
|
19
|
+
memory_arena_reset(memory_arena_identifier);
|
|
20
|
+
memory_arena_show();
|
|
21
|
+
printf("Free all memory in existing arena (3)\n");
|
|
22
|
+
memory_arena_free(memory_arena_identifier);
|
|
23
|
+
memory_arena_show();
|
|
24
|
+
printf("Reset all memory in all arenas\n");
|
|
25
|
+
memory_arena_reset_all();
|
|
26
|
+
memory_arena_show();
|
|
27
|
+
printf("Free all memory\n");
|
|
28
|
+
memory_arena_free_all();
|
|
29
|
+
memory_arena_show();
|
|
30
|
+
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
memory_arena.h -- *Full Library* Header File
|
|
4
|
+
|
|
5
|
+
Used to create areas / regions of memory
|
|
6
|
+
|
|
7
|
+
- Uses basic 32-bit integers as identifiers for lifetimes
|
|
8
|
+
- Automatically expands when new allocations don't fit existing
|
|
9
|
+
- Automatically zeros data blocks upon reset / free
|
|
10
|
+
|
|
11
|
+
See the end of this file for an example.
|
|
12
|
+
|
|
13
|
+
Public Functions:
|
|
14
|
+
|
|
15
|
+
static u32 memory_arena_init(i64 capacity);
|
|
16
|
+
static void *memory_arena_allocate(u32 identifier, i64 capacity);
|
|
17
|
+
static void memory_arena_reset(u32 identifier);
|
|
18
|
+
static void memory_arena_reset_all(void);
|
|
19
|
+
static void memory_arena_free(u32 identifier);
|
|
20
|
+
static void memory_arena_free_all(void);
|
|
21
|
+
static void memory_arena_show(void);
|
|
22
|
+
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#ifndef MEMORY_ARENA_H_Minaswan /* Prevent multiple inclusions */
|
|
26
|
+
#define MEMORY_ARENA_H_Minaswan
|
|
27
|
+
#define MEMORY_ARENA_VERSION "0.1.0"
|
|
28
|
+
#ifndef TYPEDEFS_H_Minaswan /* Prevents multiple inclusions */
|
|
29
|
+
#define TYPEDEFS_H_Minaswan
|
|
30
|
+
#define TYPEDEFS_VERSION "0.1.1"
|
|
31
|
+
#include <stdint.h>
|
|
32
|
+
|
|
33
|
+
typedef uint8_t b8; /* Booleans */
|
|
34
|
+
typedef uint16_t b16;
|
|
35
|
+
typedef uint32_t b32;
|
|
36
|
+
typedef uint64_t b64;
|
|
37
|
+
|
|
38
|
+
typedef char c8; /* Characters */
|
|
39
|
+
typedef unsigned char uc8;
|
|
40
|
+
typedef char as8; /* Auto string */
|
|
41
|
+
|
|
42
|
+
typedef uint8_t u8; /* Unsigned Numbers */
|
|
43
|
+
typedef uint16_t u16;
|
|
44
|
+
typedef uint32_t u32;
|
|
45
|
+
typedef uint64_t u64;
|
|
46
|
+
|
|
47
|
+
typedef int8_t i8; /* Signed Numbers */
|
|
48
|
+
typedef int16_t i16;
|
|
49
|
+
typedef int32_t i32;
|
|
50
|
+
typedef int64_t i64;
|
|
51
|
+
|
|
52
|
+
typedef float f32; /* Floating Point Numbers */
|
|
53
|
+
typedef double f64;
|
|
54
|
+
#endif /* TYPEDEFS_H_Minaswan */
|
|
55
|
+
|
|
56
|
+
#ifndef DEFINES_H_Minaswan /* Prevents multiple inclusions */
|
|
57
|
+
#define DEFINES_H_Minaswan
|
|
58
|
+
#define DEFINES_VERSION "0.1.1"
|
|
59
|
+
#ifndef NULL
|
|
60
|
+
#define NULL ((void *) 0)
|
|
61
|
+
#endif
|
|
62
|
+
#ifndef FALSE
|
|
63
|
+
#define FALSE 0
|
|
64
|
+
#endif
|
|
65
|
+
#ifndef TRUE
|
|
66
|
+
#define TRUE 1
|
|
67
|
+
#endif
|
|
68
|
+
#ifndef NO
|
|
69
|
+
#define NO 0
|
|
70
|
+
#endif
|
|
71
|
+
#ifndef YES
|
|
72
|
+
#define YES 1
|
|
73
|
+
#endif
|
|
74
|
+
#ifndef OFF
|
|
75
|
+
#define OFF 0
|
|
76
|
+
#endif
|
|
77
|
+
#ifndef ON
|
|
78
|
+
#define ON 1
|
|
79
|
+
#endif
|
|
80
|
+
#ifndef MIN
|
|
81
|
+
#define MIN(x, y) (x) < (y) ? (x) : (y)
|
|
82
|
+
#endif
|
|
83
|
+
#ifndef MAX
|
|
84
|
+
#define MAX(x, y) (x) > (y) ? (x) : (y)
|
|
85
|
+
#endif
|
|
86
|
+
#ifndef KB
|
|
87
|
+
#define KB(x) ((u64)(x) * 1024)
|
|
88
|
+
#endif
|
|
89
|
+
#ifndef MB
|
|
90
|
+
#define MB(x) ((u64)(x) * 1024 * 1024)
|
|
91
|
+
#endif
|
|
92
|
+
#ifndef GB
|
|
93
|
+
#define GB(x) ((u64)(x) * 1024 * 1024 * 1024)
|
|
94
|
+
#endif
|
|
95
|
+
#endif /* DEFINES_H_Minaswan */
|
|
96
|
+
|
|
97
|
+
#include <stdio.h>
|
|
98
|
+
#include <stdlib.h>
|
|
99
|
+
#include <string.h>
|
|
100
|
+
|
|
101
|
+
/* Public Functions */
|
|
102
|
+
static u32 memory_arena_init(i64 capacity);
|
|
103
|
+
static void *memory_arena_allocate(u32 identifier, i64 capacity);
|
|
104
|
+
static void memory_arena_reset(u32 identifier);
|
|
105
|
+
static void memory_arena_reset_all(void);
|
|
106
|
+
static void memory_arena_free(u32 identifier);
|
|
107
|
+
static void memory_arena_free_all(void);
|
|
108
|
+
static void memory_arena_show(void);
|
|
109
|
+
|
|
110
|
+
#ifdef MEMORY_ARENA_IMPLEMENTATION
|
|
111
|
+
#ifndef DEFAULT_MEMORY_ARENA_CAPACITY
|
|
112
|
+
#define DEFAULT_MEMORY_ARENA_CAPACITY 4096
|
|
113
|
+
#endif
|
|
114
|
+
|
|
115
|
+
struct memory_arena_data {
|
|
116
|
+
u64 capacity;
|
|
117
|
+
u64 position;
|
|
118
|
+
u32 identifier;
|
|
119
|
+
void *data;
|
|
120
|
+
|
|
121
|
+
struct memory_arena_data *next;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
struct memory_arena_data *memory_arena_list = NULL;
|
|
125
|
+
|
|
126
|
+
static u32 memory_arena_init(i64 capacity)
|
|
127
|
+
{
|
|
128
|
+
struct memory_arena_data *arena = NULL, *next = NULL;
|
|
129
|
+
u32 next_identifier = 0;
|
|
130
|
+
|
|
131
|
+
for(arena = memory_arena_list; arena; arena = arena->next) {
|
|
132
|
+
if(arena->identifier > next_identifier)
|
|
133
|
+
next_identifier = arena->identifier;
|
|
134
|
+
}
|
|
135
|
+
next_identifier++;
|
|
136
|
+
if(capacity == 0)
|
|
137
|
+
capacity = DEFAULT_MEMORY_ARENA_CAPACITY;
|
|
138
|
+
else if(capacity < 0) /* Negative is multiples of default */
|
|
139
|
+
capacity *= -DEFAULT_MEMORY_ARENA_CAPACITY;
|
|
140
|
+
|
|
141
|
+
arena = calloc(1, sizeof(struct memory_arena_data));
|
|
142
|
+
arena->capacity = capacity;
|
|
143
|
+
arena->position = 0;
|
|
144
|
+
arena->identifier = next_identifier;
|
|
145
|
+
arena->data = calloc(capacity, sizeof(c8));
|
|
146
|
+
arena->next = NULL;
|
|
147
|
+
if(!memory_arena_list)
|
|
148
|
+
memory_arena_list = arena;
|
|
149
|
+
else {
|
|
150
|
+
next = memory_arena_list;
|
|
151
|
+
while(next && next->next)
|
|
152
|
+
next = next->next;
|
|
153
|
+
next->next = arena;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return arena->identifier;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static void *memory_arena_allocate(u32 identifier, i64 capacity)
|
|
160
|
+
{
|
|
161
|
+
struct memory_arena_data *arena = NULL, *next = NULL;
|
|
162
|
+
u64 position = 0;
|
|
163
|
+
|
|
164
|
+
if(capacity == 0)
|
|
165
|
+
capacity = DEFAULT_MEMORY_ARENA_CAPACITY;
|
|
166
|
+
else if(capacity < 0) /* Negative is multiples of default */
|
|
167
|
+
capacity *= -DEFAULT_MEMORY_ARENA_CAPACITY;
|
|
168
|
+
|
|
169
|
+
for(arena = memory_arena_list; arena; arena = arena->next) {
|
|
170
|
+
if(arena->identifier == identifier) {
|
|
171
|
+
if(arena->capacity >= capacity) {
|
|
172
|
+
position = arena->position;
|
|
173
|
+
arena->position += capacity;
|
|
174
|
+
arena->capacity -= capacity;
|
|
175
|
+
return ((uc8 *)arena->data + position);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/* Didn't find an arena or we found an arena but it didn't have space */
|
|
180
|
+
arena = calloc(1, sizeof(struct memory_arena_data));
|
|
181
|
+
arena->capacity = 0;
|
|
182
|
+
arena->position = capacity;
|
|
183
|
+
arena->identifier = identifier;
|
|
184
|
+
arena->data = calloc(capacity, sizeof(c8));
|
|
185
|
+
arena->next = NULL;
|
|
186
|
+
if(!memory_arena_list)
|
|
187
|
+
memory_arena_list = arena;
|
|
188
|
+
else {
|
|
189
|
+
next = memory_arena_list;
|
|
190
|
+
while(next && next->next)
|
|
191
|
+
next = next->next;
|
|
192
|
+
next->next = arena;
|
|
193
|
+
}
|
|
194
|
+
return arena->data;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static void memory_wipe(void *data, u64 capacity)
|
|
198
|
+
{
|
|
199
|
+
volatile unsigned char *volatile_data = (volatile unsigned char *)data;
|
|
200
|
+
|
|
201
|
+
if(!volatile_data || !data || capacity == 0)
|
|
202
|
+
return;
|
|
203
|
+
|
|
204
|
+
while(capacity--)
|
|
205
|
+
*volatile_data++ = 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static void memory_arena_reset(u32 identifier)
|
|
209
|
+
{
|
|
210
|
+
struct memory_arena_data *arena = NULL;
|
|
211
|
+
|
|
212
|
+
for(arena = memory_arena_list; arena; arena = arena->next) {
|
|
213
|
+
if(arena->identifier == identifier) {
|
|
214
|
+
arena->capacity += arena->position;
|
|
215
|
+
memory_wipe(arena->data, arena->capacity);
|
|
216
|
+
arena->position = 0;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static void memory_arena_reset_all(void)
|
|
222
|
+
{
|
|
223
|
+
struct memory_arena_data *arena = NULL;
|
|
224
|
+
|
|
225
|
+
for(arena = memory_arena_list; arena; arena = arena->next) {
|
|
226
|
+
arena->capacity += arena->position;
|
|
227
|
+
memory_wipe(arena->data, arena->capacity);
|
|
228
|
+
arena->position = 0;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
static void memory_arena_free(u32 identifier)
|
|
233
|
+
{
|
|
234
|
+
struct memory_arena_data *arena = NULL, *next = NULL;
|
|
235
|
+
|
|
236
|
+
arena = memory_arena_list;
|
|
237
|
+
while(arena && arena->identifier == identifier) {
|
|
238
|
+
next = arena->next;
|
|
239
|
+
memory_wipe(arena->data, arena->capacity + arena->position);
|
|
240
|
+
free(arena->data);
|
|
241
|
+
free(arena);
|
|
242
|
+
memory_arena_list = arena = next;
|
|
243
|
+
}
|
|
244
|
+
for(; arena; arena = next) {
|
|
245
|
+
next = arena->next;
|
|
246
|
+
while(next && next->identifier == identifier) {
|
|
247
|
+
if(next->next)
|
|
248
|
+
arena->next = next->next;
|
|
249
|
+
else
|
|
250
|
+
arena->next = NULL;
|
|
251
|
+
memory_wipe(arena->data, arena->capacity + arena->position);
|
|
252
|
+
free(next->data);
|
|
253
|
+
free(next);
|
|
254
|
+
next = arena->next;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
static void memory_arena_free_all(void)
|
|
260
|
+
{
|
|
261
|
+
struct memory_arena_data *arena = NULL, *next = NULL;
|
|
262
|
+
|
|
263
|
+
for(arena = memory_arena_list; arena; arena = next) {
|
|
264
|
+
next = arena->next;
|
|
265
|
+
memory_wipe(arena->data, arena->capacity + arena->position);
|
|
266
|
+
free(arena->data);
|
|
267
|
+
free(arena);
|
|
268
|
+
}
|
|
269
|
+
memory_arena_list = NULL;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
static void memory_arena_show(void)
|
|
273
|
+
{
|
|
274
|
+
struct memory_arena_data *arena = NULL;
|
|
275
|
+
u32 top_identifier = 0, identifier = 0;
|
|
276
|
+
b8 found = FALSE;
|
|
277
|
+
|
|
278
|
+
for(arena = memory_arena_list; arena; arena = arena->next) {
|
|
279
|
+
if(arena->identifier > top_identifier)
|
|
280
|
+
top_identifier = arena->identifier;
|
|
281
|
+
}
|
|
282
|
+
if(!top_identifier)
|
|
283
|
+
printf("\nNo memory arenas are currently created.\n");
|
|
284
|
+
else {
|
|
285
|
+
printf("\nCurrent Memory Arenas - Version: %s\n\n", MEMORY_ARENA_VERSION);
|
|
286
|
+
printf("Id Used Unused Total Data / Location\n");
|
|
287
|
+
printf("-------------------------------------------------------\n");
|
|
288
|
+
while(identifier <= top_identifier) {
|
|
289
|
+
for(arena = memory_arena_list; arena; arena = arena->next) {
|
|
290
|
+
if(arena->identifier == identifier) {
|
|
291
|
+
if(!found)
|
|
292
|
+
printf("%-6d %-10ld %-10ld %-10ld %p\n", arena->identifier, arena->position,
|
|
293
|
+
arena->capacity, arena->position + arena->capacity, arena->data);
|
|
294
|
+
else
|
|
295
|
+
printf(" %-4d %-10ld %-10ld %-10ld %p\n", arena->identifier, arena->position,
|
|
296
|
+
arena->capacity, arena->position + arena->capacity, arena->data);
|
|
297
|
+
found = TRUE;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
identifier++;
|
|
301
|
+
found = FALSE;
|
|
302
|
+
}
|
|
303
|
+
printf("-------------------------------------------------------\n");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
#endif /* MEMORY_ARENA_IMPLEMENTATION */
|
|
307
|
+
#endif /* MEMORY_ARENA_H_Minaswan */
|
|
308
|
+
|
|
309
|
+
/*
|
|
310
|
+
Example:
|
|
311
|
+
|
|
312
|
+
#define MEMORY_ARENA_IMPLEMENTATION
|
|
313
|
+
#include "memory_arena.h"
|
|
314
|
+
|
|
315
|
+
i32 main(i32 argc, c8 *argv[])
|
|
316
|
+
{
|
|
317
|
+
i32 memory_arena_identifier = 0;
|
|
318
|
+
c8 *data1 = NULL, *data2 = NULL, *data3 = NULL;
|
|
319
|
+
|
|
320
|
+
printf("Direct allocate 4096 bytes in literal memory arena 1\n");
|
|
321
|
+
data1 = memory_arena_allocate(1, 4096);
|
|
322
|
+
printf("Direct allocate 16 KB in literal memory arena 2\n");
|
|
323
|
+
data2 = memory_arena_allocate(2, KB(16));
|
|
324
|
+
printf("Initialize a 4 MB block in a new (3) memory arena\n");
|
|
325
|
+
memory_arena_identifier = memory_arena_init(MB(4));
|
|
326
|
+
printf("Allocate 1 MB block from existing memory arena (3)\n");
|
|
327
|
+
data3 = memory_arena_allocate(memory_arena_identifier, MB(1));
|
|
328
|
+
memory_arena_show();
|
|
329
|
+
printf("Reset all memory in existing arena (3)\n");
|
|
330
|
+
memory_arena_reset(memory_arena_identifier);
|
|
331
|
+
memory_arena_show();
|
|
332
|
+
printf("Free all memory in existing arena (3)\n");
|
|
333
|
+
memory_arena_free(memory_arena_identifier);
|
|
334
|
+
memory_arena_show();
|
|
335
|
+
printf("Reset all memory in all arenas\n");
|
|
336
|
+
memory_arena_reset_all();
|
|
337
|
+
memory_arena_show();
|
|
338
|
+
printf("Free all memory\n");
|
|
339
|
+
memory_arena_free_all();
|
|
340
|
+
memory_arena_show();
|
|
341
|
+
|
|
342
|
+
return 0;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
*/
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#define MEMORY_ARENA_IMPLEMENTATION
|
|
2
|
+
#include "memory_arena.h"
|
|
3
|
+
|
|
4
|
+
void show_arena_data(i32 arena_id, c8 *a, c8 *b, c8 *c)
|
|
5
|
+
{
|
|
6
|
+
printf("data%da = %-44.44s ...\n", arena_id, a);
|
|
7
|
+
printf("data%da strlen = %ld\n", arena_id, strlen(a));
|
|
8
|
+
printf("data%db = %-44.44s ...\n", arena_id, b);
|
|
9
|
+
printf("data%db strlen = %ld\n", arena_id, strlen(b));
|
|
10
|
+
printf("data%dc = %-44.44s ...\n", arena_id, c);
|
|
11
|
+
printf("data%dc strlen = %ld\n", arena_id, strlen(c));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
i32 main(i32 argc, c8 *argv[])
|
|
15
|
+
{
|
|
16
|
+
i32 i = 0, j = 0, memory_arena_identifier1 = 0, memory_arena_identifier2 = 0;
|
|
17
|
+
c8 *data1a = NULL, *data1b = NULL, *data1c = NULL;
|
|
18
|
+
c8 *data2a = NULL, *data2b = NULL, *data2c = NULL;
|
|
19
|
+
c8 *data3a = NULL, *data3b = NULL, *data3c = NULL;
|
|
20
|
+
i32 *other_data = NULL, *medium_data = NULL;
|
|
21
|
+
f64 *large_data = NULL, *large_data2 = NULL;
|
|
22
|
+
|
|
23
|
+
printf("\nDirect allocate using 1 (literal) with 3 x 4096 byte allocations, filled with some data:\n");
|
|
24
|
+
data1a = memory_arena_allocate(1, 4096);
|
|
25
|
+
j = 'a';
|
|
26
|
+
for(i = 0; i < 4095; i++)
|
|
27
|
+
data1a[i] = j + i % 26;
|
|
28
|
+
data1a[4095] = '\0';
|
|
29
|
+
data1b = memory_arena_allocate(1, 4096);
|
|
30
|
+
j = 'A';
|
|
31
|
+
for(i = 0; i < 4095; i++)
|
|
32
|
+
data1b[i] = j + i % 26;
|
|
33
|
+
data1b[4095] = '\0';
|
|
34
|
+
data1c = memory_arena_allocate(1, 4096);
|
|
35
|
+
j = '0';
|
|
36
|
+
for(i = 0; i < 4095; i++)
|
|
37
|
+
data1c[i] = j + i % 10;
|
|
38
|
+
data1c[4095] = '\0';
|
|
39
|
+
show_arena_data(1, data1a, data1b, data1c);
|
|
40
|
+
|
|
41
|
+
printf("\nDirect allocate using 2 (literal) with 3 x 8 KB allocations, filled with some data:\n");
|
|
42
|
+
data2a = memory_arena_allocate(2, KB(8));
|
|
43
|
+
j = 'a';
|
|
44
|
+
for(i = 0; i < KB(8) - 1; i++)
|
|
45
|
+
data2a[i] = j + i % 26;
|
|
46
|
+
data2a[KB(8) - 1] = '\0';
|
|
47
|
+
data2b = memory_arena_allocate(2, KB(8));
|
|
48
|
+
j = 'A';
|
|
49
|
+
for(i = 0; i < KB(8) - 1; i++)
|
|
50
|
+
data2b[i] = j + i % 26;
|
|
51
|
+
data2b[KB(8) - 1] = '\0';
|
|
52
|
+
data2c = memory_arena_allocate(2, KB(8));
|
|
53
|
+
j = '0';
|
|
54
|
+
for(i = 0; i < KB(8) - 1; i++)
|
|
55
|
+
data2c[i] = j + i % 10;
|
|
56
|
+
data2c[KB(8) - 1] = '\0';
|
|
57
|
+
show_arena_data(2, data2a, data2b, data2c);
|
|
58
|
+
|
|
59
|
+
printf("\nDirect allocate using 3 (literal) with 8 KB, 128 KB and 16 KB blocks, fill with some data:\n");
|
|
60
|
+
data3a = memory_arena_allocate(3, 8192);
|
|
61
|
+
j = 'a';
|
|
62
|
+
for(i = 0; i < 8191; i++)
|
|
63
|
+
data3a[i] = j + i % 26;
|
|
64
|
+
data3a[8191] = '\0';
|
|
65
|
+
data3b = memory_arena_allocate(3, KB(128));
|
|
66
|
+
j = 'A';
|
|
67
|
+
for(i = 0; i < KB(128) - 1; i++)
|
|
68
|
+
data3b[i] = j + i % 26;
|
|
69
|
+
data3b[KB(128) - 1] = '\0';
|
|
70
|
+
data3c = memory_arena_allocate(3, KB(16));
|
|
71
|
+
j = '0';
|
|
72
|
+
for(i = 0; i < KB(16) - 1; i++)
|
|
73
|
+
data3c[i] = j + i % 10;
|
|
74
|
+
data3c[KB(16) - 1] = '\0';
|
|
75
|
+
show_arena_data(3, data3a, data3b, data3c);
|
|
76
|
+
|
|
77
|
+
printf("\nInitalize a new memory arena (4) with a single 4 KB (default) block\n");
|
|
78
|
+
memory_arena_identifier1 = memory_arena_init(0);
|
|
79
|
+
printf("Allocate 2048 bytes in memory arena (4) and fill with some data:\n");
|
|
80
|
+
other_data = memory_arena_allocate(memory_arena_identifier1, 2048);
|
|
81
|
+
j = '0';
|
|
82
|
+
for(i = 0; i < 512; i++)
|
|
83
|
+
other_data[i] = j + i % 10;
|
|
84
|
+
printf("other = ");
|
|
85
|
+
for(i = 0; i < 22; i++)
|
|
86
|
+
printf("%d", other_data[i]);
|
|
87
|
+
printf(" ...\n");
|
|
88
|
+
|
|
89
|
+
printf("\nInitalize a new large dynamic memory arena (5) with a single 100 MB block\n");
|
|
90
|
+
memory_arena_identifier2 = memory_arena_init(MB(100));
|
|
91
|
+
printf("Allocate 50 MB in memory arena (5) and fill with some data:\n");
|
|
92
|
+
large_data = memory_arena_allocate(memory_arena_identifier2, MB(50));
|
|
93
|
+
j = '0';
|
|
94
|
+
for(i = 0; i < MB(50) / 8; i++)
|
|
95
|
+
large_data[i] = j + i % 10;
|
|
96
|
+
printf("large> = ");
|
|
97
|
+
for(i = 0; i < 22; i++)
|
|
98
|
+
printf("%.f", large_data[i]);
|
|
99
|
+
printf(" ...\n");
|
|
100
|
+
printf("large< = ");
|
|
101
|
+
for(i = MB(50) / 8 - 30; i < MB(50) / 8 - 8; i++)
|
|
102
|
+
printf("%.f", large_data[i]);
|
|
103
|
+
printf(" ...\n");
|
|
104
|
+
|
|
105
|
+
printf("Direct (over) allocate 75 MB in memory arena (5) and fill with some data:\n");
|
|
106
|
+
large_data2 = memory_arena_allocate(memory_arena_identifier2, MB(75));
|
|
107
|
+
j = '0';
|
|
108
|
+
for(i = 0; i < MB(75) / 8; i++)
|
|
109
|
+
large_data2[i] = j + i % 10;
|
|
110
|
+
printf("large> = ");
|
|
111
|
+
for(i = 0; i < 22; i++)
|
|
112
|
+
printf("%.f", large_data2[i]);
|
|
113
|
+
printf(" ...\n");
|
|
114
|
+
printf("large< = ");
|
|
115
|
+
for(i = MB(75) / 8 - 30; i < MB(75) / 8 - 8; i++)
|
|
116
|
+
printf("%.f", large_data2[i]);
|
|
117
|
+
printf(" ...\n");
|
|
118
|
+
|
|
119
|
+
printf("Allocate another 25 MB in memory arena (5) and fill with some data:\n");
|
|
120
|
+
medium_data = memory_arena_allocate(memory_arena_identifier1, MB(25));
|
|
121
|
+
j = '0';
|
|
122
|
+
for(i = 0; i < MB(25) / 4; i++)
|
|
123
|
+
medium_data[i] = j + i % 10;
|
|
124
|
+
printf("medium = ");
|
|
125
|
+
for(i = 0; i < 22; i++)
|
|
126
|
+
printf("%d", medium_data[i]);
|
|
127
|
+
printf(" ...\n");
|
|
128
|
+
|
|
129
|
+
printf("\n--- All memory arenas created ---");
|
|
130
|
+
memory_arena_show();
|
|
131
|
+
|
|
132
|
+
memory_arena_reset(3);
|
|
133
|
+
printf("\n--- Reset arena #3 ---\n");
|
|
134
|
+
show_arena_data(3, data3a, data3b, data3c);
|
|
135
|
+
|
|
136
|
+
memory_arena_show();
|
|
137
|
+
|
|
138
|
+
memory_arena_free(3);
|
|
139
|
+
printf("\n--- Free arena #3 ---");
|
|
140
|
+
memory_arena_show();
|
|
141
|
+
|
|
142
|
+
memory_arena_reset(1);
|
|
143
|
+
printf("\n--- Reset arena #1 ---\n");
|
|
144
|
+
show_arena_data(1, data1a, data1b, data1c);
|
|
145
|
+
memory_arena_show();
|
|
146
|
+
|
|
147
|
+
memory_arena_free(1);
|
|
148
|
+
printf("\n--- Free arena #1 ---");
|
|
149
|
+
memory_arena_show();
|
|
150
|
+
|
|
151
|
+
memory_arena_free(memory_arena_identifier1);
|
|
152
|
+
printf("\n--- Free arena #%d ---", memory_arena_identifier1);
|
|
153
|
+
memory_arena_show();
|
|
154
|
+
|
|
155
|
+
memory_arena_reset_all();
|
|
156
|
+
printf("\n--- Reset all ---\n");
|
|
157
|
+
show_arena_data(2, data2a, data2b, data2c);
|
|
158
|
+
printf("large> = ");
|
|
159
|
+
for(i = 0; i < 25; i++)
|
|
160
|
+
printf("%.f", large_data[i]);
|
|
161
|
+
printf(" ...\n");
|
|
162
|
+
printf("large< = ");
|
|
163
|
+
for(i = MB(100) / 8 - 30; i < MB(100) / 8 - 5; i++)
|
|
164
|
+
printf("%.f", large_data[i]);
|
|
165
|
+
printf(" ...\n");
|
|
166
|
+
memory_arena_show();
|
|
167
|
+
|
|
168
|
+
memory_arena_free_all();
|
|
169
|
+
printf("\n--- Free all ---\n");
|
|
170
|
+
memory_arena_show();
|
|
171
|
+
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
File without changes
|
|
File without changes
|