ztmux 3.7.17

A Rust port of tmux — the full terminal multiplexer, server and client
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
use core::ptr::null_mut;
use std::ptr::NonNull;

pub trait ListEntry<T, Discriminant = ()> {
    unsafe fn field(this: *mut Self) -> *mut list_entry<T>;
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct list_head<T> {
    pub lh_first: *mut T,
}
pub const fn list_head_initializer<T>() -> list_head<T> {
    list_head {
        lh_first: null_mut(),
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct list_entry<T> {
    pub le_next: *mut T,
    pub le_prev: *mut *mut T,
}

impl<T> Default for list_entry<T> {
    fn default() -> Self {
        Self { le_next: Default::default(), le_prev: Default::default() }
    }
}

pub unsafe fn list_first<T>(head: *mut list_head<T>) -> *mut T {
    unsafe { (*head).lh_first }
}


pub unsafe fn list_next<T, Discriminant>(elm: *mut T) -> *mut T
where
    T: ListEntry<T, Discriminant>,
{
    unsafe { (*ListEntry::field(elm)).le_next }
}

pub unsafe fn list_foreach<T, D>(head: *mut list_head<T>) -> ListIterator<T, D>
where
    T: ListEntry<T, D>,
{
    ListIterator {
        curr: unsafe { NonNull::new(list_first(head)) },
        _phantom: std::marker::PhantomData,
    }
}

// this implementation can be used in place of safe and non-safe
pub struct ListIterator<T, D> {
    curr: Option<NonNull<T>>,
    _phantom: std::marker::PhantomData<D>,
}
impl<T, D> Iterator for ListIterator<T, D>
where
    T: ListEntry<T, D>,
{
    type Item = NonNull<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let curr = self.curr?.as_ptr();
        std::mem::replace(&mut self.curr, NonNull::new(unsafe { list_next(curr) }))
    }
}

pub unsafe fn list_insert_head<T, D>(head: *mut list_head<T>, elm: *mut T)
where
    T: ListEntry<T, D>,
{
    unsafe {
        (*ListEntry::field(elm)).le_next = (*head).lh_first;
        if !(*ListEntry::field(elm)).le_next.is_null() {
            (*ListEntry::field((*head).lh_first)).le_prev =
                &raw mut (*ListEntry::field(elm)).le_next;
        }
        (*head).lh_first = elm;
        (*ListEntry::field(elm)).le_prev = &raw mut (*head).lh_first;
    }
}

pub unsafe fn list_remove<T, D>(elm: *mut T)
where
    T: ListEntry<T, D>,
{
    unsafe {
        if !(*ListEntry::field(elm)).le_next.is_null() {
            (*ListEntry::field((*ListEntry::field(elm)).le_next)).le_prev =
                (*ListEntry::field(elm)).le_prev;
        }
        *(*ListEntry::field(elm)).le_prev = (*ListEntry::field(elm)).le_next;
    }
}

// tailq

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct tailq_head<T> {
    pub tqh_first: *mut T,
    pub tqh_last: *mut *mut T,
}

macro_rules! TAILQ_HEAD_INITIALIZER {
    ($ident:ident) => {
        $crate::compat::queue::tailq_head {
            tqh_first: null_mut(),
            tqh_last: unsafe { &raw mut $ident.tqh_first },
        }
    };
}
pub(crate) use TAILQ_HEAD_INITIALIZER;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct tailq_entry<T> {
    pub tqe_next: *mut T,
    pub tqe_prev: *mut *mut T,
}

impl<T> Default for tailq_entry<T> {
    fn default() -> Self {
        Self {
            tqe_next: null_mut(),
            tqe_prev: null_mut(),
        }
    }
}

impl<T> std::fmt::Debug for tailq_entry<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("tailq_entry")
            .field("tqe_next", &self.tqe_next)
            .field("tqe_prev", &self.tqe_prev)
            .finish()
    }
}

pub trait Entry<T, Discriminant = ()> {
    unsafe fn entry(this: *mut Self) -> *mut tailq_entry<T>;
}

pub unsafe fn tailq_init<T>(head: *mut tailq_head<T>) {
    unsafe {
        (*head).tqh_first = core::ptr::null_mut();
        (*head).tqh_last = &raw mut (*head).tqh_first;
    }
}

pub fn tailq_init_<T>(head: &mut tailq_head<T>) {
    head.tqh_first = core::ptr::null_mut();
    head.tqh_last = &raw mut head.tqh_first;
}

pub unsafe fn tailq_first<T>(head: *mut tailq_head<T>) -> *mut T {
    unsafe { (*head).tqh_first }
}

pub unsafe fn tailq_next<T, Q, D>(elm: *mut T) -> *mut Q
where
    T: Entry<Q, D>,
{
    unsafe { (*Entry::entry(elm)).tqe_next }
}

