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 */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* To walk the memory roots for garbage collection */
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "major_gc.h"
|
|
|
|
#include "minor_gc.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "roots.h"
|
1995-12-20 02:40:34 -08:00
|
|
|
#include "stack.h"
|
1995-11-26 06:38:29 -08:00
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Roots registered from C functions */
|
|
|
|
|
|
|
|
value * local_roots = NULL;
|
|
|
|
|
|
|
|
struct global_root {
|
|
|
|
value * root;
|
|
|
|
struct global_root * next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct global_root * global_roots = NULL;
|
|
|
|
|
1995-10-30 02:20:08 -08:00
|
|
|
void (*scan_roots_hook) P((scanning_action)) = NULL;
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Register a global C root */
|
|
|
|
|
|
|
|
void register_global_root(r)
|
|
|
|
value * r;
|
|
|
|
{
|
|
|
|
struct global_root * gr;
|
|
|
|
gr = (struct global_root *) stat_alloc(sizeof(struct global_root));
|
|
|
|
gr->root = r;
|
|
|
|
gr->next = global_roots;
|
|
|
|
global_roots = gr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The hashtable of frame descriptors */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned long retaddr;
|
|
|
|
short frame_size;
|
|
|
|
short num_live;
|
|
|
|
short live_ofs[1];
|
|
|
|
} frame_descr;
|
|
|
|
|
|
|
|
static frame_descr ** frame_descriptors = NULL;
|
|
|
|
static int frame_descriptors_mask;
|
|
|
|
|
|
|
|
#define Hash_retaddr(addr) \
|
|
|
|
(((unsigned long)(addr) >> 3) & frame_descriptors_mask)
|
|
|
|
|
|
|
|
extern long * caml_frametable[];
|
|
|
|
|
|
|
|
static void init_frame_descriptors()
|
|
|
|
{
|
|
|
|
long num_descr, tblsize, i, j, len;
|
|
|
|
long * tbl;
|
|
|
|
frame_descr * d;
|
|
|
|
unsigned long h;
|
|
|
|
|
|
|
|
/* Count the frame descriptors */
|
|
|
|
num_descr = 0;
|
|
|
|
for (i = 0; caml_frametable[i] != 0; i++)
|
|
|
|
num_descr += *(caml_frametable[i]);
|
|
|
|
|
|
|
|
/* The size of the hashtable is a power of 2 greater or equal to
|
|
|
|
2 times the number of descriptors */
|
|
|
|
tblsize = 4;
|
|
|
|
while (tblsize < 2 * num_descr) tblsize *= 2;
|
|
|
|
|
|
|
|
/* Allocate the hash table */
|
|
|
|
frame_descriptors =
|
|
|
|
(frame_descr **) stat_alloc(tblsize * sizeof(frame_descr *));
|
|
|
|
for (i = 0; i < tblsize; i++) frame_descriptors[i] = NULL;
|
|
|
|
frame_descriptors_mask = tblsize - 1;
|
|
|
|
|
|
|
|
/* Fill the hash table */
|
|
|
|
for (i = 0; caml_frametable[i] != 0; i++) {
|
|
|
|
tbl = caml_frametable[i];
|
|
|
|
len = *tbl;
|
|
|
|
d = (frame_descr *)(tbl + 1);
|
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
h = Hash_retaddr(d->retaddr);
|
|
|
|
while (frame_descriptors[h] != NULL) {
|
|
|
|
h = (h+1) & frame_descriptors_mask;
|
|
|
|
}
|
|
|
|
frame_descriptors[h] = d;
|
|
|
|
d = (frame_descr *)
|
|
|
|
(((unsigned long)d +
|
|
|
|
sizeof(char *) + sizeof(short) + sizeof(short) +
|
|
|
|
sizeof(short) * d->num_live + sizeof(frame_descr *) - 1)
|
|
|
|
& -sizeof(frame_descr *));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Communication with [caml_start_program] and [caml_call_gc]. */
|
|
|
|
|
1995-07-17 09:10:45 -07:00
|
|
|
extern value caml_globals[];
|
1995-07-10 02:48:27 -07:00
|
|
|
extern char * caml_bottom_of_stack, * caml_top_of_stack;
|
|
|
|
extern unsigned long caml_last_return_address;
|
|
|
|
extern value gc_entry_regs[];
|
|
|
|
|
1995-12-19 07:09:33 -08:00
|
|
|
/* Structure of markers stored in stack by [callback] to skip C portion
|
|
|
|
of stack */
|
|
|
|
|
|
|
|
struct callback_link {
|
|
|
|
char * bottom_of_stack;
|
|
|
|
unsigned long return_address;
|
|
|
|
};
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Call [oldify] on all stack roots, C roots and global roots */
|
|
|
|
|
|
|
|
void oldify_local_roots ()
|
|
|
|
{
|
|
|
|
char * sp;
|
|
|
|
unsigned long retaddr;
|
|
|
|
frame_descr * d;
|
|
|
|
unsigned long h;
|
1995-07-17 09:10:45 -07:00
|
|
|
int i, j, n, ofs;
|
1995-07-10 02:48:27 -07:00
|
|
|
short * p;
|
1995-07-17 09:10:45 -07:00
|
|
|
value glob;
|
|
|
|
value * root, * block;
|
1995-07-10 02:48:27 -07:00
|
|
|
struct global_root * gr;
|
|
|
|
|
|
|
|
/* The global roots */
|
1995-07-17 09:10:45 -07:00
|
|
|
for (i = 0; caml_globals[i] != 0; i++) {
|
|
|
|
glob = caml_globals[i];
|
|
|
|
for (j = 0; j < Wosize_val(glob); j++)
|
1995-10-30 02:20:08 -08:00
|
|
|
oldify(Field(glob, j), &Field(glob, j));
|
1995-07-17 09:10:45 -07:00
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
|
|
|
|
/* The stack */
|
|
|
|
if (frame_descriptors == NULL) init_frame_descriptors();
|
|
|
|
sp = caml_bottom_of_stack;
|
|
|
|
retaddr = caml_last_return_address;
|
1995-11-26 06:38:29 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
1995-07-10 02:48:27 -07:00
|
|
|
while (sp < caml_top_of_stack) {
|
1995-11-26 06:38:29 -08:00
|
|
|
#else
|
|
|
|
while (sp > caml_top_of_stack) {
|
|
|
|
#endif
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Find the descriptor corresponding to the return address */
|
|
|
|
h = Hash_retaddr(retaddr);
|
|
|
|
while(1) {
|
|
|
|
d = frame_descriptors[h];
|
|
|
|
if (d->retaddr == retaddr) break;
|
|
|
|
h = (h+1) & frame_descriptors_mask;
|
|
|
|
}
|
1995-12-19 08:00:40 -08:00
|
|
|
if (d->frame_size >= 0) {
|
1995-12-19 07:09:33 -08:00
|
|
|
/* Scan the roots in this frame */
|
1995-12-19 08:00:40 -08:00
|
|
|
for (p = d->live_ofs, n = d->num_live; n > 0; n--, p++) {
|
1995-12-19 07:09:33 -08:00
|
|
|
ofs = *p;
|
|
|
|
if (ofs & 1) {
|
|
|
|
root = &gc_entry_regs[ofs >> 1];
|
|
|
|
} else {
|
|
|
|
root = (value *)(sp + ofs);
|
|
|
|
}
|
|
|
|
oldify(*root, root);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
1995-12-19 07:09:33 -08:00
|
|
|
/* Move to next frame */
|
1995-11-26 06:38:29 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
1995-12-19 07:09:33 -08:00
|
|
|
sp += d->frame_size;
|
1995-11-26 06:38:29 -08:00
|
|
|
#else
|
1995-12-19 07:09:33 -08:00
|
|
|
sp -= d->frame_size;
|
1995-11-26 06:38:29 -08:00
|
|
|
#endif
|
1995-12-19 07:09:33 -08:00
|
|
|
retaddr = Saved_return_address(sp);
|
1995-07-10 02:48:27 -07:00
|
|
|
#ifdef Already_scanned
|
1995-12-19 07:09:33 -08:00
|
|
|
/* Stop here if the frame has already been scanned during earlier GCs */
|
|
|
|
if (Already_scanned(sp, retaddr)) break;
|
|
|
|
/* Mark frame as already scanned */
|
|
|
|
Mark_scanned(sp, retaddr);
|
1995-07-10 02:48:27 -07:00
|
|
|
#endif
|
1995-12-19 07:09:33 -08:00
|
|
|
} else {
|
|
|
|
/* This marks the top of a stack chunk for an ML callback.
|
|
|
|
Skip C portion of stack and continue with next ML stack chunk. */
|
1995-12-19 08:00:40 -08:00
|
|
|
retaddr = Callback_link(sp)->return_address;
|
|
|
|
sp = Callback_link(sp)->bottom_of_stack;
|
1995-12-19 07:09:33 -08:00
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
/* Local C roots */
|
|
|
|
for (block = local_roots; block != NULL; block = (value *) block [1]){
|
|
|
|
for (root = block - (long) block [0]; root < block; root++){
|
1995-10-30 02:20:08 -08:00
|
|
|
oldify (*root, root);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Global C roots */
|
|
|
|
for (gr = global_roots; gr != NULL; gr = gr->next) {
|
1995-10-30 02:20:08 -08:00
|
|
|
oldify(*(gr->root), gr->root);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
1995-10-30 02:20:08 -08:00
|
|
|
/* Hook */
|
|
|
|
if (scan_roots_hook != NULL) (*scan_roots_hook)(oldify);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call [darken] on all roots */
|
|
|
|
|
|
|
|
void darken_all_roots ()
|
|
|
|
{
|
|
|
|
char * sp;
|
|
|
|
unsigned long retaddr;
|
|
|
|
frame_descr * d;
|
|
|
|
unsigned long h;
|
1995-07-17 09:10:45 -07:00
|
|
|
int i, j, n, ofs;
|
1995-07-10 02:48:27 -07:00
|
|
|
short * p;
|
1995-07-17 09:10:45 -07:00
|
|
|
value glob;
|
|
|
|
value * block, * root;
|
1995-07-10 02:48:27 -07:00
|
|
|
struct global_root * gr;
|
|
|
|
|
|
|
|
/* The global roots */
|
1995-07-17 09:10:45 -07:00
|
|
|
for (i = 0; caml_globals[i] != 0; i++) {
|
|
|
|
glob = caml_globals[i];
|
|
|
|
for (j = 0; j < Wosize_val(glob); j++)
|
|
|
|
darken(Field(glob, j));
|
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
|
|
|
|
/* The stack */
|
|
|
|
if (frame_descriptors == NULL) init_frame_descriptors();
|
|
|
|
sp = caml_bottom_of_stack;
|
|
|
|
retaddr = caml_last_return_address;
|
1995-11-26 06:38:29 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
1995-07-10 02:48:27 -07:00
|
|
|
while (sp < caml_top_of_stack) {
|
1995-11-26 06:38:29 -08:00
|
|
|
#else
|
|
|
|
while (sp > caml_top_of_stack) {
|
|
|
|
#endif
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Find the descriptor corresponding to the return address */
|
|
|
|
h = Hash_retaddr(retaddr);
|
|
|
|
while(1) {
|
|
|
|
d = frame_descriptors[h];
|
|
|
|
if (d->retaddr == retaddr) break;
|
|
|
|
h = (h+1) & frame_descriptors_mask;
|
|
|
|
}
|
1995-12-19 08:00:40 -08:00
|
|
|
if (d->frame_size >= 0) {
|
1995-12-19 07:09:33 -08:00
|
|
|
/* Scan the roots in this frame */
|
1995-12-19 08:00:40 -08:00
|
|
|
for (p = d->live_ofs, n = d->num_live; n > 0; n--, p++) {
|
1995-12-19 07:09:33 -08:00
|
|
|
ofs = *p;
|
|
|
|
if (ofs & 1) {
|
|
|
|
darken(gc_entry_regs[ofs >> 1]);
|
|
|
|
} else {
|
|
|
|
darken(*((value *)(sp + ofs)));
|
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
1995-12-19 07:09:33 -08:00
|
|
|
/* Move to next frame */
|
1995-11-26 06:38:29 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
1995-12-19 07:09:33 -08:00
|
|
|
sp += d->frame_size;
|
1995-11-26 06:38:29 -08:00
|
|
|
#else
|
1995-12-19 07:09:33 -08:00
|
|
|
sp -= d->frame_size;
|
1995-11-26 06:38:29 -08:00
|
|
|
#endif
|
1995-12-19 07:09:33 -08:00
|
|
|
retaddr = Saved_return_address(sp);
|
1995-07-10 02:48:27 -07:00
|
|
|
#ifdef Mask_already_scanned
|
1995-12-19 07:09:33 -08:00
|
|
|
retaddr = Mask_already_scanned(retaddr);
|
1995-07-10 02:48:27 -07:00
|
|
|
#endif
|
1995-12-19 07:09:33 -08:00
|
|
|
} else {
|
|
|
|
/* This marks the top of a stack chunk for an ML callback.
|
|
|
|
Skip C portion of stack and continue with next ML stack chunk. */
|
1995-12-19 08:00:40 -08:00
|
|
|
retaddr = Callback_link(sp)->return_address;
|
|
|
|
sp = Callback_link(sp)->bottom_of_stack;
|
1995-12-19 07:09:33 -08:00
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
Assert(sp == caml_top_of_stack);
|
|
|
|
|
|
|
|
/* Local C roots */
|
|
|
|
for (block = local_roots; block != NULL; block = (value *) block [1]){
|
|
|
|
for (root = block - (long) block [0]; root < block; root++){
|
|
|
|
darken (*root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Global C roots */
|
|
|
|
for (gr = global_roots; gr != NULL; gr = gr->next) {
|
|
|
|
darken (*(gr->root));
|
|
|
|
}
|
1995-10-30 02:20:08 -08:00
|
|
|
/* Hook */
|
|
|
|
if (scan_roots_hook != NULL) (*scan_roots_hook)(darken);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|