compile: object_id is no more the object pointer
[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 bigint object_id_counter = 1000000;
18
19 #ifdef WITH_LIBGC
20 #define GC_DEBUG
21 #include <gc/gc.h>
22 int nolibgc = 0;
23 void *simple_alloc(size_t s0);
24 void *alloc(size_t s0)
25 {
26 if (!nolibgc) { return GC_MALLOC(s0); }
27 else return simple_alloc(s0);
28 }
29 #define alloc simple_alloc
30 #endif
31
32 void * alloc(size_t s0)
33 {
34 static char * alloc_pos = NULL;
35 static size_t alloc_size = 0;
36 void * res;
37 size_t s = ((s0+3)/4)*4;
38 if(alloc_size < s) {
39 alloc_size = s + 1024*1024;
40 alloc_pos = (char *)calloc(alloc_size, 1);
41 }
42 res = alloc_pos;
43 alloc_size -= s;
44 alloc_pos += s;
45 return res;
46 }
47
48 int glob_argc;
49 char **glob_argv;
50 val_t G_sys;
51
52 void exithandler(int s) {
53 fprintf(stderr, "Recieved signal %d\n", s);
54 nit_exit(1);
55 }
56 void prepare_signals(void) {
57 #if WITH_LIBGC
58 nolibgc = (getenv("NIT_NOLIBGC") != NULL);
59 if (!nolibgc) {
60 GC_INIT();
61 }
62 #endif
63 signal(SIGINT,exithandler);
64 signal(SIGABRT,exithandler);
65 signal(SIGSEGV,exithandler);
66 signal(SIGILL, exithandler);
67 signal(SIGFPE, exithandler);
68 signal(SIGTERM,exithandler);
69 signal(SIGBUS, exithandler);
70 }
71 struct trace_t *tracehead = NULL;
72 void nit_exit(int i) {
73 fprintf(stderr, ",---- Stack trace -- - - -\n");
74 while(tracehead != NULL) {
75 fprintf(stderr, "| %s (%s:%d)\n", tracehead->meth, tracehead->file, tracehead->line);
76 if (tracehead == tracehead->prev) break;
77 tracehead = tracehead->prev;
78 }
79 fprintf(stderr, "`------------------- - - -\n");
80 exit(i);
81 }