pub unsafe fn tailq_last<T>(head: *mut tailq_head<T>) -> *mut T {
    unsafe { *(*(*head).tqh_last.cast::<tailq_head<T>>()).tqh_last }
}

pub unsafe fn tailq_prev<T, Q, D>(elm: *mut T) -> *mut Q
where
    T: Entry<Q, D>,
{
    unsafe {
        let head: *mut tailq_head<Q> = (*Entry::entry(elm)).tqe_prev.cast();
        *(*head).tqh_last
    }
}

pub unsafe fn tailq_empty<T>(head: *const tailq_head<T>) -> bool {
    unsafe { (*head).tqh_first.is_null() }
}

pub unsafe fn tailq_insert_head<T, D>(head: *mut tailq_head<T>, elm: *mut T)
where
    T: Entry<T, D>,
{
    unsafe {
        (*T::entry(elm)).tqe_next = (*head).tqh_first;

        if !(*T::entry(elm)).tqe_next.is_null() {
            (*T::entry((*head).tqh_first)).tqe_prev = &raw mut (*T::entry(elm)).tqe_next;
        } else {
            (*head).tqh_last = &raw mut (*T::entry(elm)).tqe_next;
        }

        (*head).tqh_first = elm;
        (*T::entry(elm)).tqe_prev = &raw mut (*head).tqh_first;
    }
}

pub unsafe fn tailq_insert_tail<T, D>(head: *mut tailq_head<T>, elm: *mut T)
where
    T: Entry<T, D>,
{
    unsafe {
        (*Entry::<_, D>::entry(elm)).tqe_next = null_mut();
        (*Entry::<_, D>::entry(elm)).tqe_prev = (*head).tqh_last;
        *(*head).tqh_last = elm;
        (*head).tqh_last = &raw mut (*Entry::<_, D>::entry(elm)).tqe_next;
    }
}

pub unsafe fn tailq_insert_after<T, D>(head: *mut tailq_head<T>, listelm: *mut T, elm: *mut T)
where
    T: Entry<T, D>,
{
    unsafe {
        (*T::entry(elm)).tqe_next = (*T::entry(listelm)).tqe_next;

        if !(*T::entry(elm)).tqe_next.is_null() {
            (*T::entry((*T::entry(elm)).tqe_next)).tqe_prev = &raw mut (*T::entry(elm)).tqe_next;
        } else {
            (*head).tqh_last = &raw mut (*T::entry(elm)).tqe_next;
        }

        (*T::entry(listelm)).tqe_next = elm;
        (*T::entry(elm)).tqe_prev = &raw mut (*T::entry(listelm)).tqe_next;
    }
}

pub unsafe fn tailq_insert_before<T, D>(listelm: *mut T, elm: *mut T)
where
    T: Entry<T, D>,
{
    unsafe {
        (*T::entry(elm)).tqe_prev = (*T::entry(listelm)).tqe_prev;
        (*T::entry(elm)).tqe_next = listelm;
        *(*T::entry(listelm)).tqe_prev = elm;
        (*T::entry(listelm)).tqe_prev = &raw mut (*T::entry(elm)).tqe_next;
    }
}

pub unsafe fn tailq_remove<T, D>(head: *mut tailq_head<T>, elm: *mut T)
where
    T: Entry<T, D>,
{
    unsafe {
        if !(*Entry::<_, D>::entry(elm)).tqe_next.is_null() {
            (*Entry::<_, D>::entry((*Entry::<_, D>::entry(elm)).tqe_next)).tqe_prev =
                (*Entry::<_, D>::entry(elm)).tqe_prev;
        } else {
            (*head).tqh_last = (*Entry::<_, D>::entry(elm)).tqe_prev;
        }
        *(*Entry::<_, D>::entry(elm)).tqe_prev = (*Entry::<_, D>::entry(elm)).tqe_next;
    }
}

pub unsafe fn tailq_replace<T, D>(head: *mut tailq_head<T>, elm: *mut T, elm2: *mut T)
where
    T: Entry<T, D>,
{
    unsafe {
        (*Entry::<_, D>::entry(elm2)).tqe_next = (*Entry::<_, D>::entry(elm)).tqe_next;
        if !(*Entry::<_, D>::entry(elm2)).tqe_next.is_null() {
            (*Entry::<_, D>::entry((*Entry::<_, D>::entry(elm2)).tqe_next)).tqe_prev =
                &raw mut (*Entry::<_, D>::entry(elm2)).tqe_next;
        } else {
            (*head).tqh_last = &raw mut (*Entry::<_, D>::entry(elm2)).tqe_next;
        }
        (*Entry::<_, D>::entry(elm2)).tqe_prev = (*Entry::<_, D>::entry(elm)).tqe_prev;
        *(*Entry::<_, D>::entry(elm2)).tqe_prev = elm2;
    }
}

