clib: move files to the clib directory
[nit.git] / clib / nit_main.c
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
4 *
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
11 * another product.
12 */
13
14 #include "nit_common.h"
15 #include <signal.h>
16 #include <stdarg.h>
17
18 #ifdef WITH_LIBGC
19 #define GC_DEBUG
20 #include <gc/gc.h>
21 int nolibgc = 0;
22 void *simple_alloc(size_t s0);
23 void *alloc(size_t s0)
24 {
25 if (!nolibgc) { return GC_MALLOC(s0); }
26 else return simple_alloc(s0);
27 }
28 #define alloc simple_alloc
29 #endif
30
31 void * alloc(size_t s0)
32 {
33 static char * alloc_pos = NULL;
34 static size_t alloc_size = 0;
35 void * res;
36 size_t s = ((s0+3)/4)*4;
37 if(alloc_size < s) {
38 alloc_size = s + 1024*1024;
39 alloc_pos = (char *)calloc(alloc_size, 1);
40 }
41 res = alloc_pos;
42 alloc_size -= s;
43 alloc_pos += s;
44 return res;
45 }
46
47 int glob_argc;
48 char **glob_argv;
49 val_t G_sys;
50
51 void exithandler(int s) {
52 fprintf(stderr, "Recieved signal %d\n", s);
53 nit_exit(1);
54 }
55 void prepare_signals(void) {
56 #if WITH_LIBGC
57 nolibgc = (getenv("NIT_NOLIBGC") != NULL);
58 if (!nolibgc) {
59 GC_INIT();
60 }
61 #endif
62 signal(SIGINT,exithandler);
63 signal(SIGABRT,exithandler);
64 signal(SIGSEGV,exithandler);
65 signal(SIGILL, exithandler);
66 signal(SIGFPE, exithandler);
67 signal(SIGTERM,exithandler);
68 signal(SIGBUS, exithandler);
69 }
70 struct trace_t *tracehead = NULL;
71 void nit_exit(int i) {
72 fprintf(stderr, ",---- Stack trace -- - - -\n");
73 while(tracehead != NULL) {
74 fprintf(stderr, "| %s (%s:%d)\n", tracehead->meth, tracehead->file, tracehead->line);
75 if (tracehead == tracehead->prev) break;
76 tracehead = tracehead->prev;
77 }
78 fprintf(stderr, "`------------------- - - -\n");
79 exit(i);
80 }