1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, 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: stacks.c 7064 2005-09-22 14:21:50Z xleroy $ */
16 /* To initialize and resize the stacks */
25 CAMLexport value * caml_stack_low;
26 CAMLexport value * caml_stack_high;
27 CAMLexport value * caml_stack_threshold;
28 CAMLexport value * caml_extern_sp;
29 CAMLexport value * caml_trapsp;
30 CAMLexport value * caml_trap_barrier;
31 value caml_global_data;
33 uintnat caml_max_stack_size; /* also used in gc_ctrl.c */
35 void caml_init_stack (uintnat initial_max_size)
37 caml_stack_low = (value *) caml_stat_alloc(Stack_size);
38 caml_stack_high = caml_stack_low + Stack_size / sizeof (value);
39 caml_stack_threshold = caml_stack_low + Stack_threshold / sizeof (value);
40 caml_extern_sp = caml_stack_high;
41 caml_trapsp = caml_stack_high;
42 caml_trap_barrier = caml_stack_high + 1;
43 caml_max_stack_size = initial_max_size;
44 caml_gc_message (0x08, "Initial stack limit: %luk bytes\n",
45 caml_max_stack_size / 1024 * sizeof (value));
48 void caml_realloc_stack(asize_t required_space)
51 value * new_low, * new_high, * new_sp;
54 Assert(caml_extern_sp >= caml_stack_low);
55 size = caml_stack_high - caml_stack_low;
57 if (size >= caml_max_stack_size) caml_raise_stack_overflow();
59 } while (size < caml_stack_high - caml_extern_sp + required_space);
60 caml_gc_message (0x08, "Growing stack to %"
61 ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
62 (uintnat) size * sizeof(value) / 1024);
63 new_low = (value *) caml_stat_alloc(size * sizeof(value));
64 new_high = new_low + size;
67 ((char *) new_high - ((char *) caml_stack_high - (char *) (ptr)))
69 new_sp = (value *) shift(caml_extern_sp);
70 memmove((char *) new_sp,
71 (char *) caml_extern_sp,
72 (caml_stack_high - caml_extern_sp) * sizeof(value));
73 caml_stat_free(caml_stack_low);
74 caml_trapsp = (value *) shift(caml_trapsp);
75 caml_trap_barrier = (value *) shift(caml_trap_barrier);
76 for (p = caml_trapsp; p < new_high; p = Trap_link(p))
77 Trap_link(p) = (value *) shift(Trap_link(p));
78 caml_stack_low = new_low;
79 caml_stack_high = new_high;
80 caml_stack_threshold = caml_stack_low + Stack_threshold / sizeof (value);
81 caml_extern_sp = new_sp;
86 CAMLprim value caml_ensure_stack_capacity(value required_space)
88 asize_t req = Long_val(required_space);
89 if (caml_extern_sp - req < caml_stack_low) caml_realloc_stack(req);
93 void caml_change_max_stack_size (uintnat new_max_size)
95 asize_t size = caml_stack_high - caml_extern_sp
96 + Stack_threshold / sizeof (value);
98 if (new_max_size < size) new_max_size = size;
99 if (new_max_size != caml_max_stack_size){
100 caml_gc_message (0x08, "Changing stack limit to %luk bytes\n",
101 new_max_size * sizeof (value) / 1024);
103 caml_max_stack_size = new_max_size;