pub unsafe fn tailq_foreach_const<T, D>(
    head: *const tailq_head<T>,
) -> ConstTailqForwardIterator<T, D>
where
    T: Entry<T, D>,
{
    unsafe {
        ConstTailqForwardIterator {
            curr: NonNull::new((*head).tqh_first),
            _phantom: std::marker::PhantomData,
        }
    }
}
// this implementation can be used in place of safe and non-safe
pub struct ConstTailqForwardIterator<T, D> {
    curr: Option<NonNull<T>>,
    _phantom: std::marker::PhantomData<D>,
}
impl<T, D> Iterator for ConstTailqForwardIterator<T, D>
where
    T: Entry<T, D>,
{
    type Item = NonNull<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let curr = self.curr?.as_ptr();
        std::mem::replace(&mut self.curr, NonNull::new(unsafe { tailq_next(curr) }))
    }
}

pub unsafe fn tailq_foreach<T, D>(head: *mut tailq_head<T>) -> TailqForwardIterator<T, D>
where
    T: Entry<T, D>,
{
    unsafe {
        TailqForwardIterator {
            curr: NonNull::new(tailq_first(head)),
            _phantom: std::marker::PhantomData,
        }
    }
}

// this implementation can be used in place of safe and non-safe
pub struct TailqForwardIterator<T, D> {
    curr: Option<NonNull<T>>,
    _phantom: std::marker::PhantomData<D>,
}
impl<T, D> Iterator for TailqForwardIterator<T, D>
where
    T: Entry<T, D>,
{
    type Item = NonNull<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let curr = self.curr?.as_ptr();
        std::mem::replace(&mut self.curr, NonNull::new(unsafe { tailq_next(curr) }))
    }
}

pub unsafe fn tailq_foreach_reverse<T, D>(head: *mut tailq_head<T>) -> TailqReverseIterator<T, D>
where
    T: Entry<T, D>,
{
    unsafe {
        TailqReverseIterator {
            curr: NonNull::new(tailq_last(head)),
            _phantom: std::marker::PhantomData,
        }
    }
}

// this implementation can be used in place of safe and non-safe
pub struct TailqReverseIterator<T, D> {
    curr: Option<NonNull<T>>,
    _phantom: std::marker::PhantomData<D>,
}
impl<T, D> Iterator for TailqReverseIterator<T, D>
where
    T: Entry<T, D>,
{
    type Item = NonNull<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let curr = self.curr?.as_ptr();
        std::mem::replace(&mut self.curr, NonNull::new(unsafe { tailq_prev(curr) }))
    }
}

#[inline]
pub unsafe fn tailq_concat<T, D>(head1: *mut tailq_head<T>, head2: *mut tailq_head<T>)
where
    T: Entry<T, D>,
{
    unsafe {
        if !tailq_empty::<T>(head2) {
            *(*head1).tqh_last = (*head2).tqh_first;
            (*Entry::entry((*head2).tqh_first)).tqe_prev = (*head1).tqh_last;
            (*head1).tqh_last = (*head2).tqh_last;
            tailq_init(head2);
        }
    }
}

macro_rules! impl_tailq_entry {
    ($struct_name:ident, $attribute_field_name:ident, $attribute_field_ty:ty) => {
        impl $crate::compat::queue::Entry<$struct_name> for $struct_name {
            unsafe fn entry(this: *mut Self) -> *mut $attribute_field_ty {
                unsafe { &raw mut (*this).$attribute_field_name }
            }
        }
    };
}
pub(crate) use impl_tailq_entry;

#[cfg(test)]
mod tests {
    use super::*;

    // ---- TAILQ test fixtures --------------------------------------------
    //
    // A minimal doubly-linked tail-queue node keyed by an i32. This mirrors the
    // C `TAILQ_ENTRY(type)` pattern from vendor/tmux/compat/queue.h (lines
    // 416-420) where each node embeds a `{ tqe_next; tqe_prev; }`. We drive the
    // ported TAILQ_* ops in isolation with no dependency on server globals.
    #[repr(C)]
    struct TNode {
        key: i32,
        entry: tailq_entry<TNode>,
    }

    impl Entry<TNode> for TNode {
        unsafe fn entry(this: *mut Self) -> *mut tailq_entry<TNode> {
            unsafe { &raw mut (*this).entry }
        }
    }

    unsafe fn tmake(key: i32) -> *mut TNode {
        Box::into_raw(Box::new(TNode {
            key,
            entry: tailq_entry::default(),
        }))
    }

