Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | 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 "tdf.h"
33
#include "error.h"
34
#include "xalloc.h"
35
 
36
 
37
/*
38
    FREE OBJECTS
39
 
40
    These variables indicate the free tdf.  There is an array containing
41
    lists of small blocks, plus a single larger block.
42
*/
43
 
44
#define free_tdf_max	16
45
static tdf *free_tdf = NULL ;
46
static unsigned free_tdf_left = 0 ;
47
static tdf *free_tdf_array [ free_tdf_max ] = {
48
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
49
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
50
} ;
51
 
52
 
53
/*
54
    GENERATE A NEW OBJECT BLOCK
55
 
56
    This routine generates a new blcok of tdf of size sz.  Small blocks
57
    are allocated from the tdf array, others from the main tdf list.
58
*/
59
 
60
tdf *gen_tdf
61
    PROTO_N ( ( sz ) )
62
    PROTO_T ( unsigned sz )
63
{
64
    tdf *p ;
65
    unsigned n = sz ;
66
 
67
    if ( n < free_tdf_max ) {
68
	/* Allocate from small block array */
69
	p = free_tdf_array [n] ;
70
	if ( p ) {
71
	    free_tdf_array [n] = TAIL_list ( p ) ;
72
	    return ( p ) ;
73
	}
74
    }
75
 
76
    /* Allocate from large block */
77
    if ( n > free_tdf_left ) {
78
	free_tdf_left = 1000 ;
79
	free_tdf = xmalloc_nof ( tdf, free_tdf_left ) ;
80
    }
81
    p = free_tdf ;
82
    free_tdf += sz ;
83
    free_tdf_left -= sz ;
84
    return ( p ) ;
85
}
86
 
87
 
88
/*
89
    DESTROY AN OBJECT BLOCK
90
 
91
    This routine destroys the block of tdf p of size sz.  Only small
92
    blocks are recycled.
93
*/
94
 
95
void destroy_tdf
96
    PROTO_N ( ( p, sz ) )
97
    PROTO_T ( tdf *p X unsigned sz )
98
{
99
    unsigned n = sz ;
100
    if ( p && n < free_tdf_max ) {
101
	TAIL_list ( p ) = free_tdf_array [n] ;
102
	free_tdf_array [n] = p ;
103
    }
104
    return ;
105
}
106
 
107
 
108
/*
109
    DUMMY OBJECT BLOCK DESTRUCTOR
110
 
111
    This routine is a dummy destructor which does nothing.
112
*/
113
 
114
void dummy_destroy_tdf
115
    PROTO_N ( ( p, sz ) )
116
    PROTO_T ( tdf *p X unsigned sz )
117
{
118
    UNUSED ( p ) ;
119
    UNUSED ( sz ) ;
120
    return ;
121
}
122
 
123
 
124
/*
125
    DESTROY A LIST OF OBJECT BLOCKS
126
 
127
    This routine destroys the list p of blocks of tdf of size sz.  The
128
    list is added to the appropriate entry of the free tdf array.
129
*/
130
 
131
void destroy_tdf_list
132
    PROTO_N ( ( p, sz ) )
133
    PROTO_T ( tdf *p X unsigned sz )
134
{
135
    unsigned n = sz + 1 ;
136
    if ( p && n < free_tdf_max ) {
137
	tdf *q = p ;
138
	while ( TAIL_list ( p ) ) p = TAIL_list ( p ) ;
139
	TAIL_list ( p ) = free_tdf_array [n] ;
140
	free_tdf_array [n] = q ;
141
    }
142
    return ;
143
}
144
 
145
 
146
/*
147
    FIND THE LENGTH OF A LIST
148
 
149
    This routine calculates the length of the list p.
150
*/
151
 
152
unsigned length_tdf_list
153
    PROTO_N ( ( p ) )
154
    PROTO_T ( tdf *p )
155
{
156
    tdf *q ;
157
    unsigned n = 0 ;
158
    for ( q = p ; q != NULL ; q = TAIL_list ( q ) ) n++ ;
159
    return ( n ) ;
160
}
161
 
162
 
163
/*
164
    REVERSE A LIST
165
 
166
    This routine reverses the order of the list p.
167
*/
168
 
169
tdf *reverse_tdf_list
170
    PROTO_N ( ( p ) )
171
    PROTO_T ( tdf *p )
172
{
173
    tdf *r = NULL ;
174
    tdf *q = p ;
175
    while ( q != NULL ) {
176
	tdf *nq = TAIL_list ( q ) ;
177
	TAIL_list ( q ) = r ;
178
	r = q ;
179
	q = nq ;
180
    }
181
    return ( r ) ;
182
}
183
 
184
 
185
/*
186
    APPEND TWO LISTS
187
 
188
    This routine appends the lists of tdf blocks p and q.
189
*/
190
 
191
tdf *append_tdf_list
192
    PROTO_N ( ( p, q ) )
193
    PROTO_T ( tdf *p X tdf *q )
194
{
195
    tdf *r = p ;
196
    if ( r == NULL ) return ( q ) ;
197
    while ( TAIL_list ( r ) ) r = TAIL_list ( r ) ;
198
    TAIL_list ( r ) = q ;
199
    return ( p ) ;
200
}
201
 
202
 
203
/*
204
    FIND THE LAST MEMBER OF A LIST
205
 
206
    This routine returns the last member of the list of tdf blocks p.
207
*/
208
 
209
tdf *end_tdf_list
210
    PROTO_N ( ( p ) )
211
    PROTO_T ( tdf *p )
212
{
213
    tdf *r = p ;
214
    if ( r == NULL ) return ( NULL ) ;
215
    while ( TAIL_list ( r ) ) r = TAIL_list ( r ) ;
216
    return ( r ) ;
217
}
218
 
219
 
220
/*
221
    ASSERTION ROUTINES
222
 
223
    These routine implement the assertion checks.
224
*/
225
 
226
#ifdef ASSERTS
227
#define assert_calculus assertion
228
#include "assert_def.h"
229
#endif