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) {
93 /* Process GC runtime selection */
94 if (getenv("NIT_GC_OPTION") != NULL
) {
95 char *opt
=getenv("NIT_GC_OPTION");
96 if (strcmp(opt
, "boehm")==0) {
100 fprintf(stderr
, "Compiled without Boehm GC support. Using default '%s'.\n", def
);
102 } else if (strcmp(opt
, "nitgc")==0) {
104 } else if (strcmp(opt
, "malloc")==0) {
105 gc_option
= gc_opt_malloc
;
106 } else if (strcmp(opt
, "large")==0) {
108 } else if (strcmp(opt
, "help")==0) {
109 fprintf(stderr
, "NIT_GC_OPTION accepts 'nitgc'"
113 ", 'large', 'malloc'. Default is '%s'.\n", def
);
116 fprintf(stderr
, "Invalid GC option in NIT_GC_OPTION environment variable. Using default '%s'.\n", def
);
120 /* Initialize GC (if needed) */
123 case boehm
: GC_INIT(); break;
125 case nitgc
: Nit_gc_init(); break;
126 default: break; /* Nothing */
129 void prepare_signals(void) {
130 initialize_gc_option();
132 signal(SIGINT
,exithandler
);
133 signal(SIGABRT
,exithandler
);
134 signal(SIGSEGV
,exithandler
);
135 signal(SIGILL
, exithandler
);
136 signal(SIGFPE
, exithandler
);
137 signal(SIGTERM
,exithandler
);
138 signal(SIGBUS
, exithandler
);
140 struct stack_frame_t
*stack_frame_head
= NULL
;
141 void nit_exit(int i
) {
142 char *opt
=getenv("NIT_NO_STACK");
143 if (opt
== NULL
|| strcmp(opt
, "0")==0) {
144 fprintf(stderr
, ",---- Stack trace -- - - -\n");
145 while(stack_frame_head
!= NULL
) {
146 fprintf(stderr
, "| %s (%s:%d)\n", stack_frame_head
->meth
, stack_frame_head
->file
, stack_frame_head
->line
);
147 if (stack_frame_head
== stack_frame_head
->prev
) break;
148 stack_frame_head
= stack_frame_head
->prev
;
150 fprintf(stderr
, "`------------------- - - -\n");
155 void nit_abort(const char* s
, const char* msg
, const char* loc
, int line
) {
156 fprintf(stderr
, s
, msg
);
157 fprintf(stderr
, " (%s", loc
);
158 if (line
!= 0) fprintf(stderr
, ":%d", line
);
159 fprintf(stderr
, ")\n");
163 /* Register reference to Nit object with the latest extern method called. */
164 void nitni_local_ref_add( struct nitni_ref
*ref
) {
165 struct nitni_ref_array_link
**link_p
;
166 struct nitni_ref_array_link
* link
= NULL
;
168 /* find usable link or link to create */
169 link_p
= &( stack_frame_head
->nitni_local_ref_head
);
170 while ( (*link_p
) != NULL
&&
171 (*link_p
)->count
>= NITNI_REF_ARRAY_LINK_SIZE
) {
172 link_p
= &((*link_p
)->next
);
175 /* create link if needed */
176 if ( *link_p
== NULL
) {
177 link
= malloc( sizeof(struct nitni_ref_array_link
) );
187 link
->reg
[ link
->count
] = ref
;
191 /* Clean all references associated to the current (but returning) extern method. */
192 void nitni_local_ref_clean( void ) {
193 struct nitni_ref_array_link
* link
,
197 link
= stack_frame_head
->nitni_local_ref_head
;
198 while ( link
!= NULL
)
200 for ( i
= 0; i
< link
->count
; i
++ ) {
201 free( link
->reg
[ i
] );
205 if ( link
->count
== NITNI_REF_ARRAY_LINK_SIZE
)
213 stack_frame_head
->nitni_local_ref_head
= NULL
;