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) {
91 /* Process GC runtime selection */
92 if (getenv("NIT_GC_OPTION") != NULL
) {
93 char *opt
=getenv("NIT_GC_OPTION");
94 if (strcmp(opt
, "boehm")==0) {
98 fprintf(stderr
, "Compiled without Boehm GC support. Using default '%s'.\n", def
);
100 } else if (strcmp(opt
, "nitgc")==0) {
102 } else if (strcmp(opt
, "large")==0) {
104 } else if (strcmp(opt
, "help")==0) {
105 fprintf(stderr
, "NIT_GC_OPTION accepts 'nitgc'"
109 ", 'large'. Default is '%s'.\n", def
);
112 fprintf(stderr
, "Invalid GC option in NIT_GC_OPTION environment variable. Using default '%s'.\n", def
);
116 /* Initialize GC (if needed) */
119 case boehm
: GC_INIT(); break;
121 case nitgc
: Nit_gc_init(); break;
122 default: break; /* Nothing */
125 void prepare_signals(void) {
126 initialize_gc_option();
128 signal(SIGINT
,exithandler
);
129 signal(SIGABRT
,exithandler
);
130 signal(SIGSEGV
,exithandler
);
131 signal(SIGILL
, exithandler
);
132 signal(SIGFPE
, exithandler
);
133 signal(SIGTERM
,exithandler
);
134 signal(SIGBUS
, exithandler
);
136 struct stack_frame_t
*stack_frame_head
= NULL
;
137 void nit_exit(int i
) {
138 fprintf(stderr
, ",---- Stack trace -- - - -\n");
139 while(stack_frame_head
!= NULL
) {
140 fprintf(stderr
, "| %s (%s:%d)\n", stack_frame_head
->meth
, stack_frame_head
->file
, stack_frame_head
->line
);
141 if (stack_frame_head
== stack_frame_head
->prev
) break;
142 stack_frame_head
= stack_frame_head
->prev
;
144 fprintf(stderr
, "`------------------- - - -\n");