Debian Codegraph @

debian/external_deps/fetk/maloc/src/vsys/vset.c

raw
1 /*
2 * ***************************************************************************
3 * MALOC = < Minimal Abstraction Layer for Object-oriented C >
4 * Copyright (C) 1994-- Michael Holst
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * rcsid="$Id: vset.c,v 1.16 2010/08/12 05:40:33 fetk Exp $"
21 * ***************************************************************************
22 */
23
24 /*
25 * ***************************************************************************
26 * File: vset.c
27 *
28 * Purpose: Class Vset: methods.
29 *
30 * Author: Michael Holst
31 * ***************************************************************************
32 */
33
34 #include "vset_p.h"
35
36 VEMBED(rcsid="$Id: vset.c,v 1.16 2010/08/12 05:40:33 fetk Exp $")
37
38 /*
39 * ***************************************************************************
40 * Class Vset: Inlineable methods
41 * ***************************************************************************
42 */
43 #if !defined(VINLINE_MALOC)
44
45 /*
46 * ***************************************************************************
47 * Routine: Vset_num
48 *
49 * Purpose: Return the number of things currently in the list.
50 *
51 * Author: Michael Holst
52 * ***************************************************************************
53 */
54 VPUBLIC int Vset_num(Vset *thee)
55 {
56 return thee->numT;
57 }
58
59 /*
60 * ***************************************************************************
61 * Routine: Vset_access
62 *
63 * Purpose: Access an object in an arbitrary place in the list.
64 *
65 * Author: Michael Holst
66 * ***************************************************************************
67 */
68 VPUBLIC char *Vset_access(Vset *thee, int i)
69 {
70 if ((i >= 0) && (i < thee->numT))
71 return &( thee->table[ i>>thee->blockPower ]
72 [ thee->sizeT*(i&thee->blockModulo) ] );
73 else
74 return VNULL;
75 }
76
77 /*
78 * ***************************************************************************
79 * Routine: Vset_create
80 *
81 * Purpose: Create an object on the end of the list.
82 *
83 * Author: Michael Holst
84 * ***************************************************************************
85 */
86 VPUBLIC char *Vset_create(Vset *thee)
87 {
88 if ( (((thee->numT)>>thee->blockPower) >= thee->numBlocks)
89 || (((thee->numT+1)%thee->prtT) == 0) ) {
90 return Vset_createLast(thee);
91 } else {
92 (thee->numT)++;
93 return Vset_access(thee,thee->numT-1);
94 }
95 }
96
97 /*
98 * ***************************************************************************
99 * Routine: Vset_destroy
100 *
101 * Purpose: Delete an object from the end of the list.
102 *
103 * Author: Michael Holst
104 * ***************************************************************************
105 */
106 VPUBLIC void Vset_destroy(Vset *thee)
107 {
108 if ( (((thee->numT-1)>>thee->blockPower) < thee->numBlocks-1)
109 || (thee->numT == 1) || (((thee->numT)%thee->prtT) == 0)) {
110 Vset_destroyLast(thee);
111 } else {
112 (thee->numT)--;
113 }
114 }
115
116 /*
117 * ***************************************************************************
118 * Routine: Vset_first
119 *
120 * Purpose: Return the first object in the set.
121 *
122 * Author: Michael Holst
123 * ***************************************************************************
124 */
125 VPUBLIC char *Vset_first(Vset *thee)
126 {
127 thee->curT = 0;
128 return Vset_access(thee, thee->curT);
129 }
130
131 /*
132 * ***************************************************************************
133 * Routine: Vset_last
134 *
135 * Purpose: Return the last object in the set.
136 *
137 * Author: Michael Holst
138 * ***************************************************************************
139 */
140 VPUBLIC char *Vset_last(Vset *thee)
141 {
142 thee->curT = thee->numT-1;
143 return Vset_access(thee, thee->curT);
144 }
145
146 /*
147 * ***************************************************************************
148 * Routine: Vset_next
149 *
150 * Purpose: Return the next object in the set.
151 *
152 * Author: Michael Holst
153 * ***************************************************************************
154 */
155 VPUBLIC char *Vset_next(Vset *thee)
156 {
157 thee->curT++;
158 if (thee->curT < thee->numT)
159 return Vset_access(thee, thee->curT);
160 else return VNULL;
161 }
162
163 /*
164 * ***************************************************************************
165 * Routine: Vset_prev
166 *
167 * Purpose: Return the prev object in the set.
168 *
169 * Author: Michael Holst
170 * ***************************************************************************
171 */
172 VPUBLIC char *Vset_prev(Vset *thee)
173 {
174 thee->curT--;
175 if (thee->curT >= 0)
176 return Vset_access(thee, thee->curT);
177 else return VNULL;
178 }
179
180 /*
181 * ***************************************************************************
182 * Routine: Vset_peekFirst
183 *
184 * Purpose: Return the first object in the set.
185 *
186 * Author: Michael Holst
187 * ***************************************************************************
188 */
189 VPUBLIC char *Vset_peekFirst(Vset *thee)
190 {
191 return Vset_access(thee, 0);
192 }
193
194 /*
195 * ***************************************************************************
196 * Routine: Vset_peekLast
197 *
198 * Purpose: Return the last object in the set.
199 *
200 * Author: Michael Holst
201 * ***************************************************************************
202 */
203 VPUBLIC char *Vset_peekLast(Vset *thee)
204 {
205 return Vset_access(thee, thee->numT-1);
206 }
207
208 #endif /* if !defined(VINLINE_MALOC) */
209 /*
210 * ***************************************************************************
211 * Class Vset: Non-inlineable methods
212 * ***************************************************************************
213 */
214
215 /*
216 * ***************************************************************************
217 * Routine: Vset_ctor
218 *
219 * Purpose: Construct the set object.
220 *
221 * Author: Michael Holst
222 * ***************************************************************************
223 */
224 VPUBLIC Vset* Vset_ctor(Vmem *vmem,
225 const char *tname, int tsize, int tmaxNum, int ioKey)
226 {
227 char name[VMAX_ARGLEN];
228 Vset *thee = VNULL;
229 int K;
230
231 VDEBUGIO("Vset_ctor: CREATING object..");
232
233 thee = Vmem_malloc( VNULL, 1, sizeof(Vset) );
234 if (vmem == VNULL) {
235 sprintf(name, "Vset:%s", tname);
236 thee->vmem = Vmem_ctor( name );
237 thee->iMadeVmem = 1;
238 } else {
239 thee->vmem = vmem;
240 thee->iMadeVmem = 0;
241 }
242
243 VASSERT( tsize > 0 );
244 VASSERT( tmaxNum > 0 );
245
246 /* set object name and size */
247 strncpy(thee->nameT, tname, VMAX_ARGLEN);
248 thee->sizeT = tsize;
249
250 /* set blockSize as a power of two for quick block division via shifts */
251 thee->blockPower = VBLOCK_POWER;
252 thee->blockSize = (1 << thee->blockPower);
253
254 /* determine maxObjects such that: [maxObjects >= tmaxNum] */
255 /* AND such that for some integer K: [maxObjects = K * blockSize] */
256 /* (this most excellent elegant expression is due to mr. bond) */
257 K = (int)( (tmaxNum - 1)/(thee->blockSize) ) + 1;
258 thee->maxObjects = K * thee->blockSize;
259 VASSERT( thee->maxObjects >= tmaxNum );
260
261 /* set blockMax and blockModulo using maxObjects and blockSize */
262 thee->blockMax = (thee->maxObjects / thee->blockSize);
263 thee->blockModulo = (thee->blockSize - 1);
264
265 /* create the table of blocks */
266 thee->table = Vmem_malloc( thee->vmem, thee->blockMax, sizeof( char* ) );
267
268 /* now initialize the block data */
269 Vset_initData(thee);
270
271 VDEBUGIO("..done.\n");
272
273 /* some i/o */
274 if (ioKey) {
275 Vnm_print(0,
276 "Vset_ctor: %d (%d) %s [%d bs/%s, %d %s/bl, %d bls, %d:o bs]\n",
277 (thee->blockSize * thee->blockMax), tmaxNum,
278 thee->nameT, thee->sizeT, thee->nameT,
279 thee->blockSize, thee->nameT, thee->blockMax,
280 (VPTRSIZE * thee->blockMax) );
281 }
282
283 return thee;
284 }
285
286 /*
287 * ***************************************************************************
288 * Routine: Vset_dtor
289 *
290 * Purpose: Destroy the set object.
291 *
292 * Author: Michael Holst
293 * ***************************************************************************
294 */
295 VPUBLIC void Vset_dtor(Vset **thee)
296 {
297 VASSERT( (*thee) != VNULL );
298 if ((*thee) != VNULL) {
299 Vset_reset(*thee);
300 Vmem_free( (*thee)->vmem, (*thee)->blockMax, sizeof(char*),
301 (void**)&((*thee)->table) );
302
303 VDEBUGIO("Vset_dtor: DESTROYING object..");
304 if ((*thee)->iMadeVmem) Vmem_dtor( &((*thee)->vmem) );
305 Vmem_free( VNULL, 1, sizeof(Vset), (void**)thee );
306 VDEBUGIO("..done.\n");
307
308 (*thee) = VNULL;
309 }
310 }
311
312 /*
313 * ***************************************************************************
314 * Routine: Vset_createLast
315 *
316 * Purpose: Create an object on the end of the list.
317 *
318 * Author: Michael Holst
319 * ***************************************************************************
320 */
321 VPUBLIC char *Vset_createLast(Vset *thee)
322 {
323 /* get block number and index into block */
324 int blockIdx = thee->sizeT*(thee->numT & thee->blockModulo);
325 int blockNum = thee->numT >> thee->blockPower;
326
327 /* do we need a new block yet? */
328 if (blockNum >= thee->numBlocks) {
329 VASSERT( blockNum == thee->numBlocks );
330 thee->table[blockNum] = Vmem_malloc(thee->vmem,
331 thee->blockSize, thee->sizeT);
332 VASSERT ( thee->table[blockNum] != VNULL );
333 thee->numBlocks++;
334 VASSERT ( thee->numBlocks <= thee->blockMax );
335 }
336
337 /* increase global object count by one */
338 thee->numT++;
339
340 /* some i/o if needed */
341 /* *** Vnm_print(0,"Vset_create: numT = %d\n", thee->numT); *** */
342 if ( (thee->numT%thee->prtT) == 0 )
343 Vnm_print(0,"[%s:c%d]",thee->nameT,thee->numT);
344
345 /* return new obj */
346 return &(thee->table[blockNum][blockIdx]);
347 }
348
349 /*
350 * ***************************************************************************
351 * Routine: Vset_destroyLast
352 *
353 * Purpose: Free up the object currently on the end of the list.
354 *
355 * Author: Michael Holst
356 * ***************************************************************************
357 */
358 VPUBLIC void Vset_destroyLast(Vset *thee)
359 {
360 int blockNum;
361
362 /* some i/o if needed */
363 /* *** Vnm_print(0,"Vset_destroy: numT = %d\n", thee->numT); *** */
364 if ( (thee->numT%thee->prtT) == 0 )
365 Vnm_print(0,"[%s:d%d]",thee->nameT,thee->numT);
366
367 /* decrease global object count by one */
368 thee->numT--;
369
370 /* has block been completely emptied? */
371 blockNum = thee->numT >> thee->blockPower;
372 if (blockNum < thee->numBlocks-1) {
373 VASSERT( blockNum == thee->numBlocks-2 );
374 thee->numBlocks--;
375 Vmem_free( thee->vmem, thee->blockSize, thee->sizeT,
376 (void**)&(thee->table[thee->numBlocks]) );
377 thee->table[thee->numBlocks] = VNULL;
378 } else if (thee->numT == 0) {
379 VASSERT( thee->numBlocks == 1 );
380 thee->numBlocks = 0;
381 Vmem_free( thee->vmem, thee->blockSize, thee->sizeT,
382 (void**)&(thee->table[0]) );
383 thee->table[0] = VNULL;
384 }
385 }
386
387 /*
388 * ***************************************************************************
389 * Routine: Vset_initData
390 *
391 * Purpose: Initialize the Vset data (thee).
392 *
393 * Author: Michael Holst
394 * ***************************************************************************
395 */
396 VPUBLIC void Vset_initData(Vset *thee)
397 {
398 int j;
399 thee->numBlocks = 0;
400 thee->curT = 0;
401 thee->numT = 0;
402 thee->prtT = 10000;
403 for (j=0; j<thee->blockMax; j++) thee->table[j]=VNULL;
404 }
405
406 /*
407 * ***************************************************************************
408 * Routine: Vset_reset
409 *
410 * Purpose: Release all Ram controlled by this (thee) and re-initialize.
411 *
412 * Author: Michael Holst
413 * ***************************************************************************
414 */
415 VPUBLIC void Vset_reset(Vset *thee)
416 {
417 int j;
418 while (Vset_num(thee) > 0) Vset_destroy(thee);
419 for (j=0; j<thee->blockMax; j++)
420 if (thee->table[j] != VNULL)
421 Vmem_free( thee->vmem, thee->blockSize, thee->sizeT,
422 (void**)&(thee->table[j]) );
423 Vset_initData(thee);
424 }
425
426 /*
427 * ***************************************************************************
428 * Routine: Vset_check
429 *
430 * Purpose: Get and return the RAM Control Block (thee) information.
431 *
432 * Author: Michael Holst
433 * ***************************************************************************
434 */
435 VPUBLIC void Vset_check(Vset *thee,
436 int *tnum, int *tsize, int *tVecUse, int *tVecMal, int *tVecOhd)
437 {
438 (*tnum) = thee->numT;
439 (*tsize) = thee->sizeT;
440 (*tVecUse) = thee->sizeT * thee->numT;
441 (*tVecMal) = thee->sizeT * (thee->numBlocks * thee->blockSize);
442 (*tVecOhd) = VPTRSIZE * thee->blockMax;
443 }
444
445 /*
446 * ***************************************************************************
447 * Routine: Vset_memChk
448 *
449 * Purpose: Print the exact current malloc usage.
450 *
451 * Author: Michael Holst
452 * ***************************************************************************
453 */
454 VPUBLIC void Vset_memChk(Vset *thee)
455 {
456 if (thee->iMadeVmem) Vmem_print(thee->vmem);
457 }
458