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 */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1997-12-10 05:27:29 -08:00
|
|
|
#include <stdlib.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include "fail.h"
|
|
|
|
#include "freelist.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "gc_ctrl.h"
|
|
|
|
#include "major_gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "minor_gc.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
1995-12-22 08:48:17 -08:00
|
|
|
#include "signals.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-08-12 05:37:26 -07:00
|
|
|
/* Allocate a block of the requested size, to be passed to
|
1998-08-07 11:43:39 -07:00
|
|
|
[add_to_heap] later.
|
|
|
|
[request] must be a multiple of [Page_size].
|
|
|
|
[alloc_for_heap] returns NULL if the request cannot be satisfied.
|
|
|
|
The returned pointer is a hp, but the header must be initialized by
|
|
|
|
the caller.
|
1995-05-04 03:15:53 -07:00
|
|
|
*/
|
1998-08-07 11:43:39 -07:00
|
|
|
header_t *alloc_for_heap (asize_t request)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *mem;
|
1997-05-13 07:45:38 -07:00
|
|
|
void *block;
|
1998-08-07 11:43:39 -07:00
|
|
|
Assert (request % Page_size == 0);
|
|
|
|
mem = aligned_malloc (request + sizeof (heap_chunk_head),
|
1997-05-13 07:45:38 -07:00
|
|
|
sizeof (heap_chunk_head), &block);
|
1998-08-07 11:43:39 -07:00
|
|
|
if (mem == NULL) return NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
mem += sizeof (heap_chunk_head);
|
1998-08-07 11:43:39 -07:00
|
|
|
Chunk_size (mem) = request;
|
1997-05-13 07:45:38 -07:00
|
|
|
Chunk_block (mem) = block;
|
1998-08-07 11:43:39 -07:00
|
|
|
return (header_t *) mem;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-08-12 05:37:26 -07:00
|
|
|
/* Use this function to free a block allocated with [alloc_for_heap]
|
|
|
|
if you don't add it with [add_to_heap].
|
|
|
|
*/
|
|
|
|
void free_for_heap (header_t *mem)
|
|
|
|
{
|
|
|
|
free (Chunk_block (mem));
|
|
|
|
}
|
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
/* Take a block of memory as argument, which must be the result of a
|
|
|
|
call to [alloc_for_heap], and insert it into the heap chaining.
|
|
|
|
The contents of the block must be a sequence of valid objects and
|
|
|
|
fragments: no space between objects and no trailing garbage. If
|
|
|
|
some objects are blue, they must be added to the free list by the
|
|
|
|
caller. All other objects must have the color [allocation_color(mem)].
|
|
|
|
The caller must update [allocated_words] if applicable.
|
|
|
|
Return value: 0 if no error; -1 in case of error.
|
|
|
|
*/
|
1998-08-12 05:37:26 -07:00
|
|
|
int add_to_heap (header_t *mem)
|
1998-08-07 11:43:39 -07:00
|
|
|
{
|
|
|
|
asize_t i;
|
1998-08-12 05:37:26 -07:00
|
|
|
char *m = (char *) mem;
|
|
|
|
Assert (Chunk_size (m) % Page_size == 0);
|
1998-08-07 11:43:39 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
/* Should check the contents of the block. */
|
|
|
|
#endif /* debug */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
/* Extend the page table as needed. */
|
1998-08-12 05:37:26 -07:00
|
|
|
if (Page (m) < page_low){
|
1998-08-07 11:43:39 -07:00
|
|
|
page_table_entry *block, *new_page_table;
|
1998-08-12 05:37:26 -07:00
|
|
|
asize_t new_page_low = Page (m);
|
1998-08-07 11:43:39 -07:00
|
|
|
asize_t new_size = page_high - new_page_low;
|
|
|
|
|
|
|
|
gc_message (0x08, "Growing page table to %lu entries\n", new_size);
|
|
|
|
block = malloc (new_size * sizeof (page_table_entry));
|
|
|
|
if (block == NULL){
|
|
|
|
gc_message (0x08, "No room for growing page table\n", 0);
|
|
|
|
return -1;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
new_page_table = block - new_page_low;
|
|
|
|
for (i = new_page_low; i < page_low; i++) new_page_table [i] = Not_in_heap;
|
|
|
|
for (i = page_low; i < page_high; i++) new_page_table [i] = page_table [i];
|
|
|
|
free (page_table + page_low);
|
|
|
|
page_table = new_page_table;
|
|
|
|
page_low = new_page_low;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-08-12 05:37:26 -07:00
|
|
|
if (Page (m + Chunk_size (m)) > page_high){
|
1998-08-07 11:43:39 -07:00
|
|
|
page_table_entry *block, *new_page_table;
|
1998-08-12 05:37:26 -07:00
|
|
|
asize_t new_page_high = Page (m + Chunk_size (m));
|
1998-08-07 11:43:39 -07:00
|
|
|
asize_t new_size = new_page_high - page_low;
|
|
|
|
|
|
|
|
gc_message (0x08, "Growing page table to %lu entries\n", new_size);
|
|
|
|
block = malloc (new_size * sizeof (page_table_entry));
|
|
|
|
if (block == NULL){
|
|
|
|
gc_message (0x08, "No room for growing page table\n", 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
new_page_table = block - page_low;
|
|
|
|
for (i = page_low; i < page_high; i++) new_page_table [i] = page_table [i];
|
|
|
|
for (i = page_high; i < new_page_high; i++){
|
1995-05-04 03:15:53 -07:00
|
|
|
new_page_table [i] = Not_in_heap;
|
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
free (page_table + page_low);
|
|
|
|
page_table = new_page_table;
|
|
|
|
page_high = new_page_high;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark the pages as being in the heap. */
|
1998-08-12 05:37:26 -07:00
|
|
|
for (i = Page (m); i < Page (m + Chunk_size (m)); i++){
|
1998-08-07 11:43:39 -07:00
|
|
|
page_table [i] = In_heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Chain this heap block. */
|
1998-09-17 08:09:17 -07:00
|
|
|
{
|
1998-08-07 11:43:39 -07:00
|
|
|
char **last = &heap_start;
|
|
|
|
char *cur = *last;
|
|
|
|
|
1998-08-12 05:37:26 -07:00
|
|
|
while (cur != NULL && cur < m){
|
1997-05-13 07:45:38 -07:00
|
|
|
last = &(Chunk_next (cur));
|
1995-05-04 03:15:53 -07:00
|
|
|
cur = *last;
|
|
|
|
}
|
1998-08-12 05:37:26 -07:00
|
|
|
Chunk_next (m) = cur;
|
|
|
|
*last = m;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-09-17 08:09:17 -07:00
|
|
|
|
|
|
|
/* Update the heap bounds as needed. */
|
|
|
|
/* already done: if (m < heap_start) heap_start = m; */
|
|
|
|
if (m + Chunk_size (m) > heap_end) heap_end = m + Chunk_size (m);
|
|
|
|
|
1998-08-12 05:37:26 -07:00
|
|
|
stat_heap_size += Chunk_size (m);
|
1998-08-07 11:43:39 -07:00
|
|
|
return 0;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
/* Allocate more memory from malloc for the heap.
|
|
|
|
Return a blue block of at least the requested size (in words).
|
|
|
|
The caller must insert the block into the free list.
|
|
|
|
Return NULL when out of memory.
|
|
|
|
*/
|
|
|
|
static char *expand_heap (mlsize_t request)
|
|
|
|
{
|
|
|
|
header_t *mem;
|
|
|
|
asize_t malloc_request;
|
|
|
|
|
|
|
|
malloc_request = round_heap_chunk_size (Bhsize_wosize (request));
|
|
|
|
gc_message (0x04, "Growing heap to %luk bytes\n",
|
|
|
|
(stat_heap_size + malloc_request) / 1024);
|
|
|
|
mem = alloc_for_heap (malloc_request);
|
|
|
|
if (mem == NULL){
|
|
|
|
gc_message (0x04, "No room for growing heap\n", 0);
|
|
|
|
return NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
Assert (Wosize_bhsize (malloc_request) >= request);
|
2000-01-02 08:10:00 -08:00
|
|
|
Hd_hp (mem) = Make_header (Wosize_bhsize (malloc_request), 0, Caml_blue);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
if (add_to_heap (mem) != 0){
|
|
|
|
free (mem);
|
|
|
|
return NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
return Bp_hp (mem);
|
|
|
|
}
|
|
|
|
|
1997-05-13 07:45:38 -07:00
|
|
|
/* Remove the heap chunk [chunk] from the heap and give the memory back
|
1998-08-07 11:43:39 -07:00
|
|
|
to [free].
|
1997-05-13 07:45:38 -07:00
|
|
|
*/
|
1997-09-02 05:55:01 -07:00
|
|
|
void shrink_heap (char *chunk)
|
1997-05-13 07:45:38 -07:00
|
|
|
{
|
|
|
|
char **cp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Never deallocate the first block, because heap_start is both the
|
|
|
|
first block and the base address for page numbers, and we don't
|
|
|
|
want to shift the page table, it's too messy (see above).
|
|
|
|
It will never happen anyway, because of the way compaction works.
|
|
|
|
(see compact.c)
|
|
|
|
*/
|
|
|
|
if (chunk == heap_start) return;
|
|
|
|
|
|
|
|
stat_heap_size -= Chunk_size (chunk);
|
1998-08-07 11:43:39 -07:00
|
|
|
gc_message (0x04, "Shrinking heap to %luk bytes\n", stat_heap_size / 1024);
|
1997-05-13 07:45:38 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
mlsize_t i;
|
|
|
|
for (i = 0; i < Wsize_bsize (Chunk_size (chunk)); i++){
|
2000-04-03 01:34:22 -07:00
|
|
|
((value *) chunk) [i] = Debug_free_shrink;
|
1997-05-13 07:45:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove [chunk] from the list of chunks. */
|
|
|
|
cp = &heap_start;
|
|
|
|
while (*cp != chunk) cp = &(Chunk_next (*cp));
|
|
|
|
*cp = Chunk_next (chunk);
|
|
|
|
|
|
|
|
/* Remove the pages of [chunk] from the page table. */
|
|
|
|
for (i = Page (chunk); i < Page (chunk + Chunk_size (chunk)); i++){
|
|
|
|
page_table [i] = Not_in_heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the [malloc]ed block that contains [chunk]. */
|
|
|
|
free (Chunk_block (chunk));
|
|
|
|
}
|
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
color_t allocation_color (void *hp)
|
|
|
|
{
|
|
|
|
if (gc_phase == Phase_mark
|
|
|
|
|| (gc_phase == Phase_sweep && (addr)hp >= (addr)gc_sweep_hp)){
|
2000-01-02 08:10:00 -08:00
|
|
|
return Caml_black;
|
1998-08-07 11:43:39 -07:00
|
|
|
}else{
|
|
|
|
Assert (gc_phase == Phase_idle
|
|
|
|
|| (gc_phase == Phase_sweep && (addr)hp < (addr)gc_sweep_hp));
|
2000-01-02 08:10:00 -08:00
|
|
|
return Caml_white;
|
1998-08-07 11:43:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc_shr (mlsize_t wosize, tag_t tag)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *hp, *new_block;
|
|
|
|
|
|
|
|
hp = fl_allocate (wosize);
|
|
|
|
if (hp == NULL){
|
|
|
|
new_block = expand_heap (wosize);
|
1995-07-24 05:46:59 -07:00
|
|
|
if (new_block == NULL) {
|
|
|
|
if (in_minor_collection)
|
|
|
|
fatal_error ("Fatal error: out of memory.\n");
|
|
|
|
else
|
|
|
|
raise_out_of_memory ();
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
fl_add_block (new_block);
|
|
|
|
hp = fl_allocate (wosize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert (Is_in_heap (Val_hp (hp)));
|
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
/* Inline expansion of allocation_color. */
|
1997-05-13 07:45:38 -07:00
|
|
|
if (gc_phase == Phase_mark
|
1997-05-15 06:26:08 -07:00
|
|
|
|| (gc_phase == Phase_sweep && (addr)hp >= (addr)gc_sweep_hp)){
|
2000-01-02 08:10:00 -08:00
|
|
|
Hd_hp (hp) = Make_header (wosize, tag, Caml_black);
|
1995-05-04 03:15:53 -07:00
|
|
|
}else{
|
1997-05-13 07:45:38 -07:00
|
|
|
Assert (gc_phase == Phase_idle
|
1997-05-19 08:42:21 -07:00
|
|
|
|| (gc_phase == Phase_sweep && (addr)hp < (addr)gc_sweep_hp));
|
2000-01-02 08:10:00 -08:00
|
|
|
Hd_hp (hp) = Make_header (wosize, tag, Caml_white);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
Assert (Hd_hp (hp) == Make_header (wosize, tag, allocation_color (hp)));
|
1995-05-04 03:15:53 -07:00
|
|
|
allocated_words += Whsize_wosize (wosize);
|
1995-08-08 06:37:34 -07:00
|
|
|
if (allocated_words > Wsize_bsize (minor_heap_size)) urge_major_slice ();
|
2000-04-03 01:34:22 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
for (i = 0; i < wosize; i++){
|
|
|
|
Field (Val_hp (hp), i) = Debug_uninit_major;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
return Val_hp (hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use this function to tell the major GC to speed up when you use
|
2000-03-03 11:29:11 -08:00
|
|
|
finalized blocks to automatically deallocate extra-heap stuff.
|
1995-05-04 03:15:53 -07:00
|
|
|
The GC will do at least one cycle every [max] allocated words;
|
|
|
|
[mem] is the number of words allocated this time.
|
|
|
|
Note that only [mem/max] is relevant. You can use numbers of bytes
|
|
|
|
(or kilobytes, ...) instead of words. You can change units between
|
1998-08-07 11:43:39 -07:00
|
|
|
calls to [adjust_gc_speed].
|
1995-05-04 03:15:53 -07:00
|
|
|
*/
|
1997-09-02 05:55:01 -07:00
|
|
|
void adjust_gc_speed (mlsize_t mem, mlsize_t max)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
if (max == 0) max = 1;
|
|
|
|
if (mem > max) mem = max;
|
1998-08-07 11:43:39 -07:00
|
|
|
extra_heap_memory += (double) mem / (double) max;
|
|
|
|
if (extra_heap_memory > 1.0){
|
|
|
|
extra_heap_memory = 1.0;
|
|
|
|
urge_major_slice ();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
if (extra_heap_memory > (double) Wsize_bsize (minor_heap_size)
|
|
|
|
/ 2.0 / (double) stat_heap_size) {
|
1995-08-08 06:37:34 -07:00
|
|
|
urge_major_slice ();
|
1998-08-07 11:43:39 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* You must use [initialize] to store the initial value in a field of
|
|
|
|
a shared block, unless you are sure the value is not a young block.
|
|
|
|
A block value [v] is a shared block if and only if [Is_in_heap (v)]
|
|
|
|
is true.
|
|
|
|
*/
|
|
|
|
/* [initialize] never calls the GC, so you may call it while an object is
|
|
|
|
unfinished (i.e. just after a call to [alloc_shr].) */
|
1997-09-02 05:55:01 -07:00
|
|
|
void initialize (value *fp, value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
*fp = val;
|
1998-10-26 11:19:32 -08:00
|
|
|
if (Is_in_heap (fp) && Is_block (val) && Is_young (val)){
|
1995-05-04 03:15:53 -07:00
|
|
|
*ref_table_ptr++ = fp;
|
|
|
|
if (ref_table_ptr >= ref_table_limit){
|
|
|
|
realloc_ref_table ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* You must use [modify] to change a field of an existing shared block,
|
|
|
|
unless you are sure the value being overwritten is not a shared block and
|
|
|
|
the value being written is not a young block. */
|
|
|
|
/* [modify] never calls the GC. */
|
1997-09-02 05:55:01 -07:00
|
|
|
void modify (value *fp, value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
Modify (fp, val);
|
|
|
|
}
|
|
|
|
|
1997-11-20 07:30:43 -08:00
|
|
|
void * stat_alloc (asize_t sz)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1997-11-20 07:30:43 -08:00
|
|
|
void * result = malloc (sz);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
if (result == NULL) raise_out_of_memory ();
|
2000-04-03 01:34:22 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
value *p;
|
|
|
|
for (p = result; p < (value *) ((char *) result + sz); p++){
|
|
|
|
*p = Debug_uninit_stat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-11-20 07:30:43 -08:00
|
|
|
void stat_free (void * blk)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
free (blk);
|
|
|
|
}
|
|
|
|
|
1997-11-20 07:30:43 -08:00
|
|
|
void * stat_resize (void * blk, asize_t sz)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1997-11-20 07:30:43 -08:00
|
|
|
void * result = realloc (blk, sz);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
if (result == NULL) raise_out_of_memory ();
|
|
|
|
return result;
|
|
|
|
}
|