File: dyntab.c

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (764 lines) | stat: -rw-r--r-- 17,105 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*
 *  dyntab.c
 *
 *  $Id: dyntab.c,v 1.1.1.1 2006/04/11 17:56:16 source Exp $
 *
 *  Dynamic Tables
 *  
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *  
 *  Copyright (C) 1998-2006 OpenLink Software
 *  
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *  
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *  
 *  
*/

#include <libutil.h>
#include "dyntab.h"

#undef malloc
#undef calloc
#undef free

#define RNDUP_BYTES	4
#define RNDUP(x)  ((((x) + RNDUP_BYTES - 1) / RNDUP_BYTES) * RNDUP_BYTES)

#define KEYFL_UNIQUE		0x0001

#define INITNKEYS		2
#define INCRNKEYS		2

struct htelem_t
  {
    struct htelem_t *	next;
    struct htelem_t **	pprev;
  };
typedef struct htelem_t htelem_t;

struct htkey_t
  {
    u_short		flags;
    hthashfun_t		hashFunc;
    htcomparefun_t	compareFunc;
    htelem_t **		hashTable;
    u_int		hashSize;
    u_int		recordCount;
  };
typedef struct htkey_t htkey_t;

struct httable_t
  {
    u_int		maxRecords;
    u_int		numRecords;
    u_int		freeRecords;
    u_short		incrRecords;
    u_int		recordSize;
    htrecord_t *	records;
    u_short		maxKeys;
    u_short		numKeys;
    u_short		headerSize;
    htkey_t *		keys;
    htcreatefun_t	createFunc;
    void *		createData;
    htdestroyfun_t	destroyFunc;
  };
typedef struct httable_t httable_t;


/*
 *  Create a dynamic table
 *
 *  On entry:
 *    pTable		pointer to dyntable_t (handle)
 *    initRecords	initial number of records (pointers), or 0
 *    incrRecords	record increment when table full
 *    createFunc	user supplied constructor (optional)
 *    createData	argument for (*createFunc) ()
 *    destroyFunc	user supplied destructor (optional)
 */
int
dtab_create_table (
    dyntable_t *	pTable,
    u_int		recordSize,
    u_int		initRecords,
    u_short		incrRecords,
    htcreatefun_t	createFunc,
    void *		createData,
    htdestroyfun_t	destroyFunc)
{
  dyntable_t table;

  if (pTable == (dyntable_t *)0)
    return DTAB_INVALID_ARG;

  *pTable = (dyntable_t)0;

  table = (httable_t *) calloc (1, sizeof (httable_t));
  if (table == (dyntable_t)0)
    return DTAB_NO_MEMORY;

  /*
   *  Allocate pointers for the records
   */
  if (incrRecords == 0)
    incrRecords = 10;

  if (initRecords != 0)
    {
      table->records = (htrecord_t *) calloc (initRecords, sizeof (htrecord_t));
      if (table->records == (htrecord_t *)0)
	{
	  free (table);
	  return DTAB_NO_MEMORY;
	}
    }

  table->maxRecords = initRecords;
  table->incrRecords = incrRecords;
  table->recordSize = recordSize;
  table->createFunc = createFunc;
  table->createData = createData;
  table->destroyFunc = destroyFunc;
  table->headerSize = RNDUP (sizeof (httable_t *));

  *pTable = table;

  return DTAB_SUCCESS;
}


/*
 *  Destroys the dynamic tablespace.
 *  This also frees all the records in the table.
 *  The user supplied destructor is called before the record is freed.
 */
