gc: default is always nitgc
[nit.git] / clib / nit_main.c
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
4 *
5 * This file is free software, which comes along with NIT. This software is
6 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 * is kept unaltered, and a notification of the changes is added.
10 * You are allowed to redistribute it and sell it, alone or is a part of
11 * another product.
12 */
13
14 #include "nit_common.h"
15 #include <signal.h>
16 #include <stdarg.h>
17 #include "gc.h"
18
19 bigint object_id_counter = 1000000;
20 enum gc_option { large, gc_opt_malloc, boehm, nitgc } gc_option;
21
22 #ifdef WITH_LIBGC
23 #define GC_DEBUG
24 #include <gc/gc.h>
25 #endif
26
27 void *raw_alloc(size_t s0)
28 {
29 switch (gc_option) {
30 case nitgc: return malloc(s0);
31 case gc_opt_malloc: return malloc(s0);
32 default: return alloc(s0);
33 }
34 }
35
36 void register_static_object(val_t *o)
37 {
38 switch (gc_option) {
39 case nitgc: GC_add_static_object(o); break;
40 default: break;
41 }
42 return;
43 }
44
45 void *large_alloc(size_t s0)
46 {
47 static char * alloc_pos = NULL;
48 static size_t alloc_size = 0;
49 void * res;
50 size_t s = ((s0+3)/4)*4;
51 if(alloc_size < s) {
52 alloc_size = s + 1024*1024;
53 alloc_pos = (char *)calloc(alloc_size, 1);
54 }
55 res = alloc_pos;
56 alloc_size -= s;
57 alloc_pos += s;
58 return res;
59 }
60
61 void * alloc(size_t s0)
62 {
63 switch (gc_option) {
64 #ifdef WITH_LIBGC
65 case boehm: return GC_MALLOC(s0);
66 #endif
67 case nitgc: return Nit_gc_malloc(s0);
68 case gc_opt_malloc: return calloc(1, s0);
69 case large:
70 default: return large_alloc(s0);
71 }
72 }
73
74 int glob_argc;
75 char **glob_argv;
76 val_t G_sys;
77
78 void exithandler(int s) {
79 fprintf(stderr, "Recieved signal %d\n", s);
80 nit_exit(1);
81 }
82 void initialize_gc_option(void) {
83 /* GC default */
84 char *def;
85 gc_option = nitgc;
86 def = "nitgc";
87
88 /* Process GC runtime selection */
89 if (getenv("NIT_GC_OPTION") != NULL) {
90 char *opt=getenv("NIT_GC_OPTION");
91 if (strcmp(opt, "boehm")==0) {
92 #ifdef WITH_LIBGC
93 gc_option = boehm;
94 #else
95 fprintf(stderr, "Compiled without Boehm GC support. Using default '%s'.\n", def);
96 #endif
97 } else if (strcmp(opt, "nitgc")==0) {
98 gc_option = nitgc;
99 } else if (strcmp(opt, "malloc")==0) {
100 gc_option = gc_opt_malloc;
101 } else if (strcmp(opt, "large")==0) {
102 gc_option = large;
103 } else if (strcmp(opt, "help")==0) {
104 fprintf(stderr, "NIT_GC_OPTION accepts 'nitgc'"
105 #ifdef WITH_LIBGC
106 ", 'boehm'"
107 #endif
108 ", 'large', 'malloc'. Default is '%s'.\n", def);
109 exit(1);
110 } else {
111 fprintf(stderr, "Invalid GC option in NIT_GC_OPTION environment variable. Using default '%s'.\n", def);
112 }
113 }
114
115 /* Initialize GC (if needed) */
116 switch(gc_option) {
117 #ifdef WITH_LIBGC
118 case boehm: GC_INIT(); break;
119 #endif
120 case nitgc: Nit_gc_init(); break;
121 default: break; /* Nothing */
122 }
123
124 /* Initialize global references list */
125 nitni_global_ref_list_init();
126 }
127 void prepare_signals(void) {
128 initialize_gc_option();
129
130 signal(SIGINT,exithandler);
131 signal(SIGABRT,exithandler);
132 signal(SIGSEGV,exithandler);
133 signal(SIGILL, exithandler);
134 signal(SIGFPE, exithandler);
135 signal(SIGTERM,exithandler);
136 signal(SIGBUS, exithandler);
137 }
138 struct stack_frame_t *stack_frame_head = NULL;
139 void nit_exit(int i) {
140 char *opt=getenv("NIT_NO_STACK");
141 if (opt == NULL || strcmp(opt, "0")==0) {
142 fprintf(stderr, ",---- Stack trace -- - - -\n");
143 while(stack_frame_head != NULL) {
144 fprintf(stderr, "| %s (%s:%d)\n", stack_frame_head->meth, stack_frame_head->file, stack_frame_head->line);
145 if (stack_frame_head == stack_frame_head->prev) break;
146 stack_frame_head = stack_frame_head->prev;
147 }
148 fprintf(stderr, "`------------------- - - -\n");
149 }
150 exit(i);
151 }
152
153 void nit_abort(const char* s, const char* msg, const char* loc, int line) {
154 fprintf(stderr, s, msg);
155 fprintf(stderr, " (%s", loc);
156 if (line != 0) fprintf(stderr, ":%d", line);
157 fprintf(stderr, ")\n");
158 nit_exit(1);
159 }
160
161 /* Register reference to Nit object with the latest extern method called. */
162 void nitni_local_ref_add( struct nitni_ref *ref ) {
163 struct nitni_ref_array_link **link_p;
164 struct nitni_ref_array_link * link = NULL;
165
166 /* find usable link or link to create */
167 link_p = &( stack_frame_head->nitni_local_ref_head );
168 while ( (*link_p) != NULL &&
169 (*link_p)->count >= NITNI_REF_ARRAY_LINK_SIZE ) {
170 link_p = &((*link_p)->next);
171 }
172
173 /* create link if needed */
174 if ( *link_p == NULL ) {
175 link = malloc( sizeof(struct nitni_ref_array_link) );
176 link->count = 0;
177 link->next = NULL;
178
179 (*link_p) = link;
180 } else {
181 link = *link_p;
182 }
183
184 /* add to link */
185 link->reg[ link->count ] = ref;
186 link->count ++;
187 }
188
189 /* Clean all references associated to the current (but returning) extern method. */
190 void nitni_local_ref_clean( void ) {
191 struct nitni_ref_array_link * link,
192 * last_link;
193 int i;
194
195 link = stack_frame_head->nitni_local_ref_head;
196 while ( link != NULL )
197 {
198 for ( i = 0; i < link->count; i ++ ) {
199 if ( link->reg[i]->count == 0 ) { /* not registered globally */
200 free( link->reg[ i ] );
201 }
202 }
203
204 last_link = link;
205 if ( link->count == NITNI_REF_ARRAY_LINK_SIZE )
206 link = link->next;
207 else
208 link = NULL;
209
210 free(last_link);
211 }
212
213 stack_frame_head->nitni_local_ref_head = NULL;
214 }
215
216 struct nitni_global_ref_list_t *nitni_global_ref_list;
217 void nitni_global_ref_list_init() {
218 nitni_global_ref_list = (struct nitni_global_ref_list_t*)malloc(sizeof(struct nitni_global_ref_list_t));
219 nitni_global_ref_list->head = NULL;
220 nitni_global_ref_list->tail = NULL;
221 }
222
223 void nitni_global_ref_add( struct nitni_ref *ref ) {
224 if ( nitni_global_ref_list->head == NULL ) {
225 nitni_global_ref_list->head = ref;
226 nitni_global_ref_list->tail = ref;
227
228 ref->prev = NULL;
229 } else {
230 nitni_global_ref_list->tail->next = ref;
231 ref->prev = nitni_global_ref_list->tail;
232 }
233
234 ref->next = NULL;
235 }
236
237 void nitni_global_ref_remove( struct nitni_ref *ref ) {
238 if ( ref->prev == NULL ) {
239 nitni_global_ref_list->head = ref->next;
240 } else {
241 ref->prev->next = ref->next;
242 }
243
244 if ( ref->next == NULL ) {
245 nitni_global_ref_list->tail = ref->prev;
246 } else {
247 ref->next->prev = ref->prev;
248 }
249 }
250
251 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
252 if ( ref->count == 0 ) /* not registered */
253 {
254 /* add to list */
255 nitni_global_ref_add( ref );
256 }
257
258 ref->count ++;
259 }
260
261 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
262 if ( ref->count == 1 ) /* was last reference */
263 {
264 /* remove from list */
265 nitni_global_ref_remove( ref );
266 }
267
268 ref->count --;
269 }