Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include <limits.h>
63
#include "c_types.h"
64
#include "error.h"
65
#include "char.h"
66
#include "ustring.h"
67
#include "xalloc.h"
68
 
69
 
70
/*
71
    CONTROLLED VERSION OF MALLOC
72
 
73
    All the program's memory allocation is through the routines defined in
74
    this file.  This routine allocates sz bytes of memory.
75
*/
76
 
7 7u83 77
gen_ptr
78
xmalloc(gen_size sz)
2 7u83 79
{
7 7u83 80
	gen_ptr p;
81
	if (sz == 0) {
82
		sz = 1;
83
	}
84
	p = malloc((size_t)sz);
85
	if (p == NULL) {
86
		max_errors = ULONG_MAX;
87
		error(ERROR_INTERNAL, "Memory allocation error");
88
		term_error(1);
89
	}
90
	return (p);
2 7u83 91
}
92
 
93
 
94
/*
95
    CONTROLLED VERSION OF REALLOC
96
 
97
    This routine reallocates the block of memory p to contain sz bytes.
98
    p can be the result of a previous memory allocation routine, or NULL.
99
*/
100
 
7 7u83 101
gen_ptr
102
xrealloc(gen_ptr p, gen_size sz)
2 7u83 103
{
7 7u83 104
	gen_ptr q;
105
	if (sz == 0) {
106
		sz = 1;
107
	}
108
	if (p) {
109
		q = realloc(p,(size_t)sz);
110
	} else {
111
		q = malloc((size_t)sz);
112
	}
113
	if (q == NULL) {
114
		max_errors = ULONG_MAX;
115
		error(ERROR_INTERNAL, "Memory allocation error");
116
		term_error(1);
117
	}
118
	return (q);
2 7u83 119
}
120
 
121
 
122
/*
123
    CONTROLLED VERSION OF FREE
124
 
125
    This routine frees the block of memory p.  p can be the result of a
126
    previous memory allocation routine, or NULL.
127
*/
128
 
7 7u83 129
void
130
xfree(gen_ptr p)
2 7u83 131
{
7 7u83 132
	if (p) {
133
		free(p);
134
	}
135
	return;
2 7u83 136
}
137
 
138
 
139
/*
140
    STRING ALLOCATION BUFFER
141
 
142
    This buffer is used in the allocation of small strings.
143
*/
144
 
7 7u83 145
static gen_size chars_left = 0;
146
static string chars_free = NULL;
2 7u83 147
 
148
 
149
/*
150
    ALLOCATE SPACE FOR A STRING
151
 
152
    This routine allocates space for n characters.  The memory allocation
153
    is buffered except for very long strings.
154
*/
155
 
7 7u83 156
string
157
xustr(gen_size n)
2 7u83 158
{
7 7u83 159
	string r;
160
	if (n < 1000) {
161
		/* Small strings */
162
		if (n >= chars_left) {
163
			chars_left = 5000;
164
			chars_free = xmalloc_nof(character, chars_left);
165
		}
166
		r = chars_free;
167
		chars_free += n;
168
		chars_left -= n;
169
	} else {
170
		/* Large strings */
171
		r = xmalloc_nof(character, n);
2 7u83 172
	}
7 7u83 173
	return (r);
2 7u83 174
}
175
 
176
 
177
/*
178
    FREE SPACE ALLOCATED FOR A STRING
179
 
180
    This routine frees the space allocated by a previous call to xustr.
181
    For small strings the memory is only freed for the last call to xustr.
182
*/
183
 
7 7u83 184
void
185
xufree(string s, gen_size n)
2 7u83 186
{
7 7u83 187
	if (s) {
188
		if (n < 1000) {
189
			/* Small strings */
190
			if (s + n == chars_free) {
191
				chars_free = s;
192
				chars_left += n;
193
			}
194
		} else {
195
			/* Large strings */
196
			xfree_nof(s);
197
		}
2 7u83 198
	}
7 7u83 199
	return;
2 7u83 200
}
201
 
202
 
203
/*
204
    COPY A STRING OF A GIVEN LENGTH
205
 
206
    This routine allocates space for a persistent copy of the string s
207
    of length n.  There is only one copy of each small string, otherwise
208
    xustr is used to allocate the space.
209
*/
210
 
7 7u83 211
string
212
xustrncpy(string s, gen_size n)
2 7u83 213
{
7 7u83 214
	string r;
215
	if (n < 2) {
216
		/* Small strings */
217
		static character buff[NO_CHAR][2];
218
		int c = (int)s[0];
219
		if (c < NO_CHAR) {
220
			r = buff[c];
221
			r[0] = (character)c;
222
			r[1] = 0;
223
			return (r);
224
		}
2 7u83 225
	}
7 7u83 226
	/* Large strings */
227
	r = xustr(n + 1);
228
	ustrcpy_v(r, s);
229
	return (r);
2 7u83 230
}
231
 
232
 
233
/*
234
    COPY A STRING
235
 
236
    This routine allocates space for a persistent copy of the string s.
237
*/
238
 
7 7u83 239
string
240
xustrcpy(string s)
2 7u83 241
{
7 7u83 242
	gen_size n;
243
	if (s == NULL) {
244
		return (NULL);
245
	}
246
	n = (gen_size)ustrlen(s);
247
	return (xustrncpy(s, n));
2 7u83 248
}
249
 
250
 
251
/*
252
    CONCATENATE TWO STRINGS
253
 
254
    This routine allocates space for a persistent copy of the string s
255
    followed by the string t.  The memory is allocated using xustr.
256
*/
257
 
7 7u83 258
string
259
xustrcat(string s, string t)
2 7u83 260
{
7 7u83 261
	string r;
262
	gen_size n, m;
263
	if (s == NULL) {
264
		return (xustrcpy(t));
265
	}
266
	if (t == NULL) {
267
		return (xustrcpy(s));
268
	}
269
	n = (gen_size)ustrlen(s);
270
	m = n + (gen_size)ustrlen(t) + 1;
271
	r = xustr(m);
272
	ustrcpy_v(r, s);
273
	ustrcpy_v(r + n, t);
274
	return (r);
2 7u83 275
}
276
 
277
 
278
/*
279
    COPY A NUMBER OF CHARACTERS
280
 
281
    This routine copies n characters from t to s.
282
*/
283
 
7 7u83 284
void
285
xumemcpy(string s, string t, gen_size n)
2 7u83 286
{
7 7u83 287
	if (n) {
288
		memcpy_v((gen_ptr)s, (gen_ptr)t, (size_t)n);
289
	}
290
	return;
2 7u83 291
}
292
 
293
 
294
/*
295
    COMPARE TWO SEQUENCES OF CHARACTERS
296
 
297
    This routine compares the n characters given by s and t.
298
*/
299
 
7 7u83 300
int
301
xumemcmp(string s, string t, gen_size n)
2 7u83 302
{
7 7u83 303
	if (s == t || n == 0) {
304
		return (0);
305
	}
306
	return (memcmp((gen_ptr)s,(gen_ptr)t,(size_t)n));
2 7u83 307
}