-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgc.c
More file actions
296 lines (255 loc) · 8.35 KB
/
Copy pathgc.c
File metadata and controls
296 lines (255 loc) · 8.35 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* @file gc.c
* @brief This file contains the implementation for a Copying Garbage Collector.
* @author matac
* @date 2023/09/23
*/
#include "gc.h"
#include <assert.h>
#include <errno.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* ========================================================================== */
/* mini_cpgc_malloc */
/* ========================================================================== */
/**
* @struct Block_Header
* @brief Object metadata for garbage-collected heap objects.
*
* This struct stores metadata for each object allocated in the heap.
* It includes information like the size of the object.
*
* @var Block_Header::flags
* Flags indicating the state of the block. Use FL_ALLOC for allocated blocks
* and FL_FREE for blocks that are in the free list.
*
* @var Block_Header::size
* The size of the object, in bytes.
*
* @var Block_Header::next_free
* A pointer to the next free object in the free list.
*/
typedef struct block_header {
size_t flags;
size_t size;
struct block_header *next_free;
} Block_Header;
/**
* @struct Heap_Header
* @brief Metadata for managing the heap used in garbage collection.
*
* This struct contains metadata for heap management, including the size of the
* heap and a pointer to the current position within the heap for subsequent
* allocations.
*
* @var Heap_Header::size
* The total size of the heap, in bytes.
*
* @var Heap_Header::current
* The current position within the heap for new allocations. This is updated
* each time a new object is allocated.
*
* @var Heap_Header::end
* The end position of the heap. This marks the last byte that can be allocated
* within the heap.
*/
typedef struct heap_header {
size_t size;
size_t current;
size_t end;
} Heap_Header;
Block_Header *free_list;
Heap_Header *from_start;
Heap_Header *to_start;
#define TINY_HEAP_SIZE 0x4000
#define PTRSIZE ((size_t)sizeof(void *))
#define HEAP_HEADER_SIZE ((size_t)sizeof(Heap_Header))
#define BLOCK_HEADER_SIZE ((size_t)sizeof(Block_Header))
#define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
#define NEXT_HEADER(x) ((Block_Header *)((size_t)(x + 1) + x->size))
#define FL_ALLOC 0x1
#define FL_FREE 0x0
#define FL_TEST(x, f) (((Block_Header *)x)->flags & f)
/**
* @fn void heap_init(size_t req_size)
* @brief Initializes the heap areas for From-space and To-space.
*
* Initializes two heap areas: From-space and To-space, both with the same size,
* specified by the req_size parameter. If the given req_size is smaller than
* TINY_HEAP_SIZE, then TINY_HEAP_SIZE is used as the size.
*
* @param req_size The requested size of the heap areas in bytes.
* @return None
*/
void heap_init(size_t req_size) {
void *p1, *p2;
if (req_size < TINY_HEAP_SIZE)
req_size = TINY_HEAP_SIZE;
p1 = malloc(req_size + PTRSIZE + HEAP_HEADER_SIZE);
from_start = (Heap_Header *)ALIGN((size_t)p1, PTRSIZE);
from_start->size = req_size;
from_start->current = (size_t)(from_start + 1);
from_start->end = (size_t)(from_start + 1 + req_size);
p2 = malloc(req_size + PTRSIZE + HEAP_HEADER_SIZE);
to_start = (Heap_Header *)ALIGN((size_t)p2, PTRSIZE);
to_start->size = req_size;
to_start->current = (size_t)(to_start + 1);
to_start->end = (size_t)(to_start + 1 + req_size);
}
/**
* @fn void *mini_cpgc_malloc(size_t req_size)
* @brief Allocates memory in the From-space heap area.
*
* Allocates a memory block of size req_size in the From-space heap.
* The function aligns the requested size to the nearest PTRSIZE boundary.
* If the size is zero or negative, the function returns NULL.
*
* @param req_size The requested size of the memory block in bytes.
* @return A pointer to the allocated memory block, or NULL if the allocation
* failed.
* @warning The function does not check for heap overflow or perform garbage
* collection.
*/
void *mini_cpgc_malloc(size_t req_size) {
Block_Header *p, *prev;
req_size = ALIGN(req_size, PTRSIZE);
if (req_size <= 0) {
return NULL;
}
p = (Block_Header *)from_start->current;
p->size = req_size;
p->flags = FL_ALLOC;
from_start->current = from_start->current + req_size;
return (void *)(p + 1);
}
/**
* @fn void mini_cpgc_free(void *ptr)
* @brief Frees a memory block allocated by mini_cpgc_malloc.
*
* This function takes a pointer to a memory block previously allocated with
* mini_cpgc_malloc and adds it back to the free list for potential future
* reuse.
*
* @param ptr A pointer to the memory block to be freed.
*/
void mini_cpgc_free(void *ptr) {
Block_Header *target, *hit;
target = (Block_Header *)ptr - 1;
if (free_list == NULL) {
free_list = target;
target->next_free = target;
target->flags = FL_FREE;
return;
}
/* search join point of target to free_list */
for (hit = free_list; !(target > hit && target < hit->next_free);
hit = hit->next_free)
/* heap end? And hit(search)? */
if (hit >= hit->next_free && (target > hit || target < hit->next_free))
break;
if (NEXT_HEADER(target) == hit->next_free) {
/* merge */
target->size += (hit->next_free->size + BLOCK_HEADER_SIZE);
target->next_free = hit->next_free->next_free;
} else {
/* join next free block */
target->next_free = hit->next_free;
}
if (NEXT_HEADER(hit) == target) {
/* merge */
hit->size += (target->size + BLOCK_HEADER_SIZE);
hit->next_free = target->next_free;
} else {
/* join before free block */
hit->next_free = target;
}
free_list = hit;
}
/* ========================================================================== */
/* mini_cpgc */
/* ========================================================================== */
/**
* @brief Copy a block from the "from" heap to the "to" heap.
*
* This function copies a block, including its header, from the source heap
* to the destination heap. The destination heap's free pointer is then
* updated.
*
* @param from_block Pointer to the block in the "from" heap to be copied.
* @param pfree Pointer to the current free space in the "to" heap.
* @return Returns a pointer to the new block in the "to" heap.
*/
Block_Header *copy(Block_Header *from_block, void *pfree) {
Block_Header *to_block;
to_block = memcpy(pfree, from_block, BLOCK_HEADER_SIZE + from_block->size);
pfree = (void *)(pfree + BLOCK_HEADER_SIZE + from_block->size);
return to_block;
}
/**
* @brief Swap the "from" and "to" heaps.
*
* This function swaps the pointers for the "from" and "to" heaps,
* effectively making the "to" heap the new "from" heap for the next
* garbage collection cycle.
*/
void swap() {
Heap_Header *tmp;
tmp = from_start;
from_start = to_start;
to_start = tmp;
free_list = NULL;
}
/**
* @brief Perform the copying garbage collection.
*
* This function iterates over all the blocks in the "from" heap, copies
* the allocated blocks to the "to" heap, and then swaps the heaps.
*/
void copying(void) {
Block_Header *p;
void *pfree = (void *)to_start + 1;
for (p = (Block_Header *)from_start + 1; (size_t)p < (size_t)from_start->end;
p = NEXT_HEADER(p)) {
if (p->flags == FL_ALLOC) {
copy(p, pfree);
}
}
swap();
}
/* ========================================================================== */
/* test */
/* ========================================================================== */
static void test_mini_cpgc_malloc_free(void) {
void *p;
/* malloc check */
unsigned int alloc_size = 9;
p = mini_cpgc_malloc(alloc_size);
assert((size_t)(from_start + 1) ==
(size_t)(from_start->current - ALIGN(alloc_size, PTRSIZE)));
/* free check */
mini_cpgc_free(p);
assert((Block_Header *)p - 1 == free_list);
}
static void test_garbage_collect(void) {
void *p1, *p2;
p1 = mini_cpgc_malloc(100);
p2 = mini_cpgc_malloc(100);
assert(FL_TEST(((Block_Header *)p1 - 1), FL_ALLOC));
assert(FL_TEST(((Block_Header *)p2 - 1), FL_ALLOC));
mini_cpgc_free(p1);
copying();
assert(FL_TEST((Block_Header *)((from_start + 1) - 1), FL_ALLOC));
}
static void test(void) {
heap_init(TINY_HEAP_SIZE);
test_mini_cpgc_malloc_free();
}
int main(int argc, char **argv) {
if (argc == 2 && strcmp(argv[1], "test") == 0)
test();
return 0;
}