1 /***********************************************************************/
5 /* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../LICENSE. */
12 /***********************************************************************/
14 /* $Id: startup.c 9131 2008-11-18 10:24:43Z doligez $ */
21 #include "backtrace.h"
33 #include "natdynlink.h"
38 extern int caml_parser_trace;
39 CAMLexport header_t caml_atom_table[256];
40 char * caml_code_area_start, * caml_code_area_end;
42 /* Initialize the atom table and the static data and code area limits. */
44 struct segment { char * begin; char * end; };
46 static void init_atoms(void)
48 extern struct segment caml_data_segments[], caml_code_segments[];
51 for (i = 0; i < 256; i++) {
52 caml_atom_table[i] = Make_header(0, i, Caml_white);
54 if (caml_page_table_add(In_static_data,
55 caml_atom_table, caml_atom_table + 256) != 0)
56 caml_fatal_error("Fatal error: not enough memory for the initial page table");
58 for (i = 0; caml_data_segments[i].begin != 0; i++) {
59 if (caml_page_table_add(In_static_data,
60 caml_data_segments[i].begin,
61 caml_data_segments[i].end) != 0)
62 caml_fatal_error("Fatal error: not enough memory for the initial page table");
65 caml_code_area_start = caml_code_segments[0].begin;
66 caml_code_area_end = caml_code_segments[0].end;
67 for (i = 1; caml_code_segments[i].begin != 0; i++) {
68 if (caml_code_segments[i].begin < caml_code_area_start)
69 caml_code_area_start = caml_code_segments[i].begin;
70 if (caml_code_segments[i].end > caml_code_area_end)
71 caml_code_area_end = caml_code_segments[i].end;
75 /* Configuration parameters and flags */
77 static uintnat percent_free_init = Percent_free_def;
78 static uintnat max_percent_free_init = Max_percent_free_def;
79 static uintnat minor_heap_init = Minor_heap_def;
80 static uintnat heap_chunk_init = Heap_chunk_def;
81 static uintnat heap_size_init = Init_heap_def;
82 static uintnat max_stack_init = Max_stack_def;
84 /* Parse the CAMLRUNPARAM variable */
85 /* The option letter for each runtime option is the first letter of the
86 last word of the ML name of the option (see [stdlib/gc.mli]).
87 Except for l (maximum stack size) and h (initial heap size).
89 /* Note: option l is irrelevant to the native-code runtime. */
91 /* If you change these functions, see also their copy in byterun/startup.c */
93 static void scanmult (char *opt, uintnat *var)
97 sscanf (opt, "=%u%c", &val, &mult);
98 sscanf (opt, "=0x%x%c", &val, &mult);
100 case 'k': *var = (uintnat) val * 1024; break;
101 case 'M': *var = (uintnat) val * 1024 * 1024; break;
102 case 'G': *var = (uintnat) val * 1024 * 1024 * 1024; break;
103 default: *var = (uintnat) val; break;
107 static void parse_camlrunparam(void)
109 char *opt = getenv ("OCAMLRUNPARAM");
112 if (opt == NULL) opt = getenv ("CAMLRUNPARAM");
115 while (*opt != '\0'){
117 case 's': scanmult (opt, &minor_heap_init); break;
118 case 'i': scanmult (opt, &heap_chunk_init); break;
119 case 'h': scanmult (opt, &heap_size_init); break;
120 case 'l': scanmult (opt, &max_stack_init); break;
121 case 'o': scanmult (opt, &percent_free_init); break;
122 case 'O': scanmult (opt, &max_percent_free_init); break;
123 case 'v': scanmult (opt, &caml_verb_gc); break;
124 case 'b': caml_record_backtrace(Val_true); break;
125 case 'p': caml_parser_trace = 1; break;
126 case 'a': scanmult (opt, &p); caml_set_allocation_policy (p); break;
132 /* These are termination hooks used by the systhreads library */
133 struct longjmp_buffer caml_termination_jmpbuf;
134 void (*caml_termination_hook)(void *) = NULL;
136 extern value caml_start_program (void);
137 extern void caml_init_ieee_floats (void);
138 extern void caml_init_signals (void);
140 void caml_main(char **argv)
144 static char proc_self_exe[256];
148 caml_init_ieee_floats();
149 caml_init_custom_operations();
153 parse_camlrunparam();
154 caml_init_gc (minor_heap_init, heap_size_init, heap_chunk_init,
155 percent_free_init, max_percent_free_init);
159 if (exe_name == NULL) exe_name = "";
161 if (caml_executable_name(proc_self_exe, sizeof(proc_self_exe)) == 0)
162 exe_name = proc_self_exe;
164 exe_name = caml_search_exe_in_path(exe_name);
166 exe_name = caml_search_exe_in_path(exe_name);
168 caml_sys_init(exe_name, argv);
169 if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
170 if (caml_termination_hook != NULL) caml_termination_hook(NULL);
173 res = caml_start_program();
174 if (Is_exception_result(res))
175 caml_fatal_uncaught_exception(Extract_exception(res));
178 void caml_startup(char **argv)