Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 42... Line 72...
42
 
72
 
43
    All the program's memory allocation is through the routines defined in
73
    All the program's memory allocation is through the routines defined in
44
    this file.  This routine allocates sz bytes of memory.
74
    this file.  This routine allocates sz bytes of memory.
45
*/
75
*/
46
 
76
 
47
gen_ptr xmalloc
77
gen_ptr
48
    PROTO_N ( ( sz ) )
-
 
49
    PROTO_T ( gen_size sz )
78
xmalloc(gen_size sz)
50
{
79
{
51
    gen_ptr p ;
80
	gen_ptr p;
52
    if ( sz == 0 ) sz = 1 ;
81
	if (sz == 0) {
-
 
82
		sz = 1;
-
 
83
	}
53
    p = malloc ( ( size_t ) sz ) ;
84
	p = malloc((size_t)sz);
54
    if ( p == NULL ) {
85
	if (p == NULL) {
55
	max_errors = ULONG_MAX ;
86
		max_errors = ULONG_MAX;
56
	error ( ERROR_INTERNAL, "Memory allocation error" ) ;
87
		error(ERROR_INTERNAL, "Memory allocation error");
57
	term_error ( 1 ) ;
88
		term_error(1);
58
    }
89
	}
59
    return ( p ) ;
90
	return (p);
60
}
91
}
61
 
92
 
62
 
93
 
63
/*
94
/*
64
    CONTROLLED VERSION OF REALLOC
95
    CONTROLLED VERSION OF REALLOC
65
 
96
 
66
    This routine reallocates the block of memory p to contain sz bytes.
97
    This routine reallocates the block of memory p to contain sz bytes.
67
    p can be the result of a previous memory allocation routine, or NULL.
98
    p can be the result of a previous memory allocation routine, or NULL.
68
*/
99
*/
69
 
100
 
70
gen_ptr xrealloc
101
gen_ptr
71
    PROTO_N ( ( p, sz ) )
-
 
72
    PROTO_T ( gen_ptr p X gen_size sz )
102
xrealloc(gen_ptr p, gen_size sz)
73
{
103
{
74
    gen_ptr q ;
104
	gen_ptr q;
75
    if ( sz == 0 ) sz = 1 ;
105
	if (sz == 0) {
-
 
106
		sz = 1;
-
 
107
	}
76
    if ( p ) {
108
	if (p) {
77
	q = realloc ( p, ( size_t ) sz ) ;
109
		q = realloc(p,(size_t)sz);
78
    } else {
110
	} else {
79
	q = malloc ( ( size_t ) sz ) ;
111
		q = malloc((size_t)sz);
80
    }
112
	}
81
    if ( q == NULL ) {
113
	if (q == NULL) {
82
	max_errors = ULONG_MAX ;
114
		max_errors = ULONG_MAX;
83
	error ( ERROR_INTERNAL, "Memory allocation error" ) ;
115
		error(ERROR_INTERNAL, "Memory allocation error");
84
	term_error ( 1 ) ;
116
		term_error(1);
85
    }
117
	}
86
    return ( q ) ;
118
	return (q);
87
}
119
}
88
 
120
 
89
 
121
 
90
/*
122
/*
91
    CONTROLLED VERSION OF FREE
123
    CONTROLLED VERSION OF FREE
92
 
124
 
93
    This routine frees the block of memory p.  p can be the result of a
125
    This routine frees the block of memory p.  p can be the result of a
94
    previous memory allocation routine, or NULL.
126
    previous memory allocation routine, or NULL.
95
*/
127
*/
96
 
128
 
97
void xfree
129
void
98
    PROTO_N ( ( p ) )
-
 
99
    PROTO_T ( gen_ptr p )
130
xfree(gen_ptr p)
100
{
131
{
-
 
132
	if (p) {
101
    if ( p ) free ( p ) ;
133
		free(p);
-
 
134
	}
102
    return ;
135
	return;
103
}
136
}
104
 
137
 
105
 
138
 
106
/*
139
/*
107
    STRING ALLOCATION BUFFER
140
    STRING ALLOCATION BUFFER
108
 
141
 
109
    This buffer is used in the allocation of small strings.
142
    This buffer is used in the allocation of small strings.
110
*/
143
*/
111
 
144
 
112
static gen_size chars_left = 0 ;
145
static gen_size chars_left = 0;
113
static string chars_free = NULL ;
146
static string chars_free = NULL;
114
 
147
 
115
 
148
 