    // Collect the keys by walking TAILQ_FIRST / TAILQ_NEXT (queue.h 425, 427).
    unsafe fn tkeys(head: *mut tailq_head<TNode>) -> Vec<i32> {
        unsafe {
            let mut out = Vec::new();
            let mut n = tailq_first(head);
            while !n.is_null() {
                out.push((*n).key);
                n = tailq_next::<_, TNode, ()>(n);
            }
            out
        }
    }

    // Collect keys walking the forward iterator (TAILQ_FOREACH, queue.h 436).
    unsafe fn tkeys_foreach(head: *mut tailq_head<TNode>) -> Vec<i32> {
        unsafe { tailq_foreach::<TNode, ()>(head).map(|p| (*p.as_ptr()).key).collect() }
    }

    // Collect keys walking backwards (TAILQ_FOREACH_REVERSE, queue.h 448).
    unsafe fn tkeys_reverse(head: *mut tailq_head<TNode>) -> Vec<i32> {
        unsafe {
            tailq_foreach_reverse::<TNode, ()>(head)
                .map(|p| (*p.as_ptr()).key)
                .collect()
        }
    }

    unsafe fn tfree_all(head: *mut tailq_head<TNode>) {
        unsafe {
            let mut n = tailq_first(head);
            while !n.is_null() {
                let next = tailq_next::<_, TNode, ()>(n);
                drop(Box::from_raw(n));
                n = next;
            }
            tailq_init(head);
        }
    }

