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
|
/***********************************************************************/
/* */
/* Objective Caml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../LICENSE. */
/* */
/***********************************************************************/
/* $Id$ */
#include <stdio.h>
#include "config.h"
#include "misc.h"
#include "memory.h"
#ifdef HAS_UI
#include "ui.h"
#endif
#ifdef DEBUG
int caml_failed_assert (char * expr, char * file, int line)
{
fprintf (stderr, "file %s; line %d ### Assertion failed: %s\n",
file, line, expr);
fflush (stderr);
exit (100);
return 1; /* not reached */
}
#endif
unsigned long verb_gc = 0;
void gc_message (int level, char *msg, unsigned long arg)
{
if (level < 0 || (verb_gc & level) != 0){
#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
}
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
}
char *aligned_malloc (asize_t size, int modulo, void **block)
{
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);
#ifdef DEBUG
{
unsigned long *p;
unsigned long *p0 = (void *) *block,
*p1 = (void *) (aligned_mem - modulo),
*p2 = (void *) (aligned_mem - modulo + size),
*p3 = (void *) ((char *) *block + size + Page_size);
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
return (char *) (aligned_mem - modulo);
}
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);
}
|