116
/*
149
/*
117
    ALLOCATE SPACE FOR A STRING
150
    ALLOCATE SPACE FOR A STRING
118
 
151
 
119
    This routine allocates space for n characters.  The memory allocation
152
    This routine allocates space for n characters.  The memory allocation
120
    is buffered except for very long strings.
153
    is buffered except for very long strings.
121
*/
154
*/
122
 
155
 
123
string xustr
156
string
124
    PROTO_N ( ( n ) )
-
 
125
    PROTO_T ( gen_size n )
157
xustr(gen_size n)
126
{
158
{
127
    string r ;
159
	string r;
128
    if ( n < 1000 ) {
160
	if (n < 1000) {
129
	/* Small strings */
161
		/* Small strings */
130
	if ( n >= chars_left ) {
162
		if (n >= chars_left) {
131
	    chars_left = 5000 ;
163
			chars_left = 5000;
132
	    chars_free = xmalloc_nof ( character, chars_left ) ;
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);
133
	}
172
	}
134
	r = chars_free ;
-
 
135
	chars_free += n ;
-
 
136
	chars_left -= n ;
-
 
137
    } else {
-
 
138
	/* Large strings */
-
 
139
	r = xmalloc_nof ( character, n ) ;
-
 
140
    }
-
 
141
    return ( r ) ;
173
	return (r);
142
}
174
}
143
 
175
 
144
 
176
 
145
/*
177
/*
146
    FREE SPACE ALLOCATED FOR A STRING
178
    FREE SPACE ALLOCATED FOR A STRING
147
 
179
 
148
    This routine frees the space allocated by a previous call to xustr.
180
    This routine frees the space allocated by a previous call to xustr.
149
    For small strings the memory is only freed for the last call to xustr.
181
    For small strings the memory is only freed for the last call to xustr.
150
*/
182
*/
151
 
-
 
152
void xufree
-
 
153
    PROTO_N ( ( s, n ) )
-
 
154
    PROTO_T ( string s X gen_size n )
-
 
155
{
-
 
156
    if ( s ) {
-
 
157
	if ( n < 1000 ) {
-
 
158
	    /* Small strings */
-
 
159
	    if ( s + n == chars_free ) {
-
 
160
		chars_free = s ;
-
 
161
		chars_left += n ;
-
 
162
	    }
-
 
163
	} else {
-
 
164
	    /* Large strings */
-
 
165
	    xfree_nof ( s ) ;
-
 
166
	}
-
 
167
    }
-
 
168
    return ;
-
 
169
}
-
 
170
 
-
 
171
 
-
 
172
/*
-
 
173
    COPY A STRING OF A GIVEN LENGTH
-
 
174
 
183
 
175
    This routine allocates space for a persistent copy of the string s
-
 
176
    of length n.  There is only one copy of each small string, otherwise
-
 
177
    xustr is used to allocate the space.
-
 
178
*/
184
void
179
 
-
 
180
string xustrncpy
-
 
181
    PROTO_N ( ( s, n ) )
-
 
182
    PROTO_T ( string s X gen_size n )
185
xufree(string s, gen_size n)
183
{
186
{
184
    string r ;
187
	if (s) {
185
    if ( n < 2 ) {
188
		if (n < 1000) {
186
	/* Small strings */
189
			/* Small strings */
187
	static character buff [ NO_CHAR ] [2] ;
190
			if (s + n == chars_free) {
188
	int c = ( int ) s [0] ;
191
				chars_free = s;
189
	if ( c < NO_CHAR ) {
192
				chars_left += n;
-
 
193
			}
190
	    r = buff [c] ;
194
		} else {
191
	    r [0] = ( character ) c ;
195
			/* Large strings */
192
	    r [1] = 0 ;
196
			xfree_nof(s);
193
	    return ( r ) ;
197
		}
194
	}
198
	}
195
    }
-
 
196
    /* Large strings */
-
 
197
    r = xustr ( n + 1 ) ;
-
 
198
    ustrcpy_v ( r, s ) ;
-
 
199
    return ( r ) ;
199
	return;
200
}
200
}
201
 
201
 
202
 
202
 
203
/*
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
 
-
 
211
string
-
 
212
xustrncpy(string s, gen_size n)
-
 
213
{
-
 
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
		}
-
 
225
	}
-
 
226
	/* Large strings */
-
 
227
	r = xustr(n + 1);
-
 
228
	ustrcpy_v(r, s);
-
 
229
	return (r);
-
 
230
}
-
 
231
 
-
 
232
 
-
 
