1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/***********************************************************************/
/* */
/* Objective Caml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* Automatique. Distributed only by permission. */
/* */
/***********************************************************************/
/* $Id$ */
#include <stdio.h>
#include "config.h"
#include "misc.h"
#ifdef HAS_UI
#include "ui.h"
#endif
#ifdef DEBUG
void failed_assert (char * expr, char * file, int line)
{
fprintf (stderr, "Assertion failed: %s; file %s; line %d\n",
expr, file, line);
exit (100);
}
static unsigned long seed = 0x12345;
unsigned long not_random (void)
{
seed = seed * 65537 + 12345;
return seed;
}
#endif
int verb_gc;
void gc_message (char *msg, long unsigned int arg)
{
if (verb_gc){
#ifdef HAS_UI
ui_print_stderr(msg, (void *) arg);
#else
fprintf (stderr, msg, arg);
fflush (stderr);
#endif
}
}
void fatal_error (char *msg)
{
#ifdef HAS_UI
ui_print_stderr("%s", msg);
ui_exit (2);
#else
fprintf (stderr, "%s", msg);
exit(2);
#endif
}
void fatal_error_arg (char *fmt, char *arg)
{
#ifdef HAS_UI
ui_print_stderr(fmt, arg);
ui_exit (2);
#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.
*/
void memmov (char * dst, char * src, unsigned long length)
{
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--){
*dst++ = *src++;
}
}else{
/* Copy the first few bytes. */
i = (unsigned long) dst % sizeof (long);
if (i != 0){
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;
}
} 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--){
*(long *) dst = *(long *) src;
dst += sizeof (long); src += sizeof (long);
}
/* Then copy the last few bytes. */
for (i = length % sizeof (long); i > 0; i--){
*dst++ = *src++;
}
}
}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).
Copy byte by byte. */
for (; length > 0; length--){
*--dst = *--src;
}
}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--){
*--dst = *--src; --length;
}
/* Then copy as many entire words as possible. */
for (i = length / sizeof (long); i > 0; i--){
dst -= sizeof (long); src -= sizeof (long);
*(long *) dst = *(long *) src;
}
/* Then copy the last few bytes. */
for (i = length % sizeof (long); i > 0; i--){
*--dst = *--src;
}
}
}
}
#endif /* USING_MEMMOV */
char *aligned_malloc (asize_t size, int modulo, void **block)
/* output */
{
char *raw_mem;
unsigned long aligned_mem;
Assert (modulo < Page_size);
raw_mem = (char *) malloc (size + Page_size);
if (raw_mem == NULL) return NULL;
*block = raw_mem;
raw_mem += modulo; /* Address to be aligned */
aligned_mem = (((unsigned long) raw_mem / Page_size + 1) * Page_size);
return (char *) (aligned_mem - modulo);
}
|