-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_utils.c
97 lines (85 loc) · 2.32 KB
/
gc_utils.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
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
/* ************************************************************************** */
/* LE - / */
/* / */
/* gc_utils.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: aviscogl <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/01/10 13:12:10 by aviscogl #+# ## ## #+# */
/* Updated: 2018/01/10 15:51:14 by aviscogl ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "gc.h"
t_gc_list *gc_ptr_index(uintptr_t ptr)
{
int h;
int i;
t_gc_list *e;
if (ptr > g_gc.max || ptr < g_gc.min)
return (NULL);
h = HASH(ptr) % P_MAP_SIZE;
i = 0;
if ((e = gc_ptr_list_search(ptr, g_gc.pointer_map[h])))
return (e);
while (++i + h < P_MAP_SIZE || (h - i > 0))
{
if ((h - i > 0)
&& (e = gc_ptr_list_search(ptr, g_gc.pointer_map[h - i])))
return (e);
if ((h + i < P_MAP_SIZE)
&& (e = gc_ptr_list_search(ptr, g_gc.pointer_map[h + i])))
return (e);
}
return (NULL);
}
t_gc_list *gc_ptr_list_search(uintptr_t ptr, t_gc_list *e)
{
while (e)
{
if (ptr >= e->data.start
&& e->data.start + e->data.size >= ptr)
return (e);
e = e->next;
}
return (NULL);
}
void gc_mark_stack(void)
{
uint8_t tmp;
t_gc_list *e;
gc_mark(g_gc.stack_start, &tmp);
e = g_gc.globals;
while (e)
{
gc_mark((uint8_t *)e->data.start,
(uint8_t *)(e->data.start + e->data.size));
e = e->next;
}
}
void swap_ptr(uint8_t **a, uint8_t **b)
{
uint8_t *tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void debug_pointer_list(void)
{
t_gc_list *m;
int i;
i = -1;
printf("pointers lists:\n");
while (++i < P_MAP_SIZE)
{
m = g_gc.pointer_map[i];
if (m)
printf(" - g_gc.pointer_map[%d]:\n", i);
while (m)
{
printf(" { ptr: %p, size: %lu, marked: %d }\n",
(void *)m->data.start, m->data.size, m->data.marked);
m = m->next;
}
}
}