Subversion Repositories tendra.SVN

Rev

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

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