-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode3.h
More file actions
191 lines (151 loc) · 2.9 KB
/
Copy pathnode3.h
File metadata and controls
191 lines (151 loc) · 2.9 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef NODE_H
#define NODE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
typedef struct __mem { // Memory linked list
void *data; // Pointer to data
size_t size; // Size of data pointer
struct __mem *next; // Next item in linked list
} __mem;
// Create a mem struct (initialised)
static struct __mem *
node_group (){
__mem *group = (__mem *)malloc(sizeof(__mem));
if (group){
group->data = NULL;
group->size = 0;
group->next = NULL;
}
return group;
}
// Allocates memory
static void *
nalloc(__mem **group, size_t size){
if (size == 0 || !group)
return NULL;
// Locate next available slot
__mem *cur = *group;
while (cur){
if (cur->data == NULL || cur->size == 0)
break;
cur = cur->next;
}
// No slot was found
if (!cur){
cur = (__mem *)malloc(sizeof(__mem));
if (!cur)
goto alloc_fail;
// append this element to the HEAD
cur->data = NULL;
cur->size = 0;
cur->next = *group;
*group = cur;
}
// Handle allocation
cur->data = malloc(size);
if (!cur->data){
if (cur->size == 0){
*group = cur->next;
free(cur);
}
goto alloc_fail;
}
cur->size = size;
return cur->data;
alloc_fail:
return NULL;
}
// Destroys all memory in a group
static void
nabsolve(__mem **group){
if (!group || !*group)
return;
__mem *cur = *group;
while (cur != NULL){
__mem *tmp = cur->next;
free(cur->data);
free(cur);
cur = tmp;
}
*group = NULL;
}
// Free a node
static int
nfree(__mem **group, void *mem){
if (!group || !*group || !mem)
return 1;
__mem *cur = *group;
while (cur){
if (mem == cur->data){
free(cur->data);
cur->data = NULL;
cur->size = 0;
return 0;
}
cur = cur->next;
}
// Could not find node
return 1;
}
// Realloc implementation
static void *
nrealloc(__mem **group, void *mem, size_t new_size){
if (!group || !*group || !mem)
return NULL;
__mem *cur = *group;
while (cur){
if (mem == cur->data){
__mem *new_mem = (__mem *)realloc(cur->data, new_size);
if (new_mem == NULL){
nfree(group, cur->data);
return NULL;
}
cur->data = new_mem;
cur->size = new_size;
return cur->data;
}
}
return NULL;
}
// Return the total allocated size
static size_t
node_alloc_total(__mem *group){
size_t sum = 0;
while (group){
sum += group->size;
group = group->next;
}
return sum;
}
// Return the total in use size
static size_t
node_mem_total(__mem *group){
size_t sum = 0;
while (group){
sum += group->size;
sum += sizeof(__mem);
group = group->next;
}
return sum;
}
// strdup implementation
static char *
node_strdup(__mem **group, const char *str){
if (!str) return NULL;
size_t str_size = 0;
while(str[str_size])
str_size++;
if (str_size == 0) return NULL;
char *out = (char *)nalloc(group, str_size + 1);
if (!out) return NULL;
for (int i = 0; i < str_size; i++)
out[i] = str[i];
out[str_size] = '\0';
return out;
}
#ifdef __cplusplus
}
#endif
#endif