int
dtab_destroy_table (dyntable_t *pTable)
{
  dyntable_t table;
  u_int i;

  if (pTable == (dyntable_t *)0 || (table = *pTable) == (dyntable_t)0)
    return DTAB_INVALID_ARG;

  /*
   *  Destroy all the records
   */
  if (table->records)
    {
      for (i = 0; i < table->numRecords; i++)
	{
	  if (table->records[i])
	    {
	      /*
	       *  Call the user's destructor on this record
	       */
	      if (table->destroyFunc)
	        (*table->destroyFunc) (table->records[i] + table->headerSize);
	      free (table->records[i]);
	    }
	}
      free (table->records);
    }

  /*
   *  Destroy the hash tables
   */
  if (table->keys)
    {
      for (i = 0; i < table->numKeys; i++)
	free (table->keys[i].hashTable);
      free (table->keys);
    }

  memset (table, 0, sizeof (*table));
  free (table);
  *pTable = (dyntable_t)0;

  return DTAB_SUCCESS;
}


/*
 *  Define a hash table on the record.
 *  There can be as many hash tables on the record as needed.
 *  NOTE:
 *    DEFINE ALL THE HASH TABLES BEFORE RECORDS ARE CREATED!
 */
int
dtab_define_key (
    dyntable_t		table,
    hthashfun_t		hashFunc,
    u_int		hashSize,
    htcomparefun_t	compareFunc,
    int			unique)
{
  htkey_t key;

  /*
   *  Validate arguments
   */
  if (table == (dyntable_t)0 || hashSize == 0 ||
      hashFunc == (hthashfun_t)0 || compareFunc == (htcomparefun_t)0)
    {
      return DTAB_INVALID_ARG;
    }

  /*
   *  Enough room in the key definitions table?
   */
  if (table->numKeys >= table->maxKeys)
    {
      htkey_t *oldKeys;
      htkey_t *newKeys;
      u_short numKeys;

      oldKeys = table->keys;
      numKeys = (table->maxKeys == 0) ? INITNKEYS : table->maxKeys + INCRNKEYS;
      newKeys = (htkey_t *) calloc (numKeys, sizeof (htkey_t));
      if (newKeys == (htkey_t *)0)
	return DTAB_NO_MEMORY;
      if (oldKeys)
	{
	  memcpy (newKeys, table->keys, table->maxKeys * sizeof (htkey_t));
	  free (table->keys);
	}
      table->keys = newKeys;
      table->maxKeys = numKeys;
    }

  /*
   *  Build the new key definition record
   */
  key.recordCount = 0;
  key.flags = unique ? KEYFL_UNIQUE : 0;
  key.compareFunc = compareFunc;
  key.hashFunc = hashFunc;
  key.hashSize = hashSize;
  key.hashTable = (htelem_t **) calloc (hashSize, sizeof (htelem_t *));
  if (key.hashTable == (htelem_t **)0)
    return DTAB_NO_MEMORY;

  table->keys[table->numKeys++] = key;
  table->headerSize =
      RNDUP (table->numKeys * sizeof (htelem_t) + sizeof (httable_t *));

  return DTAB_SUCCESS;
}


/*
 *  Create a new record.
 *
 *  This function does NOT insert the record in the hash tables,
 *  but it is inserted in the master table. So dtab_foreach (table,0,...)
 *  will work for the created but not inserted records.
 */
int
dtab_create_record (dyntable_t table, htrecord_t *pRecord)
{
  htrecord_t record;
  htrecord_t *pStore;

  /*
   *  Validate arguments
   */
  if (table == (dyntable_t)0)
    return DTAB_INVALID_ARG;

  *pRecord = (htrecord_t)0;

  if (pRecord == (htrecord_t *)0)
    return DTAB_INVALID_ARG;

  /*
   *  Allocate the record
   */
  record = (htrecord_t) calloc (1, table->headerSize + table->recordSize);
  if (record == (htrecord_t)0)
    return DTAB_NO_MEMORY;

  /*
   *  Link the record to the table
   */
  *((httable_t **) &record[table->numKeys * sizeof (htelem_t)]) = table;

  /*
   *  Find a slot for the pointer to the record
   */
  if (table->freeRecords)
    {
      for (pStore = table->records; *pStore != (htrecord_t)0; pStore++)
	;
      table->freeRecords--;
    }
  else
    {
      if (table->numRecords >= table->maxRecords)
	{
	  htrecord_t *oldRecords;
	  htrecord_t *newRecords;
	  u_int maxRecords;

	  oldRecords = table->records;
	  maxRecords = table->maxRecords + table->incrRecords;
	  newRecords = (htrecord_t *) calloc (maxRecords, sizeof (htrecord_t));
	  if (newRecords == (htrecord_t *)0)
	    {
	      free (record);
	      return DTAB_NO_MEMORY;
	    }
	  if (oldRecords)
	    {
	      memcpy (newRecords, table->records,
		  table->maxRecords * sizeof (htrecord_t));
	      free (table->records);
	    }
	  pStore = &newRecords[table->numRecords++];
	  table->records = newRecords;
	  table->maxRecords = maxRecords;
	}
      else
	pStore = &table->records[table->numRecords++];
    }

  *pStore = record;
  *pRecord = record += table->headerSize;

  /*
   *  Call user function to initialize the record
   */
  if (table->createFunc)
    (*table->createFunc) (record, table->createData);

  return DTAB_SUCCESS;
}


