1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* To initialize and resize the stacks */
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLexport value * caml_stack_low;
|
|
|
|
CAMLexport value * caml_stack_high;
|
|
|
|
CAMLexport value * caml_stack_threshold;
|
|
|
|
CAMLexport value * caml_extern_sp;
|
|
|
|
CAMLexport value * caml_trapsp;
|
|
|
|
CAMLexport value * caml_trap_barrier;
|
|
|
|
value caml_global_data;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
uintnat caml_max_stack_size; /* also used in gc_ctrl.c */
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
void caml_init_stack (uintnat initial_max_size)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stack_low = (value *) caml_stat_alloc(Stack_size);
|
|
|
|
caml_stack_high = caml_stack_low + Stack_size / sizeof (value);
|
|
|
|
caml_stack_threshold = caml_stack_low + Stack_threshold / sizeof (value);
|
|
|
|
caml_extern_sp = caml_stack_high;
|
|
|
|
caml_trapsp = caml_stack_high;
|
|
|
|
caml_trap_barrier = caml_stack_high + 1;
|
|
|
|
caml_max_stack_size = initial_max_size;
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "Initial stack limit: %luk bytes\n",
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_max_stack_size / 1024 * sizeof (value));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_realloc_stack(asize_t required_space)
|
1999-11-29 11:04:56 -08:00
|
|
|
{
|
1995-05-04 03:15:53 -07:00
|
|
|
asize_t size;
|
|
|
|
value * new_low, * new_high, * new_sp;
|
|
|
|
value * p;
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert(caml_extern_sp >= caml_stack_low);
|
|
|
|
size = caml_stack_high - caml_stack_low;
|
2001-05-28 08:14:18 -07:00
|
|
|
do {
|
2004-01-01 08:42:43 -08:00
|
|
|
if (size >= caml_max_stack_size) caml_raise_stack_overflow();
|
2001-05-28 08:14:18 -07:00
|
|
|
size *= 2;
|
2003-12-31 06:20:40 -08:00
|
|
|
} while (size < caml_stack_high - caml_extern_sp + required_space);
|
2005-09-22 07:21:50 -07:00
|
|
|
caml_gc_message (0x08, "Growing stack to %"
|
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
|
|
|
|
(uintnat) size * sizeof(value) / 1024);
|
2003-12-31 06:20:40 -08:00
|
|
|
new_low = (value *) caml_stat_alloc(size * sizeof(value));
|
1995-05-04 03:15:53 -07:00
|
|
|
new_high = new_low + size;
|
|
|
|
|
|
|
|
#define shift(ptr) \
|
2003-12-31 06:20:40 -08:00
|
|
|
((char *) new_high - ((char *) caml_stack_high - (char *) (ptr)))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
new_sp = (value *) shift(caml_extern_sp);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove((char *) new_sp,
|
2003-12-31 06:20:40 -08:00
|
|
|
(char *) caml_extern_sp,
|
|
|
|
(caml_stack_high - caml_extern_sp) * sizeof(value));
|
|
|
|
caml_stat_free(caml_stack_low);
|
|
|
|
caml_trapsp = (value *) shift(caml_trapsp);
|
|
|
|
caml_trap_barrier = (value *) shift(caml_trap_barrier);
|
|
|
|
for (p = caml_trapsp; p < new_high; p = Trap_link(p))
|
1995-05-04 03:15:53 -07:00
|
|
|
Trap_link(p) = (value *) shift(Trap_link(p));
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stack_low = new_low;
|
|
|
|
caml_stack_high = new_high;
|
|
|
|
caml_stack_threshold = caml_stack_low + Stack_threshold / sizeof (value);
|
|
|
|
caml_extern_sp = new_sp;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
#undef shift
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_ensure_stack_capacity(value required_space)
|
2001-05-28 08:14:18 -07:00
|
|
|
{
|
|
|
|
asize_t req = Long_val(required_space);
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_extern_sp - req < caml_stack_low) caml_realloc_stack(req);
|
2001-05-28 08:14:18 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
void caml_change_max_stack_size (uintnat new_max_size)
|
1997-05-13 07:45:38 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
asize_t size = caml_stack_high - caml_extern_sp
|
|
|
|
+ Stack_threshold / sizeof (value);
|
1997-05-13 07:45:38 -07:00
|
|
|
|
|
|
|
if (new_max_size < size) new_max_size = size;
|
2003-12-31 06:20:40 -08:00
|
|
|
if (new_max_size != caml_max_stack_size){
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "Changing stack limit to %luk bytes\n",
|
|
|
|
new_max_size * sizeof (value) / 1024);
|
1997-10-14 08:48:45 -07:00
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_max_stack_size = new_max_size;
|
1997-05-13 07:45:38 -07:00
|
|
|
}
|