-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgc.h
More file actions
146 lines (122 loc) · 4.12 KB
/
gc.h
File metadata and controls
146 lines (122 loc) · 4.12 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
#ifndef _GC_H_
#define _GC_H_
struct GC_StackRoot
{
void **root;
struct GC_StackRoot *next;
#if !defined(NDEBUG)
int live;
const char *name;
const char *file;
long line;
#endif
};
#if defined(NDEBUG)
# define GC_PROTECT(V) struct GC_StackRoot _sr_##V; _sr_##V.root= (void *)&V; GC_push_root(&_sr_##V)
# define GC_UNPROTECT(V) GC_pop_root(&_sr_##V)
#else
# define GC_PROTECT(V) struct GC_StackRoot _sr_##V; _sr_##V.root= (void *)&V; GC_push_root(&_sr_##V, #V, __FILE__, __LINE__)
# define GC_UNPROTECT(V) GC_pop_root(&_sr_##V, #V, __FILE__, __LINE__)
#endif
#define GC_INIT()
#define GC_init()
#if !defined(GC_API)
# define GC_API
#endif
GC_API void *GC_malloc(size_t nbytes);
GC_API void *GC_malloc_atomic(size_t nbytes);
GC_API void *GC_realloc(void *ptr, size_t lbs);
GC_API void GC_free(void *ptr);
GC_API size_t GC_size(void *ptr);
GC_API void GC_add_root(void *root);
GC_API void GC_delete_root(void *root);
GC_API void GC_mark(void *ptr);
GC_API void GC_mark_leaf(void *ptr);
GC_API void GC_sweep(void);
GC_API void GC_gcollect(void);
GC_API size_t GC_count_objects(void);
GC_API size_t GC_count_bytes(void);
GC_API double GC_count_fragments(void);
GC_API void *GC_first_object(void);
GC_API void *GC_next_object(void *prev);
GC_API int GC_atomic(void *ptr);
#ifndef NDEBUG
GC_API void *GC_check(void *ptr);
GC_API void *GC_stamp(void *ptr, const char *file, long line, const char *func);
GC_API const char *GC_file(void *ptr);
GC_API long GC_line(void *ptr);
GC_API const char *GC_function(void *ptr);
#else
# define GC_check(PTR) (PTR)
# define GC_stamp(PTR, FILE, LINE, FUNC) (PTR)
# define GC_file(PTR) "?"
# define GC_line(PTR) 0
# define GC_function(PTR) "?"
#endif
typedef void (*GC_finaliser_t)(void *ptr, void *data);
GC_API void GC_register_finaliser(void *ptr, GC_finaliser_t finaliser, void *data);
extern struct GC_StackRoot *GC_stack_roots;
#if defined(NDEBUG)
GC_API inline void GC_push_root(struct GC_StackRoot *sr)
{
sr->next= GC_stack_roots;
GC_stack_roots= sr;
}
GC_API inline void GC_pop_root(struct GC_StackRoot *sr)
{
# if 0
GC_stack_roots= sr->next;
# else /* paranoid version for broken code warns of mismatched pops with a SEGV */
struct GC_StackRoot *nr= sr->next;
while (nr != GC_stack_roots) GC_stack_roots= GC_stack_roots->next;
# endif
}
#else
GC_API inline void GC_push_root(struct GC_StackRoot *sr, const char *name, const char *file, int line)
{
sr->next= GC_stack_roots;
sr->name= name;
sr->file= file;
sr->line= line;
sr->live= 1;
GC_stack_roots= sr;
}
static int GC_roots_include(struct GC_StackRoot *roots, struct GC_StackRoot *root)
{
while (roots) {
if (roots == root) return 1;
roots= roots->next;
}
return 0;
}
GC_API inline void GC_pop_root(struct GC_StackRoot *sr, const char *name, const char *file, int line)
{
struct GC_StackRoot *nr= sr->next;
struct GC_StackRoot *gr= GC_stack_roots;
if (!sr->live) { fprintf(stderr, "*** %s %d %s: STALE POP IN GC_pop_root\n", file, line, name); goto die; }
sr->live= 0;
if (GC_roots_include(nr, sr)) { fprintf(stderr, "*** %s %d %s: CYCLE IN GC_pop_root\n", file, line, name); goto die; }
int n= 0;
while (nr != gr) {
if (n++ > 10) { fprintf(stderr, "*** %s %d %s: LOOP IN GC_pop_root\n", file, line, name); goto die; }
gr= gr->next;
}
GC_stack_roots= gr;
return;
die:
fprintf(stderr, "* gc stack roots = %p %s %ld %s\n", gr, gr->file, gr->line, gr->name);
fprintf(stderr, "* popped root = %p %s %ld %s\n", sr, sr->file, sr->line, sr->name);
while (nr) {
fprintf(stderr, "* next root = %p %s %ld %s\n", nr, nr ? nr->file : 0, nr ? nr->line : 0, nr ? nr->name : 0);
nr= nr->next;
}
abort();
}
#endif
typedef void (*GC_pre_mark_function_t)(void);
extern GC_pre_mark_function_t GC_pre_mark_function;
typedef void (*GC_mark_function_t)(void *ptr);
extern GC_mark_function_t GC_mark_function;
typedef void (*GC_free_function_t)(void *ptr);
extern GC_free_function_t GC_free_function;
#endif /* _GC_H_ */