/*
 *  Delete a record from its table.
 *  The user supplied destructor is called, if supplied.
 *  The record is freed.
 */
int
dtab_delete_record (htrecord_t *pRecord)
{
  htrecord_t record;
  htrecord_t internalRec;
  httable_t **pTable;
  httable_t *table;
  u_int ri;
  u_int ki;

  if (pRecord == (htrecord_t *)0 || (record = *pRecord) == (htrecord_t)0)
    return DTAB_INVALID_ARG;

  /*
   *  Get the pointer to the table
   */
  pTable = (httable_t **) &record[- sizeof (htrecord_t *)];
  table = *pTable;
  if (table == NULL)
    return DTAB_INVALID_ARG;

  /*
   *  Search the record in the table
   */
  internalRec = record - table->headerSize;
  for (ri = 0; ri < table->numRecords; ri++)
    {
      if (table->records[ri] == internalRec)
	{
	  /*
	   *  Call user function to free the record
	   */
	  if (table->destroyFunc)
	    (*table->destroyFunc) (record);

	  /*
	   *  Remove from the hash chains
	   */
	  for (ki = 0; ki < table->numKeys; ki++)
	    {
              htelem_t *elem = &((htelem_t *) internalRec)[ki];
	      if (elem->next || elem->pprev)
		{
		  table->keys[ki].recordCount--;
		  if (elem->pprev)
		    *elem->pprev = elem->next;
		  if (elem->next)
		    elem->next->pprev = elem->pprev;
		}
	    }

	  /*
	   *  Remove the record from the table
	   */
	  table->records[ri] = (htrecord_t)0;
	  table->freeRecords++;

	  *pTable = (httable_t *)0;	/* Clear table link */
	  *pRecord = (htrecord_t)0;	/* Clear application handle */

	  free (internalRec);

	  return DTAB_SUCCESS;
	}
    }

  return DTAB_INVALID_ARG;
}


/*
 *  Add a record to the table.
 *  This record must be allocated with dtab_create_record.
 *  The record is inserted in all the defined hash tables.
 *  Note: after changing one of the key values in the record, call
 *  this function again to re-establish the position on the hash chains.
 */
