1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
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$ */
|
|
|
|
|
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"
|
2004-06-14 08:17:43 -07:00
|
|
|
#include "major_gc.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#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
|
|
|
|
2008-02-29 04:56:15 -08:00
|
|
|
extern uintnat caml_percent_free; /* major_gc.c */
|
|
|
|
|
2008-01-03 01:37:10 -08:00
|
|
|
/* Page table management */
|
|
|
|
|
|
|
|
#define Page(p) ((uintnat) (p) >> Page_log)
|
|
|
|
#define Page_mask ((uintnat) -1 << Page_log)
|
|
|
|
|
2008-12-03 10:09:09 -08:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
|
|
|
|
|
|
|
/* 64-bit implementation:
|
|
|
|
The page table is represented sparsely as a hash table
|
2008-01-03 01:37:10 -08:00
|
|
|
with linear probing */
|
|
|
|
|
|
|
|
struct page_table {
|
|
|
|
mlsize_t size; /* size == 1 << (wordsize - shift) */
|
|
|
|
int shift;
|
|
|
|
mlsize_t mask; /* mask == size - 1 */
|
|
|
|
mlsize_t occupancy;
|
|
|
|
uintnat * entries; /* [size] */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct page_table caml_page_table;
|
|
|
|
|
|
|
|
/* Page table entries are the logical 'or' of
|
|
|
|
- the key: address of a page (low Page_log bits = 0)
|
|
|
|
- the data: a 8-bit integer */
|
|
|
|
|
|
|
|
#define Page_entry_matches(entry,addr) \
|
|
|
|
((((entry) ^ (addr)) & Page_mask) == 0)
|
|
|
|
|
|
|
|
/* Multiplicative Fibonacci hashing
|
|
|
|
(Knuth, TAOCP vol 3, section 6.4, page 518).
|
|
|
|
HASH_FACTOR is (sqrt(5) - 1) / 2 * 2^wordsize. */
|
|
|
|
#ifdef ARCH_SIXTYFOUR
|
|
|
|
#define HASH_FACTOR 11400714819323198486UL
|
|
|
|
#else
|
|
|
|
#define HASH_FACTOR 2654435769UL
|
2002-06-03 07:21:50 -07:00
|
|
|
#endif
|
2008-01-03 01:37:10 -08:00
|
|
|
#define Hash(v) (((v) * HASH_FACTOR) >> caml_page_table.shift)
|
|
|
|
|
|
|
|
int caml_page_table_lookup(void * addr)
|
|
|
|
{
|
|
|
|
uintnat h, e;
|
|
|
|
|
|
|
|
h = Hash(Page(addr));
|
|
|
|
/* The first hit is almost always successful, so optimize for this case */
|
|
|
|
e = caml_page_table.entries[h];
|
|
|
|
if (Page_entry_matches(e, (uintnat)addr)) return e & 0xFF;
|
|
|
|
while(1) {
|
|
|
|
if (e == 0) return 0;
|
|
|
|
h = (h + 1) & caml_page_table.mask;
|
|
|
|
e = caml_page_table.entries[h];
|
|
|
|
if (Page_entry_matches(e, (uintnat)addr)) return e & 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int caml_page_table_initialize(mlsize_t bytesize)
|
|
|
|
{
|
|
|
|
uintnat pagesize = Page(bytesize);
|
|
|
|
|
|
|
|
caml_page_table.size = 1;
|
|
|
|
caml_page_table.shift = 8 * sizeof(uintnat);
|
|
|
|
/* Aim for initial load factor between 1/4 and 1/2 */
|
|
|
|
while (caml_page_table.size < 2 * pagesize) {
|
|
|
|
caml_page_table.size <<= 1;
|
|
|
|
caml_page_table.shift -= 1;
|
|
|
|
}
|
|
|
|
caml_page_table.mask = caml_page_table.size - 1;
|
|
|
|
caml_page_table.occupancy = 0;
|
|
|
|
caml_page_table.entries = calloc(caml_page_table.size, sizeof(uintnat));
|
|
|
|
if (caml_page_table.entries == NULL)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int caml_page_table_resize(void)
|
|
|
|
{
|
|
|
|
struct page_table old = caml_page_table;
|
|
|
|
uintnat * new_entries;
|
|
|
|
uintnat i, h;
|
|
|
|
|
2008-02-29 04:56:15 -08:00
|
|
|
caml_gc_message (0x08, "Growing page table to %lu entries\n",
|
2008-01-03 01:37:10 -08:00
|
|
|
caml_page_table.size);
|
|
|
|
|
|
|
|
new_entries = calloc(2 * old.size, sizeof(uintnat));
|
|
|
|
if (new_entries == NULL) {
|
|
|
|
caml_gc_message (0x08, "No room for growing page table\n", 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
caml_page_table.size = 2 * old.size;
|
|
|
|
caml_page_table.shift = old.shift - 1;
|
|
|
|
caml_page_table.mask = caml_page_table.size - 1;
|
|
|
|
caml_page_table.occupancy = old.occupancy;
|
|
|
|
caml_page_table.entries = new_entries;
|
|
|
|
|
|
|
|
for (i = 0; i < old.size; i++) {
|
|
|
|
uintnat e = old.entries[i];
|
|
|
|
if (e == 0) continue;
|
|
|
|
h = Hash(Page(e));
|
|
|
|
while (caml_page_table.entries[h] != 0)
|
|
|
|
h = (h + 1) & caml_page_table.mask;
|
|
|
|
caml_page_table.entries[h] = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(old.entries);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int caml_page_table_modify(uintnat page, int toclear, int toset)
|
|
|
|
{
|
|
|
|
uintnat h;
|
|
|
|
|
|
|
|
Assert ((page & ~Page_mask) == 0);
|
|
|
|
|
|
|
|
/* Resize to keep load factor below 1/2 */
|
|
|
|
if (caml_page_table.occupancy * 2 >= caml_page_table.size) {
|
|
|
|
if (caml_page_table_resize() != 0) return -1;
|
|
|
|
}
|
|
|
|
h = Hash(Page(page));
|
|
|
|
while (1) {
|
|
|
|
if (caml_page_table.entries[h] == 0) {
|
|
|
|
caml_page_table.entries[h] = page | toset;
|
|
|
|
caml_page_table.occupancy++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Page_entry_matches(caml_page_table.entries[h], page)) {
|
|
|
|
caml_page_table.entries[h] =
|
|
|
|
(caml_page_table.entries[h] & ~toclear) | toset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
h = (h + 1) & caml_page_table.mask;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-03 10:09:09 -08:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* 32-bit implementation:
|
|
|
|
The page table is represented as a 2-level array of unsigned char */
|
|
|
|
|
|
|
|
CAMLexport unsigned char * caml_page_table[Pagetable1_size];
|
|
|
|
static unsigned char caml_page_table_empty[Pagetable2_size] = { 0, };
|
|
|
|
|
|
|
|
int caml_page_table_initialize(mlsize_t bytesize)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < Pagetable1_size; i++)
|
|
|
|
caml_page_table[i] = caml_page_table_empty;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int caml_page_table_modify(uintnat page, int toclear, int toset)
|
|
|
|
{
|
|
|
|
uintnat i = Pagetable_index1(page);
|
|
|
|
uintnat j = Pagetable_index2(page);
|
|
|
|
|
|
|
|
if (caml_page_table[i] == caml_page_table_empty) {
|
|
|
|
unsigned char * new_tbl = calloc(Pagetable2_size, 1);
|
|
|
|
if (new_tbl == 0) return -1;
|
|
|
|
caml_page_table[i] = new_tbl;
|
|
|
|
}
|
|
|
|
caml_page_table[i][j] = (caml_page_table[i][j] & ~toclear) | toset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-01-03 01:37:10 -08:00
|
|
|
int caml_page_table_add(int kind, void * start, void * end)
|
|
|
|
{
|
|
|
|
uintnat pstart = (uintnat) start & Page_mask;
|
|
|
|
uintnat pend = ((uintnat) end - 1) & Page_mask;
|
|
|
|
uintnat p;
|
|
|
|
|
|
|
|
for (p = pstart; p <= pend; p += Page_size)
|
|
|
|
if (caml_page_table_modify(p, 0, kind) != 0) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int caml_page_table_remove(int kind, void * start, void * end)
|
|
|
|
{
|
|
|
|
uintnat pstart = (uintnat) start & Page_mask;
|
|
|
|
uintnat pend = ((uintnat) end - 1) & Page_mask;
|
|
|
|
uintnat p;
|
|
|
|
|
|
|
|
for (p = pstart; p <= pend; p += Page_size)
|
|
|
|
if (caml_page_table_modify(p, kind, 0) != 0) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-06-03 07:21:50 -07:00
|
|
|
|
1998-08-12 05:37:26 -07:00
|
|
|
/* Allocate a block of the requested size, to be passed to
|
2003-12-31 06:20:40 -08:00
|
|
|
[caml_add_to_heap] later.
|
1998-08-07 11:43:39 -07:00
|
|
|
[request] must be a multiple of [Page_size].
|
2003-12-31 06:20:40 -08:00
|
|
|
[caml_alloc_for_heap] returns NULL if the request cannot be satisfied.
|
1998-08-07 11:43:39 -07:00
|
|
|
The returned pointer is a hp, but the header must be initialized by
|
|
|
|
the caller.
|
1995-05-04 03:15:53 -07:00
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
char *caml_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);
|
2003-12-29 14:15:02 -08:00
|
|
|
mem = caml_aligned_malloc (request + sizeof (heap_chunk_head),
|
|
|
|
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;
|
2002-06-03 07:21:50 -07:00
|
|
|
return mem;
|
1998-08-07 11:43:39 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
/* Use this function to free a block allocated with [caml_alloc_for_heap]
|
|
|
|
if you don't add it with [caml_add_to_heap].
|
1998-08-12 05:37:26 -07:00
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_free_for_heap (char *mem)
|
1998-08-12 05:37:26 -07:00
|
|
|
{
|
|
|
|
free (Chunk_block (mem));
|
|
|
|
}
|
|
|
|
|
2002-05-28 09:57:31 -07:00
|
|
|
/* Take a chunk of memory as argument, which must be the result of a
|
2003-12-31 06:20:40 -08:00
|
|
|
call to [caml_alloc_for_heap], and insert it into the heap chaining.
|
2002-05-28 09:57:31 -07:00
|
|
|
The contents of the chunk must be a sequence of valid blocks and
|
2001-10-09 07:35:20 -07:00
|
|
|
fragments: no space between blocks and no trailing garbage. If
|
|
|
|
some blocks are blue, they must be added to the free list by the
|
2008-02-29 04:56:15 -08:00
|
|
|
caller. All other blocks must have the color [caml_allocation_color(m)].
|
2003-12-31 06:20:40 -08:00
|
|
|
The caller must update [caml_allocated_words] if applicable.
|
1998-08-07 11:43:39 -07:00
|
|
|
Return value: 0 if no error; -1 in case of error.
|
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
int caml_add_to_heap (char *m)
|
1998-08-07 11:43:39 -07:00
|
|
|
{
|
1998-08-12 05:37:26 -07:00
|
|
|
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
|
|
|
|
2005-03-24 09:20:54 -08:00
|
|
|
caml_gc_message (0x04, "Growing heap to %luk bytes\n",
|
|
|
|
(caml_stat_heap_size + Chunk_size (m)) / 1024);
|
|
|
|
|
2008-01-03 01:37:10 -08:00
|
|
|
/* Register block in page table */
|
|
|
|
if (caml_page_table_add(In_heap, m, m + Chunk_size(m)) != 0)
|
|
|
|
return -1;
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2002-05-28 09:57:31 -07:00
|
|
|
/* Chain this heap chunk. */
|
1998-09-17 08:09:17 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
char **last = &caml_heap_start;
|
1998-08-07 11:43:39 -07:00
|
|
|
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;
|
2002-05-28 09:57:31 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
++ caml_stat_heap_chunks;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-09-17 08:09:17 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_stat_heap_size += Chunk_size (m);
|
|
|
|
if (caml_stat_heap_size > caml_stat_top_heap_size){
|
|
|
|
caml_stat_top_heap_size = caml_stat_heap_size;
|
|
|
|
}
|
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.
|
2008-02-29 04:56:15 -08:00
|
|
|
Return a blue block of at least the requested size.
|
|
|
|
The blue block is chained to a sequence of blue blocks (through their
|
|
|
|
field 0); the last block of the chain is pointed by field 1 of the
|
|
|
|
first. There may be a fragment after the last block.
|
|
|
|
The caller must insert the blocks into the free list.
|
2002-12-12 10:59:11 -08:00
|
|
|
The request must be less than or equal to Max_wosize.
|
1998-08-07 11:43:39 -07:00
|
|
|
Return NULL when out of memory.
|
|
|
|
*/
|
|
|
|
static char *expand_heap (mlsize_t request)
|
|
|
|
{
|
2008-02-29 04:56:15 -08:00
|
|
|
char *mem, *hp, *prev;
|
|
|
|
asize_t over_request, malloc_request, remain;
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2008-02-29 04:56:15 -08:00
|
|
|
Assert (request <= Max_wosize);
|
|
|
|
over_request = request + request / 100 * caml_percent_free;
|
|
|
|
malloc_request = caml_round_heap_chunk_size (Bhsize_wosize (over_request));
|
2003-12-31 06:20:40 -08:00
|
|
|
mem = caml_alloc_for_heap (malloc_request);
|
1998-08-07 11:43:39 -07:00
|
|
|
if (mem == NULL){
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x04, "No room for growing heap\n", 0);
|
1998-08-07 11:43:39 -07:00
|
|
|
return NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2008-02-29 04:56:15 -08:00
|
|
|
remain = malloc_request;
|
|
|
|
prev = hp = mem;
|
|
|
|
/* XXX find a way to do this with a call to caml_make_free_blocks */
|
|
|
|
while (Wosize_bhsize (remain) > Max_wosize){
|
|
|
|
Hd_hp (hp) = Make_header (Max_wosize, 0, Caml_blue);
|
|
|
|
#ifdef DEBUG
|
|
|
|
caml_set_fields (Bp_hp (hp), 0, Debug_free_major);
|
|
|
|
#endif
|
|
|
|
hp += Bhsize_wosize (Max_wosize);
|
|
|
|
remain -= Bhsize_wosize (Max_wosize);
|
|
|
|
Field (Op_hp (mem), 1) = Field (Op_hp (prev), 0) = (value) Op_hp (hp);
|
|
|
|
prev = hp;
|
|
|
|
}
|
|
|
|
if (remain > 1){
|
|
|
|
Hd_hp (hp) = Make_header (Wosize_bhsize (remain), 0, Caml_blue);
|
|
|
|
#ifdef DEBUG
|
|
|
|
caml_set_fields (Bp_hp (hp), 0, Debug_free_major);
|
|
|
|
#endif
|
|
|
|
Field (Op_hp (mem), 1) = Field (Op_hp (prev), 0) = (value) Op_hp (hp);
|
|
|
|
Field (Op_hp (hp), 0) = (value) NULL;
|
|
|
|
}else{
|
|
|
|
Field (Op_hp (prev), 0) = (value) NULL;
|
|
|
|
if (remain == 1) Hd_hp (hp) = Make_header (0, 0, Caml_white);
|
|
|
|
}
|
|
|
|
Assert (Wosize_hp (mem) >= request);
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_add_to_heap (mem) != 0){
|
|
|
|
caml_free_for_heap (mem);
|
1998-08-07 11:43:39 -07:00
|
|
|
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
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_shrink_heap (char *chunk)
|
1997-05-13 07:45:38 -07:00
|
|
|
{
|
|
|
|
char **cp;
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
/* Never deallocate the first block, because caml_heap_start is both the
|
1997-05-13 07:45:38 -07:00
|
|
|
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)
|
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
if (chunk == caml_heap_start) return;
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_stat_heap_size -= Chunk_size (chunk);
|
|
|
|
caml_gc_message (0x04, "Shrinking heap to %luk bytes\n",
|
2008-02-29 04:56:15 -08:00
|
|
|
(unsigned long) caml_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
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
-- caml_stat_heap_chunks;
|
2002-05-28 09:57:31 -07:00
|
|
|
|
1997-05-13 07:45:38 -07:00
|
|
|
/* Remove [chunk] from the list of chunks. */
|
2003-12-31 06:20:40 -08:00
|
|
|
cp = &caml_heap_start;
|
1997-05-13 07:45:38 -07:00
|
|
|
while (*cp != chunk) cp = &(Chunk_next (*cp));
|
|
|
|
*cp = Chunk_next (chunk);
|
|
|
|
|
|
|
|
/* Remove the pages of [chunk] from the page table. */
|
2008-01-03 01:37:10 -08:00
|
|
|
caml_page_table_remove(In_heap, chunk, chunk + Chunk_size (chunk));
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2002-02-05 09:11:33 -08:00
|
|
|
/* Free the [malloc] block that contains [chunk]. */
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_free_for_heap (chunk);
|
1997-05-13 07:45:38 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
color_t caml_allocation_color (void *hp)
|
1998-08-07 11:43:39 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_gc_phase == Phase_mark
|
|
|
|
|| (caml_gc_phase == Phase_sweep && (addr)hp >= (addr)caml_gc_sweep_hp)){
|
2000-01-02 08:10:00 -08:00
|
|
|
return Caml_black;
|
1998-08-07 11:43:39 -07:00
|
|
|
}else{
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (caml_gc_phase == Phase_idle
|
|
|
|
|| (caml_gc_phase == Phase_sweep
|
|
|
|
&& (addr)hp < (addr)caml_gc_sweep_hp));
|
2000-01-02 08:10:00 -08:00
|
|
|
return Caml_white;
|
1998-08-07 11:43:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport value caml_alloc_shr (mlsize_t wosize, tag_t tag)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *hp, *new_block;
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
if (wosize > Max_wosize) caml_raise_out_of_memory ();
|
2004-01-02 11:23:29 -08:00
|
|
|
hp = caml_fl_allocate (wosize);
|
1995-05-04 03:15:53 -07:00
|
|
|
if (hp == NULL){
|
|
|
|
new_block = expand_heap (wosize);
|
1995-07-24 05:46:59 -07:00
|
|
|
if (new_block == NULL) {
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_in_minor_collection)
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_fatal_error ("Fatal error: out of memory.\n");
|
1995-07-24 05:46:59 -07:00
|
|
|
else
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_out_of_memory ();
|
1995-07-24 05:46:59 -07:00
|
|
|
}
|
2008-02-29 04:56:15 -08:00
|
|
|
caml_fl_add_blocks (new_block);
|
2004-01-02 11:23:29 -08:00
|
|
|
hp = caml_fl_allocate (wosize);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Assert (Is_in_heap (Val_hp (hp)));
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
/* Inline expansion of caml_allocation_color. */
|
|
|
|
if (caml_gc_phase == Phase_mark
|
|
|
|
|| (caml_gc_phase == Phase_sweep && (addr)hp >= (addr)caml_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{
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (caml_gc_phase == Phase_idle
|
|
|
|
|| (caml_gc_phase == Phase_sweep
|
|
|
|
&& (addr)hp < (addr)caml_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
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (Hd_hp (hp) == Make_header (wosize, tag, caml_allocation_color (hp)));
|
|
|
|
caml_allocated_words += Whsize_wosize (wosize);
|
|
|
|
if (caml_allocated_words > Wsize_bsize (caml_minor_heap_size)){
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_urge_major_slice ();
|
2003-12-31 06:20:40 -08:00
|
|
|
}
|
2000-04-03 01:34:22 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
uintnat i;
|
2000-04-03 01:34:22 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-06-14 08:17:43 -07:00
|
|
|
/* Dependent memory is all memory blocks allocated out of the heap
|
|
|
|
that depend on the GC (and finalizers) for deallocation.
|
2004-10-07 04:13:15 -07:00
|
|
|
For the GC to take dependent memory into account when computing
|
|
|
|
its automatic speed setting,
|
2004-06-14 08:17:43 -07:00
|
|
|
you must call [caml_alloc_dependent_memory] when you alloate some
|
|
|
|
dependent memory, and [caml_free_dependent_memory] when you
|
2004-10-07 04:13:15 -07:00
|
|
|
free it. In both cases, you pass as argument the size (in bytes)
|
|
|
|
of the block being allocated or freed.
|
2004-06-14 08:17:43 -07:00
|
|
|
*/
|
|
|
|
CAMLexport void caml_alloc_dependent_memory (mlsize_t nbytes)
|
|
|
|
{
|
|
|
|
caml_dependent_size += nbytes / sizeof (value);
|
|
|
|
caml_dependent_allocated += nbytes / sizeof (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport void caml_free_dependent_memory (mlsize_t nbytes)
|
|
|
|
{
|
|
|
|
if (caml_dependent_size < nbytes / sizeof (value)){
|
|
|
|
caml_dependent_size = 0;
|
|
|
|
}else{
|
|
|
|
caml_dependent_size -= nbytes / sizeof (value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Use this function to tell the major GC to speed up when you use
|
2004-06-14 08:17:43 -07:00
|
|
|
finalized blocks to automatically deallocate resources (other
|
|
|
|
than memory). The GC will do at least one cycle every [max]
|
|
|
|
allocated resources; [res] is the number of resources allocated
|
|
|
|
this time.
|
|
|
|
Note that only [res/max] is relevant. The units (and kind of
|
|
|
|
resource) can change between calls to [caml_adjust_gc_speed].
|
1995-05-04 03:15:53 -07:00
|
|
|
*/
|
2004-06-14 08:17:43 -07:00
|
|
|
CAMLexport void caml_adjust_gc_speed (mlsize_t res, mlsize_t max)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
if (max == 0) max = 1;
|
2004-06-14 08:17:43 -07:00
|
|
|
if (res > max) res = max;
|
|
|
|
caml_extra_heap_resources += (double) res / (double) max;
|
|
|
|
if (caml_extra_heap_resources > 1.0){
|
|
|
|
caml_extra_heap_resources = 1.0;
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_urge_major_slice ();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2004-06-14 08:17:43 -07:00
|
|
|
if (caml_extra_heap_resources
|
|
|
|
> (double) Wsize_bsize (caml_minor_heap_size) / 2.0
|
|
|
|
/ (double) Wsize_bsize (caml_stat_heap_size)) {
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_urge_major_slice ();
|
1998-08-07 11:43:39 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
/* You must use [caml_initialize] to store the initial value in a field of
|
1995-05-04 03:15:53 -07:00
|
|
|
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.
|
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
/* [caml_initialize] never calls the GC, so you may call it while an block is
|
|
|
|
unfinished (i.e. just after a call to [caml_alloc_shr].) */
|
|
|
|
void caml_initialize (value *fp, value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
*fp = val;
|
2001-08-21 03:00:11 -07:00
|
|
|
if (Is_block (val) && Is_young (val) && Is_in_heap (fp)){
|
2007-05-04 07:05:13 -07:00
|
|
|
if (caml_ref_table.ptr >= caml_ref_table.limit){
|
|
|
|
caml_realloc_ref_table (&caml_ref_table);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2007-05-04 07:05:13 -07:00
|
|
|
*caml_ref_table.ptr++ = fp;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
/* You must use [caml_modify] to change a field of an existing shared block,
|
1995-05-04 03:15:53 -07:00
|
|
|
unless you are sure the value being overwritten is not a shared block and
|
|
|
|
the value being written is not a young block. */
|
2003-12-31 06:20:40 -08:00
|
|
|
/* [caml_modify] never calls the GC. */
|
|
|
|
void caml_modify (value *fp, value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
Modify (fp, val);
|
|
|
|
}
|
|
|
|
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport void * caml_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
|
|
|
|
2001-06-21 02:14:07 -07:00
|
|
|
/* malloc() may return NULL if size is 0 */
|
2004-01-01 08:42:43 -08:00
|
|
|
if (result == NULL && sz != 0) caml_raise_out_of_memory ();
|
2000-04-03 01:34:22 -07:00
|
|
|
#ifdef DEBUG
|
2001-12-04 02:53:19 -08:00
|
|
|
memset (result, Debug_uninit_stat, sz);
|
2000-04-03 01:34:22 -07:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport void caml_stat_free (void * blk)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
free (blk);
|
|
|
|
}
|
|
|
|
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport void * caml_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
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
if (result == NULL) caml_raise_out_of_memory ();
|
1995-05-04 03:15:53 -07:00
|
|
|
return result;
|
|
|
|
}
|