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
, gc_opt_malloc
, boehm
, nitgc
} gc_option
;
27 void *raw_alloc(size_t s0
)
30 case nitgc
: return malloc(s0
);
31 case gc_opt_malloc
: return malloc(s0
);
32 default: return alloc(s0
);
36 void register_static_object(val_t
*o
)
39 case nitgc
: GC_add_static_object(o
); break;
45 void *large_alloc(size_t s0
)
47 static char * alloc_pos
= NULL
;
48 static size_t alloc_size
= 0;
50 size_t s
= ((s0
+3)/4)*4;
52 alloc_size
= s
+ 1024*1024;
53 alloc_pos
= (char *)calloc(alloc_size
, 1);
61 void * alloc(size_t s0
)
65 case boehm
: return GC_MALLOC(s0
);
67 case nitgc
: return Nit_gc_malloc(s0
);
68 case gc_opt_malloc
: return calloc(1, s0
);
70 default: return large_alloc(s0
);
78 void exithandler(int s
) {
79 fprintf(stderr
, "Recieved signal %d\n", s
);
82 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 '%s'.\n", def
);
97 } else if (strcmp(opt
, "nitgc")==0) {
99 } else if (strcmp(opt
, "malloc")==0) {
100 gc_option
= gc_opt_malloc
;
101 } else if (strcmp(opt
, "large")==0) {
103 } else if (strcmp(opt
, "help")==0) {
104 fprintf(stderr
, "NIT_GC_OPTION accepts 'nitgc'"
108 ", 'large', 'malloc'. Default is '%s'.\n", def
);
111 fprintf(stderr
, "Invalid GC option in NIT_GC_OPTION environment variable. Using default '%s'.\n", def
);
115 /* Initialize GC (if needed) */
118 case boehm
: GC_INIT(); break;
120 case nitgc
: Nit_gc_init(); break;
121 default: break; /* Nothing */
124 /* Initialize global references list */
125 nitni_global_ref_list_init();
127 void prepare_signals(void) {
128 initialize_gc_option();
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
);
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
;
148 fprintf(stderr
, "`------------------- - - -\n");
153 void nit_abort(const char* s
, const char* msg
, const char* loc
, int line
) {
154 fprintf(stderr
, "Runtime error: ");
155 fprintf(stderr
, s
, msg
);
156 fprintf(stderr
, " (%s", loc
);
157 if (line
!= 0) fprintf(stderr
, ":%d", line
);
158 fprintf(stderr
, ")\n");
162 /* Register reference to Nit object with the latest extern method called. */
163 void nitni_local_ref_add( struct nitni_ref
*ref
) {
164 struct nitni_ref_array_link
**link_p
;
165 struct nitni_ref_array_link
* link
= NULL
;
167 /* find usable link or link to create */
168 link_p
= &( stack_frame_head
->nitni_local_ref_head
);
169 while ( (*link_p
) != NULL
&&
170 (*link_p
)->count
>= NITNI_REF_ARRAY_LINK_SIZE
) {
171 link_p
= &((*link_p
)->next
);
174 /* create link if needed */
175 if ( *link_p
== NULL
) {
176 link
= malloc( sizeof(struct nitni_ref_array_link
) );
186 link
->reg
[ link
->count
] = ref
;
190 /* Clean all references associated to the current (but returning) extern method. */
191 void nitni_local_ref_clean( void ) {
192 struct nitni_ref_array_link
* link
,
196 link
= stack_frame_head
->nitni_local_ref_head
;
197 while ( link
!= NULL
)
199 for ( i
= 0; i
< link
->count
; i
++ ) {
200 if ( link
->reg
[i
]->count
== 0 ) { /* not registered globally */
201 free( link
->reg
[ i
] );
206 if ( link
->count
== NITNI_REF_ARRAY_LINK_SIZE
)
214 stack_frame_head
->nitni_local_ref_head
= NULL
;
217 struct nitni_global_ref_list_t
*nitni_global_ref_list
;
218 void nitni_global_ref_list_init() {
219 nitni_global_ref_list
= (struct nitni_global_ref_list_t
*)malloc(sizeof(struct nitni_global_ref_list_t
));
220 nitni_global_ref_list
->head
= NULL
;
221 nitni_global_ref_list
->tail
= NULL
;
224 void nitni_global_ref_add( struct nitni_ref
*ref
) {
225 if ( nitni_global_ref_list
->head
== NULL
) {
226 nitni_global_ref_list
->head
= ref
;
227 nitni_global_ref_list
->tail
= ref
;
231 nitni_global_ref_list
->tail
->next
= ref
;
232 ref
->prev
= nitni_global_ref_list
->tail
;
238 void nitni_global_ref_remove( struct nitni_ref
*ref
) {
239 if ( ref
->prev
== NULL
) {
240 nitni_global_ref_list
->head
= ref
->next
;
242 ref
->prev
->next
= ref
->next
;
245 if ( ref
->next
== NULL
) {
246 nitni_global_ref_list
->tail
= ref
->prev
;
248 ref
->next
->prev
= ref
->prev
;
252 extern void nitni_global_ref_incr( struct nitni_ref
*ref
) {
253 if ( ref
->count
== 0 ) /* not registered */
256 nitni_global_ref_add( ref
);
262 extern void nitni_global_ref_decr( struct nitni_ref
*ref
) {
263 if ( ref
->count
== 1 ) /* was last reference */
265 /* remove from list */
266 nitni_global_ref_remove( ref
);