    // TAILQ_INIT (queue.h 462) leaves an empty queue: first is NULL and last
    // points back at first. TAILQ_EMPTY (queue.h 433) must be true.
    #[test]
    fn tailq_init_makes_empty() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            assert!(tailq_empty(&raw const head));
            assert!(head.tqh_first.is_null());
            assert_eq!(head.tqh_last, &raw mut head.tqh_first);
            assert!(tailq_first(&raw mut head).is_null());
            // last on empty is NULL, so both iterators yield nothing.
            assert!(tailq_last(&raw mut head).is_null());
            assert!(tkeys_foreach(&raw mut head).is_empty());
            assert!(tkeys_reverse(&raw mut head).is_empty());
        }
    }

    // tailq_init_ is the &mut-taking twin of tailq_init; same postconditions.
    #[test]
    fn tailq_init_ref_makes_empty() {
        let mut head: tailq_head<TNode> = tailq_head {
            tqh_first: null_mut(),
            tqh_last: null_mut(),
        };
        tailq_init_(&mut head);
        assert!(head.tqh_first.is_null());
        assert_eq!(head.tqh_last, &raw mut head.tqh_first);
    }

    // TAILQ_INSERT_HEAD (queue.h 467) prepends: inserting 1 then 2 then 3 yields
    // front-to-back order 3,2,1. first/last and both traversal directions agree.
    #[test]
    fn tailq_insert_head_prepends() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            for k in [1, 2, 3] {
                tailq_insert_head(&raw mut head, tmake(k));
            }
            assert!(!tailq_empty(&raw const head));
            assert_eq!(tkeys(&raw mut head), vec![3, 2, 1]);
            assert_eq!(tkeys_foreach(&raw mut head), vec![3, 2, 1]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![1, 2, 3]);
            assert_eq!((*tailq_first(&raw mut head)).key, 3);
            assert_eq!((*tailq_last(&raw mut head)).key, 1);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_INSERT_TAIL (queue.h 477) appends: 1,2,3 stays 1,2,3. Exercises the
    // tqh_last bookkeeping that keeps the reverse walk consistent.
    #[test]
    fn tailq_insert_tail_appends() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            for k in [1, 2, 3] {
                tailq_insert_tail(&raw mut head, tmake(k));
            }
            assert_eq!(tkeys(&raw mut head), vec![1, 2, 3]);
            assert_eq!(tkeys_foreach(&raw mut head), vec![1, 2, 3]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![3, 2, 1]);
            assert_eq!((*tailq_first(&raw mut head)).key, 1);
            assert_eq!((*tailq_last(&raw mut head)).key, 3);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_INSERT_AFTER (queue.h 484): insert after a middle element and after
    // the current tail. The after-tail case must update tqh_last so the reverse
    // walk still starts at the new tail.
    #[test]
    fn tailq_insert_after_middle_and_tail() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let a = tmake(1);
            let c = tmake(3);
            tailq_insert_tail(&raw mut head, a);
            tailq_insert_tail(&raw mut head, c);
            // after 'a' (middle insert): 1,2,3
            tailq_insert_after(&raw mut head, a, tmake(2));
            assert_eq!(tkeys(&raw mut head), vec![1, 2, 3]);
            // after 'c' (tail insert): 1,2,3,4
            tailq_insert_after(&raw mut head, c, tmake(4));
            assert_eq!(tkeys(&raw mut head), vec![1, 2, 3, 4]);
            assert_eq!((*tailq_last(&raw mut head)).key, 4);
            assert_eq!(tkeys_reverse(&raw mut head), vec![4, 3, 2, 1]);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_INSERT_BEFORE (queue.h 494): insert before a middle element and
    // before the head. Before-head effectively becomes the new first.
    #[test]
    fn tailq_insert_before_middle_and_head() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let a = tmake(2);
            let b = tmake(4);
            tailq_insert_tail(&raw mut head, a);
            tailq_insert_tail(&raw mut head, b);
            // before 'b': 2,3,4
            tailq_insert_before(b, tmake(3));
            assert_eq!(tkeys(&raw mut head), vec![2, 3, 4]);
            // before 'a' (the head): 1,2,3,4
            tailq_insert_before(a, tmake(1));
            assert_eq!(tkeys(&raw mut head), vec![1, 2, 3, 4]);
            assert_eq!((*tailq_first(&raw mut head)).key, 1);
            assert_eq!(tkeys_reverse(&raw mut head), vec![4, 3, 2, 1]);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_NEXT / TAILQ_PREV (queue.h 427, 431) navigation over a built queue.
    #[test]
    fn tailq_next_prev_navigation() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let n1 = tmake(10);
            let n2 = tmake(20);
            let n3 = tmake(30);
            tailq_insert_tail(&raw mut head, n1);
            tailq_insert_tail(&raw mut head, n2);
            tailq_insert_tail(&raw mut head, n3);

            assert_eq!((*tailq_next::<_, TNode, ()>(n1)).key, 20);
            assert_eq!((*tailq_next::<_, TNode, ()>(n2)).key, 30);
            assert!(tailq_next::<_, TNode, ()>(n3).is_null());

            assert_eq!((*tailq_prev::<_, TNode, ()>(n3)).key, 20);
            assert_eq!((*tailq_prev::<_, TNode, ()>(n2)).key, 10);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_REMOVE (queue.h 501) for the three distinct branches: removing the
    // head, a middle node, and the tail (the else branch that fixes tqh_last).
    #[test]
    fn tailq_remove_head_middle_tail() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let n1 = tmake(1);
            let n2 = tmake(2);
            let n3 = tmake(3);
            let n4 = tmake(4);
            for n in [n1, n2, n3, n4] {
                tailq_insert_tail(&raw mut head, n);
            }
            // remove middle
            tailq_remove(&raw mut head, n2);
            drop(Box::from_raw(n2));
            assert_eq!(tkeys(&raw mut head), vec![1, 3, 4]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![4, 3, 1]);
            // remove tail (else branch updates tqh_last)
            tailq_remove(&raw mut head, n4);
            drop(Box::from_raw(n4));
            assert_eq!(tkeys(&raw mut head), vec![1, 3]);
            assert_eq!((*tailq_last(&raw mut head)).key, 3);
            assert_eq!(tkeys_reverse(&raw mut head), vec![3, 1]);
            // remove head
            tailq_remove(&raw mut head, n1);
            drop(Box::from_raw(n1));
            assert_eq!(tkeys(&raw mut head), vec![3]);
            assert_eq!((*tailq_first(&raw mut head)).key, 3);
            // remove last remaining -> empty
            tailq_remove(&raw mut head, n3);
            drop(Box::from_raw(n3));
            assert!(tailq_empty(&raw const head));
            assert_eq!(head.tqh_last, &raw mut head.tqh_first);
        }
    }

    // TAILQ_REPLACE (queue.h 512): swap a node in place, both middle and tail.
    #[test]
    fn tailq_replace_middle_and_tail() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let n1 = tmake(1);
            let n2 = tmake(2);
            let n3 = tmake(3);
            for n in [n1, n2, n3] {
                tailq_insert_tail(&raw mut head, n);
            }
            // replace middle n2 with key 20
            let r = tmake(20);
            tailq_replace(&raw mut head, n2, r);
            drop(Box::from_raw(n2));
            assert_eq!(tkeys(&raw mut head), vec![1, 20, 3]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![3, 20, 1]);
            // replace tail n3 with key 30 (else branch updates tqh_last)
            let r2 = tmake(30);
            tailq_replace(&raw mut head, n3, r2);
            drop(Box::from_raw(n3));
            assert_eq!(tkeys(&raw mut head), vec![1, 20, 30]);
            assert_eq!((*tailq_last(&raw mut head)).key, 30);
            assert_eq!(tkeys_reverse(&raw mut head), vec![30, 20, 1]);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_CONCAT (queue.h 524): splice head2 onto the end of head1 and leave
    // head2 empty. Also verify the no-op path when head2 is empty.
    #[test]
    fn tailq_concat_splices_and_empties() {
        unsafe {
            let mut h1: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            let mut h2: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut h1);
            tailq_init(&raw mut h2);
            for k in [1, 2] {
                tailq_insert_tail(&raw mut h1, tmake(k));
            }
            for k in [3, 4] {
                tailq_insert_tail(&raw mut h2, tmake(k));
            }
            tailq_concat(&raw mut h1, &raw mut h2);
            assert_eq!(tkeys(&raw mut h1), vec![1, 2, 3, 4]);
            assert_eq!(tkeys_reverse(&raw mut h1), vec![4, 3, 2, 1]);
            assert!(tailq_empty(&raw const h2));
            assert_eq!(h2.tqh_last, &raw mut h2.tqh_first);

            // concat with an empty head2 is a no-op.
            let mut h3: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut h3);
            tailq_concat(&raw mut h1, &raw mut h3);
            assert_eq!(tkeys(&raw mut h1), vec![1, 2, 3, 4]);
            tfree_all(&raw mut h1);
        }
    }

    // The const forward iterator (TAILQ_FOREACH over a *const head) must visit
    // the same nodes in the same order as the mutable one.
    #[test]
    fn tailq_foreach_const_matches() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            for k in [5, 6, 7] {
                tailq_insert_tail(&raw mut head, tmake(k));
            }
            let got: Vec<i32> = tailq_foreach_const::<TNode, ()>(&raw const head)
                .map(|p| (*p.as_ptr()).key)
                .collect();
            assert_eq!(got, vec![5, 6, 7]);
            tfree_all(&raw mut head);
        }
    }

    // ---- LIST test fixtures ---------------------------------------------
    //
    // Doubly-linked LIST node (LIST_ENTRY, queue.h 172-176) with le_next and
    // le_prev. Only head-insert plus arbitrary remove and forward traversal are
    // ported, matching what the crate uses.
    #[repr(C)]
    struct LNode {
        key: i32,
        entry: list_entry<LNode>,
    }

    impl ListEntry<LNode> for LNode {
        unsafe fn field(this: *mut Self) -> *mut list_entry<LNode> {
            unsafe { &raw mut (*this).entry }
        }
    }

    unsafe fn lmake(key: i32) -> *mut LNode {
        Box::into_raw(Box::new(LNode {
            key,
            entry: list_entry::default(),
        }))
    }

    unsafe fn lkeys(head: *mut list_head<LNode>) -> Vec<i32> {
        unsafe {
            let mut out = Vec::new();
            let mut n = list_first(head);
            while !n.is_null() {
                out.push((*n).key);
                n = list_next::<_, ()>(n);
            }
            out
        }
    }

    unsafe fn lfree_all(head: *mut list_head<LNode>) {
        unsafe {
            let mut n = list_first(head);
            while !n.is_null() {
                let next = list_next::<_, ()>(n);
                drop(Box::from_raw(n));
                n = next;
            }
            (*head).lh_first = null_mut();
        }
    }

    // LIST_HEAD_INITIALIZER / LIST_INSERT_HEAD (queue.h 169, 218): the head list
    // starts empty (lh_first NULL) and prepends, so 1,2,3 becomes 3,2,1.
    #[test]
    fn list_insert_head_prepends() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            assert!(list_first(&raw mut head).is_null());
            for k in [1, 2, 3] {
                list_insert_head(&raw mut head, lmake(k));
            }
            assert_eq!(lkeys(&raw mut head), vec![3, 2, 1]);
            let got: Vec<i32> = list_foreach::<LNode, ()>(&raw mut head)
                .map(|p| (*p.as_ptr()).key)
                .collect();
            assert_eq!(got, vec![3, 2, 1]);
            assert_eq!((*list_first(&raw mut head)).key, 3);
            lfree_all(&raw mut head);
        }
    }

    // LIST_REMOVE (queue.h 225) for head, middle and tail. le_prev indirection
    // must keep the chain intact after each removal.
    #[test]
    fn list_remove_head_middle_tail() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            // insert_head of 1,2,3,4 -> order is 4,3,2,1
            let n1 = lmake(1);
            let n2 = lmake(2);
            let n3 = lmake(3);
            let n4 = lmake(4);
            for n in [n1, n2, n3, n4] {
                list_insert_head(&raw mut head, n);
            }
            assert_eq!(lkeys(&raw mut head), vec![4, 3, 2, 1]);
            // remove middle (n3): 4,2,1
            list_remove::<_, ()>(n3);
            drop(Box::from_raw(n3));
            assert_eq!(lkeys(&raw mut head), vec![4, 2, 1]);
            // remove head (n4): 2,1
            list_remove::<_, ()>(n4);
            drop(Box::from_raw(n4));
            assert_eq!(lkeys(&raw mut head), vec![2, 1]);
            assert_eq!((*list_first(&raw mut head)).key, 2);
            // remove tail (n1): 2
            list_remove::<_, ()>(n1);
            drop(Box::from_raw(n1));
            assert_eq!(lkeys(&raw mut head), vec![2]);
            // remove last: empty
            list_remove::<_, ()>(n2);
            drop(Box::from_raw(n2));
            assert!(list_first(&raw mut head).is_null());
        }
    }

    // LIST_NEXT (queue.h 184): forward navigation, next of the tail is NULL.
    #[test]
    fn list_next_navigation() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            let a = lmake(1);
            let b = lmake(2);
            list_insert_head(&raw mut head, b);
            list_insert_head(&raw mut head, a); // order: 1,2
            assert_eq!((*list_next::<_, ()>(a)).key, 2);
            assert!(list_next::<_, ()>(b).is_null());
            lfree_all(&raw mut head);
        }
    }

    // A single-element TAILQ: first == last, TAILQ_NEXT/TAILQ_PREV are NULL
    // (queue.h 425/427/431), and both traversal directions yield one key.
    #[test]
    fn tailq_single_element() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let n = tmake(99);
            tailq_insert_tail(&raw mut head, n);
            assert_eq!(tailq_first(&raw mut head), n);
            assert_eq!(tailq_last(&raw mut head), n);
            assert!(tailq_next::<_, TNode, ()>(n).is_null());
            assert!(tailq_prev::<_, TNode, ()>(n).is_null());
            assert_eq!(tkeys_foreach(&raw mut head), vec![99]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![99]);
            tfree_all(&raw mut head);
        }
    }

    // Removing the head node repeatedly keeps forward and reverse traversals
    // consistent at every step and finally restores the empty invariant
    // (tqh_last points back at tqh_first, queue.h 462/501).
    #[test]
    fn tailq_remove_head_until_empty() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let nodes: Vec<*mut TNode> = (0..5).map(|k| tmake(k)).collect();
            for &n in &nodes {
                tailq_insert_tail(&raw mut head, n);
            }
            for expected_first in 0..5 {
                assert_eq!((*tailq_first(&raw mut head)).key, expected_first);
                let fwd = tkeys_foreach(&raw mut head);
                let mut rev = tkeys_reverse(&raw mut head);
                rev.reverse();
                assert_eq!(fwd, rev, "forward and reverse disagree");
                let h = tailq_first(&raw mut head);
                tailq_remove(&raw mut head, h);
                drop(Box::from_raw(h));
            }
            assert!(tailq_empty(&raw const head));
            assert_eq!(head.tqh_last, &raw mut head.tqh_first);
        }
    }

    // TAILQ_REPLACE at the head (queue.h 512): the replacement becomes first
    // and the reverse walk still terminates correctly.
    #[test]
    fn tailq_replace_head() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let n1 = tmake(1);
            let n2 = tmake(2);
            tailq_insert_tail(&raw mut head, n1);
            tailq_insert_tail(&raw mut head, n2);
            let r = tmake(10);
            tailq_replace(&raw mut head, n1, r);
            drop(Box::from_raw(n1));
            assert_eq!((*tailq_first(&raw mut head)).key, 10);
            assert_eq!(tkeys(&raw mut head), vec![10, 2]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![2, 10]);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_PREV of the first element resolves to NULL: its tqe_prev points at
    // the head, whose tqh_last dereferences the first node's (NULL) tqe_next.
    #[test]
    fn tailq_prev_of_first_is_null() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let a = tmake(1);
            let b = tmake(2);
            tailq_insert_tail(&raw mut head, a);
            tailq_insert_tail(&raw mut head, b);
            assert!(tailq_prev::<_, TNode, ()>(a).is_null());
            assert_eq!((*tailq_prev::<_, TNode, ()>(b)).key, 1);
            tfree_all(&raw mut head);
        }
    }

    // Bulk TAILQ_INSERT_TAIL of 100 nodes: forward keys are 0..100, reverse is
    // the mirror, and first/last are the endpoints. Threads tqh_last over many
    // ops (queue.h 477).
    #[test]
    fn tailq_insert_tail_bulk() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            for k in 0..100 {
                tailq_insert_tail(&raw mut head, tmake(k));
            }
            let fwd: Vec<i32> = (0..100).collect();
            let rev: Vec<i32> = (0..100).rev().collect();
            assert_eq!(tkeys(&raw mut head), fwd);
            assert_eq!(tkeys_foreach(&raw mut head), fwd);
            assert_eq!(tkeys_reverse(&raw mut head), rev);
            assert_eq!((*tailq_first(&raw mut head)).key, 0);
            assert_eq!((*tailq_last(&raw mut head)).key, 99);
            tfree_all(&raw mut head);
        }
    }

    // TAILQ_CONCAT onto an empty head1 (queue.h 524) adopts head2's list intact
    // and leaves head2 empty.
    #[test]
    fn tailq_concat_into_empty() {
        unsafe {
            let mut h1: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            let mut h2: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut h1);
            tailq_init(&raw mut h2);
            for k in [7, 8, 9] {
                tailq_insert_tail(&raw mut h2, tmake(k));
            }
            tailq_concat(&raw mut h1, &raw mut h2);
            assert_eq!(tkeys(&raw mut h1), vec![7, 8, 9]);
            assert_eq!(tkeys_reverse(&raw mut h1), vec![9, 8, 7]);
            assert_eq!((*tailq_last(&raw mut h1)).key, 9);
            assert!(tailq_empty(&raw const h2));
            assert_eq!(h2.tqh_last, &raw mut h2.tqh_first);
            tfree_all(&raw mut h1);
        }
    }

    // Repeated TAILQ_INSERT_BEFORE at a fixed anchor (queue.h 494) builds an
    // ascending run in front of the anchor.
    #[test]
    fn tailq_insert_before_builds_ascending() {
        unsafe {
            let mut head: tailq_head<TNode> = tailq_head {
                tqh_first: null_mut(),
                tqh_last: null_mut(),
            };
            tailq_init(&raw mut head);
            let anchor = tmake(100);
            tailq_insert_tail(&raw mut head, anchor);
            for k in [1, 2, 3, 4] {
                tailq_insert_before(anchor, tmake(k));
            }
            assert_eq!(tkeys(&raw mut head), vec![1, 2, 3, 4, 100]);
            assert_eq!(tkeys_reverse(&raw mut head), vec![100, 4, 3, 2, 1]);
            assert_eq!((*tailq_first(&raw mut head)).key, 1);
            tfree_all(&raw mut head);
        }
    }

    // A single-element LIST: LIST_NEXT is NULL (queue.h 184) and the forward
    // walk yields exactly one key.
    #[test]
    fn list_single_element() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            let n = lmake(5);
            list_insert_head(&raw mut head, n);
            assert_eq!(list_first(&raw mut head), n);
            assert!(list_next::<_, ()>(n).is_null());
            assert_eq!(lkeys(&raw mut head), vec![5]);
            lfree_all(&raw mut head);
        }
    }

    // An empty LIST: lh_first is NULL and both the raw walk and the iterator
    // yield nothing.
    #[test]
    fn list_empty() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            assert!(list_first(&raw mut head).is_null());
            assert!(lkeys(&raw mut head).is_empty());
            let got: Vec<i32> = list_foreach::<LNode, ()>(&raw mut head)
                .map(|p| (*p.as_ptr()).key)
                .collect();
            assert!(got.is_empty());
        }
    }

    // Bulk LIST_INSERT_HEAD of 50 then LIST_REMOVE of every even key: the
    // le_prev back-links must keep the surviving odd chain intact (queue.h
    // 218/225).
    #[test]
    fn list_bulk_insert_and_sparse_remove() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            let nodes: Vec<*mut LNode> = (0..50).map(|k| lmake(k)).collect();
            for &n in &nodes {
                list_insert_head(&raw mut head, n);
            }
            // insert_head reverses: keys are 49,48,...,0.
            let all: Vec<i32> = (0..50).rev().collect();
            assert_eq!(lkeys(&raw mut head), all);
            // Remove every even key. nodes[i].key == i, so index parity selects
            // the node without dereferencing (freed nodes must not be read).
            for (i, &n) in nodes.iter().enumerate() {
                if i % 2 == 0 {
                    list_remove::<_, ()>(n);
                    drop(Box::from_raw(n));
                }
            }
            let odds: Vec<i32> = (0..50).rev().filter(|k| k % 2 != 0).collect();
            assert_eq!(lkeys(&raw mut head), odds);
            // Free the survivors (the odd-indexed nodes).
            for (i, &n) in nodes.iter().enumerate() {
                if i % 2 != 0 {
                    list_remove::<_, ()>(n);
                    drop(Box::from_raw(n));
                }
            }
            assert!(list_first(&raw mut head).is_null());
        }
    }

    // Removing the head element updates lh_first through le_prev (queue.h 225),
    // then removing the new head empties the list.
    #[test]
    fn list_remove_head_updates_first() {
        unsafe {
            let mut head: list_head<LNode> = list_head_initializer();
            let a = lmake(1);
            let b = lmake(2);
            list_insert_head(&raw mut head, a);
            list_insert_head(&raw mut head, b); // order: 2,1
            assert_eq!((*list_first(&raw mut head)).key, 2);
            list_remove::<_, ()>(b);
            drop(Box::from_raw(b));
            assert_eq!((*list_first(&raw mut head)).key, 1);
            list_remove::<_, ()>(a);
            drop(Box::from_raw(a));
            assert!(list_first(&raw mut head).is_null());
        }
    }
}