-
Notifications
You must be signed in to change notification settings - Fork 4
/
hashlist.c
1395 lines (1128 loc) · 31.7 KB
/
hashlist.c
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @addtogroup cp_hashlist
*/
/** @{ */
/**
* @file
* Implementation for cp_hashlist Collection with linked elements and hash key
* and with cp_hashlist_iterator.
*
* @copydoc collection
*/
/* ----------------------------------------------------------------- */
#include <stdio.h> /* debug */
#include <stdlib.h>
#include <errno.h>
#include "collection.h"
#include "log.h"
#include "common.h"
#include "hashlist.h"
#include "linked_list.h"
#include "config.h"
/* debug */
#ifndef CP_HASHLIST_MULTIPLE_VALUES
#define CP_HASHLIST_MULTIPLE_VALUES 1
#endif
/* internal methods */
/* internal lock function */
int cp_hashlist_txlock(cp_hashlist *list, int type);
/* internal unlock function */
int cp_hashlist_txunlock(cp_hashlist *list);
static cp_hashlist_entry *
cp_hashlist_create_entry(cp_hashlist *list, int mode,
void *key, void *value);
static void
cp_hashlist_entry_delete(cp_hashlist_entry *entry);
static void cp_hashlist_entry_release_by_option(cp_hashlist *list,
cp_hashlist_entry *entry,
int mode);
/**
* insert an entry
*
* @param list the collection object
* @param entry the entry to insert
* @return entry the inserted entry
*/
static cp_hashlist_entry *
cp_hashlist_insert_internal(cp_hashlist *list, cp_hashlist_entry *entry);
/**
* remove first entry matching key
*
* @param list the collection object
* @param key that is searched for
* @retval entry if successful the removed entry
* @retval NULL if the entry was not found
*/
static cp_hashlist_entry *
cp_hashlist_remove_internal(cp_hashlist *list, void *key);
/**
* remove specified entry
*
* @param list the collection object
* @param entry the entry to remove
* @retval entry if successful the passed entry
* @retval NULL if the entry was not found
*/
static cp_hashlist_entry *
cp_hashlist_remove_entry_internal(cp_hashlist *list,
cp_hashlist_entry *entry);
static void *cp_hashlist_get_internal(cp_hashlist *list, void *key);
/* end internal methods */
cp_hashlist *
cp_hashlist_create_by_option(int mode,
unsigned long size_hint,
cp_hashfunction hash_fn,
cp_compare_fn compare_fn,
cp_copy_fn copy_key,
cp_destructor_fn free_key,
cp_copy_fn copy_value,
cp_destructor_fn free_value)
{
cp_hashlist *list = (cp_hashlist *) calloc(1, sizeof(cp_hashlist));
if (list == NULL)
{
errno = ENOMEM;
return NULL;
}
list->table_size = cp_hashtable_choose_size(size_hint);
list->items = 0;
list->table = (cp_hashlist_entry **) calloc(list->table_size, sizeof(cp_hashlist_entry *));
if (list->table == NULL)
{
errno = ENOMEM;
return NULL;
}
list->hash_fn = hash_fn;
list->compare_fn = compare_fn;
list->copy_key = NULL;
list->copy_value = NULL;
list->head = list->tail = NULL;
list->lock = calloc(1, sizeof(cp_lock));
if (list->lock == NULL)
{
free(list->table);
free(list);
errno = ENOMEM;
return NULL;
}
if (!(mode & COLLECTION_MODE_NOSYNC))
if (cp_lock_init(list->lock, NULL))
{
free(list->lock);
free(list->table);
free(list);
return NULL;
}
list->mode = mode;
list->copy_key = copy_key;
list->copy_value = copy_value;
list->free_key = free_key;
list->free_value = free_value;
list->min_size = size_hint;
list->fill_factor_min = CP_HASHTABLE_DEFAULT_MIN_FILL_FACTOR;
list->fill_factor_max = CP_HASHTABLE_DEFAULT_MAX_FILL_FACTOR;
return list;
}
cp_hashlist *cp_hashlist_create_by_mode(int mode,
unsigned long size_hint,
cp_hashfunction hash_fn,
cp_compare_fn compare_fn)
{
return
cp_hashlist_create_by_option(mode, size_hint, hash_fn, compare_fn,
NULL, NULL, NULL, NULL);
}
static cp_hashlist_entry *
cp_hashlist_create_entry(cp_hashlist *list, int mode,
void *key, void *value)
{
cp_copy_fn ck = list->copy_key;
cp_copy_fn cv = list->copy_value;
cp_hashlist_entry *entry =
(cp_hashlist_entry *) calloc(1, sizeof(cp_hashlist_entry));
if (entry == NULL)
{
errno = ENOMEM;
return NULL;
}
entry->next = entry->prev = entry->bucket = NULL;
if (mode & COLLECTION_MODE_COPY)
{
entry->key = ck ? (*ck)(key) : key;
entry->value = cv ? (*cv)(value) : value;
}
else
{
entry->key = key;
entry->value = value;
}
return entry;
}
static void
cp_hashlist_destroy_internal(cp_hashlist *list,
int mode,
cp_destructor_fn dk,
cp_destructor_fn dv)
{
int syncbit = list->mode & COLLECTION_MODE_NOSYNC;
if (!syncbit)
{
int rc;
#if (CP_DEBUGLEVEL >= 2)
cp_thread self = cp_thread_self();
if (list->dtr_caller && list->dtr_caller != self)
DEBUGMSG("cp_hashlist_destroy_internal has already been invoked by a different thread: %ld ", list->dtr_caller);
list->dtr_caller = self;
#endif
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE);
}
while (list->head != NULL)
{
cp_hashlist_entry *entry = list->head;
list->head = entry->next;
if (mode & COLLECTION_MODE_DEEP)
{
if (dk) (*dk)(entry->key);
if (dv) (*dv)(entry->value);
}
cp_hashlist_entry_delete(entry);
}
if (!syncbit) cp_hashlist_txunlock(list);
if (list->lock)
{
cp_lock_destroy(list->lock);
free(list->lock);
}
free(list->table);
free(list);
}
void cp_hashlist_destroy(cp_hashlist *list)
{
cp_hashlist_destroy_internal(list, list->mode,
list->free_key, list->free_value);
}
void cp_hashlist_destroy_deep(cp_hashlist *list)
{
cp_hashlist_set_mode(list, COLLECTION_MODE_DEEP);
cp_hashlist_destroy_custom(list, list->free_key, list->free_value);
}
void cp_hashlist_destroy_by_option(cp_hashlist *list, int mode)
{
if (list)
cp_hashlist_destroy_internal(list, mode,
list->free_key, list->free_value);
}
void cp_hashlist_destroy_custom(cp_hashlist *list,
cp_destructor_fn dk,
cp_destructor_fn dv)
{
if (list)
cp_hashlist_destroy_internal(list, list->mode, dk, dv);
}
int cp_hashlist_callback(cp_hashlist *list,
int (*cb)(void *key, void *value, void *id),
void *id)
{
cp_hashlist_entry *entry;
if (list == NULL || cb == NULL)
{
errno = EINVAL;
return -1;
}
if (cp_hashlist_txlock(list, COLLECTION_LOCK_READ)) return -1;
entry = list->head;
while (entry != NULL)
{
if ((*cb)(entry->key, entry->value, id) != 0)
break;
entry = entry->next;
}
cp_hashlist_txunlock(list);
return 0;
}
int cp_hashlist_get_mode(cp_hashlist *list)
{
return list->mode;
}
int cp_hashlist_set_mode(cp_hashlist *list, int mode)
{
int rc = EINVAL;
if (list)
{
int syncbit = list->mode & COLLECTION_MODE_NOSYNC;
/* can't set NOSYNC in the middle of a transaction */
if ((list->mode & COLLECTION_MODE_IN_TRANSACTION) &&
(mode & COLLECTION_MODE_NOSYNC)) return EINVAL;
syncbit = list->mode & COLLECTION_MODE_NOSYNC;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return -1;
list->mode |= mode;
if (!syncbit) cp_hashlist_txunlock(list);
rc = 0;
}
return rc;
}
int cp_hashlist_unset_mode(cp_hashlist *list, int mode)
{
int syncbit;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return -1;
syncbit = list->mode & COLLECTION_MODE_NOSYNC;
list->mode &= list->mode ^ mode;
if (!syncbit) cp_hashlist_txunlock(list);
return 0;
}
int cp_hashlist_set_min_size(cp_hashlist *list, unsigned long min_size)
{
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return -1;
list->min_size = min_size;
cp_hashlist_txunlock(list);
return 0;
}
int cp_hashlist_set_max_fill_factor(cp_hashlist *list, int fill_factor)
{
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return -1;
list->fill_factor_max = fill_factor;
cp_hashlist_txunlock(list);
return 0;
}
int cp_hashlist_set_min_fill_factor(cp_hashlist *list, int fill_factor)
{
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return -1;
list->fill_factor_min = fill_factor;
cp_hashlist_txunlock(list);
return 0;
}
void *cp_hashlist_resize_nosync(cp_hashlist *list, unsigned long new_size)
{
unsigned long old_size;
cp_hashlist_entry **old_table;
cp_hashlist_entry *entry, *next, **insert;
unsigned long i, index;
old_size = list->table_size;
old_table = list->table;
new_size = cp_hashtable_choose_size(new_size);
if (old_size == new_size) /* not enough gap to make a change yet */
return list;
else
list->table_size = new_size;
#ifdef __TRACE__
DEBUGMSG("resizing table (nosync): %d to %d\n", old_size, list->table_size);
#endif
list->table = (cp_hashlist_entry **) calloc(list->table_size, sizeof(cp_hashlist_entry *));
if (list->table == NULL)
{
errno = ENOMEM;
return NULL;
}
for (i = 0; i < old_size; i++)
{
entry = old_table[i];
while (entry != NULL)
{
index = abs((*list->hash_fn)(entry->key)) % list->table_size;
next = entry->bucket;
entry->bucket = NULL;
insert = &list->table[index];
while (*insert != NULL) insert = &(*insert)->bucket;
*insert = entry;
entry = next;
}
}
free(old_table);
return list;
}
void *cp_hashlist_append(cp_hashlist *list, void *key, void *value)
{
return cp_hashlist_append_by_option(list, key, value, list->mode);
}
static int cp_hashlist_contains_internal(cp_hashlist *list, void *key)
{
int rc = 0;
cp_hashlist_entry *curr;
int index = abs((*list->hash_fn)(key)) % list->table_size;
curr = list->table[index];
while (curr != NULL)
{
if ((*list->compare_fn)(key, curr->key) == 0)
{
rc = 1;
break;
}
curr = curr->bucket;
}
return rc;
}
int cp_hashlist_contains(cp_hashlist *list, void *key)
{
int rc = 0;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_READ)) return -1;
rc = cp_hashlist_contains_internal(list, key);
cp_hashlist_txunlock(list);
return rc;
}
void *cp_hashlist_append_by_option(cp_hashlist *list,
void *key,
void *value,
int mode)
{
cp_hashlist_entry *entry;
void *res = NULL;
int rc = 0;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
if ((mode & COLLECTION_MODE_MULTIPLE_VALUES) == 0
&& cp_hashlist_contains_internal(list, key))
{
rc = EINVAL;
goto DONE;
}
if ((entry = cp_hashlist_create_entry(list, mode, key, value)) == NULL)
{
rc = ENOMEM;
goto DONE;
}
cp_hashlist_insert_internal(list, entry);
if (list->tail)
{
entry->prev = list->tail;
list->tail->next = entry;
list->tail = entry;
}
else list->tail = list->head = entry;
res = value;
DONE:
cp_hashlist_txunlock(list);
if (rc) errno = rc;
return res;
}
void *cp_hashlist_insert(cp_hashlist *list, void *key, void *value)
{
return cp_hashlist_insert_by_option(list, key, value, list->mode);
}
void *cp_hashlist_insert_by_option(cp_hashlist *list, void *key,
void *value, int mode)
{
cp_hashlist_entry *entry;
void *res = NULL;
int rc = 0;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
if ((mode & COLLECTION_MODE_MULTIPLE_VALUES) == 0
&& cp_hashlist_contains_internal(list, key))
{
rc = EINVAL;
goto DONE;
}
if ((entry = cp_hashlist_create_entry(list, mode, key, value)) == NULL)
{
rc = errno;
goto DONE;
}
cp_hashlist_insert_internal(list, entry);
if (list->head)
{
entry->next = list->head;
list->head->prev = entry;
list->head = entry;
}
else list->head = list->tail = entry;
res = value;
DONE:
cp_hashlist_txunlock(list);
if (rc) errno = rc;
return res;
}
void *cp_hashlist_get(cp_hashlist *list, void *key)
{
void *res = NULL;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_READ)) return NULL;
res = cp_hashlist_get_internal(list, key);
#if CP_HASHLIST_MULTIPLE_VALUES
if (!(list->mode & COLLECTION_MODE_MULTIPLE_VALUES))
#endif
if (res) res = ((cp_hashlist_entry *) res)->value;
cp_hashlist_txunlock(list);
return res;
}
static void *cp_hashlist_unlink_internal(cp_hashlist *list,
cp_hashlist_entry *holder,
int mode)
{
void *res = NULL;
if (holder)
{
if (holder->next)
holder->next->prev = holder->prev;
else
list->tail = holder->prev;
if (holder->prev)
holder->prev->next = holder->next;
else
list->head = holder->next;
res = holder->value;
cp_hashlist_entry_release_by_option(list, holder, mode);
}
return res;
}
void *cp_hashlist_remove_by_option(cp_hashlist *list, void *key, int mode)
{
void *res;
cp_hashlist_entry *holder;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
holder = cp_hashlist_remove_internal(list, key);
res = cp_hashlist_unlink_internal(list, holder, mode);
cp_hashlist_txunlock(list);
return res;
}
void *cp_hashlist_remove(cp_hashlist *list, void *key)
{
return cp_hashlist_remove_by_option(list, key, list->mode);
}
void *cp_hashlist_remove_deep(cp_hashlist *list, void *key)
{
return
cp_hashlist_remove_by_option(list, key,
list->mode | COLLECTION_MODE_DEEP);
}
void *cp_hashlist_get_head(cp_hashlist *list)
{
void *res;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
res = list->head ? list->head->value : NULL;
cp_hashlist_txunlock(list);
return res;
}
void *cp_hashlist_get_tail(cp_hashlist *list)
{
void *res;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
res = list->tail ? list->tail->value : NULL;
cp_hashlist_txunlock(list);
return res;
}
void *cp_hashlist_remove_head(cp_hashlist *list)
{
return cp_hashlist_remove_head_by_option(list, list->mode);
}
void *cp_hashlist_remove_head_by_option(cp_hashlist *list, int mode)
{
cp_hashlist_entry *entry;
void *res = NULL;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
entry = list->head;
if (entry)
{
if (!cp_hashlist_remove_entry_internal(list, entry)) /* should _not_ happen */
printf("failed to remove item from cp_hashlist table: %s\n", (char *) entry->value);
list->head = list->head->next;
if (list->head)
list->head->prev = NULL;
else
list->tail = NULL;
res = entry->value;
cp_hashlist_entry_release_by_option(list, entry, mode);
}
cp_hashlist_txunlock(list);
return res;
}
void *cp_hashlist_remove_tail(cp_hashlist *list)
{
return cp_hashlist_remove_tail_by_option(list, list->mode);
}
void *cp_hashlist_remove_tail_by_option(cp_hashlist *list, int mode)
{
cp_hashlist_entry *entry;
void *res = NULL;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE)) return NULL;
entry = list->tail;
if (entry)
{
if (!cp_hashlist_remove_entry_internal(list, entry)) /* should _not_ happen */
printf("failed to remove item from cp_hashlist table: %s\n", (char *) entry->value);
list->tail = entry->prev;
if (list->tail)
list->tail->next = NULL;
else
list->head = NULL;
res = entry->value;
cp_hashlist_entry_release_by_option(list, entry, mode);
}
cp_hashlist_txunlock(list);
return res;
}
unsigned long cp_hashlist_item_count(cp_hashlist *list)
{
unsigned long count;
if (cp_hashlist_txlock(list, COLLECTION_LOCK_READ)) return -1;
count = list->items;
cp_hashlist_txunlock(list);
return count;
}
int cp_hashlist_lock_internal(cp_hashlist *list, int lock_mode)
{
int rc = -1;
switch (lock_mode)
{
case COLLECTION_LOCK_READ:
rc = cp_lock_rdlock(list->lock);
break;
case COLLECTION_LOCK_WRITE:
rc = cp_lock_wrlock(list->lock);
break;
case COLLECTION_LOCK_NONE:
rc = 0;
break;
default:
errno = EINVAL;
break;
}
return rc;
}
int cp_hashlist_unlock_internal(cp_hashlist *list)
{
return cp_lock_unlock(list->lock);
}
int cp_hashlist_txlock(cp_hashlist *list, int type)
{
#if (CP_DEBUGLEVEL >= 2)
if (list->dtr_caller)
{
cp_thread dself = cp_thread_self();
if (dself != list->dtr_caller)
{
DEBUGMSG("request for lock after destructor has been called on a differrent thread: %ld", list->dtr_caller);
}
}
#endif
if (list->mode & COLLECTION_MODE_NOSYNC) return 0;
if (list->mode & COLLECTION_MODE_IN_TRANSACTION &&
list->txtype == COLLECTION_LOCK_WRITE)
{
cp_thread self = cp_thread_self();
if (cp_thread_equal(self, list->txowner)) return 0;
}
return cp_hashlist_lock_internal(list, type);
}
int cp_hashlist_txunlock(cp_hashlist *list)
{
#if (CP_DEBUGLEVEL >= 2)
if (list->dtr_caller)
{
cp_thread dself = cp_thread_self();
if (dself != list->dtr_caller)
{
DEBUGMSG("releasing lock after destructor has been called on a differrent thread: %ld", list->dtr_caller);
}
}
#endif
if (list->mode & COLLECTION_MODE_NOSYNC) return 0;
if (list->mode & COLLECTION_MODE_IN_TRANSACTION &&
list->txtype == COLLECTION_LOCK_WRITE)
{
cp_thread self = cp_thread_self();
if (cp_thread_equal(self, list->txowner)) return 0;
}
return cp_hashlist_unlock_internal(list);
}
/* lock and set the transaction indicators */
int cp_hashlist_lock(cp_hashlist *list, int type)
{
int rc;
#if (CP_DEBUGLEVEL >= 2)
if (list->dtr_caller)
{
cp_thread dself = cp_thread_self();
if (dself != list->dtr_caller)
{
DEBUGMSG("request for lock after destructor has been called on a differrent thread: %p", list->dtr_caller);
}
}
#endif
if ((list->mode & COLLECTION_MODE_NOSYNC)) return EINVAL;
if ((rc = cp_hashlist_lock_internal(list, type))) return rc;
list->txtype = type;
list->txowner = cp_thread_self();
list->mode |= COLLECTION_MODE_IN_TRANSACTION;
return 0;
}
/* unset the transaction indicators and unlock */
int cp_hashlist_unlock(cp_hashlist *list)
{
cp_thread self;
#if (CP_DEBUGLEVEL >= 2)
if (list->dtr_caller)
{
cp_thread dself = cp_thread_self();
if (dself != list->dtr_caller)
{
DEBUGMSG("releasing lock after destructor has been called on a differrent thread: %p", list->dtr_caller);
}
}
#endif
if ((list->mode & COLLECTION_MODE_NOSYNC)) return EINVAL;
self = cp_thread_self();
if (list->txowner == self)
{
list->txtype = 0;
list->txowner = 0;
list->mode ^= COLLECTION_MODE_IN_TRANSACTION;
}
else if (list->txtype == COLLECTION_LOCK_WRITE)
return -1;
return cp_hashlist_unlock_internal(list);
}
void *cp_hashlist_entry_get_key(cp_hashlist_entry *entry)
{
return (entry) ? entry->key : NULL;
}
void *cp_hashlist_entry_get_value(cp_hashlist_entry *entry)
{
return (entry) ? entry->value : NULL;
}
int cp_hashlist_is_empty(cp_hashlist *list)
{
return cp_hashlist_item_count(list) == 0;
}
/****************************************************************************
* *
* cp_hashlist_iterator implementation *
* *
****************************************************************************/
cp_hashlist_iterator* cp_hashlist_create_iterator(cp_hashlist *list, int type)
{
int rc = - 1;
cp_hashlist_iterator *iterator =
(cp_hashlist_iterator *) malloc(sizeof(cp_hashlist_iterator));
if (iterator == NULL)
{
errno = CP_MEMORY_ALLOCATION_FAILURE;
return NULL;
}
iterator->list = list;
iterator->pos = &list->head;
iterator->lock_type = type;
switch (type)
{
case COLLECTION_LOCK_READ :
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_READ);
break;
case COLLECTION_LOCK_WRITE :
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE);
break;
default :
rc = 0;
}
if (rc) /* locking failed */
{
free(iterator);
errno = CP_LOCK_FAILED;
iterator = NULL;
}
return iterator;
}
int cp_hashlist_iterator_init(cp_hashlist_iterator *iterator,
cp_hashlist *list, int type)
{
int rc;
iterator->list = list;
iterator->pos = &list->head;
iterator->lock_type = type;
switch (type)
{
case COLLECTION_LOCK_READ :
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_READ);
break;
case COLLECTION_LOCK_WRITE :
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE);
break;
default :
rc = 0;
}
return rc;
}
int cp_hashlist_iterator_head(cp_hashlist_iterator *iterator)
{
if (iterator == NULL) return -1;
iterator->pos = &iterator->list->head;
return 0;
}
int cp_hashlist_iterator_tail(cp_hashlist_iterator *iterator)
{
if (iterator == NULL) return -1;
iterator->pos = &iterator->list->tail;
return 0;
}
int cp_hashlist_iterator_init_tail(cp_hashlist_iterator *iterator, cp_hashlist *list, int type)
{
int rc;
iterator->list = list;
iterator->pos = &list->tail;
iterator->lock_type = type;
switch (type)
{
case COLLECTION_LOCK_READ :
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_READ);
break;
case COLLECTION_LOCK_WRITE :
rc = cp_hashlist_txlock(list, COLLECTION_LOCK_WRITE);
break;
default :
rc = 0;
}
return rc;
}
int cp_hashlist_iterator_to_key(cp_hashlist_iterator *iterator, void *key)
{
cp_hashlist_entry *entry = NULL;
#if CP_HASHLIST_MULTIPLE_VALUES
if ((iterator->list->mode & COLLECTION_MODE_MULTIPLE_VALUES))
{
cp_list *res = cp_hashlist_get_internal(iterator->list, key);
if (res)
{
entry = cp_list_get_head(res);
cp_list_destroy(res);
}
}
else
#endif
entry = cp_hashlist_get_internal(iterator->list, key);
if (entry == NULL) return -1;
if (entry->prev)
iterator->pos = &entry->prev->next;
else
iterator->pos = &iterator->list->head;
return 0;
}
int cp_hashlist_iterator_release(cp_hashlist_iterator *iterator)
{
int rc = 0;
if (iterator->lock_type != COLLECTION_LOCK_NONE)
rc = cp_hashlist_txunlock(iterator->list);
return rc;
}
int cp_hashlist_iterator_destroy(cp_hashlist_iterator *iterator)