syntax: 'meth' -> 'fun', 'attr' -> 'var'
[nit.git] / lib / nit_main.c
1 #include "nit_common.h"
2 #include <signal.h>
3 #include <stdarg.h>
4
5 #ifdef WITH_LIBGC
6 #define GC_DEBUG
7 #include <gc/gc.h>
8 int nolibgc = 0;
9 void *simple_alloc(size_t s0);
10 void *alloc(size_t s0)
11 {
12 if (!nolibgc) { return GC_MALLOC(s0); }
13 else return simple_alloc(s0);
14 }
15 #define alloc simple_alloc
16 #endif
17
18 void * alloc(size_t s0)
19 {
20 static char * alloc_pos = NULL;
21 static size_t alloc_size = 0;
22 void * res;
23 size_t s = ((s0+3)/4)*4;
24 if(alloc_size < s) {
25 alloc_size = s + 1024*1024;
26 alloc_pos = (char *)calloc(alloc_size, 1);
27 }
28 res = alloc_pos;
29 alloc_size -= s;
30 alloc_pos += s;
31 return res;
32 }
33
34 int glob_argc;
35 char **glob_argv;
36 val_t G_sys;
37
38 void exithandler(int s) {
39 fprintf(stderr, "Recieved signal %d\n", s);
40 nit_exit(1);
41 }
42 void prepare_signals(void) {
43 #if WITH_LIBGC
44 nolibgc = (getenv("NIT_NOLIBGC") != NULL);
45 if (!nolibgc) {
46 GC_INIT();
47 }
48 #endif
49 signal(SIGINT,exithandler);
50 signal(SIGABRT,exithandler);
51 signal(SIGSEGV,exithandler);
52 signal(SIGILL, exithandler);
53 signal(SIGFPE, exithandler);
54 signal(SIGTERM,exithandler);
55 }
56 struct trace_t *tracehead = NULL;
57 void nit_exit(int i) {
58 fprintf(stderr, ",---- Stack trace -- - - -\n");
59 while(tracehead != NULL) {
60 fprintf(stderr, "| %s (%s:%d)\n", tracehead->meth, tracehead->file, tracehead->line);
61 if (tracehead == tracehead->prev) break;
62 tracehead = tracehead->prev;
63 }
64 fprintf(stderr, "`------------------- - - -\n");
65 exit(i);
66 }