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
|
|
|
/* */
|
|
|
|
/* Damien Doligez, projet Para, 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
|
|
|
#include <string.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "fail.h"
|
2000-01-07 08:51:58 -08:00
|
|
|
#include "finalise.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "gc.h"
|
|
|
|
#include "gc_ctrl.h"
|
|
|
|
#include "major_gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "minor_gc.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "roots.h"
|
1995-12-22 08:48:17 -08:00
|
|
|
#include "signals.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
asize_t minor_heap_size;
|
1995-12-22 09:49:35 -08:00
|
|
|
char *young_start = NULL, *young_end = NULL;
|
|
|
|
char *young_ptr = NULL, *young_limit = NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
static value **ref_table = NULL, **ref_table_end, **ref_table_threshold;
|
|
|
|
value **ref_table_ptr = NULL, **ref_table_limit;
|
|
|
|
static asize_t ref_table_size, ref_table_reserve;
|
1995-07-24 05:46:59 -07:00
|
|
|
int in_minor_collection = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void set_minor_heap_size (asize_t size)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *new_heap;
|
|
|
|
value **new_table;
|
|
|
|
|
|
|
|
Assert (size >= Minor_heap_min);
|
|
|
|
Assert (size <= Minor_heap_max);
|
|
|
|
Assert (size % sizeof (value) == 0);
|
1995-07-10 02:48:27 -07:00
|
|
|
if (young_ptr != young_end) minor_collection ();
|
|
|
|
Assert (young_ptr == young_end);
|
1995-05-04 03:15:53 -07:00
|
|
|
new_heap = (char *) stat_alloc (size);
|
|
|
|
if (young_start != NULL){
|
1997-11-20 07:30:43 -08:00
|
|
|
stat_free (young_start);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
young_start = new_heap;
|
|
|
|
young_end = new_heap + size;
|
1995-12-22 09:49:35 -08:00
|
|
|
young_limit = young_start;
|
1995-07-10 02:48:27 -07:00
|
|
|
young_ptr = young_end;
|
1995-05-04 03:15:53 -07:00
|
|
|
minor_heap_size = size;
|
|
|
|
|
|
|
|
ref_table_size = minor_heap_size / sizeof (value) / 8;
|
|
|
|
ref_table_reserve = 256;
|
|
|
|
new_table = (value **) stat_alloc ((ref_table_size + ref_table_reserve)
|
1997-05-19 08:42:21 -07:00
|
|
|
* sizeof (value *));
|
1997-11-20 07:30:43 -08:00
|
|
|
if (ref_table != NULL) stat_free (ref_table);
|
1995-05-04 03:15:53 -07:00
|
|
|
ref_table = new_table;
|
|
|
|
ref_table_ptr = ref_table;
|
|
|
|
ref_table_threshold = ref_table + ref_table_size;
|
|
|
|
ref_table_limit = ref_table_threshold;
|
|
|
|
ref_table_end = ref_table + ref_table_size + ref_table_reserve;
|
|
|
|
}
|
|
|
|
|
2002-02-05 09:11:33 -08:00
|
|
|
static value oldify_todo_list = 0;
|
2002-01-18 07:13:26 -08:00
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
/* Note that the tests on the tag depend on the fact that Infix_tag,
|
|
|
|
Forward_tag, and No_scan_tag are contiguous. */
|
|
|
|
|
2002-01-18 07:13:26 -08:00
|
|
|
void oldify_one (value v, value *p)
|
2000-01-02 08:10:21 -08:00
|
|
|
{
|
|
|
|
value result, field0;
|
|
|
|
header_t hd;
|
|
|
|
mlsize_t sz, i;
|
|
|
|
tag_t tag;
|
|
|
|
|
|
|
|
tail_call:
|
|
|
|
if (Is_block (v) && Is_young (v)){
|
|
|
|
Assert (Hp_val (v) >= young_ptr);
|
|
|
|
hd = Hd_val (v);
|
2002-01-18 07:13:26 -08:00
|
|
|
if (hd == 0){ /* If already forwarded */
|
|
|
|
*p = Field (v, 0); /* then forward pointer is first field. */
|
2000-01-02 08:10:21 -08:00
|
|
|
}else{
|
2002-01-18 07:13:26 -08:00
|
|
|
tag = Tag_hd (hd);
|
2002-01-20 09:39:10 -08:00
|
|
|
if (tag < Infix_tag){
|
2002-01-18 07:13:26 -08:00
|
|
|
sz = Wosize_hd (hd);
|
|
|
|
result = alloc_shr (sz, tag);
|
|
|
|
*p = result;
|
|
|
|
field0 = Field (v, 0);
|
|
|
|
Hd_val (v) = 0; /* Set forward flag */
|
|
|
|
Field (v, 0) = result; /* and forward pointer. */
|
|
|
|
if (sz > 1){
|
|
|
|
Field (result, 0) = field0;
|
|
|
|
Field (result, 1) = oldify_todo_list; /* Add this block */
|
|
|
|
oldify_todo_list = v; /* to the "to do" list. */
|
|
|
|
}else{
|
|
|
|
Assert (sz == 1);
|
|
|
|
p = &Field (result, 0);
|
|
|
|
v = field0;
|
|
|
|
goto tail_call;
|
|
|
|
}
|
2002-01-20 09:39:10 -08:00
|
|
|
}else if (tag >= No_scan_tag){
|
|
|
|
sz = Wosize_hd (hd);
|
|
|
|
result = alloc_shr (sz, tag);
|
|
|
|
for (i = 0; i < sz; i++) Field (result, i) = Field (v, i);
|
|
|
|
Hd_val (v) = 0; /* Set forward flag */
|
|
|
|
Field (v, 0) = result; /* and forward pointer. */
|
|
|
|
*p = result;
|
|
|
|
}else if (tag == Infix_tag){
|
|
|
|
mlsize_t offset = Infix_offset_hd (hd);
|
|
|
|
oldify_one (v - offset, p); /* This cannot recurse deeper than 1. */
|
|
|
|
*p += offset;
|
|
|
|
}else{
|
|
|
|
Assert (tag == Forward_tag);
|
|
|
|
v = Forward_val (v); /* Follow the forwarding */
|
|
|
|
goto tail_call; /* then oldify. */
|
2000-01-02 08:10:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
*p = v;
|
|
|
|
}
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-01-18 07:13:26 -08:00
|
|
|
/* Finish the work that was put off by oldify_one.
|
|
|
|
Note that oldify_one itself is called by oldify_mopup, so we
|
|
|
|
have to be careful to remove the first entry from the list before
|
|
|
|
oldifying its fields. */
|
|
|
|
void oldify_mopup (void)
|
|
|
|
{
|
|
|
|
value v, new_v, f;
|
|
|
|
mlsize_t i;
|
|
|
|
|
2002-02-05 09:11:33 -08:00
|
|
|
while (oldify_todo_list != 0){
|
2002-01-18 07:13:26 -08:00
|
|
|
v = oldify_todo_list; /* Get the head. */
|
|
|
|
Assert (Hd_val (v) == 0); /* It must be forwarded. */
|
|
|
|
new_v = Field (v, 0); /* Follow forward pointer. */
|
|
|
|
oldify_todo_list = Field (new_v, 1); /* Remove from list. */
|
|
|
|
|
|
|
|
f = Field (new_v, 0);
|
|
|
|
if (Is_block (f) && Is_young (f)){
|
|
|
|
oldify_one (f, &Field (new_v, 0));
|
|
|
|
}
|
|
|
|
for (i = 1; i < Wosize_val (new_v); i++){
|
|
|
|
f = Field (v, i);
|
|
|
|
if (Is_block (f) && Is_young (f)){
|
2002-01-20 14:20:58 -08:00
|
|
|
oldify_one (f, &Field (new_v, i));
|
2002-01-18 07:13:26 -08:00
|
|
|
}else{
|
|
|
|
Field (new_v, i) = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-07 08:51:58 -08:00
|
|
|
/* Make sure the minor heap is empty by performing a minor collection
|
|
|
|
if needed.
|
|
|
|
*/
|
|
|
|
void empty_minor_heap (void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value **r;
|
2000-01-07 08:51:58 -08:00
|
|
|
|
|
|
|
if (young_ptr != young_end){
|
|
|
|
in_minor_collection = 1;
|
|
|
|
gc_message (0x02, "<", 0);
|
|
|
|
oldify_local_roots();
|
2002-01-18 07:13:26 -08:00
|
|
|
for (r = ref_table; r < ref_table_ptr; r++) oldify_one (**r, *r);
|
|
|
|
oldify_mopup ();
|
2001-08-13 06:53:51 -07:00
|
|
|
if (young_ptr < young_limit) young_ptr = young_limit;
|
2000-01-07 08:51:58 -08:00
|
|
|
stat_minor_words += Wsize_bsize (young_end - young_ptr);
|
|
|
|
young_ptr = young_end;
|
|
|
|
ref_table_ptr = ref_table;
|
|
|
|
ref_table_limit = ref_table_threshold;
|
|
|
|
gc_message (0x02, ">", 0);
|
|
|
|
in_minor_collection = 0;
|
|
|
|
}
|
|
|
|
final_empty_young ();
|
2000-03-17 05:22:36 -08:00
|
|
|
#ifdef DEBUG
|
2000-04-03 01:34:22 -07:00
|
|
|
{
|
|
|
|
value *p;
|
|
|
|
for (p = (value *) young_start; p < (value *) young_end; ++p){
|
|
|
|
*p = Debug_free_minor;
|
|
|
|
}
|
|
|
|
}
|
2000-03-17 05:22:36 -08:00
|
|
|
#endif
|
2000-01-07 08:51:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do a minor collection and a slice of major collection, call finalisation
|
|
|
|
functions, etc.
|
|
|
|
Leave the minor heap empty.
|
|
|
|
*/
|
|
|
|
void minor_collection (void)
|
|
|
|
{
|
1995-05-04 03:15:53 -07:00
|
|
|
long prev_alloc_words = allocated_words;
|
|
|
|
|
2000-01-07 08:51:58 -08:00
|
|
|
empty_minor_heap ();
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
stat_promoted_words += allocated_words - prev_alloc_words;
|
|
|
|
++ stat_minor_collections;
|
2002-02-05 09:11:33 -08:00
|
|
|
major_collection_slice (0);
|
1995-08-08 06:37:34 -07:00
|
|
|
force_major_slice = 0;
|
2002-02-05 09:11:33 -08:00
|
|
|
|
2000-01-07 08:51:58 -08:00
|
|
|
final_do_calls ();
|
|
|
|
|
|
|
|
empty_minor_heap ();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value check_urgent_gc (value extra_root)
|
1996-11-15 07:25:26 -08:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (extra_root);
|
|
|
|
if (force_major_slice) minor_collection();
|
|
|
|
CAMLreturn (extra_root);
|
1996-11-15 07:25:26 -08:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void realloc_ref_table (void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{ Assert (ref_table_ptr == ref_table_limit);
|
|
|
|
Assert (ref_table_limit <= ref_table_end);
|
|
|
|
Assert (ref_table_limit >= ref_table_threshold);
|
|
|
|
|
|
|
|
if (ref_table_limit == ref_table_threshold){
|
1998-08-07 11:43:39 -07:00
|
|
|
gc_message (0x08, "ref_table threshold crossed\n", 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
ref_table_limit = ref_table_end;
|
1995-08-08 06:37:34 -07:00
|
|
|
urge_major_slice ();
|
1995-07-24 05:46:59 -07:00
|
|
|
}else{ /* This will almost never happen with the bytecode interpreter. */
|
1995-05-04 03:15:53 -07:00
|
|
|
asize_t sz;
|
|
|
|
asize_t cur_ptr = ref_table_ptr - ref_table;
|
1995-08-08 06:37:34 -07:00
|
|
|
Assert (force_major_slice);
|
1995-12-22 08:48:17 -08:00
|
|
|
|
1995-07-24 05:46:59 -07:00
|
|
|
ref_table_size *= 2;
|
1995-05-04 03:15:53 -07:00
|
|
|
sz = (ref_table_size + ref_table_reserve) * sizeof (value *);
|
1998-08-07 11:43:39 -07:00
|
|
|
gc_message (0x08, "Growing ref_table to %ldk bytes\n", (long) sz / 1024);
|
1995-05-04 03:15:53 -07:00
|
|
|
ref_table = (value **) realloc ((char *) ref_table, sz);
|
|
|
|
if (ref_table == NULL) fatal_error ("Fatal error: ref_table overflow\n");
|
|
|
|
ref_table_end = ref_table + ref_table_size + ref_table_reserve;
|
|
|
|
ref_table_threshold = ref_table + ref_table_size;
|
|
|
|
ref_table_ptr = ref_table + cur_ptr;
|
|
|
|
ref_table_limit = ref_table_end;
|
|
|
|
}
|
|
|
|
}
|