2016-02-18 07:11:59 -08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* OCaml */
|
|
|
|
/* */
|
|
|
|
/* Damien Doligez, projet Para, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
|
|
|
/* en Automatique. */
|
|
|
|
/* */
|
|
|
|
/* All rights reserved. This file is distributed under the terms of */
|
|
|
|
/* the GNU Lesser General Public License version 2.1, with the */
|
|
|
|
/* special exception on linking described in the file LICENSE. */
|
|
|
|
/* */
|
|
|
|
/**************************************************************************/
|
1995-08-09 08:06:35 -07:00
|
|
|
|
1997-12-10 05:27:29 -08:00
|
|
|
#include <stdlib.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <string.h>
|
2015-06-08 06:21:42 -07:00
|
|
|
#include "caml/address_class.h"
|
2015-12-02 08:40:07 -08:00
|
|
|
#include "caml/config.h"
|
2014-12-27 06:41:49 -08:00
|
|
|
#include "caml/fail.h"
|
|
|
|
#include "caml/freelist.h"
|
|
|
|
#include "caml/gc.h"
|
|
|
|
#include "caml/gc_ctrl.h"
|
|
|
|
#include "caml/major_gc.h"
|
|
|
|
#include "caml/memory.h"
|
|
|
|
#include "caml/major_gc.h"
|
|
|
|
#include "caml/minor_gc.h"
|
|
|
|
#include "caml/misc.h"
|
|
|
|
#include "caml/mlvalues.h"
|
|
|
|
#include "caml/signals.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
int caml_huge_fallback_count = 0;
|
|
|
|
/* Number of times that mmapping big pages fails and we fell back to small
|
|
|
|
pages. This counter is available to the program through
|
|
|
|
[Gc.huge_fallback_count].
|
|
|
|
*/
|
|
|
|
|
|
|
|
uintnat caml_use_huge_pages = 0;
|
|
|
|
/* True iff the program allocates heap chunks by mmapping huge pages.
|
|
|
|
This is set when parsing [OCAMLRUNPARAM] and must stay constant
|
|
|
|
after that.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
|
|
|
|
/* Initialize the [alloc_for_heap] system.
|
|
|
|
This function must be called exactly once, and it must be called
|
|
|
|
before the first call to [alloc_for_heap].
|
|
|
|
It returns 0 on success and -1 on failure.
|
|
|
|
*/
|
|
|
|
int caml_init_alloc_for_heap (void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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.
|
2015-11-20 08:54:26 -08:00
|
|
|
[request] will be rounded up to some implementation-dependent size.
|
|
|
|
The caller must use [Chunk_size] on the result to recover the actual
|
|
|
|
size.
|
|
|
|
Return NULL if the request cannot be satisfied. The returned pointer
|
|
|
|
is a hp, but the header (and the contents) 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
|
|
|
{
|
2015-11-20 08:54:26 -08:00
|
|
|
if (caml_use_huge_pages){
|
|
|
|
#ifdef HAS_HUGE_PAGES
|
|
|
|
uintnat size = Round_mmap_size (sizeof (heap_chunk_head) + request);
|
|
|
|
void *block;
|
|
|
|
char *mem;
|
|
|
|
block = mmap (NULL, size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
|
|
|
|
if (block == MAP_FAILED) return NULL;
|
|
|
|
mem = (char *) block + sizeof (heap_chunk_head);
|
|
|
|
Chunk_size (mem) = size - sizeof (heap_chunk_head);
|
|
|
|
Chunk_block (mem) = block;
|
|
|
|
return mem;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}else{
|
|
|
|
char *mem;
|
|
|
|
void *block;
|
|
|
|
|
|
|
|
request = ((request + Page_size - 1) >> Page_log) << Page_log;
|
|
|
|
mem = caml_aligned_malloc (request + sizeof (heap_chunk_head),
|
|
|
|
sizeof (heap_chunk_head), &block);
|
|
|
|
if (mem == NULL) return NULL;
|
|
|
|
mem += sizeof (heap_chunk_head);
|
|
|
|
Chunk_size (mem) = request;
|
|
|
|
Chunk_block (mem) = block;
|
|
|
|
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
|
|
|
{
|
2015-11-20 08:54:26 -08:00
|
|
|
if (caml_use_huge_pages){
|
|
|
|
#ifdef HAS_HUGE_PAGES
|
|
|
|
munmap (Chunk_block (mem), Chunk_size (mem) + sizeof (heap_chunk_head));
|
|
|
|
#else
|
|
|
|
CAMLassert (0);
|
|
|
|
#endif
|
|
|
|
}else{
|
|
|
|
free (Chunk_block (mem));
|
|
|
|
}
|
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
|
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.
|
2012-04-17 01:21:52 -07:00
|
|
|
|
|
|
|
See also: caml_compact_heap, which duplicates most of this function.
|
1998-08-07 11:43:39 -07:00
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
int caml_add_to_heap (char *m)
|
1998-08-07 11:43:39 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* Should check the contents of the block. */
|
2015-11-20 08:54:26 -08:00
|
|
|
#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",
|
2014-12-24 12:18:22 -08:00
|
|
|
(Bsize_wsize (caml_stat_heap_wsz) + Chunk_size (m)) / 1024);
|
2005-03-24 09:20:54 -08:00
|
|
|
|
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
|
|
|
|
2014-12-24 12:18:22 -08:00
|
|
|
caml_stat_heap_wsz += Wsize_bsize (Chunk_size (m));
|
|
|
|
if (caml_stat_heap_wsz > caml_stat_top_heap_wsz){
|
|
|
|
caml_stat_top_heap_wsz = caml_stat_heap_wsz;
|
2004-01-02 11:23:29 -08:00
|
|
|
}
|
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.
|
2014-12-12 07:18:04 -08:00
|
|
|
[request] is a number of words and must be less than or equal
|
|
|
|
to [Max_wosize].
|
1998-08-07 11:43:39 -07:00
|
|
|
Return NULL when out of memory.
|
|
|
|
*/
|
2014-12-12 07:18:04 -08:00
|
|
|
static value *expand_heap (mlsize_t request)
|
1998-08-07 11:43:39 -07:00
|
|
|
{
|
2014-12-24 12:18:22 -08:00
|
|
|
/* these point to headers, but we do arithmetic on them, hence [value *]. */
|
|
|
|
value *mem, *hp, *prev;
|
2008-02-29 04:56:15 -08:00
|
|
|
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);
|
2015-11-20 08:54:26 -08:00
|
|
|
over_request = request + request / 100 * caml_percent_free;
|
|
|
|
malloc_request = caml_clip_heap_chunk_wsz (over_request);
|
2014-12-24 12:18:22 -08:00
|
|
|
mem = (value *) caml_alloc_for_heap (Bsize_wsize (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
|
|
|
}
|
2015-11-20 08:54:26 -08:00
|
|
|
remain = Wsize_bsize (Chunk_size (mem));
|
2008-02-29 04:56:15 -08:00
|
|
|
prev = hp = mem;
|
2012-09-10 03:25:18 -07:00
|
|
|
/* FIXME find a way to do this with a call to caml_make_free_blocks */
|
2014-12-24 12:18:22 -08:00
|
|
|
while (Wosize_whsize (remain) > Max_wosize){
|
2008-02-29 04:56:15 -08:00
|
|
|
Hd_hp (hp) = Make_header (Max_wosize, 0, Caml_blue);
|
|
|
|
#ifdef DEBUG
|
2014-12-16 11:36:35 -08:00
|
|
|
caml_set_fields (Val_hp (hp), 0, Debug_free_major);
|
2008-02-29 04:56:15 -08:00
|
|
|
#endif
|
2014-12-24 12:18:22 -08:00
|
|
|
hp += Whsize_wosize (Max_wosize);
|
|
|
|
remain -= Whsize_wosize (Max_wosize);
|
|
|
|
Field (Val_hp (mem), 1) = Field (Val_hp (prev), 0) = Val_hp (hp);
|
2008-02-29 04:56:15 -08:00
|
|
|
prev = hp;
|
|
|
|
}
|
|
|
|
if (remain > 1){
|
2014-12-24 12:18:22 -08:00
|
|
|
Hd_hp (hp) = Make_header (Wosize_whsize (remain), 0, Caml_blue);
|
2008-02-29 04:56:15 -08:00
|
|
|
#ifdef DEBUG
|
2014-12-16 11:36:35 -08:00
|
|
|
caml_set_fields (Val_hp (hp), 0, Debug_free_major);
|
2008-02-29 04:56:15 -08:00
|
|
|
#endif
|
2014-12-24 12:18:22 -08:00
|
|
|
Field (Val_hp (mem), 1) = Field (Val_hp (prev), 0) = Val_hp (hp);
|
|
|
|
Field (Val_hp (hp), 0) = (value) NULL;
|
2008-02-29 04:56:15 -08:00
|
|
|
}else{
|
2014-12-24 12:18:22 -08:00
|
|
|
Field (Val_hp (prev), 0) = (value) NULL;
|
2008-02-29 04:56:15 -08:00
|
|
|
if (remain == 1) Hd_hp (hp) = Make_header (0, 0, Caml_white);
|
|
|
|
}
|
|
|
|
Assert (Wosize_hp (mem) >= request);
|
2014-12-24 12:18:22 -08:00
|
|
|
if (caml_add_to_heap ((char *) mem) != 0){
|
|
|
|
caml_free_for_heap ((char *) mem);
|
1998-08-07 11:43:39 -07:00
|
|
|
return NULL;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2014-12-12 07:18:04 -08:00
|
|
|
return Op_hp (mem);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2011-12-30 08:28:57 -08:00
|
|
|
/* Never deallocate the first chunk, 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)
|
2014-12-12 07:18:04 -08:00
|
|
|
XXX FIXME this has become false with the fix to PR#5389 (see compact.c)
|
1997-05-13 07:45:38 -07:00
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
if (chunk == caml_heap_start) return;
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2014-12-24 12:18:22 -08:00
|
|
|
caml_stat_heap_wsz -= Wsize_bsize (Chunk_size (chunk));
|
|
|
|
caml_gc_message (0x04, "Shrinking heap to %luk words\n",
|
|
|
|
(unsigned long) caml_stat_heap_wsz / 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
|
|
|
{
|
2013-11-21 09:02:55 -08:00
|
|
|
if (caml_gc_phase == Phase_mark || caml_gc_phase == Phase_clean
|
2003-12-31 06:20:40 -08:00
|
|
|
|| (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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 03:09:16 -08:00
|
|
|
static inline value caml_alloc_shr_aux (mlsize_t wosize, tag_t tag,
|
|
|
|
int raise_oom)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2014-12-12 07:18:04 -08:00
|
|
|
header_t *hp;
|
|
|
|
value *new_block;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2015-11-23 04:52:04 -08:00
|
|
|
if (wosize > Max_wosize) {
|
|
|
|
if (raise_oom)
|
|
|
|
caml_raise_out_of_memory ();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
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) {
|
2015-11-23 04:51:02 -08:00
|
|
|
if (!raise_oom)
|
2015-11-09 04:36:43 -08:00
|
|
|
return 0;
|
2015-11-23 04:51:02 -08:00
|
|
|
else if (caml_in_minor_collection)
|
|
|
|
caml_fatal_error ("Fatal error: out of memory.\n");
|
|
|
|
else
|
|
|
|
caml_raise_out_of_memory ();
|
1995-07-24 05:46:59 -07:00
|
|
|
}
|
2014-12-12 07:18:04 -08:00
|
|
|
caml_fl_add_blocks ((value) 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. */
|
2013-11-21 09:02:55 -08:00
|
|
|
if (caml_gc_phase == Phase_mark || caml_gc_phase == Phase_clean
|
2003-12-31 06:20:40 -08:00
|
|
|
|| (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);
|
2014-12-12 07:18:04 -08:00
|
|
|
if (caml_allocated_words > caml_minor_heap_wsz){
|
2015-11-20 08:54:26 -08:00
|
|
|
CAML_INSTR_INT ("request_major/alloc_shr@", 1);
|
|
|
|
caml_request_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);
|
|
|
|
}
|
|
|
|
|
2015-11-23 03:09:16 -08:00
|
|
|
CAMLexport value caml_alloc_shr_no_raise (mlsize_t wosize, tag_t tag)
|
|
|
|
{
|
|
|
|
return caml_alloc_shr_aux(wosize, tag, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport value caml_alloc_shr (mlsize_t wosize, tag_t tag)
|
|
|
|
{
|
|
|
|
return caml_alloc_shr_aux(wosize, tag, 1);
|
2015-11-09 04:36:43 -08:00
|
|
|
}
|
|
|
|
|
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){
|
2015-11-20 08:54:26 -08:00
|
|
|
CAML_INSTR_INT ("request_major/adjust_gc_speed_1@", 1);
|
2004-06-14 08:17:43 -07:00
|
|
|
caml_extra_heap_resources = 1.0;
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_request_major_slice ();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2004-06-14 08:17:43 -07:00
|
|
|
if (caml_extra_heap_resources
|
2014-12-12 07:18:04 -08:00
|
|
|
> (double) caml_minor_heap_wsz / 2.0
|
2014-12-24 12:18:22 -08:00
|
|
|
/ (double) caml_stat_heap_wsz) {
|
2015-11-20 08:54:26 -08:00
|
|
|
CAML_INSTR_INT ("request_major/adjust_gc_speed_2@", 1);
|
|
|
|
caml_request_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.
|
|
|
|
*/
|
2013-05-22 05:56:54 -07:00
|
|
|
/* [caml_initialize] never calls the GC, so you may call it while a block is
|
2003-12-31 06:20:40 -08:00
|
|
|
unfinished (i.e. just after a call to [caml_alloc_shr].) */
|
2013-08-01 01:24:07 -07:00
|
|
|
/* PR#6084 workaround: define it as a weak symbol */
|
|
|
|
CAMLexport CAMLweakdef void caml_initialize (value *fp, value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2013-06-01 00:43:45 -07:00
|
|
|
CAMLassert(Is_in_heap(fp));
|
1995-05-04 03:15:53 -07:00
|
|
|
*fp = val;
|
2013-06-01 00:43:45 -07:00
|
|
|
if (Is_block (val) && Is_young (val)) {
|
2015-12-02 08:41:00 -08:00
|
|
|
add_to_ref_table (&caml_ref_table, 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. */
|
2013-06-01 00:43:45 -07:00
|
|
|
/* [caml_modify] can also be used to do assignment on data structures that are
|
|
|
|
in the minor heap instead of in the major heap. In this case, it
|
|
|
|
is a bit slower than simple assignment.
|
|
|
|
In particular, you can use [caml_modify] when you don't know whether the
|
2013-08-01 01:24:07 -07:00
|
|
|
block being changed is in the minor heap or the major heap. */
|
|
|
|
/* PR#6084 workaround: define it as a weak symbol */
|
2013-06-01 00:43:45 -07:00
|
|
|
|
2013-08-01 01:24:07 -07:00
|
|
|
CAMLexport CAMLweakdef void caml_modify (value *fp, value val)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2013-09-04 08:12:37 -07:00
|
|
|
/* The write barrier implemented by [caml_modify] checks for the
|
2013-06-01 00:43:45 -07:00
|
|
|
following two conditions and takes appropriate action:
|
|
|
|
1- a pointer from the major heap to the minor heap is created
|
|
|
|
--> add [fp] to the remembered set
|
|
|
|
2- a pointer from the major heap to the major heap is overwritten,
|
|
|
|
while the GC is in the marking phase
|
|
|
|
--> call [caml_darken] on the overwritten pointer so that the
|
|
|
|
major GC treats it as an additional root.
|
|
|
|
*/
|
|
|
|
value old;
|
|
|
|
|
2013-06-01 07:55:18 -07:00
|
|
|
if (Is_young((value)fp)) {
|
2013-06-01 00:43:45 -07:00
|
|
|
/* The modified object resides in the minor heap.
|
|
|
|
Conditions 1 and 2 cannot occur. */
|
|
|
|
*fp = val;
|
|
|
|
} else {
|
|
|
|
/* The modified object resides in the major heap. */
|
|
|
|
CAMLassert(Is_in_heap(fp));
|
|
|
|
old = *fp;
|
|
|
|
*fp = val;
|
|
|
|
if (Is_block(old)) {
|
|
|
|
/* If [old] is a pointer within the minor heap, we already
|
|
|
|
have a major->minor pointer and [fp] is already in the
|
|
|
|
remembered set. Conditions 1 and 2 cannot occur. */
|
|
|
|
if (Is_young(old)) return;
|
|
|
|
/* Here, [old] can be a pointer within the major heap.
|
|
|
|
Check for condition 2. */
|
|
|
|
if (caml_gc_phase == Phase_mark) caml_darken(old, NULL);
|
|
|
|
}
|
|
|
|
/* Check for condition 1. */
|
|
|
|
if (Is_block(val) && Is_young(val)) {
|
2015-12-02 08:41:00 -08:00
|
|
|
add_to_ref_table (&caml_ref_table, fp);
|
2013-06-01 00:43:45 -07:00
|
|
|
}
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* [sz] is a number of bytes */
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* [sz] is a number of bytes */
|
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;
|
|
|
|
}
|