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
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Damien Doligez, 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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <stdio.h>
|
2014-04-15 10:09:13 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "config.h"
|
|
|
|
#include "misc.h"
|
2001-08-28 07:47:48 -07:00
|
|
|
#include "memory.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
int caml_failed_assert (char * expr, char * file, int line)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-08 09:07:05 -08:00
|
|
|
fprintf (stderr, "file %s; line %d ### Assertion failed: %s\n",
|
|
|
|
file, line, expr);
|
|
|
|
fflush (stderr);
|
1995-05-04 03:15:53 -07:00
|
|
|
exit (100);
|
|
|
|
}
|
|
|
|
|
2014-12-16 11:36:35 -08:00
|
|
|
void caml_set_fields (value v, unsigned long start, unsigned long filler)
|
2008-02-29 04:56:15 -08:00
|
|
|
{
|
|
|
|
mlsize_t i;
|
2014-12-16 11:36:35 -08:00
|
|
|
for (i = start; i < Wosize_val (v); i++){
|
|
|
|
Field (v, i) = (value) filler;
|
2008-02-29 04:56:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-08 14:28:48 -08:00
|
|
|
#endif /* DEBUG */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
uintnat caml_verb_gc = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
void caml_gc_message (int level, char *msg, uintnat arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-12-29 14:15:02 -08:00
|
|
|
if (level < 0 || (caml_verb_gc & level) != 0){
|
1995-05-04 03:15:53 -07:00
|
|
|
fprintf (stderr, msg, arg);
|
|
|
|
fflush (stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:07:57 -08:00
|
|
|
CAMLexport void caml_fatal_error (char *msg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s", msg);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:07:57 -08:00
|
|
|
CAMLexport void caml_fatal_error_arg (char *fmt, char *arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
fprintf (stderr, fmt, arg);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:07:57 -08:00
|
|
|
CAMLexport void caml_fatal_error_arg2 (char *fmt1, char *arg1,
|
2008-02-29 04:56:15 -08:00
|
|
|
char *fmt2, char *arg2)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
fprintf (stderr, fmt1, arg1);
|
|
|
|
fprintf (stderr, fmt2, arg2);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* [size] and [modulo] are numbers of bytes */
|
2003-12-29 14:15:02 -08:00
|
|
|
char *caml_aligned_malloc (asize_t size, int modulo, void **block)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *raw_mem;
|
2005-09-22 07:21:50 -07:00
|
|
|
uintnat aligned_mem;
|
2000-08-23 10:10:03 -07:00
|
|
|
Assert (modulo < Page_size);
|
1995-05-04 03:15:53 -07:00
|
|
|
raw_mem = (char *) malloc (size + Page_size);
|
|
|
|
if (raw_mem == NULL) return NULL;
|
1997-05-13 07:45:38 -07:00
|
|
|
*block = raw_mem;
|
1997-05-19 08:42:21 -07:00
|
|
|
raw_mem += modulo; /* Address to be aligned */
|
2005-09-22 07:21:50 -07:00
|
|
|
aligned_mem = (((uintnat) raw_mem / Page_size + 1) * Page_size);
|
2000-04-03 01:34:22 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2005-10-18 07:03:43 -07:00
|
|
|
uintnat *p;
|
|
|
|
uintnat *p0 = (void *) *block,
|
|
|
|
*p1 = (void *) (aligned_mem - modulo),
|
|
|
|
*p2 = (void *) (aligned_mem - modulo + size),
|
|
|
|
*p3 = (void *) ((char *) *block + size + Page_size);
|
2000-04-03 01:34:22 -07:00
|
|
|
|
|
|
|
for (p = p0; p < p1; p++) *p = Debug_filler_align;
|
|
|
|
for (p = p1; p < p2; p++) *p = Debug_uninit_align;
|
|
|
|
for (p = p2; p < p3; p++) *p = Debug_filler_align;
|
|
|
|
}
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
return (char *) (aligned_mem - modulo);
|
|
|
|
}
|
2001-08-28 07:47:48 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
void caml_ext_table_init(struct ext_table * tbl, int init_capa)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
tbl->size = 0;
|
|
|
|
tbl->capacity = init_capa;
|
2003-12-31 06:20:40 -08:00
|
|
|
tbl->contents = caml_stat_alloc(sizeof(void *) * init_capa);
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
int caml_ext_table_add(struct ext_table * tbl, void * data)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
if (tbl->size >= tbl->capacity) {
|
|
|
|
tbl->capacity *= 2;
|
|
|
|
tbl->contents =
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_resize(tbl->contents, sizeof(void *) * tbl->capacity);
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
|
|
|
res = tbl->size;
|
|
|
|
tbl->contents[res] = data;
|
|
|
|
tbl->size++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
void caml_ext_table_free(struct ext_table * tbl, int free_entries)
|
2001-08-28 07:47:48 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (free_entries)
|
2003-12-31 06:20:40 -08:00
|
|
|
for (i = 0; i < tbl->size; i++) caml_stat_free(tbl->contents[i]);
|
|
|
|
caml_stat_free(tbl->contents);
|
2001-08-28 07:47:48 -07:00
|
|
|
}
|
2014-04-15 10:09:13 -07:00
|
|
|
|
|
|
|
CAMLexport char * caml_strdup(const char * s)
|
|
|
|
{
|
|
|
|
size_t slen = strlen(s);
|
|
|
|
char * res = caml_stat_alloc(slen + 1);
|
|
|
|
memcpy(res, s, slen + 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport char * caml_strconcat(int n, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char * res, * p;
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
va_start(args, n);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const char * s = va_arg(args, const char *);
|
|
|
|
len += strlen(s);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
res = caml_stat_alloc(len + 1);
|
|
|
|
va_start(args, n);
|
|
|
|
p = res;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
const char * s = va_arg(args, const char *);
|
|
|
|
size_t l = strlen(s);
|
|
|
|
memcpy(p, s, l);
|
|
|
|
p += l;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
*p = 0;
|
|
|
|
return res;
|
|
|
|
}
|