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