233
/*
204
    COPY A STRING
234
    COPY A STRING
205
 
235
 
206
    This routine allocates space for a persistent copy of the string s.
236
    This routine allocates space for a persistent copy of the string s.
207
*/
237
*/
208
 
238
 
209
string xustrcpy
239
string
210
    PROTO_N ( ( s ) )
-
 
211
    PROTO_T ( string s )
240
xustrcpy(string s)
212
{
241
{
213
    gen_size n ;
242
	gen_size n;
214
    if ( s == NULL ) return ( NULL ) ;
243
	if (s == NULL) {
-
 
244
		return (NULL);
-
 
245
	}
215
    n = ( gen_size ) ustrlen ( s ) ;
246
	n = (gen_size)ustrlen(s);
216
    return ( xustrncpy ( s, n ) ) ;
247
	return (xustrncpy(s, n));
217
}
248
}
218
 
249
 
219
 
250
 
220
/*
251
/*
221
    CONCATENATE TWO STRINGS
252
    CONCATENATE TWO STRINGS
222
 
253
 
223
    This routine allocates space for a persistent copy of the string s
254
    This routine allocates space for a persistent copy of the string s
224
    followed by the string t.  The memory is allocated using xustr.
255
    followed by the string t.  The memory is allocated using xustr.
225
*/
256
*/
226
 
257
 
227
string xustrcat
258
string
228
    PROTO_N ( ( s, t ) )
-
 
229
    PROTO_T ( string s X string t )
259
xustrcat(string s, string t)
230
{
260
{
231
    string r ;
261
	string r;
232
    gen_size n, m ;
262
	gen_size n, m;
-
 
263
	if (s == NULL) {
233
    if ( s == NULL ) return ( xustrcpy ( t ) ) ;
264
		return (xustrcpy(t));
-
 
265
	}
-
 
266
	if (t == NULL) {
234
    if ( t == NULL ) return ( xustrcpy ( s ) ) ;
267
		return (xustrcpy(s));
-
 
268
	}
235
    n = ( gen_size ) ustrlen ( s ) ;
269
	n = (gen_size)ustrlen(s);
236
    m = n + ( gen_size ) ustrlen ( t ) + 1 ;
270
	m = n + (gen_size)ustrlen(t) + 1;
237
    r = xustr ( m ) ;
271
	r = xustr(m);
238
    ustrcpy_v ( r, s ) ;
272
	ustrcpy_v(r, s);
239
    ustrcpy_v ( r + n, t ) ;
273
	ustrcpy_v(r + n, t);
240
    return ( r ) ;
274
	return (r);
241
}
275
}
242
 
276
 
243
 
277
 
244
/*
278
/*
245
    COPY A NUMBER OF CHARACTERS
279
    COPY A NUMBER OF CHARACTERS
246
 
280
 
247
    This routine copies n characters from t to s.
281
    This routine copies n characters from t to s.
248
*/
282
*/
249
 
283
 
250
void xumemcpy
284
void
251
    PROTO_N ( ( s, t, n ) )
-
 
252
    PROTO_T ( string s X string t X gen_size n )
285
xumemcpy(string s, string t, gen_size n)
253
{
286
{
-
 
287
	if (n) {
254
    if ( n ) memcpy_v ( ( gen_ptr ) s, ( gen_ptr ) t, ( size_t ) n ) ;
288
		memcpy_v((gen_ptr)s, (gen_ptr)t, (size_t)n);
-
 
289
	}
255
    return ;
290
	return;
256
}
291
}
257
 
292
 
258
 
293
 
259
/*
294
/*
260
    COMPARE TWO SEQUENCES OF CHARACTERS
295
    COMPARE TWO SEQUENCES OF CHARACTERS
261
 
296
 
262
    This routine compares the n characters given by s and t.
297
    This routine compares the n characters given by s and t.
263
*/
298
*/
264
 
299
 
265
int xumemcmp
300
int
266
    PROTO_N ( ( s, t, n ) )
-
 
267
    PROTO_T ( string s X string t X gen_size n )
301
xumemcmp(string s, string t, gen_size n)
268
{
302
{
269
    if ( s == t || n == 0 ) return ( 0 ) ;
303
	if (s == t || n == 0) {
-
 
304
		return (0);
-
 
305
	}
270
    return ( memcmp ( ( gen_ptr ) s, ( gen_ptr ) t, ( size_t ) n ) ) ;
306
	return (memcmp((gen_ptr)s,(gen_ptr)t,(size_t)n));
271
}
307
}