Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "error.h"
33
#include "xalloc.h"
34
 
35
 
36
/*
37
    CONTROLLED VERSION OF MALLOC
38
 
39
    All the program's memory allocation is through the routines defined in
40
    this file.  This routine allocates sz bytes of memory.
41
*/
42
 
43
gen_ptr xmalloc
44
    PROTO_N ( ( sz ) )
45
    PROTO_T ( long sz )
46
{
47
    gen_ptr p = malloc ( ( size_t ) sz ) ;
48
    if ( p == NULL ) error ( ERROR_FATAL, "Memory allocation error" ) ;
49
    return ( p ) ;
50
}
51
 
52
 
53
/*
54
    CONTROLLED VERSION OF CALLOC
55
 
56
    This routine allocates and initializes n objects of size sz bytes.
57
*/
58
 
59
gen_ptr xcalloc
60
    PROTO_N ( ( n, sz ) )
61
    PROTO_T ( long n X long sz )
62
{
63
    gen_ptr p = calloc ( ( size_t ) sz, ( size_t ) n ) ;
64
    if ( p == NULL ) error ( ERROR_FATAL, "Memory allocation error" ) ;
65
    return ( p ) ;
66
}
67
 
68
 
69
/*
70
    CONTROLLED VERSION OF REALLOC
71
 
72
    This routine reallocates the block of memory p to contain sz bytes.
73
    p can be the result of a previous memory allocation routine, or NULL.
74
*/
75
 
76
gen_ptr xrealloc
77
    PROTO_N ( ( p, sz ) )
78
    PROTO_T ( gen_ptr p X long sz )
79
{
80
    gen_ptr q ;
81
    if ( p ) {
82
	q = realloc ( p, ( size_t ) sz ) ;
83
    } else {
84
	q = malloc ( ( size_t ) sz ) ;
85
    }
86
    if ( q == NULL ) error ( ERROR_FATAL, "Memory allocation error" ) ;
87
    return ( q ) ;
88
}
89
 
90
 
91
/*
92
    CONTROLLED VERSION OF FREE
93
 
94
    This routine frees the block of memory p.  p can be the result of a
95
    previous memory allocation routine, or NULL.
96
*/
97
 
98
void xfree
99
    PROTO_N ( ( p ) )
100
    PROTO_T ( gen_ptr p )
101
{
102
    if ( p ) free ( p ) ;
103
    return ;
104
}
105
 
106
 
107
/*
108
    ALLOCATE SPACE FOR A STRING
109
 
110
    This routine allocates space for n characters.  The memory allocation
111
    is buffered except for very long strings.
112
*/
113
 
114
char *xstr
115
    PROTO_N ( ( n ) )
116
    PROTO_T ( long n )
117
{
118
    char *r ;
119
    if ( n >= 1000 ) {
120
	r = xmalloc_nof ( char, n ) ;
121
    } else {
122
	static long chars_left = 0 ;
123
	static char *chars_free = 0 ;
124
	if ( n >= chars_left ) {
125
	    chars_left = 5000 ;
126
	    chars_free = xmalloc_nof ( char, chars_left ) ;
127
	}
128
	r = chars_free ;
129
	chars_free += n ;
130
	chars_left -= n ;
131
    }
132
    return ( r ) ;
133
}
134
 
135
 
136
/*
137
    COPY A STRING
138
 
139
    This routine allocates space for a persistent copy of the string s.
140
*/
141
 
142
char *xstrcpy
143
    PROTO_N ( ( s ) )
144
    PROTO_T ( CONST char *s )
145
{
146
    long n ;
147
    char *r ;
148
    if ( s == NULL ) return ( NULL ) ;
149
    n = ( long ) strlen ( s ) + 1 ;
150
    r = xstr ( n ) ;
151
    strcpy_v ( r, s ) ;
152
    return ( r ) ;
153
}
154
 
155
 
156
/*
157
    CONCATENATE TWO STRINGS
158
 
159
    This routine allocates space for a persistent copy of the string s
160
    followed by the string t.
161
*/
162
 
163
char *xstrcat
164
    PROTO_N ( ( s, t ) )
165
    PROTO_T ( CONST char *s X CONST char *t )
166
{
167
    char *r ;
168
    long n, m ;
169
    if ( s == NULL ) return ( xstrcpy ( t ) ) ;
170
    if ( t == NULL ) return ( xstrcpy ( s ) ) ;
171
    n = ( long ) strlen ( s ) ;
172
    m = n + ( long ) strlen ( t ) + 1 ;
173
    r = xstr ( m ) ;
174
    strcpy_v ( r, s ) ;
175
    strcpy_v ( r + n, t ) ;
176
    return ( r ) ;
177
}