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
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#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 HAS_UI
|
|
|
|
#include "ui.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
2000-08-23 10:10:03 -07:00
|
|
|
void 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2001-02-19 02:01:41 -08:00
|
|
|
unsigned long verb_gc = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-08-07 11:43:39 -07:00
|
|
|
void gc_message (int level, char *msg, unsigned long arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2000-01-07 08:01:41 -08:00
|
|
|
if (level < 0 || (verb_gc & level) != 0){
|
1995-05-04 03:15:53 -07:00
|
|
|
#ifdef HAS_UI
|
1996-11-06 08:51:11 -08:00
|
|
|
ui_print_stderr(msg, (void *) arg);
|
1995-05-04 03:15:53 -07:00
|
|
|
#else
|
|
|
|
fprintf (stderr, msg, arg);
|
|
|
|
fflush (stderr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void fatal_error (char *msg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
#ifdef HAS_UI
|
1996-11-02 10:00:46 -08:00
|
|
|
ui_print_stderr("%s", msg);
|
|
|
|
ui_exit (2);
|
1995-05-04 03:15:53 -07:00
|
|
|
#else
|
|
|
|
fprintf (stderr, "%s", msg);
|
|
|
|
exit(2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void fatal_error_arg (char *fmt, char *arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
#ifdef HAS_UI
|
1996-11-02 10:00:46 -08:00
|
|
|
ui_print_stderr(fmt, arg);
|
|
|
|
ui_exit (2);
|
1995-05-04 03:15:53 -07:00
|
|
|
#else
|
|
|
|
fprintf (stderr, fmt, arg);
|
|
|
|
exit(2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
void fatal_error_arg2 (char *fmt1, char *arg1, char *fmt2, char *arg2)
|
|
|
|
{
|
|
|
|
#ifdef HAS_UI
|
|
|
|
ui_print_stderr(fmt1, arg1);
|
|
|
|
ui_print_stderr(fmt2, arg2);
|
|
|
|
ui_exit (2);
|
|
|
|
#else
|
|
|
|
fprintf (stderr, fmt1, arg1);
|
|
|
|
fprintf (stderr, fmt2, arg2);
|
|
|
|
exit(2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
char *aligned_malloc (asize_t size, int modulo, void **block)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *raw_mem;
|
|
|
|
unsigned long 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 */
|
1995-05-04 03:15:53 -07:00
|
|
|
aligned_mem = (((unsigned long) raw_mem / Page_size + 1) * Page_size);
|
2000-04-03 01:34:22 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
unsigned long *p;
|
|
|
|
unsigned long *p0 = (void *) *block,
|
|
|
|
*p1 = (void *) (aligned_mem - modulo),
|
|
|
|
*p2 = (void *) (aligned_mem - modulo + size),
|
2000-04-05 11:30:22 -07:00
|
|
|
*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
|
|
|
|
|
|
|
void ext_table_init(struct ext_table * tbl, int init_capa)
|
|
|
|
{
|
|
|
|
tbl->size = 0;
|
|
|
|
tbl->capacity = init_capa;
|
|
|
|
tbl->contents = stat_alloc(sizeof(void *) * init_capa);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ext_table_add(struct ext_table * tbl, void * data)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
if (tbl->size >= tbl->capacity) {
|
|
|
|
tbl->capacity *= 2;
|
|
|
|
tbl->contents =
|
|
|
|
stat_resize(tbl->contents, sizeof(void *) * tbl->capacity);
|
|
|
|
}
|
|
|
|
res = tbl->size;
|
|
|
|
tbl->contents[res] = data;
|
|
|
|
tbl->size++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ext_table_free(struct ext_table * tbl, int free_entries)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (free_entries)
|
|
|
|
for (i = 0; i < tbl->size; i++) stat_free(tbl->contents[i]);
|
|
|
|
stat_free(tbl->contents);
|
|
|
|
}
|