-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
3201 lines (2326 loc) · 72.6 KB
/
map.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
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <raf@raf.org>
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <raf@raf.org>
*/
/*
=head1 NAME
I<libslack(map)> - map module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/map.h>
typedef struct Map Map;
typedef struct Mapper Mapper;
typedef struct Mapping Mapping;
typedef list_release_t map_release_t;
typedef list_copy_t map_copy_t;
typedef list_cmp_t map_cmp_t;
typedef size_t map_hash_t(size_t table_size, const void *key);
typedef void map_action_t(void *key, void *item, void *data);
Map *map_create(map_release_t *destroy);
Map *map_create_sized(size_t size, map_release_t *destroy);
Map *map_create_with_hash(map_hash_t *hash, map_release_t *destroy);
Map *map_create_sized_with_hash(size_t size, map_hash_t *hash, map_release_t *destroy);
Map *map_create_with_locker(Locker *locker, map_release_t *destroy);
Map *map_create_with_locker_sized(Locker *locker, size_t size, map_release_t *destroy);
Map *map_create_with_locker_with_hash(Locker *locker, map_hash_t *hash, map_release_t *destroy);
Map *map_create_with_locker_sized_with_hash(Locker *locker, size_t size, map_hash_t *hash, map_release_t *destroy);
Map *map_create_generic(map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy);
Map *map_create_generic_sized(size_t size, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy);
Map *map_create_generic_with_locker(Locker *locker, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy);
Map *map_create_generic_with_locker_sized(Locker *locker, size_t size, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy);
int map_rdlock(const Map *map);
int map_wrlock(const Map *map);
int map_unlock(const Map *map);
void map_release(Map *map);
void *map_destroy(Map **map);
int map_own(Map *map, map_release_t *destroy);
int map_own_unlocked(Map *map, map_release_t *destroy);
map_release_t *map_disown(Map *map);
map_release_t *map_disown_unlocked(Map *map);
int map_add(Map *map, const void *key, void *value);
int map_add_unlocked(Map *map, const void *key, void *value);
int map_put(Map *map, const void *key, void *value);
int map_put_unlocked(Map *map, const void *key, void *value);
int map_insert(Map *map, const void *key, void *value, int replace);
int map_insert_unlocked(Map *map, const void *key, void *value, int replace);
int map_remove(Map *map, const void *key);
int map_remove_unlocked(Map *map, const void *key);
void *map_get(Map *map, const void *key);
void *map_get_unlocked(const Map *map, const void *key);
Mapper *mapper_create(Map *map);
Mapper *mapper_create_rdlocked(Map *map);
Mapper *mapper_create_wrlocked(Map *map);
Mapper *mapper_create_unlocked(Map *map);
void mapper_release(Mapper *mapper);
void mapper_release_unlocked(Mapper *mapper);
void *mapper_destroy(Mapper **mapper);
void *mapper_destroy_unlocked(Mapper **mapper);
int mapper_has_next(Mapper *mapper);
void *mapper_next(Mapper *mapper);
const Mapping *mapper_next_mapping(Mapper *mapper);
void mapper_remove(Mapper *mapper);
int map_has_next(Map *map);
void map_break(Map *map);
void *map_next(Map *map);
const Mapping *map_next_mapping(Map *map);
void map_remove_current(Map *map);
const void *mapping_key(const Mapping *mapping);
const void *mapping_value(const Mapping *mapping);
List *map_keys(Map *map);
List *map_keys_unlocked(Map *map);
List *map_keys_with_locker(Locker *locker, Map *map);
List *map_keys_with_locker_unlocked(Locker *locker, Map *map);
List *map_values(Map *map);
List *map_values_unlocked(Map *map);
List *map_values_with_locker(Locker *locker, Map *map);
List *map_values_with_locker_unlocked(Locker *locker, Map *map);
void map_apply(Map *map, map_action_t *action, void *data);
void map_apply_rdlocked(Map *map, map_action_t *action, void *data);
void map_apply_wrlocked(Map *map, map_action_t *action, void *data);
void map_apply_unlocked(Map *map, map_action_t *action, void *data);
ssize_t map_size(Map *map);
ssize_t map_size_unlocked(const Map *map);
=head1 DESCRIPTION
This module provides functions for manipulating and iterating over a set of
mappings from one object to another object, also known as hashes or
associative arrays. I<Map>s may own their items. I<Map>s created with a
non-C<null> destroy function use that function to destroy an item when it is
removed from the map and to destroy each item when the map itself it
destroyed. I<Map>s are hash tables with C<11> buckets by default. They grow
when necessary, approximately doubling in size each time up to a maximum
size of C<26,214,401> buckets.
=over 4
=cut
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* For snprintf() on OpenBSD-4.7 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */
#endif
#include "config.h"
#include "std.h"
#include "map.h"
#include "mem.h"
#include "err.h"
#include "locker.h"
struct Map
{
size_t size; /* number of buckets */
size_t items; /* number of items */
List **chain; /* array of hash buckets */
map_hash_t *hash; /* hash function */
map_copy_t *copy; /* key copy function */
map_cmp_t *cmp; /* key comparison function */
map_release_t *key_destroy; /* destructor function for keys */
map_release_t *value_destroy; /* destructor function for items */
Mapper *mapper; /* built-in iterator */
Locker *locker; /* locking strategy for this object */
};
struct Mapping
{
void *key; /* a map key */
void *value; /* a map value */
map_release_t *key_destroy; /* destructor function for key */
map_release_t *value_destroy; /* destructor function for value */
};
struct Mapper
{
Map *map; /* the map being iterated over */
ssize_t chain_index; /* the index of the chain of the current item */
ssize_t item_index; /* the index of the current item */
ssize_t next_chain_index; /* the index of the chain of the next item */
ssize_t next_item_index; /* the index of the next item */
};
#ifndef TEST
/* Increasing sequence of valid (i.e. prime) table sizes to choose from. */
static const size_t table_sizes[] =
{
11, 23, 47, 101, 199, 401, 797, 1601, 3203, 6397, 12799, 25601,
51199, 102397, 204803, 409597, 819187, 1638431, 3276799, 6553621,
13107197, 26214401
};
static const size_t num_table_sizes = sizeof(table_sizes) / sizeof(table_sizes[0]);
/* Average bucket length threshold that must be reached before a map grows */
static const double table_resize_factor = 2.0;
#if 0
/*
C<size_t hash(size_t size, const void *key)>
JPW hash function. Returns a hash value (in the range 0..size-1) for C<key>.
*/
static size_t hash(size_t size, const void *key)
{
unsigned char *k = key;
size_t g, h = 0;
while (*k)
if ((g = (h <<= 4, h += *k++) & 0xf0000000))
h ^= (g >> 24) ^ g;
return h % size;
}
#endif
/*
C<size_t hash(size_t size, const void *key)>
Hash function from The Practice of Programming by Kernighan and Pike (p57).
Returns a hash value (in the range 0..size-1) for C<key>.
*/
static size_t hash(size_t size, const void *key)
{
const unsigned char *k = key;
size_t h = 0;
while (*k)
h *= 31, h += *k++;
return h % size;
}
/*
C<Mapping *mapping_create(const void *key, void *value, map_release_t *destroy)>
Creates a new mapping from C<key> to C<value>. C<destroy> is the destructor
function for C<value>. On success, returns the new mapping. On error,
returns C<null> with C<errno> set appropriately.
*/
static Mapping *mapping_create(void *key, void *value, map_release_t *key_destroy, map_release_t *value_destroy)
{
Mapping *mapping;
if (!(mapping = mem_new(Mapping))) /* XXX decouple */
return NULL;
mapping->key = key;
mapping->value = value;
mapping->key_destroy = key_destroy;
mapping->value_destroy = value_destroy;
return mapping;
}
/*
C<void mapping_release(Mapping *mapping)>
Releases (deallocates) C<mapping>, destroying its value if necessary.
*/
static void mapping_release(Mapping *mapping)
{
if (!mapping)
return;
if (mapping->key_destroy)
mapping->key_destroy(mapping->key);
if (mapping->value_destroy)
mapping->value_destroy(mapping->value);
mem_release(mapping);
}
/*
=item C<Map *map_create(map_release_t *destroy)>
Creates a small I<Map> with string keys and C<destroy> as its item
destructor. It is the caller's responsibility to deallocate the new map with
I<map_release(3)> or I<map_destroy(3)>. It is strongly recommended to use
I<map_destroy(3)>, because it also sets the pointer variable to C<null>. On
success, returns the new map. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
Map *map_create(map_release_t *destroy)
{
return map_create_sized_with_hash(table_sizes[0], (map_hash_t *)hash, destroy);
}
/*
=item C<Map *map_create_sized(size_t size, map_release_t *destroy)>
Equivalent to I<map_create(3)> except that the initial number of buckets is
approximately C<size>. The actual size will be the first prime greater than
or equal to C<size> in a prebuilt sequence of primes between C<11> and
C<26,214,401> that double at each step.
=cut
*/
Map *map_create_sized(size_t size, map_release_t *destroy)
{
return map_create_sized_with_hash(size, (map_hash_t *)hash, destroy);
}
/*
=item C<Map *map_create_with_hash(map_hash_t *hash, map_release_t *destroy)>
Equivalent to I<map_create(3)> except that C<hash> is used as the hash
function. The arguments to C<hash> are a I<size_t> specifying the number of
buckets, and a I<const void *> specifying the key to hash. It must return a
I<size_t> between zero and the table size - 1.
=cut
*/
Map *map_create_with_hash(map_hash_t *hash, map_release_t *destroy)
{
return map_create_sized_with_hash(table_sizes[0], hash, destroy);
}
/*
=item C<Map *map_create_sized_with_hash(size_t size, map_hash_t *hash, map_release_t *destroy)>
Equivalent to I<map_create_sized(3)> except that C<hash> is used as the hash
function. The arguments to C<hash> are a I<size_t> specifying the number of
buckets, and a I<const void *> specifying the key to hash. It must return a
I<size_t> between zero and the table size - 1.
=cut
*/
Map *map_create_sized_with_hash(size_t size, map_hash_t *hash, map_release_t *destroy)
{
return map_create_generic_sized(size, (map_copy_t *)mem_strdup, (map_cmp_t *)strcmp, hash, (map_release_t *)free, destroy);
}
/*
=item C<Map *map_create_with_locker(Locker *locker, map_release_t *destroy)>
Equivalent to I<map_create(3)> except that multiple threads accessing the
new map will be synchronised by C<locker>.
=cut
*/
Map *map_create_with_locker(Locker *locker, map_release_t *destroy)
{
return map_create_with_locker_sized_with_hash(locker, table_sizes[0], (map_hash_t *)hash, destroy);
}
/*
=item C<Map *map_create_with_locker_sized(Locker *locker, size_t size, map_release_t *destroy)>
Equivalent to I<map_create_sized(3)> except that multiple threads accessing
the new map will be synchronised by C<locker>.
=cut
*/
Map *map_create_with_locker_sized(Locker *locker, size_t size, map_release_t *destroy)
{
return map_create_with_locker_sized_with_hash(locker, size, (map_hash_t *)hash, destroy);
}
/*
=item C<Map *map_create_with_locker_with_hash(Locker *locker, map_hash_t *hash, map_release_t *destroy)>
Equivalent to I<map_create_with_hash(3)> except that multiple threads
accessing the new map will be synchronised by C<locker>.
=cut
*/
Map *map_create_with_locker_with_hash(Locker *locker, map_hash_t *hash, map_release_t *destroy)
{
return map_create_with_locker_sized_with_hash(locker, table_sizes[0], hash, destroy);
}
/*
=item C<Map *map_create_with_locker_sized_with_hash(Locker *locker, size_t size, map_hash_t *hash, map_release_t *destroy)>
Equivalent to I<map_create_sized_with_hash(3)> except that multiple threads
accessing the new map will be synchronised by C<locker>.
=cut
*/
Map *map_create_with_locker_sized_with_hash(Locker *locker, size_t size, map_hash_t *hash, map_release_t *destroy)
{
return map_create_generic_with_locker_sized(locker, size, (map_copy_t *)mem_strdup, (map_cmp_t *)strcmp, hash, (map_release_t *)free, destroy);
}
/*
=item C<Map *map_create_generic(map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)>
Equivalent to I<map_create(3)> except that the mapping keys can be of any
type. C<copy> is used to copy mapping keys. The argument to C<copy> is the
key to be copied. It must return a copy of its argument. C<cmp> is used to
compare mapping keys. The arguments to C<cmp> are two keys to be compared.
It must return < 0 if the first compares less than the second, 0 if they
compare equal and > 0 if the first compares greater than the second. C<hash>
is the hash function. The arguments to C<hash> are a I<size_t> specifying
the number of buckets, and a I<const void *> specifying the key to hash. It
must return a I<size_t> between zero and the table size - 1. C<key_destroy>
is the destructor for mapping keys. C<value_destroy> is the destructor for
mapping values. On success, returns the new map. On error, returns C<null>
with C<errno> set appropriately.
=cut
*/
Map *map_create_generic(map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)
{
return map_create_generic_with_locker_sized(NULL, table_sizes[0], copy, cmp, hash, key_destroy, value_destroy);
}
/*
=item C<Map *map_create_generic_sized(size_t size, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)>
Equivalent to I<map_create_generic(3)> except that the initial number of
buckets is approximately C<size>. The actual size will be the first prime
greater than or equal to C<size> in a prebuilt sequence of primes between C<11>
and C<26,214,401> that double at each step.
=cut
*/
Map *map_create_generic_sized(size_t size, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)
{
return map_create_generic_with_locker_sized(NULL, size, copy, cmp, hash, key_destroy, value_destroy);
}
/*
=item C<Map *map_create_generic_with_locker(Locker *locker, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)>
Equivalent to I<map_create_generic(3)> except that multiple threads
accessing the new map will be synchronised by C<locker>.
=cut
*/
Map *map_create_generic_with_locker(Locker *locker, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)
{
return map_create_generic_with_locker_sized(locker, table_sizes[0], copy, cmp, hash, key_destroy, value_destroy);
}
/*
=item C<Map *map_create_generic_with_locker_sized(Locker *locker, size_t size, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)>
Equivalent to I<map_create_generic_sized(3)> except that multiple threads
accessing the new map will be synchronised by C<locker>.
=cut
*/
Map *map_create_generic_with_locker_sized(Locker *locker, size_t size, map_copy_t *copy, map_cmp_t *cmp, map_hash_t *hash, map_release_t *key_destroy, map_release_t *value_destroy)
{
Map *map;
size_t i;
for (i = 0; i < num_table_sizes; ++i)
{
if (table_sizes[i] >= size)
{
size = table_sizes[i];
break;
}
}
if (i == num_table_sizes)
return set_errnull(EINVAL);
if (!(map = mem_new(Map))) /* XXX decouple */
return NULL;
if (!(map->chain = mem_create(size, List *)))
{
mem_release(map);
return NULL;
}
map->size = size;
map->items = 0;
memset(map->chain, 0, map->size * sizeof(List *));
map->hash = hash;
map->copy = copy;
map->cmp = cmp;
map->key_destroy = key_destroy;
map->value_destroy = value_destroy;
map->mapper = NULL;
map->locker = locker;
return map;
}
/*
=item C<int map_rdlock(const Map *map)>
Claims a read lock on C<map> (if C<map> was created with a I<Locker>). This
is needed when multiple read-only I<map(3)> module functions need to be
called atomically. It is the client's responsibility to call
I<map_unlock(3)> after the atomic operation. The only functions that may be
called on C<map> between calls to I<map_rdlock(3)> and I<map_unlock(3)> are
any read-only I<map(3)> module functions whose name ends with C<_unlocked>.
On success, returns C<0>. On error, returns an error code.
=cut
*/
#define map_rdlock(map) ((map) ? locker_rdlock((map)->locker) : EINVAL)
#define map_wrlock(map) ((map) ? locker_wrlock((map)->locker) : EINVAL)
#define map_unlock(map) ((map) ? locker_unlock((map)->locker) : EINVAL)
int (map_rdlock)(const Map *map)
{
return map_rdlock(map);
}
/*
=item C<int map_wrlock(const Map *map)>
Claims a write lock on C<map> (if C<map> was created with a I<Locker>). This
is needed when multiple read/write I<map(3)> module functions need to be
called atomically. It is the client's responsibility to subsequently call
I<map_unlock(3)>. The only functions that may be called on C<map> between
calls to I<map_wrlock(3)> and I<map_unlock(3)> are any I<map(3)> module
functions whose name ends with C<_unlocked>. On success, returns C<0>. On
error, returns an error code.
=cut
*/
int (map_wrlock)(const Map *map)
{
return map_wrlock(map);
}
/*
=item C<int map_unlock(const Map *map)>
Unlocks a read or write lock on C<map> obtained with I<map_rdlock(3)> or
I<map_wrlock(3)> (if C<map> was created with a C<locker>). On success,
returns C<0>. On error, returns an error code.
=cut
*/
int (map_unlock)(const Map *map)
{
return map_unlock(map);
}
/*
=item C<void map_release(Map *map)>
Releases (deallocates) C<map>, destroying its items if necessary. On error,
sets C<errno> appropriately.
=cut
*/
void map_release(Map *map)
{
size_t i;
if (!map)
return;
for (i = 0; i < map->size; ++i)
list_release(map->chain[i]);
mem_release(map->chain);
mem_release(map);
}
/*
=item C<void *map_destroy(Map **map)>
Destroys (deallocates and sets to C<null>) C<*map>. Returns C<null>.
B<Note:> maps shared by multiple threads must not be destroyed until after
all threads have finished with it.
=cut
*/
void *map_destroy(Map **map)
{
if (map && *map)
{
map_release(*map);
*map = NULL;
}
return NULL;
}
/*
=item C<int map_own(Map *map, map_release_t *destroy)>
Causes C<map> to take ownership of its items. The items will be destroyed
using C<destroy> when their mappings are removed from C<map> or when C<map>
is destroyed. On success, returns C<0>. On error, returns C<-1> with
C<errno> set appropriately.
=cut
*/
int map_own(Map *map, map_release_t *destroy)
{
int ret;
int err;
if (!map || !destroy)
return set_errno(EINVAL);
if ((err = map_wrlock(map)))
return set_errno(err);
ret = map_own_unlocked(map, destroy);
if ((err = map_unlock(map)))
return set_errno(err);
return ret;
}
/*
=item C<int map_own_unlocked(Map *map, map_release_t *destroy)>
Equivalent to I<map_own(3)> except that C<map> is not write-locked.
=cut
*/
int map_own_unlocked(Map *map, map_release_t *destroy)
{
ssize_t length;
size_t c, i;
if (!map || !destroy)
return set_errno(EINVAL);
if (destroy == map->value_destroy)
return 0;
map->value_destroy = destroy;
for (c = 0; c < map->size; ++c)
{
List *chain = map->chain[c];
if (!chain)
continue;
if ((length = list_length_unlocked(chain)) == -1)
return -1;
for (i = 0; i < length; ++i)
{
Mapping *mapping = (Mapping *)list_item_unlocked(chain, i);
mapping->value_destroy = destroy;
}
}
return 0;
}
/*
=item C<map_release_t *map_disown(Map *map)>
Causes C<map> to relinquish ownership of its items. The items will not be
destroyed when their mappings are removed from C<map> or when C<map> is
destroyed. On success, returns the previous destroy function, if any. On
error, returns C<null> with C<errno> set appropriately.
=cut
*/
map_release_t *map_disown(Map *map)
{
map_release_t *ret;
int err;
if (!map)
return (map_release_t *)set_errnullf(EINVAL);
if ((err = map_wrlock(map)))
return (map_release_t *)set_errnullf(err);
ret = map_disown_unlocked(map);
if ((err = map_unlock(map)))
return (map_release_t *)set_errnullf(err);
return ret;
}
/*
=item C<map_release_t *map_disown_unlocked(Map *map)>
Equivalent to I<map_disown(3)> except that C<map> is not write-locked.
=cut
*/
map_release_t *map_disown_unlocked(Map *map)
{
ssize_t length;
size_t c, i;
map_release_t *destroy;
if (!map)
return (map_release_t *)set_errnullf(EINVAL);
if (!map->value_destroy)
return NULL;
destroy = map->value_destroy;
map->value_destroy = NULL;
for (c = 0; c < map->size; ++c)
{
List *chain = map->chain[c];
if (!chain)
continue;
if ((length = list_length_unlocked(chain)) == -1)
return NULL;
for (i = 0; i < length; ++i)
{
Mapping *mapping = (Mapping *)list_item_unlocked(chain, i);
mapping->value_destroy = NULL;
}
}
return destroy;
}
/*
C<static int map_resize(Map *map)>
Resizes C<map> to use the next prime in a prebuilt sequence of primes
between C<11> and C<26,214,401> that is greater than the current size. On
success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
*/
static int map_resize(Map *map)
{
size_t size = 0;
size_t i;
Mapper *mapper;
Map *new_map;
if (!map)
return set_errno(EINVAL);
for (i = 1; i < num_table_sizes; ++i)
{
if (table_sizes[i] > map->size)
{
size = table_sizes[i];
break;
}
}
if (i == num_table_sizes || size == 0)
return set_errno(EINVAL);
if (!(new_map = map_create_generic_sized(size, map->copy, map->cmp, map->hash, map->key_destroy, map->value_destroy)))
return -1;
if (!(mapper = mapper_create_unlocked(map)))
{
map_release(new_map);
return -1;
}
while (mapper_has_next(mapper) == 1)
{
const Mapping *mapping = mapper_next_mapping(mapper);
if (map_add_unlocked(new_map, mapping->key, mapping->value) == -1)
{
mapper_release_unlocked(mapper);
map_release(new_map);
return -1;
}
}
mapper_release_unlocked(mapper);
errno = 0;
if (map_disown_unlocked(map) == NULL && errno)
{
map_release(new_map);
return -1;
}
for (i = 0; i < map->size; ++i)
list_release(map->chain[i]);
mem_release(map->chain);
map->size = new_map->size;
map->items = new_map->items;
map->chain = new_map->chain;
map->value_destroy = new_map->value_destroy;
mem_release(new_map);
return 0;
}
/*
=item C<int map_add(Map *map, const void *key, void *value)>
Adds the C<(key, value)> mapping to C<map>, if C<key> is not already
present. Note that C<key> is copied but C<value> is not. On success, returns
C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int map_add(Map *map, const void *key, void *value)
{
return map_insert(map, key, value, 0);
}
/*
=item C<int map_add_unlocked(Map *map, const void *key, void *value)>
Equivalent to I<map_add(3)> except that C<map> is not write-locked.
=cut
*/
int map_add_unlocked(Map *map, const void *key, void *value)
{
return map_insert_unlocked(map, key, value, 0);
}
/*
=item C<int map_put(Map *map, const void *key, void *value)>
Adds the C<(key, value)> mapping to C<map>, replacing any existing C<(key,
value)> mapping. Note that C<key> is copied but C<value> is not. On success,
returns C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int map_put(Map *map, const void *key, void *value)
{
return map_insert(map, key, value, 1);
}
/*
=item C<int map_put_unlocked(Map *map, const void *key, void *value)>
Equivalent to I<map_put(3)> except that C<map> is not write-locked.
=cut
*/
int map_put_unlocked(Map *map, const void *key, void *value)
{
return map_insert_unlocked(map, key, value, 1);
}
/*
=item C<int map_insert(Map *map, const void *key, void *value, int replace)>
Adds the C<(key, value)> mapping to C<map>, replacing any existing C<(key,
value)> mapping, if C<replace> is non-zero. Note that C<key> is copied but
C<value> is not. On success, returns C<0>. On error, or if C<key> is already
present and C<replace> is zero, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int map_insert(Map *map, const void *key, void *value, int replace)
{
int ret;
int err;
if (!map || !key)
return set_errno(EINVAL);
if ((err = map_wrlock(map)))
return set_errno(err);
ret = map_insert_unlocked(map, key, value, replace);
if ((err = map_unlock(map)))
return set_errno(err);
return ret;
}
/*
=item C<int map_insert_unlocked(Map *map, const void *key, void *value, int replace)>
Equivalent to I<map_insert(3)> except that C<map> is not write-locked.
=cut
*/
int map_insert_unlocked(Map *map, const void *key, void *value, int replace)
{
Mapping *mapping;
List *chain;
ssize_t length;
size_t h, c;
if (!map || !key)
return set_errno(EINVAL);
if ((double)map->items / (double)map->size >= (double)table_resize_factor)
if (map_resize(map) == -1)
return -1;
if ((h = map->hash(map->size, key)) >= map->size)
return set_errno(EINVAL);
if (!map->chain[h] && !(map->chain[h] = list_create((map_release_t *)mapping_release)))
return -1;
chain = map->chain[h];
if ((length = list_length_unlocked(chain)) == -1)
return -1;
for (c = 0; c < length; ++c)
{
mapping = (Mapping *)list_item_unlocked(chain, c);