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 */
|
|
|
|
/* under the terms of the GNU Library General Public 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"
|
|
|
|
#ifdef HAS_UI
|
|
|
|
#include "ui.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long seed = 0x12345;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
unsigned long not_random (void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
seed = seed * 65537 + 12345;
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int verb_gc;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USING_MEMMOV
|
|
|
|
|
|
|
|
/* This should work on 64-bit machines as well as 32-bit machines.
|
|
|
|
It assumes a long is the natural size for memory reads and writes.
|
|
|
|
*/
|
1997-09-02 05:55:01 -07:00
|
|
|
void memmov (char * dst, char * src, unsigned long length)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
if ((unsigned long) dst <= (unsigned long) src){
|
|
|
|
|
|
|
|
/* Copy in ascending order. */
|
|
|
|
if (((unsigned long) src - (unsigned long) dst) % sizeof (long) != 0){
|
|
|
|
|
|
|
|
/* The pointers are not equal modulo sizeof (long).
|
|
|
|
Copy byte by byte. */
|
|
|
|
for (; length != 0; length--){
|
1997-05-19 08:42:21 -07:00
|
|
|
*dst++ = *src++;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
|
|
|
|
/* Copy the first few bytes. */
|
|
|
|
i = (unsigned long) dst % sizeof (long);
|
|
|
|
if (i != 0){
|
1997-05-19 08:42:21 -07:00
|
|
|
i = sizeof (long) - i; /* Number of bytes to copy. */
|
|
|
|
if (i > length) i = length; /* Never copy more than length.*/
|
|
|
|
for (; i != 0; i--){
|
|
|
|
*dst++ = *src++; --length;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
} Assert ((unsigned long) dst % sizeof (long) == 0);
|
|
|
|
Assert ((unsigned long) src % sizeof (long) == 0);
|
|
|
|
|
|
|
|
/* Then copy as many entire words as possible. */
|
|
|
|
for (i = length / sizeof (long); i > 0; i--){
|
1997-05-19 08:42:21 -07:00
|
|
|
*(long *) dst = *(long *) src;
|
|
|
|
dst += sizeof (long); src += sizeof (long);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then copy the last few bytes. */
|
|
|
|
for (i = length % sizeof (long); i > 0; i--){
|
1997-05-19 08:42:21 -07:00
|
|
|
*dst++ = *src++;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{ /* Copy in descending order. */
|
|
|
|
src += length; dst += length;
|
|
|
|
if (((unsigned long) dst - (unsigned long) src) % sizeof (long) != 0){
|
|
|
|
|
|
|
|
/* The pointers are not equal modulo sizeof (long).
|
1997-05-19 08:42:21 -07:00
|
|
|
Copy byte by byte. */
|
1995-05-04 03:15:53 -07:00
|
|
|
for (; length > 0; length--){
|
1997-05-19 08:42:21 -07:00
|
|
|
*--dst = *--src;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
|
|
|
|
/* Copy the first few bytes. */
|
|
|
|
i = (unsigned long) dst % sizeof (long);
|
|
|
|
if (i > length) i = length; /* Never copy more than length. */
|
|
|
|
for (; i > 0; i--){
|
1997-05-19 08:42:21 -07:00
|
|
|
*--dst = *--src; --length;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then copy as many entire words as possible. */
|
|
|
|
for (i = length / sizeof (long); i > 0; i--){
|
1997-05-19 08:42:21 -07:00
|
|
|
dst -= sizeof (long); src -= sizeof (long);
|
|
|
|
*(long *) dst = *(long *) src;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then copy the last few bytes. */
|
|
|
|
for (i = length % sizeof (long); i > 0; i--){
|
1997-05-19 08:42:21 -07:00
|
|
|
*--dst = *--src;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USING_MEMMOV */
|
|
|
|
|
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;
|
|
|
|
Assert (modulo < Page_size);
|
|
|
|
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);
|
|
|
|
return (char *) (aligned_mem - modulo);
|
|
|
|
}
|