int
dtab_add_record (htrecord_t record)
{
  httable_t *table;

  if (record == NULL)
    return DTAB_INVALID_ARG;

  table = * (httable_t **) &record[- sizeof (htrecord_t *)];
  if (table == NULL)
    return DTAB_INVALID_ARG;

  /*
   *  Any hash keys on this table?
   */
  if (table->numKeys != 0)
    {
      htelem_t *hashChain = (htelem_t *) (record - table->headerSize);
      htkey_t *key;
      int ki;

      /*
       *  Add hash entries in all the hashtables
       */
      for (key = table->keys, ki = 0; ki < table->numKeys; ki++, key++)
	{
	  u_int hashValue = key->hashFunc (record) % key->hashSize;
	  htelem_t **pHash = &key->hashTable[hashValue];
	  htelem_t *elem;
	  int doAdd;

	  /*
	   *  Remove from the hash chain
	   */
	  if (hashChain[ki].next || hashChain[ki].pprev)
	    {
	      key->recordCount--;
	      if (hashChain[ki].pprev)
		*hashChain[ki].pprev = hashChain[ki].next;
	      if (hashChain[ki].next)
	        hashChain[ki].next->pprev = hashChain[ki].pprev;
	    }

	  /*
	   *  Make sure no dups are entered when unique flag is set
	   */
	  doAdd = 1;
	  if (key->flags & KEYFL_UNIQUE)
	    {
	      for (elem = *pHash; elem; elem = elem[ki].next)
		{
		  if ((*key->compareFunc) (record,
		      (htrecord_t) elem + table->headerSize) == 0)
		    {
		      doAdd = 0;
		      break;
		    }
		}
	    }
	  if (!doAdd)
	    continue;

	  key->recordCount++;
	  if (*pHash)
	    (*pHash)[ki].pprev = &hashChain[ki].next;
	  hashChain[ki].pprev = pHash;
	  hashChain[ki].next = *pHash;
	  *pHash = hashChain;
	}
    }

  return DTAB_SUCCESS;
}


/*
 *  Process all the records, calling a user function
 *
 *  If keyNum == 0, then the master table is used to find the records.
 *  For other values [1..number_of_keys_defined], the corresponging hashtable
 *  will be used
 */
int
dtab_foreach (
    dyntable_t	table,
    int		keyNum,
    htuserfun_t	function,
    void *	argument)
{
  u_int ri;

  if (table == (httable_t *)0 || function == (htuserfun_t)0)
    return DTAB_INVALID_ARG;

  /*
   *  KeyNum 0: the master table
   */
  if (keyNum == 0)
    {
      for (ri = 0; ri < table->numRecords; ri++)
	{
	  if (table->records[ri])
	    (*function) (table->records[ri] + table->headerSize, argument);
	}
    }

  /*
   *  Other KeyNum values: use the supplied hash table
   */
  else if (keyNum <= table->numKeys)
    {
      htkey_t *key;
      u_int ki;

      key = &table->keys[--keyNum];

      for (ki = 0; ki < key->hashSize; ki++)
	{
	  htelem_t *elem, *nextElem;

	  elem = key->hashTable[ki];
	  while (elem)
	    {
	      nextElem = elem[keyNum].next;
	      (*function) ((htrecord_t) elem + table->headerSize, argument);
	      elem = nextElem;
	    }
	}
    }
  else
    return DTAB_INVALID_ARG;

  return DTAB_SUCCESS;
}


/*
 *  Check if a record exists in one of the hash indexes.
 *  It is not necessary for the supplied record to be
 *  allocated with dtab_create_record.
 *
 *  Returns TRUE when the record exists, or FALSE when it
 *  doesn't exist, or when the parameters are incorrect.
 */
int
dtab_exist (dyntable_t table, u_int keyNum, htrecord_t record)
{
  return dtab_find_record (table, keyNum, record) != NULL;
}


htrecord_t
dtab_find_record (dyntable_t table, u_int keyNum, htrecord_t record)
{
  if (table == (httable_t *)0 || record == (htrecord_t)0)
    return NULL;

  if (--keyNum <= table->numKeys)
    {
      htkey_t *key = &table->keys[keyNum];
      u_int hashValue = key->hashFunc (record) % key->hashSize;
      htelem_t *elem;

      for (elem = key->hashTable[hashValue]; elem; elem = elem[keyNum].next)
	{
	  if ((*key->compareFunc) (record,
	      (htrecord_t) elem + table->headerSize) == 0)
	    {
	      return (htrecord_t) elem + table->headerSize;
	    }
	}
    }

  return NULL;
}


/*
 *  Return the number of records in the table space.
 *
 *  KeyNum == 0:  return # records in the master table
 *  Other values: return # records on the n'th hash chain
 *
 *  Note: will return 0 for illegal argument values
 */
