1 /* This file is part of NIT ( http://www.nitlanguage.org ).
3 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
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
14 #include "nit_common.h"
19 bigint object_id_counter
= 1000000;
20 enum gc_option
{ large
, boehm
, nitgc
} gc_option
;
27 void *raw_alloc(size_t s0
)
30 case nitgc
: return malloc(s0
); break;
31 default: return alloc(s0
);
35 void register_static_object(val_t
*o
)
38 case nitgc
: GC_add_static_object(o
); break;
44 void *large_alloc(size_t s0
)
46 static char * alloc_pos
= NULL
;
47 static size_t alloc_size
= 0;
49 size_t s
= ((s0
+3)/4)*4;
51 alloc_size
= s
+ 1024*1024;
52 alloc_pos
= (char *)calloc(alloc_size
, 1);
60 void * alloc(size_t s0
)
64 case boehm
: return GC_MALLOC(s0
);
66 case nitgc
: return Nit_gc_malloc(s0
);
68 default: return large_alloc(s0
);
76 void exithandler(int s
) {
77 fprintf(stderr
, "Recieved signal %d\n", s
);
80 void initialize_gc_option(void) {
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) {
95 fprintf(stderr
, "Compiled without Boehm GC support. Using default.\n");
97 } else if (strcmp(opt
, "nitgc")==0) {
99 } else if (strcmp(opt
, "large")==0) {
102 fprintf(stderr
, "Invalid GC option in NIT_GC_OPTION environment variable. Using default.\n");
106 /* Initialize GC (if needed) */
109 case boehm
: GC_INIT(); break;
111 case nitgc
: Nit_gc_init(); break;
112 default: break; /* Nothing */
115 void prepare_signals(void) {
116 initialize_gc_option();
118 signal(SIGINT
,exithandler
);
119 signal(SIGABRT
,exithandler
);
120 signal(SIGSEGV
,exithandler
);
121 signal(SIGILL
, exithandler
);
122 signal(SIGFPE
, exithandler
);
123 signal(SIGTERM
,exithandler
);
124 signal(SIGBUS
, exithandler
);
126 struct trace_t
*tracehead
= NULL
;
127 void nit_exit(int i
) {
128 fprintf(stderr
, ",---- Stack trace -- - - -\n");
129 while(tracehead
!= NULL
) {
130 fprintf(stderr
, "| %s (%s:%d)\n", tracehead
->meth
, tracehead
->file
, tracehead
->line
);
131 if (tracehead
== tracehead
->prev
) break;
132 tracehead
= tracehead
->prev
;
134 fprintf(stderr
, "`------------------- - - -\n");