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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/* XALLOC -- X's internal memory allocator. Why does it return unsigned
* long * instead of the more common char *? Well, if you read K&R you'll
* see they say that alloc must return a pointer "suitable for conversion"
* to whatever type you really want. In a full-blown generic allocator
* there's no way to solve the alignment problems without potentially
* wasting lots of space. But we have a more limited problem. We know
* we're only ever returning pointers to structures which will have to
* be long word aligned. So we are making a stronger guarantee. It might
* have made sense to make Xalloc return char * to conform with people's
* expectations of malloc, but this makes lint happier.
*/
#ifdef WIN32
#include <X11/Xwinsock.h>
#endif
#include "Xos.h"
#include <stdio.h>
#include "misc.h"
#include "X.h"
#include "input.h"
#include "opaque.h"
#ifdef X_POSIX_C_SOURCE
#define _POSIX_C_SOURCE X_POSIX_C_SOURCE
#include <signal.h>
#undef _POSIX_C_SOURCE
#else
#if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
#include <signal.h>
#else
#define _POSIX_SOURCE
#include <signal.h>
#undef _POSIX_SOURCE
#endif
#endif
#if !defined(SYSV) && !defined(AMOEBA) && !defined(_MINIX) && !defined(WIN32) && !defined(Lynx)
#include <sys/resource.h>
#endif
#include <time.h>
#include <sys/stat.h>
#include <ctype.h> /* for isspace */
#if NeedVarargsPrototypes
#include <stdarg.h>
#endif
#ifdef __sgi__
#undef abs
#endif
#include <stdlib.h>
#ifndef INTERNAL_MALLOC
Bool Must_have_memory = FALSE;
unsigned long *
Xalloc (amount)
unsigned long amount;
{
#if !defined(__STDC__) && !defined(AMOEBA)
char *malloc();
#endif
register pointer ptr;
if ((long)amount <= 0) {
return (unsigned long *)NULL;
}
/* aligned extra on long word boundary */
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
#ifdef MEMBUG
if (!Must_have_memory && Memory_fail &&
((random() % MEM_FAIL_SCALE) < Memory_fail))
return (unsigned long *)NULL;
#endif
if ((ptr = (pointer)malloc(amount))) {
return (unsigned long *)ptr;
}
if (Must_have_memory)
FatalError("Out of memory");
return (unsigned long *)NULL;
}
/*****************
* XNFalloc
* "no failure" realloc, alternate interface to Xalloc w/o Must_have_memory
*****************/
unsigned long *
XNFalloc (amount)
unsigned long amount;
{
#if !defined(__STDC__) && !defined(AMOEBA)
char *malloc();
#endif
register pointer ptr;
if ((long)amount <= 0)
{
return (unsigned long *)NULL;
}
/* aligned extra on long word boundary */
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
ptr = (pointer)malloc(amount);
if (!ptr)
{
FatalError("Out of memory");
}
return ((unsigned long *)ptr);
}
/*****************
* Xcalloc
*****************/
unsigned long *
Xcalloc (amount)
unsigned long amount;
{
unsigned long *ret;
ret = Xalloc (amount);
if (ret)
bzero ((char *) ret, (int) amount);
return ret;
}
/*****************
* Xrealloc
*****************/
unsigned long *
Xrealloc (ptr, amount)
register pointer ptr;
unsigned long amount;
{
#if !defined(__STDC__) && !defined(AMOEBA)
char *malloc();
char *realloc();
#endif
#ifdef MEMBUG
if (!Must_have_memory && Memory_fail &&
((random() % MEM_FAIL_SCALE) < Memory_fail))
return (unsigned long *)NULL;
#endif
if ((long)amount <= 0)
{
if (ptr && !amount)
free(ptr);
return (unsigned long *)NULL;
}
amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
if (ptr)
ptr = (pointer)realloc((char *)ptr, amount);
else
ptr = (pointer)malloc(amount);
if (ptr)
return (unsigned long *)ptr;
if (Must_have_memory)
FatalError("Out of memory");
return (unsigned long *)NULL;
}
/*****************
* XNFrealloc
* "no failure" realloc, alternate interface to Xrealloc w/o Must_have_memory
*****************/
unsigned long *
XNFrealloc (ptr, amount)
register pointer ptr;
unsigned long amount;
{
if (( ptr = (pointer)Xrealloc( ptr, amount ) ) == NULL)
{
FatalError( "Out of memory" );
}
return ((unsigned long *)ptr);
}
/*****************
* Xfree
* calls free
*****************/
void
Xfree(ptr)
register pointer ptr;
{
if (ptr)
free((char *)ptr);
}
void
FatalError(char *f, ...)
{
va_list args;
fprintf(stderr, "\nFatal server error:\n");
va_start(args, f);
vfprintf(stderr, f, args);
va_end(args);
fprintf(stderr, "\n");
exit(1);
}
#endif
|