u_int
dtab_record_count (dyntable_t table, u_int keyNum)
{
  if (table == (httable_t *)0)
    return 0;

  if (keyNum == 0)
    return table->numRecords - table->freeRecords;
  else if (--keyNum < table->numKeys)
    return table->keys[keyNum].recordCount;
  else
    return 0;
}


int
dtab_make_list (
    dyntable_t		table,
    u_int		keyNum,
    u_int *		pNumRecords,
    htrecord_t **	pRecords)
{
  htrecord_t *indexTable;
  u_int numRecords;
  u_int counter;
  htkey_t *key;
  u_int ki;
  u_int ri;

  if (table == (httable_t *)0 || pRecords == NULL)
    return DTAB_INVALID_ARG;

  counter = 0;

  /*
   *  KeyNum 0: the master table
   */
  if (keyNum == 0)
    {
      numRecords = table->numRecords - table->freeRecords;
      indexTable = (htrecord_t *) malloc (numRecords * sizeof (htrecord_t));
      if (indexTable == NULL)
	return DTAB_NO_MEMORY;

      for (ri = 0; ri < table->numRecords; ri++)
	{
	  if (table->records[ri])
	    indexTable[counter++] = table->records[ri] + table->headerSize;
	}
    }

  /*
   *  Other KeyNum values: use the supplied hash table
   */
  else if (keyNum <= table->numKeys)
    {
      key = &table->keys[--keyNum];
      numRecords = key->recordCount;
      indexTable = (htrecord_t *) malloc (numRecords * sizeof (htrecord_t));
      if (indexTable == NULL)
	return DTAB_NO_MEMORY;

      for (ki = 0; ki < key->hashSize; ki++)
	{
	  htelem_t *elem, *nextElem;

	  elem = key->hashTable[ki];
	  while (elem)
	    {
	      nextElem = elem[keyNum].next;
	      indexTable[counter++] = (htrecord_t) elem + table->headerSize;
	      elem = nextElem;
	    }
	}
    }
  else
    return DTAB_INVALID_ARG;

  *pNumRecords = counter;
  *pRecords = indexTable;

  return DTAB_SUCCESS;
}


#ifdef DTAB_DEBUG
int
dtab_debug (dyntable_t table)
{
  u_int ki;
  u_int ri;

  if (table == (httable_t *)0)
    return DTAB_INVALID_ARG;

  printf ("recordSize  = %u\n", table->recordSize);
  printf ("numRecords  = %u\n", table->numRecords);
  printf ("freeRecords = %u\n", table->freeRecords);
  printf ("incrRecords = %u\n", table->incrRecords);
  printf ("maxRecords  = %u\n", table->maxRecords);
  printf ("maxKeys     = %u\n", table->maxKeys);
  printf ("numKeys     = %u\n", table->numKeys);
  printf ("headerSize  = %u\n", table->headerSize);

  printf ("alloc list  = ");
  for (ri = 0; ri < table->numRecords; ri++)
    {
      if (table->records[ri])
	putchar ('1');
      else
	putchar ('0');
    }
  putchar ('\n');

  printf ("keys:\n");
  for (ki = 0; ki < table->numKeys; ki++)
    {
      htkey_t *key = &table->keys[ki];

      printf ("  %u. compareFunc=%08lX hashFunc=%08lX flags=%04X hashSize=%u\n",
	  ki + 1,
	  (u_long) key->compareFunc, (u_long) key->hashFunc, key->flags,
	  key->hashSize);

      for (ri = 0; ri < key->hashSize; ri++)
	{
          htelem_t *hashChain = key->hashTable[ri];
	  htelem_t *elem;

	  if (hashChain == NULL)
	    printf ("    ht[%u] = NULL\n", ri);
	  else
	    {
	      printf ("    ht[%u] =", ri);
	      for (elem = hashChain; elem; elem = elem[ki].next)
		{
		  htrecord_t record = (htrecord_t) elem + table->headerSize;
		  printf (" %08lX", (u_long) record);
		}
	      putchar ('\n');
	    }
	}
    }

  return DTAB_SUCCESS;
}
#endif