Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2005 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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 "error.h"
63
#include "xalloc.h"
64
 
65
 
66
/*
67
    CONTROLLED VERSION OF MALLOC
68
 
69
    All the program's memory allocation is through the routines defined in
70
    this file.  This routine allocates sz bytes of memory.
71
*/
72
 
6 7u83 73
gen_ptr
74
xmalloc(long sz)
2 7u83 75
{
6 7u83 76
    gen_ptr p = malloc((size_t)sz);
77
    if (p == NULL)error(ERROR_FATAL, "Memory allocation error");
78
    return(p);
2 7u83 79
}
80
 
81
 
82
/*
83
    CONTROLLED VERSION OF CALLOC
84
 
85
    This routine allocates and initializes n objects of size sz bytes.
86
*/
87
 
6 7u83 88
gen_ptr
89
xcalloc(long n, long sz)
2 7u83 90
{
6 7u83 91
    gen_ptr p = calloc((size_t)sz,(size_t)n);
92
    if (p == NULL)error(ERROR_FATAL, "Memory allocation error");
93
    return(p);
2 7u83 94
}
95
 
96
 
97
/*
98
    CONTROLLED VERSION OF REALLOC
99
 
100
    This routine reallocates the block of memory p to contain sz bytes.
101
    p can be the result of a previous memory allocation routine, or NULL.
102
*/
103
 
6 7u83 104
gen_ptr
105
xrealloc(gen_ptr p, long sz)
2 7u83 106
{
6 7u83 107
    gen_ptr q;
108
    if (p) {
109
	q = realloc(p,(size_t)sz);
2 7u83 110
    } else {
6 7u83 111
	q = malloc((size_t)sz);
2 7u83 112
    }
6 7u83 113
    if (q == NULL)error(ERROR_FATAL, "Memory allocation error");
114
    return(q);
2 7u83 115
}
116
 
117
 
118
/*
119
    CONTROLLED VERSION OF FREE
120
 
121
    This routine frees the block of memory p.  p can be the result of a
122
    previous memory allocation routine, or NULL.
123
*/
124
 
6 7u83 125
void
126
xfree(gen_ptr p)
2 7u83 127
{
6 7u83 128
    if (p)free(p);
129
    return;
2 7u83 130
}
131
 
132
 
133
/*
134
    ALLOCATE SPACE FOR A STRING
135
 
136
    This routine allocates space for n characters.  The memory allocation
137
    is buffered except for very long strings.
138
*/
139
 
6 7u83 140
char *
141
xstr(long n)
2 7u83 142
{
6 7u83 143
    char *r;
144
    if (n >= 1000) {
145
	r = xmalloc_nof(char, n);
2 7u83 146
    } else {
6 7u83 147
	static long chars_left = 0;
148
	static char *chars_free = 0;
149
	if (n >= chars_left) {
150
	    chars_left = 5000;
151
	    chars_free = xmalloc_nof(char, chars_left);
2 7u83 152
	}
6 7u83 153
	r = chars_free;
154
	chars_free += n;
155
	chars_left -= n;
2 7u83 156
    }
6 7u83 157
    return(r);
2 7u83 158
}
159
 
160
 
161
/*
162
    COPY A STRING
163
 
164
    This routine allocates space for a persistent copy of the string s.
165
*/
166
 
6 7u83 167
char *
168
xstrcpy(CONST char *s)
2 7u83 169
{
6 7u83 170
    long n;
171
    char *r;
172
    if (s == NULL) return(NULL);
173
    n = (long)strlen(s) + 1;
174
    r = xstr(n);
175
    strcpy_v(r, s);
176
    return(r);
2 7u83 177
}
178
 
179
 
180
/*
181
    CONCATENATE TWO STRINGS
182
 
183
    This routine allocates space for a persistent copy of the string s
184
    followed by the string t.
185
*/
186
 
6 7u83 187
char *
188
xstrcat(CONST char *s, CONST char *t)
2 7u83 189
{
6 7u83 190
    char *r;
191
    long n, m;
192
    if (s == NULL) return(xstrcpy(t));
193
    if (t == NULL) return(xstrcpy(s));
194
    n = (long)strlen(s);
195
    m = n + (long)strlen(t) + 1;
196
    r = xstr(m);
197
    strcpy_v(r, s);
198
    strcpy_v(r + n, t);
199
    return(r);
2 7u83 200
}