-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
4536 lines (3219 loc) · 109 KB
/
list.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(list)> - list module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/list.h>
typedef struct List List;
typedef struct Lister Lister;
typedef void list_release_t(void *item);
typedef void *list_copy_t(const void *item);
typedef int list_cmp_t(const void *a, const void *b);
typedef void list_action_t(void *item, size_t *index, void *data);
typedef void *list_map_t(void *item, size_t *index, void *data);
typedef int list_query_t(void *item, size_t *index, void *data);
List *list_create(list_release_t *destroy);
List *list_make(list_release_t *destroy, ...);
List *list_vmake(list_release_t *destroy, va_list args);
List *list_copy(const List *src, list_copy_t *copy);
List *list_create_with_locker(Locker *locker, list_release_t *destroy);
List *list_make_with_locker(Locker *locker, list_release_t *destroy, ...);
List *list_vmake_with_locker(Locker *locker, list_release_t *destroy, va_list args);
List *list_copy_with_locker(Locker *locker, const List *src, list_copy_t *copy);
int list_rdlock(const List *list);
int list_wrlock(const List *list);
int list_unlock(const List *list);
void list_release(List *list);
void *list_destroy(List **list);
int list_own(List *list, list_release_t *destroy);
int list_own_unlocked(List *list, list_release_t *destroy);
list_release_t *list_disown(List *list);
list_release_t *list_disown_unlocked(List *list);
void *list_item(const List *list, ssize_t index);
void *list_item_unlocked(const List *list, ssize_t index);
int list_item_int(const List *list, ssize_t index);
int list_item_int_unlocked(const List *list, ssize_t index);
int list_empty(const List *list);
int list_empty_unlocked(const List *list);
ssize_t list_length(const List *list);
ssize_t list_length_unlocked(const List *list);
ssize_t list_last(const List *list);
ssize_t list_last_unlocked(const List *list);
List *list_remove(List *list, ssize_t index);
List *list_remove_unlocked(List *list, ssize_t index);
List *list_remove_range(List *list, ssize_t index, ssize_t range);
List *list_remove_range_unlocked(List *list, ssize_t index, ssize_t range);
List *list_insert(List *list, ssize_t index, void *item);
List *list_insert_unlocked(List *list, ssize_t index, void *item);
List *list_insert_int(List *list, ssize_t index, int item);
List *list_insert_int_unlocked(List *list, ssize_t index, int item);
List *list_insert_list(List *list, ssize_t index, const List *src, list_copy_t *copy);
List *list_insert_list_unlocked(List *list, ssize_t index, const List *src, list_copy_t *copy);
List *list_append(List *list, void *item);
List *list_append_unlocked(List *list, void *item);
List *list_append_int(List *list, int item);
List *list_append_int_unlocked(List *list, int item);
List *list_append_list(List *list, const List *src, list_copy_t *copy);
List *list_append_list_unlocked(List *list, const List *src, list_copy_t *copy);
List *list_prepend(List *list, void *item);
List *list_prepend_unlocked(List *list, void *item);
List *list_prepend_int(List *list, int item);
List *list_prepend_int_unlocked(List *list, int item);
List *list_prepend_list(List *list, const List *src, list_copy_t *copy);
List *list_prepend_list_unlocked(List *list, const List *src, list_copy_t *copy);
List *list_replace(List *list, ssize_t index, ssize_t range, void *item);
List *list_replace_unlocked(List *list, ssize_t index, ssize_t range, void *item);
List *list_replace_int(List *list, ssize_t index, ssize_t range, int item);
List *list_replace_int_unlocked(List *list, ssize_t index, ssize_t range, int item);
List *list_replace_list(List *list, ssize_t index, ssize_t range, const List *src, list_copy_t *copy);
List *list_replace_list_unlocked(List *list, ssize_t index, ssize_t range, const List *src, list_copy_t *copy);
List *list_extract(const List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_extract_unlocked(const List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_extract_with_locker(Locker *locker, const List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_extract_with_locker_unlocked(Locker *locker, const List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_push(List *list, void *item);
List *list_push_unlocked(List *list, void *item);
List *list_push_int(List *list, int item);
List *list_push_int_unlocked(List *list, int item);
void *list_pop(List *list);
void *list_pop_unlocked(List *list);
int list_pop_int(List *list);
int list_pop_int_unlocked(List *list);
void *list_shift(List *list);
void *list_shift_unlocked(List *list);
int list_shift_int(List *list);
int list_shift_int_unlocked(List *list);
List *list_unshift(List *list, void *item);
List *list_unshift_unlocked(List *list, void *item);
List *list_unshift_int(List *list, int item);
List *list_unshift_int_unlocked(List *list, int item);
List *list_splice(List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_splice_unlocked(List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_splice_with_locker(Locker *locker, List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_splice_with_locker_unlocked(Locker *locker, List *list, ssize_t index, ssize_t range, list_copy_t *copy);
List *list_sort(List *list, list_cmp_t *cmp);
List *list_sort_unlocked(List *list, list_cmp_t *cmp);
void list_apply(List *list, list_action_t *action, void *data);
void list_apply_rdlocked(List *list, list_action_t *action, void *data);
void list_apply_wrlocked(List *list, list_action_t *action, void *data);
void list_apply_unlocked(List *list, list_action_t *action, void *data);
List *list_map(List *list, list_release_t *destroy, list_map_t *map, void *data);
List *list_map_unlocked(List *list, list_release_t *destroy, list_map_t *map, void *data);
List *list_map_with_locker(Locker *locker, List *list, list_release_t *destroy, list_map_t *map, void *data);
List *list_map_with_locker_unlocked(Locker *locker, List *list, list_release_t *destroy, list_map_t *map, void *data);
List *list_grep(List *list, list_query_t *grep, void *data);
List *list_grep_unlocked(List *list, list_query_t *grep, void *data);
List *list_grep_with_locker(Locker *locker, List *list, list_query_t *grep, void *data);
List *list_grep_with_locker_unlocked(Locker *locker, List *list, list_query_t *grep, void *data);
ssize_t list_query(List *list, ssize_t *index, list_query_t *query, void *data);
ssize_t list_query_unlocked(List *list, ssize_t *index, list_query_t *query, void *data);
Lister *lister_create(List *list);
Lister *lister_create_rdlocked(List *list);
Lister *lister_create_wrlocked(List *list);
Lister *lister_create_unlocked(const List *list);
void lister_release(Lister *lister);
void lister_release_unlocked(Lister *lister);
void *lister_destroy(Lister **lister);
void *lister_destroy_unlocked(Lister **lister);
int lister_has_next(Lister *lister);
void *lister_next(Lister *lister);
int lister_next_int(Lister *lister);
void lister_remove(Lister *lister);
int list_has_next(List *list);
void list_break(List *list);
void *list_next(List *list);
int list_next_int(List *list);
void list_remove_current(List *list);
=head1 DESCRIPTION
This module provides functions for manipulating and iterating over lists of
homogeneous data (or heterogeneous data if it's polymorphic). I<List>s may
own their items. I<List>s created with a non-C<null> destroy function use
that function to destroy an item when it is removed from the list, and to
destroy each item when the list itself is destroyed. Be careful not to
insert items owned by one list into a list that doesn't own its own items
unless you know that the source list (and all of the shared items) will
outlive the destination list.
=over 4
=cut
*/
#include "config.h"
#include "std.h"
#include "list.h"
#include "mem.h"
#include "err.h"
#include "hsort.h"
#include "locker.h"
#define xor(a, b) (!(a) ^ !(b))
#define iff(a, b) !xor(a, b)
#define implies(a, b) (!(a) || (b))
struct List
{
size_t size; /* number of item slots allocated */
size_t length; /* number of items used */
void **list; /* vector of items (void *) */
list_release_t *destroy; /* item destructor, if any */
Lister *lister; /* built-in iterator */
Locker *locker; /* locking strategy for this object */
};
struct Lister
{
List *list; /* the list being iterated over */
ssize_t index; /* the index of the current item */
};
#ifndef TEST
/* Minimum list length: must be a power of 2 */
static const size_t MIN_LIST_SIZE = 4;
/*
C<int grow(List *list, size_t items)>
Allocates enough memory to add C<item> extra items to C<list> if necessary.
On success, returns C<0>. On error, returns C<-1>.
*/
static int grow(List *list, size_t items)
{
int grown = 0;
while (list->length + items > list->size)
{
if (list->size)
list->size <<= 1;
else
list->size = MIN_LIST_SIZE;
grown = 1;
}
if (grown)
return mem_resize(&list->list, list->size) ? 0 : -1;
return 0;
}
/*
C<int shrink(List *list, size_t items)>
Allocates less memory for removing C<items> items from C<list> if necessary.
On success, returns C<0>. On error, returns C<-1>.
*/
static int shrink(List *list, size_t items)
{
int shrunk = 0;
while (list->length - items < list->size >> 1)
{
if (list->size == MIN_LIST_SIZE)
break;
list->size >>= 1;
shrunk = 1;
}
if (shrunk)
return mem_resize(&list->list, list->size) ? 0 : -1;
return 0;
}
/*
C<int expand(List *list, ssize_t index, size_t range)>
Slides C<list>'s items, starting at C<index>, C<range> slots to the right to
make room for more. On success, returns C<0>. On error, returns C<-1>.
*/
static int expand(List *list, ssize_t index, size_t range)
{
if (grow(list, range) == -1)
return -1;
memmove(list->list + index + range, list->list + index, (list->length - index) * sizeof(*list->list));
list->length += range;
return 0;
}
/*
C<int contract(List *list, ssize_t index, size_t range)>
Slides C<list>'s items, starting at C<index> + C<range>, C<range> slots to
the left to close a gap starting at C<index>. On success, returns C<0>. On
error, returns C<-1>.
*/
static int contract(List *list, ssize_t index, size_t range)
{
memmove(list->list + index, list->list + index + range, (list->length - index - range) * sizeof(*list->list));
if (shrink(list, range) == -1)
return -1;
list->length -= range;
return 0;
}
/*
C<int adjust(List *list, ssize_t index, size_t range, size_t length)>
Expands or contracts C<list> as required so that C<list[index + range ..]>
occupies C<list[index + length ..]>. On success, returns C<0>. On error,
returns C<-1>.
*/
static int adjust(List *list, ssize_t index, size_t range, size_t length)
{
if (range < length)
return expand(list, index + range, length - range);
if (range > length)
return contract(list, index + length, range - length);
return 0;
}
/*
C<void killitems(List *list, size_t index, size_t range)>
Destroys the items in C<list> ranging from C<index> to C<range>.
*/
static void killitems(List *list, size_t index, size_t range)
{
while (range--)
{
if (list->destroy)
list->destroy(list->list[index]);
list->list[index++] = NULL;
}
}
/*
=item C<List *list_create(list_release_t *destroy)>
Creates a I<List> with C<destroy> as its item destructor. It is the caller's
responsibility to deallocate the new list with I<list_release(3)> or
I<list_destroy(3)>. It is strongly recommended to use I<list_destroy(3)>,
because it also sets the pointer variable to C<null>. On success, returns the
new list. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
List *list_create(list_release_t *destroy)
{
return list_create_with_locker(NULL, destroy);
}
/*
=item C<List *list_make(list_release_t *destroy, ...)>
Creates a I<List> with C<destroy> as its item destructor and the remaining
arguments as its initial items. It is the caller's responsibility to
deallocate the new list with I<list_release(3)> or I<list_destroy(3)>. It is
strongly recommended to use I<list_destroy(3)>, because it also sets the
pointer variable to C<null>. On success, returns the new list. On error,
return C<null> with C<errno> set appropriately.
=cut
*/
List *list_make(list_release_t *destroy, ...)
{
List *list;
va_list args;
va_start(args, destroy);
list = list_vmake_with_locker(NULL, destroy, args);
va_end(args);
return list;
}
/*
=item C<List *list_vmake(list_release_t *destroy, va_list args)>
Equivalent to I<list_make(3)> with the variable argument list specified
directly as for I<vprintf(3)>.
=cut
*/
List *list_vmake(list_release_t *destroy, va_list args)
{
return list_vmake_with_locker(NULL, destroy, args);
}
/*
=item C<List *list_copy(const List *src, list_copy_t *copy)>
Creates a copy of C<src> using C<copy> as the copy constructor (if not
C<null>). It is the caller's responsibility to deallocate the new list with
I<list_release(3)> or I<list_destroy(3)>. It is strongly recommended to use
I<list_destroy(3)>, because it also sets the pointer variable to C<null>. On
success, returns the new copy. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
List *list_copy(const List *src, list_copy_t *copy)
{
return list_copy_with_locker(NULL, src, copy);
}
/*
=item C<List *list_create_with_locker(Locker *locker, list_release_t *destroy)>
Equivalent to I<list_create(3)> except that multiple threads accessing the
new list will be synchronised by C<locker>.
=cut
*/
List *list_create_with_locker(Locker *locker, list_release_t *destroy)
{
List *list;
if (!(list = mem_new(List))) /* XXX decouple */
return NULL;
list->size = list->length = 0;
list->list = NULL;
list->destroy = destroy;
list->lister = NULL;
list->locker = locker;
return list;
}
/*
=item C<List *list_make_with_locker(Locker *locker, list_release_t *destroy, ...)>
Equivalent to I<list_make(3)> except that multiple threads accessing the new
list will be synchronised by C<locker>.
=cut
*/
List *list_make_with_locker(Locker *locker, list_release_t *destroy, ...)
{
List *list;
va_list args;
va_start(args, destroy);
list = list_vmake_with_locker(locker, destroy, args);
va_end(args);
return list;
}
/*
=item C<List *list_vmake_with_locker(Locker *locker, list_release_t *destroy, va_list args)>
Equivalent to I<list_vmake(3)> except that multiple threads accessing the
new list will be synchronised by C<locker>.
=cut
*/
List *list_vmake_with_locker(Locker *locker, list_release_t *destroy, va_list args)
{
List *list;
void *item;
if (!(list = list_create_with_locker(locker, destroy)))
return NULL;
while ((item = va_arg(args, void *)) != NULL)
{
if (!list_append(list, item))
{
list_release(list);
return NULL;
}
}
return list;
}
/*
=item C<List *list_copy_with_locker(Locker *locker, const List *src, list_copy_t *copy)>
Equivalent to I<list_copy(3)> except that multiple threads accessing the new
list will be synchronised by C<locker>.
=cut
*/
List *list_copy_with_locker(Locker *locker, const List *src, list_copy_t *copy)
{
List *list;
if (!src)
return set_errnull(EINVAL);
if (!(list = list_extract(src, 0, src->length, copy)))
return NULL;
list->locker = locker;
return list;
}
/*
=item C<int list_rdlock(const List *list)>
Claims a read lock on C<list> (if C<list> was created with a I<Locker>).
This is needed when multiple read-only I<list(3)> module functions need to
be called atomically. It is the client's responsibility to call
I<list_unlock(3)> after the atomic operation. The only functions that may be
called on C<list> between calls to I<list_rdlock(3)> and I<list_unlock(3)>
are any read-only I<list(3)> module functions whose name ends with
C<_unlocked>. On success, returns C<0>. On error, returns an error code.
=cut
*/
#define list_rdlock(list) ((list) ? locker_rdlock((list)->locker) : EINVAL)
#define list_wrlock(list) ((list) ? locker_wrlock((list)->locker) : EINVAL)
#define list_unlock(list) ((list) ? locker_unlock((list)->locker) : EINVAL)
int (list_rdlock)(const List *list)
{
return list_rdlock(list);
}
/*
=item C<int list_wrlock(const List *list)>
Claims a write lock on C<list> (if C<list> was created with a I<Locker>).
This is needed when multiple read/write I<list(3)> module functions need to
be called atomically. It is the client's responsibility to call
I<list_unlock(3)> after the atomic operation. The only functions that may be
called on C<list> between calls to I<list_wrlock(3)> and I<list_unlock(3)>
are any I<list(3)> module functions whose name ends with C<_unlocked>. On
success, returns C<0>. On error, returns an error code.
=cut
*/
int (list_wrlock)(const List *list)
{
return list_wrlock(list);
}
/*
=item C<int list_unlock(const List *list)>
Unlocks a read or write lock on C<list> obtained with I<list_rdlock(3)> or
I<list_wrlock(3)> (if C<list> was created with a C<locker>). On success,
returns C<0>. On error, returns an error code.
=cut
*/
int (list_unlock)(const List *list)
{
return list_unlock(list);
}
/*
=item C<void list_release(List *list)>
Releases (deallocates) C<list>, destroying its items if necessary. On error,
sets C<errno> appropriately.
=cut
*/
void list_release(List *list)
{
if (!list)
return;
if (list->list)
{
killitems(list, 0, list->length);
mem_release(list->list);
}
mem_release(list);
}
/*
=item C<void *list_destroy(List **list)>
Destroys (deallocates and sets to C<null>) C<*list>. Returns C<null>.
B<Note:> Lists shared by multiple threads must not be destroyed until after
all threads have finished with it.
=cut
*/
void *list_destroy(List **list)
{
if (list && *list)
{
list_release(*list);
*list = NULL;
}
return NULL;
}
/*
=item C<int list_own(List *list, list_release_t *destroy)>
Causes C<list> to take ownership of its items. The items will be destroyed
using C<destroy> when they are removed or when C<list> is destroyed. On
success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int list_own(List *list, list_release_t *destroy)
{
int err;
if (!list || !destroy)
return set_errno(EINVAL);
if ((err = list_wrlock(list)))
return set_errno(err);
list->destroy = destroy;
if ((err = list_unlock(list)))
return set_errno(err);
return 0;
}
/*
=item C<int list_own_unlocked(List *list, list_release_t *destroy)>
Equivalent to I<list_own(3)> except that C<list> is not write-locked.
=cut
*/
int list_own_unlocked(List *list, list_release_t *destroy)
{
if (!list || !destroy)
return set_errno(EINVAL);
list->destroy = destroy;
return 0;
}
/*
=item C<list_release_t *list_disown(List *list)>
Causes C<list> to relinquish ownership of its items. The items will not be
destroyed when they are removed from C<list> or when C<list> is destroyed.
On success, returns the previous destroy function, if any. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
list_release_t *list_disown(List *list)
{
list_release_t *destroy;
int err;
if (!list)
return (list_release_t *)set_errnullf(EINVAL);
if ((err = list_wrlock(list)))
return (list_release_t *)set_errnullf(err);
destroy = list_disown_unlocked(list);
if ((err = list_unlock(list)))
return (list_release_t *)set_errnullf(err);
return destroy;
}
/*
=item C<list_release_t *list_disown_unlocked(List *list)>
Equivalent to I<list_disown(3)> except that C<list> is not write-locked.
=cut
*/
list_release_t *list_disown_unlocked(List *list)
{
list_release_t *destroy;
if (!list)
return (list_release_t *)set_errnullf(EINVAL);
destroy = list->destroy;
list->destroy = NULL;
return destroy;
}
/*
=item C<void *list_item(const List *list, ssize_t index)>
Returns the C<index>'th item in C<list>. If C<index> is negative, it refers
to an item position relative to the end of the list (C<-1> is the position
after the last item, C<-2> is the position of the last item, and so on). On
error, returns C<null> with C<errno> set appropriately.
=cut
*/
void *list_item(const List *list, ssize_t index)
{
void *item;
int err;
if (!list)
return set_errnull(EINVAL);
if ((err = list_rdlock(list)))
return set_errnull(err);
item = list_item_unlocked(list, index);
if ((err = list_unlock(list)))
return set_errnull(err);
return item;
}
/*
=item C<void *list_item_unlocked(const List *list, ssize_t index)>
Equivalent to I<list_item(3)> except that C<list> is not read-locked.
=cut
*/
void *list_item_unlocked(const List *list, ssize_t index)
{
if (!list)
return set_errnull(EINVAL);
if (index < 0)
index = list->length + 1 + index;
if (index < 0)
return set_errnull(EINVAL);
if (index >= list->length)
return set_errnull(EINVAL);
return list->list[index];
}
/*
=item C<int list_item_int(const List *list, ssize_t index)>
Equivalent to I<list_item(3)> except that the item is cast to an integer
type.
=cut
*/
int list_item_int(const List *list, ssize_t index)
{
return (int)(long)list_item(list, index);
}
/*
=item C<int list_item_int_unlocked(const List *list, ssize_t index)>
Equivalent to I<list_item_int(3)> except that C<list> is not read-locked.
=cut
*/
int list_item_int_unlocked(const List *list, ssize_t index)
{
return (int)(long)list_item_unlocked(list, index);
}
/*
=item C<int list_empty(const List *list)>
Returns whether or not C<list> is empty. On error, returns C<-1> with
C<errno> set appropriately.
=cut
*/
int list_empty(const List *list)
{
int empty;
int err;
if (!list)
return set_errno(EINVAL);
if ((err = list_rdlock(list)))
return set_errno(err);
empty = (list->length == 0);
if ((err = list_unlock(list)))
return set_errno(err);
return empty;
}
/*
=item C<int list_empty_unlocked(const List *list)>
Equivalent to I<list_empty(3)> except that C<list> is not read-locked.
=cut
*/
int list_empty_unlocked(const List *list)
{
if (!list)
return set_errno(EINVAL);
return (list->length == 0);
}
/*
=item C<ssize_t list_length(const List *list)>
Returns the length of C<list>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
ssize_t list_length(const List *list)
{
size_t length;
int err;
if (!list)
return set_errno(EINVAL);
if ((err = list_rdlock(list)))
return set_errno(err);
length = list->length;
if ((err = list_unlock(list)))
return set_errno(err);
return length;
}
/*
=item C<ssize_t list_length_unlocked(const List *list)>
Equivalent to I<list_length(3)> except that C<list> is not read-locked.
=cut
*/
ssize_t list_length_unlocked(const List *list)
{
if (!list)
return set_errno(EINVAL);
return list->length;
}
/*
=item C<ssize_t list_last(const List *list)>
Returns the index of the last item in C<list>, or C<-1> if there are no
items. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
ssize_t list_last(const List *list)
{
ssize_t last;
int err;
if (!list)
return set_errno(EINVAL);
if ((err = list_rdlock(list)))
return set_errno(err);
last = list->length - 1;
if ((err = list_unlock(list)))
return set_errno(err);
return last;
}
/*
=item C<ssize_t list_last_unlocked(const List *list)>
Equivalent to I<list_last(3)> except that C<list> is not read-locked.
=cut
*/
ssize_t list_last_unlocked(const List *list)
{
if (!list)
return set_errno(EINVAL);
return list->length - 1;
}
/*
=item C<List *list_remove(List *list, ssize_t index)>
Removes the C<index>'th item from C<list>. If C<index> is negative, it
refers to an item position relative to the end of the list (C<-1> is the
position after the last item, C<-2> is the position of the last item and so
on). On success, returns C<list>. On error, returns C<null> with C<errno>
set appropriately.
=cut
*/
List *list_remove(List *list, ssize_t index)
{
return list_remove_range(list, index, 1);
}
/*