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$ */
|
|
|
|
|
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
|
|
|
|
2002-06-03 07:21:50 -07:00
|
|
|
#ifdef USE_MMAP_INSTEAD_OF_MALLOC
|
2004-01-01 08:42:43 -08:00
|
|
|
extern char * caml_aligned_mmap (asize_t size, int modulo, void ** block);
|
|
|
|
extern void caml_aligned_munmap (char * addr, asize_t size);
|
2002-06-03 07:21:50 -07:00
|
|
|
#endif
|
|
|
|
|
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);
|
2002-06-03 07:21:50 -07:00
|
|
|
#ifdef USE_MMAP_INSTEAD_OF_MALLOC
|
2004-01-01 08:42:43 -08:00
|
|
|
mem = caml_aligned_mmap (request + sizeof (heap_chunk_head),
|
|
|
|
sizeof (heap_chunk_head), &block);
|
2002-06-03 07:21:50 -07:00
|
|
|
#else
|
2003-12-29 14:15:02 -08:00
|
|
|
mem = caml_aligned_malloc (request + sizeof (heap_chunk_head),
|
|
|
|
sizeof (heap_chunk_head), &block);
|
2002-06-03 07:21:50 -07:00
|
|
|
#endif
|
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
|
|
|
{
|
2002-06-03 07:21:50 -07:00
|
|
|
#ifdef USE_MMAP_INSTEAD_OF_MALLOC
|
2004-01-03 04:15:49 -08:00
|
|
|
caml_aligned_munmap (Chunk_block (mem),
|
2004-01-01 08:42:43 -08:00
|
|
|
Chunk_size (mem) + sizeof (heap_chunk_head));
|
2002-06-03 07:21:50 -07:00
|
|
|
#else
|
1998-08-12 05:37:26 -07:00
|
|
|
free (Chunk_block (mem));
|
2002-06-03 07:21:50 -07:00
|
|
|
#endif
|
1998-08-12 05:37:26 -07:00
|
|
|
}
|
|
|
|
|
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
|
2003-12-31 06:20:40 -08:00
|
|
|
caller. All other blocks must have the color [caml_allocation_color(mem)].
|
|
|
|
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
|
|
|
{
|
|
|
|
asize_t i;
|
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);
|
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
/* Extend the page table as needed. */
|
2003-12-31 06:20:40 -08:00
|
|
|
if (Page (m) < caml_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);
|
2003-12-31 06:20:40 -08:00
|
|
|
asize_t new_size = caml_page_high - new_page_low;
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "Growing page table to %lu entries\n", new_size);
|
1998-08-07 11:43:39 -07:00
|
|
|
block = malloc (new_size * sizeof (page_table_entry));
|
|
|
|
if (block == NULL){
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "No room for growing page table\n", 0);
|
1998-08-07 11:43:39 -07:00
|
|
|
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;
|
2003-12-31 06:20:40 -08:00
|
|
|
for (i = new_page_low; i < caml_page_low; i++){
|
|
|
|
new_page_table [i] = Not_in_heap;
|
|
|
|
}
|
|
|
|
for (i = caml_page_low; i < caml_page_high; i++){
|
|
|
|
new_page_table [i] = caml_page_table [i];
|
|
|
|
}
|
|
|
|
free (caml_page_table + caml_page_low);
|
|
|
|
caml_page_table = new_page_table;
|
|
|
|
caml_page_low = new_page_low;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
if (Page (m + Chunk_size (m)) > caml_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));
|
2003-12-31 06:20:40 -08:00
|
|
|
asize_t new_size = new_page_high - caml_page_low;
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "Growing page table to %lu entries\n", new_size);
|
1998-08-07 11:43:39 -07:00
|
|
|
block = malloc (new_size * sizeof (page_table_entry));
|
|
|
|
if (block == NULL){
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "No room for growing page table\n", 0);
|
1998-08-07 11:43:39 -07:00
|
|
|
return -1;
|
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
new_page_table = block - caml_page_low;
|
|
|
|
for (i = caml_page_low; i < caml_page_high; i++){
|
|
|
|
new_page_table [i] = caml_page_table [i];
|
|
|
|
}
|
|
|
|
for (i = caml_page_high; i < new_page_high; i++){
|
1995-05-04 03:15:53 -07:00
|
|
|
new_page_table [i] = Not_in_heap;
|
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
free (caml_page_table + caml_page_low);
|
|
|
|
caml_page_table = new_page_table;
|
|
|
|
caml_page_high = new_page_high;
|
1998-08-07 11:43:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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++){
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_page_table [i] = In_heap;
|
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
|
|
|
|
|
|
|
/* Update the heap bounds as needed. */
|
2003-12-31 06:20:40 -08:00
|
|
|
/* already done: if (m < caml_heap_start) heap_start = m; */
|
|
|
|
if (m + Chunk_size (m) > caml_heap_end) caml_heap_end = m + Chunk_size (m);
|
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.
|
|
|
|
Return a blue block of at least the requested size (in words).
|
|
|
|
The caller must insert the block 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)
|
|
|
|
{
|
2002-06-03 07:21:50 -07:00
|
|
|
char *mem;
|
1998-08-07 11:43:39 -07:00
|
|
|
asize_t malloc_request;
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
malloc_request = caml_round_heap_chunk_size (Bhsize_wosize (request));
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
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;
|
2002-06-03 07:21:50 -07:00
|
|
|
asize_t i;
|
1997-05-13 07:45:38 -07:00
|
|
|
|
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",
|
|
|
|
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. */
|
|
|
|
for (i = Page (chunk); i < Page (chunk + Chunk_size (chunk)); i++){
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_page_table [i] = Not_in_heap;
|
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
|
|
|
}
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_fl_add_block (new_block);
|
|
|
|
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;
|
|
|
|
}
|