-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_alloc.c
55 lines (51 loc) · 1.76 KB
/
gc_alloc.c
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
/* ************************************************************************** */
/* LE - / */
/* / */
/* gc_alloc.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: aviscogl <aviscogl@student.le-101.fr> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/01/10 13:27:33 by aviscogl #+# ## ## #+# */
/* Updated: 2018/01/10 13:48:58 by aviscogl ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "gc.h"
void *gc_alloc(size_t size, char *file, int line)
{
uintptr_t ptr;
t_gc_ptr p;
if (!(ptr = (uintptr_t)malloc(size)))
return (NULL);
p = (t_gc_ptr) {
.start = ptr,
.size = size,
.marked = 3,
# ifdef DEBUG
.file = file,
.line = line
# endif
};
DEBUGP("Alloc %p with size %d in %s:%d\n", (void *)ptr, size, file, line);
if (g_gc.min > ptr)
g_gc.min = ptr;
if (g_gc.max < ptr + size)
g_gc.max = ptr + size;
gc_list_push(&g_gc.pointer_map[HASH(ptr) % P_MAP_SIZE], p);
g_gc.pointer_nb++;
if (g_gc.pointer_nb >= g_gc.limit)
gc_run();
return ((void *)ptr);
}
void gc_register(void *ptr, size_t size, char *file, int line)
{
gc_list_push(&g_gc.globals, (t_gc_ptr) {
.start = (uintptr_t)ptr,
.size = size,
.marked = true,
# ifdef DEBUG
.file = file,
.line = line
# endif
});
}