summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Mouratov <mmouratov@gmail.com>2014-05-29 05:11:47 +0600
committerMax Mouratov <mmouratov@gmail.com>2017-03-17 20:39:03 +0500
commit02a8b999f08dcbf6cfcdc1145f3286392d26ad52 (patch)
treea6d5a5f5566238184890c94efff644e9d8d2b4ae
parent1a8af593ad2c179d2f30dede6f88f71be54f40c0 (diff)
downloadocaml-02a8b999f08dcbf6cfcdc1145f3286392d26ad52.tar.gz
runtime: replacing direct calls to malloc/calloc/realloc/free with calls to caml_stat_*
A few more wrappers were added (caml_stat_alloc_noexc, caml_stat_resize_noexc, caml_stat_calloc_noexc) that do not throw an exception in case of errors and offer a compatible substitute to the corresponding stdlib functions.
-rw-r--r--asmrun/backtrace_prim.c4
-rw-r--r--asmrun/spacetime.c9
-rw-r--r--byterun/array.c4
-rw-r--r--byterun/backtrace_prim.c7
-rw-r--r--byterun/bigarray.c10
-rw-r--r--byterun/caml/memory.h3
-rw-r--r--byterun/caml/misc.h2
-rw-r--r--byterun/compare.c8
-rw-r--r--byterun/dynlink.c4
-rw-r--r--byterun/extern.c24
-rw-r--r--byterun/finalise.c6
-rw-r--r--byterun/intern.c10
-rw-r--r--byterun/major_gc.c7
-rw-r--r--byterun/memory.c32
-rw-r--r--byterun/minor_gc.c7
-rw-r--r--byterun/misc.c4
-rw-r--r--byterun/printexc.c5
-rw-r--r--byterun/str.c8
-rw-r--r--byterun/win32.c4
-rw-r--r--otherlibs/systhreads/st_posix.h20
-rw-r--r--otherlibs/systhreads/st_stubs.c8
-rw-r--r--otherlibs/systhreads/st_win32.h8
-rw-r--r--otherlibs/threads/scheduler.c4
-rw-r--r--otherlibs/win32graph/draw.c4
24 files changed, 114 insertions, 88 deletions
diff --git a/asmrun/backtrace_prim.c b/asmrun/backtrace_prim.c
index 4641d4e1bc..7691a7b4b0 100644
--- a/asmrun/backtrace_prim.c
+++ b/asmrun/backtrace_prim.c
@@ -71,8 +71,8 @@ frame_descr * caml_next_frame_descriptor(uintnat * pc, char ** sp)
int caml_alloc_backtrace_buffer(void){
CAMLassert(caml_backtrace_pos == 0);
- caml_backtrace_buffer = malloc(BACKTRACE_BUFFER_SIZE
- * sizeof(backtrace_slot));
+ caml_backtrace_buffer =
+ caml_stat_alloc_noexc(BACKTRACE_BUFFER_SIZE * sizeof(backtrace_slot));
if (caml_backtrace_buffer == NULL) return -1;
return 0;
}
diff --git a/asmrun/spacetime.c b/asmrun/spacetime.c
index 7c00b01217..aaf7f6af8e 100644
--- a/asmrun/spacetime.c
+++ b/asmrun/spacetime.c
@@ -102,7 +102,7 @@ static void reinitialise_free_node_block(void)
{
size_t index;
- start_of_free_node_block = (char*) malloc(chunk_size);
+ start_of_free_node_block = (char*) caml_stat_alloc_noexc(chunk_size);
end_of_free_node_block = start_of_free_node_block + chunk_size;
for (index = 0; index < chunk_size / sizeof(value); index++) {
@@ -254,7 +254,7 @@ void caml_spacetime_initialize(void)
void caml_spacetime_register_shapes(void* dynlinked_table)
{
shape_table* table;
- table = (shape_table*) malloc(sizeof(shape_table));
+ table = (shape_table*) caml_stat_alloc_noexc(sizeof(shape_table));
if (table == NULL) {
fprintf(stderr, "Out of memory whilst registering shape table");
abort();
@@ -279,7 +279,7 @@ void caml_spacetime_register_thread(
{
per_thread* thr;
- thr = (per_thread*) malloc(sizeof(per_thread));
+ thr = (per_thread*) caml_stat_alloc_noexc(sizeof(per_thread));
if (thr == NULL) {
fprintf(stderr, "Out of memory while registering thread for profiling\n");
abort();
@@ -725,7 +725,8 @@ static NOINLINE void* find_trie_node_from_libunwind(int for_allocation,
have_frames_already = 1;
}
else {
- frames = (struct ext_table*) malloc(sizeof(struct ext_table));
+ frames =
+ (struct ext_table*) caml_stat_alloc_noexc(sizeof(struct ext_table));
if (!frames) {
caml_fatal_error("Not enough memory for ext_table allocation");
}
diff --git a/byterun/array.c b/byterun/array.c
index faf106dddd..4b0e834df1 100644
--- a/byterun/array.c
+++ b/byterun/array.c
@@ -406,12 +406,12 @@ CAMLprim value caml_array_concat(value al)
lengths = static_lengths;
} else {
arrays = caml_stat_alloc(n * sizeof(value));
- offsets = malloc(n * sizeof(intnat));
+ offsets = caml_stat_alloc_noexc(n * sizeof(intnat));
if (offsets == NULL) {
caml_stat_free(arrays);
caml_raise_out_of_memory();
}
- lengths = malloc(n * sizeof(value));
+ lengths = caml_stat_alloc_noexc(n * sizeof(value));
if (lengths == NULL) {
caml_stat_free(offsets);
caml_stat_free(arrays);
diff --git a/byterun/backtrace_prim.c b/byterun/backtrace_prim.c
index 7d4417c824..56d40c69b7 100644
--- a/byterun/backtrace_prim.c
+++ b/byterun/backtrace_prim.c
@@ -126,7 +126,7 @@ static struct ev_info *process_debug_events(code_t code_start, value events_heap
if (*num_events == 0)
CAMLreturnT(struct ev_info *, NULL);
- events = malloc(*num_events * sizeof(struct ev_info));
+ events = caml_stat_alloc_noexc(*num_events * sizeof(struct ev_info));
if(events == NULL)
caml_fatal_error ("caml_add_debug_info: out of memory");
@@ -142,7 +142,7 @@ static struct ev_info *process_debug_events(code_t code_start, value events_heap
{
uintnat fnsz = caml_string_length(Field(ev_start, POS_FNAME)) + 1;
- events[j].ev_filename = (char*)malloc(fnsz);
+ events[j].ev_filename = (char*)caml_stat_alloc_noexc(fnsz);
if(events[j].ev_filename == NULL)
caml_fatal_error ("caml_add_debug_info: out of memory");
memcpy(events[j].ev_filename,
@@ -219,7 +219,8 @@ CAMLprim value caml_remove_debug_info(code_t start)
int caml_alloc_backtrace_buffer(void){
CAMLassert(caml_backtrace_pos == 0);
- caml_backtrace_buffer = malloc(BACKTRACE_BUFFER_SIZE * sizeof(code_t));
+ caml_backtrace_buffer =
+ caml_stat_alloc_noexc(BACKTRACE_BUFFER_SIZE * sizeof(code_t));
if (caml_backtrace_buffer == NULL) return -1;
return 0;
}
diff --git a/byterun/bigarray.c b/byterun/bigarray.c
index 8d2d0eb267..9685e48dc0 100644
--- a/byterun/bigarray.c
+++ b/byterun/bigarray.c
@@ -85,7 +85,7 @@ CAMLexport struct custom_operations caml_ba_ops = {
/* [caml_ba_alloc] will allocate a new bigarray object in the heap.
If [data] is NULL, the memory for the contents is also allocated
- (with [malloc]) by [caml_ba_alloc].
+ (with [caml_stat_alloc]) by [caml_ba_alloc].
[data] cannot point into the OCaml heap.
[dim] may point into an object in the OCaml heap.
*/
@@ -112,7 +112,7 @@ caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim)
caml_ba_element_size[flags & CAML_BA_KIND_MASK],
&size))
caml_raise_out_of_memory();
- data = malloc(size);
+ data = caml_stat_alloc_noexc(size);
if (data == NULL && size != 0) caml_raise_out_of_memory();
flags |= CAML_BA_MANAGED;
}
@@ -156,10 +156,10 @@ CAMLexport void caml_ba_finalize(value v)
break;
case CAML_BA_MANAGED:
if (b->proxy == NULL) {
- free(b->data);
+ caml_stat_free(b->data);
} else {
if (-- b->proxy->refcount == 0) {
- free(b->proxy->data);
+ caml_stat_free(b->proxy->data);
caml_stat_free(b->proxy);
}
}
@@ -455,7 +455,7 @@ CAMLexport uintnat caml_ba_deserialize(void * dst)
caml_deserialize_error("input_value: bad bigarray kind");
elt_size = caml_ba_element_size[b->flags & CAML_BA_KIND_MASK];
/* Allocate room for data */
- b->data = malloc(elt_size * num_elts);
+ b->data = caml_stat_alloc_noexc(elt_size * num_elts);
if (b->data == NULL)
caml_deserialize_error("input_value: out of memory for bigarray");
/* Read data */
diff --git a/byterun/caml/memory.h b/byterun/caml/memory.h
index e366fd83d1..9a9763b984 100644
--- a/byterun/caml/memory.h
+++ b/byterun/caml/memory.h
@@ -54,8 +54,11 @@ CAMLextern void caml_modify (value *, value);
CAMLextern void caml_initialize (value *, value);
CAMLextern value caml_check_urgent_gc (value);
CAMLextern void * caml_stat_alloc (asize_t); /* Size in bytes. */
+CAMLextern void * caml_stat_alloc_noexc (asize_t);
CAMLextern void caml_stat_free (void *);
CAMLextern void * caml_stat_resize (void *, asize_t); /* Size in bytes. */
+CAMLextern void * caml_stat_resize_noexc (void *, asize_t);
+CAMLextern void * caml_stat_calloc_noexc (asize_t, asize_t);
CAMLextern int caml_init_alloc_for_heap (void);
CAMLextern char *caml_alloc_for_heap (asize_t request); /* Size in bytes. */
CAMLextern void caml_free_for_heap (char *mem);
diff --git a/byterun/caml/misc.h b/byterun/caml/misc.h
index 7d80cd0bec..f5b4b7190c 100644
--- a/byterun/caml/misc.h
+++ b/byterun/caml/misc.h
@@ -375,7 +375,7 @@ extern struct CAML_INSTR_BLOCK *CAML_INSTR_LOG;
#define CAML_INSTR_ALLOC(t) do{ \
if (caml_stat_minor_collections >= CAML_INSTR_STARTTIME \
&& caml_stat_minor_collections < CAML_INSTR_STOPTIME){ \
- t = malloc (sizeof (struct CAML_INSTR_BLOCK)); \
+ t = caml_stat_alloc_noexc (sizeof (struct CAML_INSTR_BLOCK)); \
t->index = 0; \
t->tag[0] = ""; \
t->next = CAML_INSTR_LOG; \
diff --git a/byterun/compare.c b/byterun/compare.c
index 387ff668b7..0a1f30e345 100644
--- a/byterun/compare.c
+++ b/byterun/compare.c
@@ -46,7 +46,7 @@ struct compare_stack {
static void compare_free_stack(struct compare_stack* stk)
{
if (stk->stack != stk->init_stack) {
- free(stk->stack);
+ caml_stat_free(stk->stack);
stk->stack = NULL;
}
}
@@ -69,15 +69,15 @@ static struct compare_item * compare_resize_stack(struct compare_stack* stk,
if (stk->stack == stk->init_stack) {
newsize = COMPARE_STACK_MIN_ALLOC_SIZE;
- newstack = malloc(sizeof(struct compare_item) * newsize);
+ newstack = caml_stat_alloc_noexc(sizeof(struct compare_item) * newsize);
if (newstack == NULL) compare_stack_overflow(stk);
memcpy(newstack, stk->init_stack,
sizeof(struct compare_item) * COMPARE_STACK_INIT_SIZE);
} else {
newsize = 2 * (stk->limit - stk->stack);
if (newsize >= COMPARE_STACK_MAX_SIZE) compare_stack_overflow(stk);
- newstack =
- realloc(stk->stack, sizeof(struct compare_item) * newsize);
+ newstack = caml_stat_resize_noexc(stk->stack,
+ sizeof(struct compare_item) * newsize);
if (newstack == NULL) compare_stack_overflow(stk);
}
stk->stack = newstack;
diff --git a/byterun/dynlink.c b/byterun/dynlink.c
index f80d1f7fc7..c667ad4ffe 100644
--- a/byterun/dynlink.c
+++ b/byterun/dynlink.c
@@ -171,7 +171,7 @@ void caml_build_primitive_table(char * lib_path,
caml_fatal_error_arg("Fatal error: unknown C primitive `%s'\n", p);
caml_ext_table_add(&caml_prim_table, (void *) prim);
#ifdef DEBUG
- caml_ext_table_add(&caml_prim_name_table, strdup(p));
+ caml_ext_table_add(&caml_prim_name_table, caml_strdup(p));
#endif
}
/* Clean up */
@@ -194,7 +194,7 @@ void caml_build_primitive_table_builtin(void)
caml_ext_table_add(&caml_prim_table, (void *) caml_builtin_cprim[i]);
#ifdef DEBUG
caml_ext_table_add(&caml_prim_name_table,
- strdup(caml_names_of_builtin_cprim[i]));
+ caml_strdup(caml_names_of_builtin_cprim[i]));
#endif
}
}
diff --git a/byterun/extern.c b/byterun/extern.c
index 51240d0be9..ddd9b86cc2 100644
--- a/byterun/extern.c
+++ b/byterun/extern.c
@@ -103,7 +103,7 @@ static void free_extern_output(void);
static void extern_free_stack(void)
{
if (extern_stack != extern_stack_init) {
- free(extern_stack);
+ caml_stat_free(extern_stack);
/* Reinitialize the globals for next time around */
extern_stack = extern_stack_init;
extern_stack_limit = extern_stack + EXTERN_STACK_INIT_SIZE;
@@ -118,13 +118,13 @@ static struct extern_item * extern_resize_stack(struct extern_item * sp)
if (newsize >= EXTERN_STACK_MAX_SIZE) extern_stack_overflow();
if (extern_stack == extern_stack_init) {
- newstack = malloc(sizeof(struct extern_item) * newsize);
+ newstack = caml_stat_alloc_noexc(sizeof(struct extern_item) * newsize);
if (newstack == NULL) extern_stack_overflow();
memcpy(newstack, extern_stack_init,
sizeof(struct extern_item) * EXTERN_STACK_INIT_SIZE);
} else {
- newstack =
- realloc(extern_stack, sizeof(struct extern_item) * newsize);
+ newstack = caml_stat_resize_noexc(extern_stack,
+ sizeof(struct extern_item) * newsize);
if (newstack == NULL) extern_stack_overflow();
}
extern_stack = newstack;
@@ -161,7 +161,7 @@ static void extern_replay_trail(void)
}
if (blk == &extern_trail_first) break;
prevblk = blk->previous;
- free(blk);
+ caml_stat_free(blk);
blk = prevblk;
lim = &(blk->entries[ENTRIES_PER_TRAIL_BLOCK]);
}
@@ -179,7 +179,7 @@ static void extern_record_location(value obj)
if (extern_flags & NO_SHARING) return;
if (extern_trail_cur == extern_trail_limit) {
- struct trail_block * new_block = malloc(sizeof(struct trail_block));
+ struct trail_block * new_block = caml_stat_alloc_noexc(sizeof(struct trail_block));
if (new_block == NULL) extern_out_of_memory();
new_block->previous = extern_trail_block;
extern_trail_block = new_block;
@@ -211,7 +211,7 @@ static struct output_block * extern_output_first, * extern_output_block;
static void init_extern_output(void)
{
extern_userprovided_output = NULL;
- extern_output_first = malloc(sizeof(struct output_block));
+ extern_output_first = caml_stat_alloc_noexc(sizeof(struct output_block));
if (extern_output_first == NULL) caml_raise_out_of_memory();
extern_output_block = extern_output_first;
extern_output_block->next = NULL;
@@ -233,7 +233,7 @@ static void free_extern_output(void)
if (extern_userprovided_output != NULL) return;
for (blk = extern_output_first; blk != NULL; blk = nextblk) {
nextblk = blk->next;
- free(blk);
+ caml_stat_free(blk);
}
extern_output_first = NULL;
extern_free_stack();
@@ -252,7 +252,7 @@ static void grow_extern_output(intnat required)
extra = 0;
else
extra = required;
- blk = malloc(sizeof(struct output_block) + extra);
+ blk = caml_stat_alloc_noexc(sizeof(struct output_block) + extra);
if (blk == NULL) extern_out_of_memory();
extern_output_block->next = blk;
extern_output_block = blk;
@@ -674,7 +674,7 @@ void caml_output_val(struct channel *chan, value v, value flags)
while (blk != NULL) {
caml_really_putblock(chan, blk->data, blk->end - blk->data);
nextblk = blk->next;
- free(blk);
+ caml_stat_free(blk);
blk = nextblk;
}
}
@@ -712,7 +712,7 @@ CAMLprim value caml_output_value_to_string(value v, value flags)
memcpy(&Byte(res, ofs), blk->data, n);
ofs += n;
nextblk = blk->next;
- free(blk);
+ caml_stat_free(blk);
blk = nextblk;
}
return res;
@@ -762,7 +762,7 @@ CAMLexport void caml_output_value_to_malloc(value v, value flags,
init_extern_output();
data_len = extern_value(v, flags, header, &header_len);
- res = malloc(header_len + data_len);
+ res = caml_stat_alloc_noexc(header_len + data_len);
if (res == NULL) extern_out_of_memory();
*buf = res;
*len = header_len + data_len;
diff --git a/byterun/finalise.c b/byterun/finalise.c
index fd31921eca..ebc772effe 100644
--- a/byterun/finalise.c
+++ b/byterun/finalise.c
@@ -72,8 +72,8 @@ static struct to_do *to_do_tl = NULL;
/* [size] is a number of elements for the [to_do.item] array */
static void alloc_to_do (int size)
{
- struct to_do *result = malloc (sizeof (struct to_do)
- + size * sizeof (struct final));
+ struct to_do *result = caml_stat_alloc_noexc (sizeof (struct to_do) +
+ size * sizeof (struct final));
if (result == NULL) caml_fatal_error ("out of memory");
result->next = NULL;
result->size = size;
@@ -182,7 +182,7 @@ void caml_final_do_calls (void)
while (1){
while (to_do_hd != NULL && to_do_hd->size == 0){
struct to_do *next_hd = to_do_hd->next;
- free (to_do_hd);
+ caml_stat_free (to_do_hd);
to_do_hd = next_hd;
if (to_do_hd == NULL) to_do_tl = NULL;
}
diff --git a/byterun/intern.c b/byterun/intern.c
index 8570f0a651..9144f874c5 100644
--- a/byterun/intern.c
+++ b/byterun/intern.c
@@ -256,7 +256,7 @@ static struct intern_item * intern_stack_limit = intern_stack_init
static void intern_free_stack(void)
{
if (intern_stack != intern_stack_init) {
- free(intern_stack);
+ caml_stat_free(intern_stack);
/* Reinitialize the globals for next time around */
intern_stack = intern_stack_init;
intern_stack_limit = intern_stack + INTERN_STACK_INIT_SIZE;
@@ -279,13 +279,13 @@ static struct intern_item * intern_resize_stack(struct intern_item * sp)
if (newsize >= INTERN_STACK_MAX_SIZE) intern_stack_overflow();
if (intern_stack == intern_stack_init) {
- newstack = malloc(sizeof(struct intern_item) * newsize);
+ newstack = caml_stat_alloc_noexc(sizeof(struct intern_item) * newsize);
if (newstack == NULL) intern_stack_overflow();
memcpy(newstack, intern_stack_init,
sizeof(struct intern_item) * INTERN_STACK_INIT_SIZE);
} else {
- newstack =
- realloc(intern_stack, sizeof(struct intern_item) * newsize);
+ newstack = caml_stat_resize_noexc(intern_stack,
+ sizeof(struct intern_item) * newsize);
if (newstack == NULL) intern_stack_overflow();
}
intern_stack = newstack;
@@ -606,7 +606,7 @@ static void intern_alloc(mlsize_t whsize, mlsize_t num_objects,
}
obj_counter = 0;
if (num_objects > 0) {
- intern_obj_table = (value *) malloc(num_objects * sizeof(value));
+ intern_obj_table = (value *) caml_stat_alloc_noexc(num_objects * sizeof(value));
if (intern_obj_table == NULL) {
intern_cleanup();
caml_raise_out_of_memory();
diff --git a/byterun/major_gc.c b/byterun/major_gc.c
index 5af5519a35..1bad5ea15f 100644
--- a/byterun/major_gc.c
+++ b/byterun/major_gc.c
@@ -128,8 +128,9 @@ static void realloc_gray_vals (void)
caml_gc_message (0x08, "Growing gray_vals to %"
ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
(intnat) gray_vals_size * sizeof (value) / 512);
- new = (value *) realloc ((char *) gray_vals,
- 2 * gray_vals_size * sizeof (value));
+ new = (value *) caml_stat_resize_noexc ((char *) gray_vals,
+ 2 * gray_vals_size *
+ sizeof (value));
if (new == NULL){
caml_gc_message (0x08, "No room for growing gray_vals\n", 0);
gray_vals_cur = gray_vals;
@@ -878,7 +879,7 @@ void caml_init_major_heap (asize_t heap_size)
caml_stat_heap_wsz, 1, Caml_white);
caml_gc_phase = Phase_idle;
gray_vals_size = 2048;
- gray_vals = (value *) malloc (gray_vals_size * sizeof (value));
+ gray_vals = (value *) caml_stat_alloc_noexc (gray_vals_size * sizeof (value));
if (gray_vals == NULL)
caml_fatal_error ("Fatal error: not enough memory for the gray cache.\n");
gray_vals_cur = gray_vals;
diff --git a/byterun/memory.c b/byterun/memory.c
index 1c6b2318eb..8dfdc3927a 100644
--- a/byterun/memory.c
+++ b/byterun/memory.c
@@ -112,7 +112,8 @@ int caml_page_table_initialize(mlsize_t bytesize)
}
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));
+ caml_page_table.entries =
+ caml_stat_calloc_noexc(caml_page_table.size, sizeof(uintnat));
if (caml_page_table.entries == NULL)
return -1;
else
@@ -128,7 +129,7 @@ static int caml_page_table_resize(void)
caml_gc_message (0x08, "Growing page table to %lu entries\n",
caml_page_table.size);
- new_entries = calloc(2 * old.size, sizeof(uintnat));
+ new_entries = caml_stat_calloc_noexc(2 * old.size, sizeof(uintnat));
if (new_entries == NULL) {
caml_gc_message (0x08, "No room for growing page table\n", 0);
return -1;
@@ -149,7 +150,7 @@ static int caml_page_table_resize(void)
caml_page_table.entries[h] = e;
}
- free(old.entries);
+ caml_stat_free(old.entries);
return 0;
}
@@ -202,7 +203,7 @@ static int caml_page_table_modify(uintnat page, int toclear, int toset)
uintnat j = Pagetable_index2(page);
if (caml_page_table[i] == caml_page_table_empty) {
- unsigned char * new_tbl = calloc(Pagetable2_size, 1);
+ unsigned char * new_tbl = caml_stat_calloc_noexc(Pagetable2_size, 1);
if (new_tbl == 0) return -1;
caml_page_table[i] = new_tbl;
}
@@ -307,7 +308,7 @@ void caml_free_for_heap (char *mem)
CAMLassert (0);
#endif
}else{
- free (Chunk_block (mem));
+ caml_stat_free (Chunk_block (mem));
}
}
@@ -690,7 +691,6 @@ CAMLexport CAMLweakdef void caml_modify (value *fp, value val)
CAMLexport void * caml_stat_alloc (asize_t sz)
{
void * result = malloc (sz);
-
/* malloc() may return NULL if size is 0 */
if (result == NULL && sz != 0) caml_raise_out_of_memory ();
#ifdef DEBUG
@@ -699,6 +699,15 @@ CAMLexport void * caml_stat_alloc (asize_t sz)
return result;
}
+CAMLexport void * caml_stat_alloc_noexc (asize_t sz)
+{
+ void * result = malloc (sz);
+#ifdef DEBUG
+ memset (result, Debug_uninit_stat, sz);
+#endif
+ return result;
+}
+
CAMLexport void caml_stat_free (void * blk)
{
free (blk);
@@ -708,7 +717,16 @@ CAMLexport void caml_stat_free (void * blk)
CAMLexport void * caml_stat_resize (void * blk, asize_t sz)
{
void * result = realloc (blk, sz);
-
if (result == NULL) caml_raise_out_of_memory ();
return result;
}
+
+CAMLexport void * caml_stat_resize_noexc (void * blk, asize_t sz)
+{
+ return realloc (blk, sz);
+}
+
+CAMLexport void * caml_stat_calloc_noexc (asize_t num, asize_t sz)
+{
+ return calloc (num, sz);
+}
diff --git a/byterun/minor_gc.c b/byterun/minor_gc.c
index 5e219e61d5..e4dcbaeefa 100644
--- a/byterun/minor_gc.c
+++ b/byterun/minor_gc.c
@@ -85,7 +85,8 @@ static void alloc_generic_table (struct generic_table *tbl, asize_t sz,
tbl->size = sz;
tbl->reserve = rsv;
- new_table = (void *) malloc((tbl->size + tbl->reserve) * element_size);
+ new_table = (void *) caml_stat_alloc_noexc((tbl->size + tbl->reserve) *
+ element_size);
if (new_table == NULL) caml_fatal_error ("Fatal error: not enough memory\n");
if (tbl->base != NULL) caml_stat_free (tbl->base);
tbl->base = new_table;
@@ -151,7 +152,7 @@ void caml_set_minor_heap_size (asize_t bsz)
if (caml_young_start != NULL){
caml_page_table_remove(In_young, caml_young_start, caml_young_end);
- free (caml_young_base);
+ caml_stat_free (caml_young_base);
}
caml_young_base = new_heap_base;
caml_young_start = (value *) new_heap;
@@ -508,7 +509,7 @@ static void realloc_generic_table
tbl->size *= 2;
sz = (tbl->size + tbl->reserve) * element_size;
caml_gc_message (0x08, msg_growing, (intnat) sz/1024);
- tbl->base = (void *) realloc ((char *) tbl->base, sz);
+ tbl->base = caml_stat_resize_noexc (tbl->base, sz);
if (tbl->base == NULL){
caml_fatal_error (msg_error);
}
diff --git a/byterun/misc.c b/byterun/misc.c
index 9aa36f19e2..0c448eccc3 100644
--- a/byterun/misc.c
+++ b/byterun/misc.c
@@ -86,7 +86,7 @@ char *caml_aligned_malloc (asize_t size, int modulo, void **block)
char *raw_mem;
uintnat aligned_mem;
CAMLassert (modulo < Page_size);
- raw_mem = (char *) malloc (size + Page_size);
+ raw_mem = (char *) caml_stat_alloc_noexc (size + Page_size);
if (raw_mem == NULL) return NULL;
*block = raw_mem;
raw_mem += modulo; /* Address to be aligned */
@@ -195,7 +195,7 @@ CAMLexport char * caml_strconcat(int n, ...)
return res;
}
-/* Integer arithmetic with overflow detection */
+/* Integer arithmetic with overflow detection */
#if ! (__GNUC__ >= 5 || Caml_has_builtin(__builtin_mul_overflow))
CAMLexport int caml_umul_overflow(uintnat a, uintnat b, uintnat * res)
diff --git a/byterun/printexc.c b/byterun/printexc.c
index cb32e61b7f..3c5200ad47 100644
--- a/byterun/printexc.c
+++ b/byterun/printexc.c
@@ -27,6 +27,7 @@
#include "caml/misc.h"
#include "caml/mlvalues.h"
#include "caml/printexc.h"
+#include "caml/memory.h"
struct stringbuf {
char * ptr;
@@ -92,7 +93,7 @@ CAMLexport char * caml_format_exception(value exn)
*buf.ptr = 0; /* Terminate string */
i = buf.ptr - buf.data + 1;
- res = malloc(i);
+ res = caml_stat_alloc_noexc(i);
if (res == NULL) return NULL;
memmove(res, buf.data, i);
return res;
@@ -125,7 +126,7 @@ static void default_fatal_uncaught_exception(value exn)
caml_backtrace_pos = saved_backtrace_pos;
/* Display the uncaught exception */
fprintf(stderr, "Fatal error: exception %s\n", msg);
- free(msg);
+ caml_stat_free(msg);
/* Display the backtrace if available */
if (caml_backtrace_active && !DEBUGGER_IN_USE)
caml_print_exception_backtrace();
diff --git a/byterun/str.c b/byterun/str.c
index 1d606f2516..9ed802e8a4 100644
--- a/byterun/str.c
+++ b/byterun/str.c
@@ -401,10 +401,10 @@ CAMLexport value caml_alloc_sprintf(const char * format, ...)
res = caml_alloc_string(n);
memcpy(String_val(res), buf, n);
} else {
- /* PR#7568: if the format is in the Caml heap, the following
+ /* PR#7568: if the format is in the Caml heap, the following
caml_alloc_string could move or free the format. To prevent
this, take a copy of the format outside the Caml heap. */
- char * saved_format = caml_strdup(format);
+ char * saved_format = caml_stat_strdup(format);
/* Allocate a Caml string with length "n" as computed by vsnprintf. */
res = caml_alloc_string(n);
/* Re-do the formatting, outputting directly in the Caml string.
@@ -434,10 +434,10 @@ CAMLexport value caml_alloc_sprintf(const char * format, ...)
res = caml_alloc_string(n);
memcpy(String_val(res), buf, n);
} else {
- /* PR#7568: if the format is in the Caml heap, the following
+ /* PR#7568: if the format is in the Caml heap, the following
caml_alloc_string could move or free the format. To prevent
this, take a copy of the format outside the Caml heap. */
- char * saved_format = caml_strdup(format);
+ char * saved_format = caml_stat_strdup(format);
/* Determine actual length of output, excluding final '\0' */
va_start(args, format);
n = _vscprintf(format, args);
diff --git a/byterun/win32.c b/byterun/win32.c
index a4a36a5859..823e8e024a 100644
--- a/byterun/win32.c
+++ b/byterun/win32.c
@@ -332,7 +332,7 @@ static void store_argument(char * arg)
{
if (argc + 1 >= argvsize) {
argvsize *= 2;
- argv = (char **) realloc(argv, argvsize * sizeof(char *));
+ argv = (char **) caml_stat_resize_noexc(argv, argvsize * sizeof(char *));
if (argv == NULL) out_of_memory();
}
argv[argc++] = arg;
@@ -387,7 +387,7 @@ CAMLexport void caml_expand_command_line(int * argcp, char *** argvp)
int i;
argc = 0;
argvsize = 16;
- argv = (char **) malloc(argvsize * sizeof(char *));
+ argv = (char **) caml_stat_alloc_noexc(argvsize * sizeof(char *));
if (argv == NULL) out_of_memory();
for (i = 0; i < *argcp; i++) expand_argument((*argvp)[i]);
argv[argc] = NULL;
diff --git a/otherlibs/systhreads/st_posix.h b/otherlibs/systhreads/st_posix.h
index a751ff32e0..2ca937cdff 100644
--- a/otherlibs/systhreads/st_posix.h
+++ b/otherlibs/systhreads/st_posix.h
@@ -167,10 +167,10 @@ typedef pthread_mutex_t * st_mutex;
static int st_mutex_create(st_mutex * res)
{
int rc;
- st_mutex m = malloc(sizeof(pthread_mutex_t));
+ st_mutex m = caml_stat_alloc_noexc(sizeof(pthread_mutex_t));
if (m == NULL) return ENOMEM;
rc = pthread_mutex_init(m, NULL);
- if (rc != 0) { free(m); return rc; }
+ if (rc != 0) { caml_stat_free(m); return rc; }
*res = m;
return 0;
}
@@ -179,7 +179,7 @@ static int st_mutex_destroy(st_mutex m)
{
int rc;
rc = pthread_mutex_destroy(m);
- free(m);
+ caml_stat_free(m);
return rc;
}
@@ -208,10 +208,10 @@ typedef pthread_cond_t * st_condvar;
static int st_condvar_create(st_condvar * res)
{
int rc;
- st_condvar c = malloc(sizeof(pthread_cond_t));
+ st_condvar c = caml_stat_alloc_noexc(sizeof(pthread_cond_t));
if (c == NULL) return ENOMEM;
rc = pthread_cond_init(c, NULL);
- if (rc != 0) { free(c); return rc; }
+ if (rc != 0) { caml_stat_free(c); return rc; }
*res = c;
return 0;
}
@@ -220,7 +220,7 @@ static int st_condvar_destroy(st_condvar c)
{
int rc;
rc = pthread_cond_destroy(c);
- free(c);
+ caml_stat_free(c);
return rc;
}
@@ -250,12 +250,12 @@ typedef struct st_event_struct {
static int st_event_create(st_event * res)
{
int rc;
- st_event e = malloc(sizeof(struct st_event_struct));
+ st_event e = caml_stat_alloc_noexc(sizeof(struct st_event_struct));
if (e == NULL) return ENOMEM;
rc = pthread_mutex_init(&e->lock, NULL);
- if (rc != 0) { free(e); return rc; }
+ if (rc != 0) { caml_stat_free(e); return rc; }
rc = pthread_cond_init(&e->triggered, NULL);
- if (rc != 0) { pthread_mutex_destroy(&e->lock); free(e); return rc; }
+ if (rc != 0) { pthread_mutex_destroy(&e->lock); caml_stat_free(e); return rc; }
e->status = 0;
*res = e;
return 0;
@@ -266,7 +266,7 @@ static int st_event_destroy(st_event e)
int rc1, rc2;
rc1 = pthread_mutex_destroy(&e->lock);
rc2 = pthread_cond_destroy(&e->triggered);
- free(e);
+ caml_stat_free(e);
return rc1 != 0 ? rc1 : rc2;
}
diff --git a/otherlibs/systhreads/st_stubs.c b/otherlibs/systhreads/st_stubs.c
index cd7daa7cfd..df3c7e7cf5 100644
--- a/otherlibs/systhreads/st_stubs.c
+++ b/otherlibs/systhreads/st_stubs.c
@@ -64,7 +64,7 @@ struct caml_thread_descr {
#define Start_closure(v) (((struct caml_thread_descr *)(v))->start_closure)
#define Terminated(v) (((struct caml_thread_descr *)(v))->terminated)
-/* The infos on threads (allocated via malloc()) */
+/* The infos on threads (allocated via caml_stat_alloc()) */
struct caml_thread_struct {
value descr; /* The heap-allocated descriptor (root) */
@@ -337,7 +337,7 @@ static uintnat caml_thread_stack_usage(void)
static caml_thread_t caml_thread_new_info(void)
{
caml_thread_t th;
- th = (caml_thread_t) malloc(sizeof(struct caml_thread_struct));
+ th = (caml_thread_t) caml_stat_alloc_noexc(sizeof(struct caml_thread_struct));
if (th == NULL) return NULL;
th->descr = Val_unit; /* filled later */
#ifdef NATIVE_CODE
@@ -410,7 +410,7 @@ static void caml_thread_remove_info(caml_thread_t th)
#ifndef NATIVE_CODE
caml_stat_free(th->stack_low);
#endif
- if (th->backtrace_buffer != NULL) free(th->backtrace_buffer);
+ if (th->backtrace_buffer != NULL) caml_stat_free(th->backtrace_buffer);
#ifndef WITH_SPACETIME
caml_stat_free(th);
/* CR-soon mshinwell: consider what to do about the Spacetime trace. Could
@@ -690,7 +690,7 @@ CAMLprim value caml_thread_uncaught_exception(value exn) /* ML */
char * msg = caml_format_exception(exn);
fprintf(stderr, "Thread %d killed on uncaught exception %s\n",
Int_val(Ident(curr_thread->descr)), msg);
- free(msg);
+ caml_stat_free(msg);
if (caml_backtrace_active) caml_print_exception_backtrace();
fflush(stderr);
return Val_unit;
diff --git a/otherlibs/systhreads/st_win32.h b/otherlibs/systhreads/st_win32.h
index fa447a9c14..0003a59e65 100644
--- a/otherlibs/systhreads/st_win32.h
+++ b/otherlibs/systhreads/st_win32.h
@@ -158,7 +158,7 @@ typedef CRITICAL_SECTION * st_mutex;
static DWORD st_mutex_create(st_mutex * res)
{
- st_mutex m = malloc(sizeof(CRITICAL_SECTION));
+ st_mutex m = caml_stat_alloc_noexc(sizeof(CRITICAL_SECTION));
if (m == NULL) return ERROR_NOT_ENOUGH_MEMORY;
InitializeCriticalSection(m);
*res = m;
@@ -168,7 +168,7 @@ static DWORD st_mutex_create(st_mutex * res)
static DWORD st_mutex_destroy(st_mutex m)
{
DeleteCriticalSection(m);
- free(m);
+ caml_stat_free(m);
return 0;
}
@@ -222,7 +222,7 @@ typedef struct st_condvar_struct {
static DWORD st_condvar_create(st_condvar * res)
{
- st_condvar c = malloc(sizeof(struct st_condvar_struct));
+ st_condvar c = caml_stat_alloc_noexc(sizeof(struct st_condvar_struct));
if (c == NULL) return ERROR_NOT_ENOUGH_MEMORY;
InitializeCriticalSection(&c->lock);
c->waiters = NULL;
@@ -234,7 +234,7 @@ static DWORD st_condvar_destroy(st_condvar c)
{
TRACE1("st_condvar_destroy", c);
DeleteCriticalSection(&c->lock);
- free(c);
+ caml_stat_free(c);
return 0;
}
diff --git a/otherlibs/threads/scheduler.c b/otherlibs/threads/scheduler.c
index 147df5ef22..2d532582a9 100644
--- a/otherlibs/threads/scheduler.c
+++ b/otherlibs/threads/scheduler.c
@@ -758,7 +758,7 @@ value thread_kill(value thread) /* ML */
th->sp = NULL;
th->trapsp = NULL;
if (th->backtrace_buffer != NULL) {
- free(th->backtrace_buffer);
+ caml_stat_free(th->backtrace_buffer);
th->backtrace_buffer = NULL;
}
return retval;
@@ -771,7 +771,7 @@ value thread_uncaught_exception(value exn) /* ML */
char * msg = caml_format_exception(exn);
fprintf(stderr, "Thread %d killed on uncaught exception %s\n",
Int_val(curr_thread->ident), msg);
- free(msg);
+ caml_stat_free(msg);
if (caml_backtrace_active) caml_print_exception_backtrace();
fflush(stderr);
return Val_unit;
diff --git a/otherlibs/win32graph/draw.c b/otherlibs/win32graph/draw.c
index cb7f06c16f..94de0116a3 100644
--- a/otherlibs/win32graph/draw.c
+++ b/otherlibs/win32graph/draw.c
@@ -386,7 +386,7 @@ CAMLprim value caml_gr_fill_poly(value vect)
if (n_points < 3)
gr_fail("fill_poly: not enough points",0);
- poly = (POINT *)malloc(n_points*sizeof(POINT));
+ poly = (POINT *)caml_stat_alloc(n_points*sizeof(POINT));
p = poly;
for( i = 0; i < n_points; i++ ){
@@ -402,7 +402,7 @@ CAMLprim value caml_gr_fill_poly(value vect)
SelectObject(grwindow.gcBitmap,grwindow.CurrentBrush);
Polygon(grwindow.gc,poly,n_points);
}
- free(poly);
+ caml_stat_free(poly);
return Val_unit;
}