linked_list_r4l 0.3.0

Linked lists that supports arbitrary removal in constant time
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
// SPDX-License-Identifier: GPL-2.0

//! Raw lists.
//!
//! Copied from linux/rust/kernel/raw_list.rs.
//!
//! TODO: This module is a work in progress.

use core::{
    cell::UnsafeCell,
    iter, ptr,
    ptr::NonNull,
    sync::atomic::{AtomicBool, Ordering},
};

/// A descriptor of list elements.
///
/// It describes the type of list elements and provides a function to determine how to get the
/// links to be used on a list.
///
/// A type that may be in multiple lists simultaneously needs to implement one of these for each
/// simultaneous list.
pub trait GetLinks {
    /// The type of the entries in the list.
    type EntryType: ?Sized;

    /// Returns the links to be used when linking an entry within a list.
    fn get_links(data: &Self::EntryType) -> &Links<Self::EntryType>;
}

/// The links used to link an object on a linked list.
///
/// Instances of this type are usually embedded in structures and returned in calls to
/// [`GetLinks::get_links`].
pub struct Links<T: ?Sized> {
    inserted: AtomicBool,
    entry: UnsafeCell<ListEntry<T>>,
}

// SAFETY: `Links` can be safely sent to other threads but we restrict it to being `Send` only when
// the list entries it points to are also `Send`.
unsafe impl<T: ?Sized> Send for Links<T> {}

// SAFETY: `Links` is usable from other threads via references but we restrict it to being `Sync`
// only when the list entries it points to are also `Sync`.
unsafe impl<T: ?Sized> Sync for Links<T> {}

impl<T: ?Sized> Links<T> {
    /// Constructs a new [`Links`] instance that isn't inserted on any lists yet.
    pub const fn new() -> Self {
        Self {
            inserted: AtomicBool::new(false),
            entry: UnsafeCell::new(ListEntry::new()),
        }
    }

    fn acquire_for_insertion(&self) -> bool {
        self.inserted
            .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
            .is_ok()
    }

    fn release_after_removal(&self) {
        self.inserted.store(false, Ordering::Release);
    }
}

impl<T: ?Sized> Default for Links<T> {
    fn default() -> Self {
        Self::new()
    }
}

struct ListEntry<T: ?Sized> {
    next: Option<NonNull<T>>,
    prev: Option<NonNull<T>>,
}

impl<T: ?Sized> ListEntry<T> {
    const fn new() -> Self {
        Self {
            next: None,
            prev: None,
        }
    }
}

/// A linked list.
///
/// # Invariants
///
/// The links of objects added to a list are owned by the list.
pub struct RawList<G: GetLinks> {
    head: Option<NonNull<G::EntryType>>,
}

impl<G: GetLinks> RawList<G> {
    /// Constructs a new empty RawList.
    pub const fn new() -> Self {
        Self { head: None }
    }

    /// Returns an iterator for the list starting at the first entry.
    pub fn iter(&self) -> Iterator<'_, G> {
        Iterator::new(self.cursor_front(), self.cursor_back())
    }

    /// Returns whether the RawList is empty.
    pub const fn is_empty(&self) -> bool {
        self.head.is_none()
    }

    fn insert_after_priv(
        &mut self,
        existing: NonNull<G::EntryType>,
        new_entry: &mut ListEntry<G::EntryType>,
        new_ptr: Option<NonNull<G::EntryType>>,
    ) {
        {
            // SAFETY: It's safe to get the previous entry of `existing` because the list cannot
            // change.
            let existing_links = unsafe { &mut *G::get_links(existing.as_ref()).entry.get() };
            new_entry.next = existing_links.next;
            existing_links.next = new_ptr;
        }

        new_entry.prev = Some(existing);

        // SAFETY: It's safe to get the next entry of `existing` because the list cannot change.
        let next_links =
            unsafe { &mut *G::get_links(new_entry.next.unwrap().as_ref()).entry.get() };
        next_links.prev = new_ptr;
    }

    /// Inserts the given object after `existing`.
    ///
    /// # Safety
    ///
    /// Callers must ensure that `existing` points to a valid entry that is on the list.
    pub unsafe fn insert_after(
        &mut self,
        existing: NonNull<G::EntryType>,
        new: NonNull<G::EntryType>,
    ) -> bool {
        // SAFETY: the caller ensures new is valid.
        let links = unsafe { G::get_links(new.as_ref()) };
        if !links.acquire_for_insertion() {
            // Nothing to do if already inserted.
            return false;
        }

        // SAFETY: The links are now owned by the list, so it is safe to get a mutable reference.
        let new_entry = unsafe { &mut *links.entry.get() };
        self.insert_after_priv(existing, new_entry, Some(new));
        true
    }

    fn push_back_internal(&mut self, new: NonNull<G::EntryType>, front: bool) -> bool {
        // SAFETY: We just get the valid EntryType ptr from the caller.
        let links = unsafe { G::get_links(new.as_ref()) };
        if !links.acquire_for_insertion() {
            // Nothing to do if already inserted.
            return false;
        }

        // SAFETY: The links are now owned by the list, so it is safe to get a mutable reference.
        let new_entry = unsafe { &mut *links.entry.get() };
        let new_ptr = Some(new);
        match self.back() {
            // SAFETY: `back` is valid as the list cannot change.
            Some(back) => {
                self.insert_after_priv(back, new_entry, new_ptr);
                // if push front, update head
                if front {
                    self.head = new_ptr;
                }
            }
            None => {
                self.head = new_ptr;
                new_entry.next = new_ptr;
                new_entry.prev = new_ptr;
            }
        }
        true
    }

    /// Adds the given object to the end (back) of the list.
    ///
    /// Rawlist will save the reference as node ptr.
    /// The caller must ensure the validity of the reference while it is on
    /// the linked list.
    pub unsafe fn push_back(&mut self, new: NonNull<G::EntryType>) -> bool {
        self.push_back_internal(new, false)
    }

    /// Adds the given object to the first (front) of the list.
    ///
    /// Rawlist will save the reference as node ptr.
    /// The caller must ensure the validity of the reference while it is on
    /// the linked list.
    pub unsafe fn push_front(&mut self, new: NonNull<G::EntryType>) -> bool {
        self.push_back_internal(new, true)
    }

    fn remove_internal(&mut self, data: &G::EntryType) -> bool {
        let links = G::get_links(data);

        // SAFETY: The links are now owned by the list, so it is safe to get a mutable reference.
        let entry = unsafe { &mut *links.entry.get() };
        let next = if let Some(next) = entry.next {
            next
        } else {
            // Nothing to do if the entry is not on the list.
            return false;
        };

        if ptr::eq(data, next.as_ptr()) {
            // We're removing the only element.
            self.head = None
        } else {
            // Update the head if we're removing it.
            if let Some(raw_head) = self.head {
                if ptr::eq(data, raw_head.as_ptr()) {
                    self.head = Some(next);
                }
            }

            // SAFETY: It's safe to get the previous entry because the list cannot change.
            unsafe { &mut *G::get_links(entry.prev.unwrap().as_ref()).entry.get() }.next =
                entry.next;

            // SAFETY: It's safe to get the next entry because the list cannot change.
            unsafe { &mut *G::get_links(next.as_ref()).entry.get() }.prev = entry.prev;
        }

        // Reset the links of the element we're removing so that we know it's not on any list.
        entry.next = None;
        entry.prev = None;
        links.release_after_removal();
        true
    }

    /// Removes the given entry.
    ///
    /// # Safety
    ///
    /// Callers must ensure that `data` is either on this list or in no list. It being on another
    /// list leads to memory unsafety.
    pub unsafe fn remove(&mut self, data: &G::EntryType) -> bool {
        self.remove_internal(data)
    }

    fn pop_front_internal(&mut self) -> Option<NonNull<G::EntryType>> {
        let head = self.head?;
        // SAFETY: The head is on the list as we just got it from there and it cannot change.
        unsafe { self.remove(head.as_ref()) };
        Some(head)
    }

    /// Get and Remove the first element of the list.
    pub fn pop_front(&mut self) -> Option<NonNull<G::EntryType>> {
        self.pop_front_internal()
    }

    ///  Just Get and not remove the first element of the list.
    pub(crate) fn front(&self) -> Option<NonNull<G::EntryType>> {
        self.head
    }

    /// Just Get and not remove the last element of the list.
    pub(crate) fn back(&self) -> Option<NonNull<G::EntryType>> {
        // SAFETY: The links of head are owned by the list, so it is safe to get a reference.
        unsafe { &*G::get_links(self.head?.as_ref()).entry.get() }.prev
    }

    /// Returns a cursor starting on the first element of the list.
    pub(crate) fn cursor_front(&self) -> Cursor<'_, G> {
        Cursor::new(self, self.front())
    }

    /// Returns a cursor starting on the last element of the list.
    pub(crate) fn cursor_back(&self) -> Cursor<'_, G> {
        Cursor::new(self, self.back())
    }

    /// Returns a mut cursor starting on the first element of the list.
    pub fn cursor_front_mut(&mut self) -> CursorMut<'_, G> {
        CursorMut::new(self, self.front())
    }
}

struct CommonCursor<G: GetLinks> {
    cur: Option<NonNull<G::EntryType>>,
}

impl<G: GetLinks> CommonCursor<G> {
    const fn new(cur: Option<NonNull<G::EntryType>>) -> Self {
        Self { cur }
    }

    fn move_next(&mut self, list: &RawList<G>) {
        // `move_next` stops at None when reaching the end of list,
        // because `raw_list::Iterator` stops at None.
        // But move to next a further step beyond the ending None,
        // the cursor starts from the head again.
        match self.cur.take() {
            None => self.cur = list.head,
            Some(cur) => {
                if let Some(head) = list.head {
                    // SAFETY: We have a shared ref to the linked list, so the links can't change.
                    let links = unsafe { &*G::get_links(cur.as_ref()).entry.get() };
                    if !ptr::addr_eq(links.next.unwrap().as_ptr(), head.as_ptr()) {
                        self.cur = links.next;
                    }
                }
            }
        }
    }

    fn move_prev(&mut self, list: &RawList<G>) {
        match list.head {
            None => self.cur = None,
            Some(head) => {
                let next = match self.cur.take() {
                    None => head,
                    Some(cur) => {
                        if ptr::addr_eq(cur.as_ptr(), head.as_ptr()) {
                            return;
                        }
                        cur
                    }
                };
                // SAFETY: There's a shared ref to the list, so the links can't change.
                let links = unsafe { &*G::get_links(next.as_ref()).entry.get() };
                self.cur = links.prev;
            }
        }
    }
}

// SAFETY: The list is itself can be safely sent to other threads but we restrict it to being `Send`
// only when its entries are also `Send`.
unsafe impl<G: GetLinks> Send for RawList<G> where G::EntryType: Send {}

// SAFETY: The list is itself usable from other threads via references but we restrict it to being
// `Sync` only when its entries are also `Sync`.
unsafe impl<G: GetLinks> Sync for RawList<G> where G::EntryType: Sync {}

/// A list cursor that allows traversing a linked list and inspecting elements.
pub(crate) struct Cursor<'a, G: GetLinks> {
    cursor: CommonCursor<G>,
    list: &'a RawList<G>,
}

impl<'a, G: GetLinks> Cursor<'a, G> {
    pub(crate) fn new(list: &'a RawList<G>, cur: Option<NonNull<G::EntryType>>) -> Self {
        Self {
            list,
            cursor: CommonCursor::new(cur),
        }
    }

    /// Returns the element the cursor is currently positioned on.
    pub(crate) fn current(&self) -> Option<&'a G::EntryType> {
        let cur = self.cursor.cur?;
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &*cur.as_ptr() })
    }

    /// Returns the element pointer the cursor is currently positioned on.
    pub(crate) fn current_ptr(&self) -> Option<NonNull<G::EntryType>> {
        self.cursor.cur
    }

    /// Moves the cursor to the next element.
    pub(crate) fn move_next(&mut self) {
        self.cursor.move_next(self.list);
    }

    pub fn peek_next(&self) -> Option<&G::EntryType> {
        let mut new = CommonCursor::new(self.cursor.cur);
        new.move_next(self.list);
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &*new.cur?.as_ptr() })
    }

    pub fn peek_prev(&self) -> Option<&G::EntryType> {
        let mut new = CommonCursor::new(self.cursor.cur);
        new.move_prev(self.list);
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &*new.cur?.as_ptr() })
    }

    /// Moves the cursor to the prev element.
    pub(crate) fn move_prev(&mut self) {
        self.cursor.move_prev(self.list);
    }
}

pub struct CursorMut<'a, G: GetLinks> {
    cursor: CommonCursor<G>,
    pub(crate) list: &'a mut RawList<G>,
}

impl<'a, G: GetLinks> CursorMut<'a, G> {
    fn new(list: &'a mut RawList<G>, cur: Option<NonNull<G::EntryType>>) -> Self {
        Self {
            list,
            cursor: CommonCursor::new(cur),
        }
    }

    pub unsafe fn current_mut(&mut self) -> Option<&mut G::EntryType> {
        let cur = self.cursor.cur?;
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &mut *cur.as_ptr() })
    }

    pub fn current(&self) -> Option<&G::EntryType> {
        let cur = self.current_ptr()?;
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &mut *cur.as_ptr() })
    }

    /// Returns the element pointer the cursor is currently positioned on.
    pub(crate) fn current_ptr(&self) -> Option<NonNull<G::EntryType>> {
        self.cursor.cur
    }

    /// Removes the entry the cursor is pointing to and advances the cursor to the next entry. It
    /// returns a raw pointer to the removed element (if one is removed).
    pub fn remove_current(&mut self) -> Option<NonNull<G::EntryType>> {
        let entry = self.cursor.cur?;
        self.cursor.move_next(self.list);
        // SAFETY: The entry is on the list as we just got it from there and it cannot change.
        unsafe { self.list.remove(entry.as_ref()) };
        Some(entry)
    }

    /// Returns the element immediately after the one the cursor is positioned on.
    ///
    /// # Safety
    ///
    /// For `Box` or `Arc` that has unique access to the data behind, the method is safe.
    /// For `Arc` whose strong count is not 1, the method is not safe because it
    /// violates the safety requirements of [`Arc`].
    pub unsafe fn peek_next(&mut self) -> Option<&mut G::EntryType> {
        let mut new = CommonCursor::new(self.cursor.cur);
        new.move_next(self.list);
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &mut *new.cur?.as_ptr() })
    }

    /// Returns the element immediately before the one the cursor is positioned on.
    ///
    /// # Safety
    ///
    /// For `Box` or `Arc` that has unique access to the data behind, the method is safe.
    /// For `Arc` whose strong count is not 1, the method is not safe because it
    /// violates the safety requirements of [`Arc`].
    pub unsafe fn peek_prev(&mut self) -> Option<&mut G::EntryType> {
        let mut new = CommonCursor::new(self.cursor.cur);
        new.move_prev(self.list);
        // SAFETY: Objects must be kept alive while on the list.
        Some(unsafe { &mut *new.cur?.as_ptr() })
    }

    pub fn move_next(&mut self) {
        self.cursor.move_next(self.list);
    }

    pub fn move_prev(&mut self) {
        self.cursor.move_prev(self.list);
    }
}

impl<'a, G: GetLinks> iter::IntoIterator for &'a RawList<G> {
    type Item = &'a G::EntryType;
    type IntoIter = Iterator<'a, G>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator for the linked list.
pub struct Iterator<'a, G: GetLinks> {
    cursor_front: Cursor<'a, G>,
    cursor_back: Cursor<'a, G>,
}

impl<'a, G: GetLinks> Iterator<'a, G> {
    const fn new(cursor_front: Cursor<'a, G>, cursor_back: Cursor<'a, G>) -> Self {
        Self {
            cursor_front,
            cursor_back,
        }
    }
}

impl<'a, G: GetLinks> iter::Iterator for Iterator<'a, G> {
    type Item = &'a G::EntryType;

    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.cursor_front.current()?;
        self.cursor_front.move_next();
        Some(ret)
    }
}

impl<G: GetLinks> iter::DoubleEndedIterator for Iterator<'_, G> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let ret = self.cursor_back.current()?;
        self.cursor_back.move_prev();
        Some(ret)
    }
}

#[cfg(test)]
mod tests {
    extern crate alloc;
    use alloc::{boxed::Box, vec::Vec};
    use core::ptr::NonNull;

    struct Example {
        links: super::Links<Self>,
    }

    // SAFETY: This is the only adapter that uses `Example::links`.
    impl super::GetLinks for Example {
        type EntryType = Self;
        fn get_links(obj: &Self) -> &super::Links<Self> {
            &obj.links
        }
    }

    fn build_vector(size: usize) -> Vec<Box<Example>> {
        let mut v = Vec::new();
        v.reserve(size);
        for _ in 0..size {
            v.push(Box::new(Example {
                links: super::Links::new(),
            }));
        }
        v
    }

    #[track_caller]
    fn assert_list_contents(v: &[Box<Example>], list: &super::RawList<Example>) {
        let n = v.len();

        // Assert that the list is ok going forward.
        let mut count = 0;
        for (i, e) in list.iter().enumerate() {
            assert!(core::ptr::eq(e, &*v[i]));
            count += 1;
        }
        assert_eq!(count, n);

        // Assert that the list is ok going backwards.
        let mut count = 0;
        for (i, e) in list.iter().rev().enumerate() {
            assert!(core::ptr::eq(e, &*v[n - 1 - i]));
            count += 1;
        }
        assert_eq!(count, n);
    }

    #[track_caller]
    fn test_each_element(
        min_len: usize,
        max_len: usize,
        test: impl Fn(&mut Vec<Box<Example>>, &mut super::RawList<Example>, usize, Box<Example>),
    ) {
        for n in min_len..=max_len {
            for i in 0..n {
                let extra = Box::new(Example {
                    links: super::Links::new(),
                });
                let mut v = build_vector(n);
                let mut list = super::RawList::<Example>::new();

                // Build list.
                for j in 0..n {
                    // SAFETY: The entry was allocated above, it's not in any lists yet, is never
                    // moved, and outlives the list.
                    unsafe { list.push_back(NonNull::from(&*v[j])) };
                }

                // Call the test case.
                test(&mut v, &mut list, i, extra);

                // Check that the list is ok.
                assert_list_contents(&v, &list);
            }
        }
    }

    #[test]
    fn test_push_back() {
        const MAX: usize = 10;
        let v = build_vector(MAX);
        let mut list = super::RawList::<Example>::new();

        for n in 1..=MAX {
            // SAFETY: The entry was allocated above, it's not in any lists yet, is never moved,
            // and outlives the list.
            unsafe { list.push_back(NonNull::from(&*v[n - 1])) };
            assert_list_contents(&v[..n], &list);
        }
    }

    #[test]
    fn test_push_front() {
        const MAX: usize = 10;
        let v = build_vector(MAX);
        let mut list = super::RawList::<Example>::new();

        for n in 1..=MAX {
            // SAFETY: The entry was allocated above, it's not in any lists yet, is never moved,
            // and outlives the list.
            println!("push front: {}", MAX - n);
            unsafe { list.push_front(NonNull::from(&*v[MAX - n])) };
            assert_list_contents(&v[MAX - n..MAX], &list);
        }
    }

    #[test]
    fn test_one_removal() {
        test_each_element(1, 10, |v, list, i, _| {
            // Remove the i-th element.
            // SAFETY: The i-th element was added to the list above, and wasn't removed yet.
            unsafe { list.remove(&v[i]) };
            v.remove(i);
        });
    }

    #[test]
    fn test_one_insert_after() {
        test_each_element(1, 10, |v, list, i, extra| {
            // Insert after the i-th element.
            // SAFETY: The i-th element was added to the list above, and wasn't removed yet.
            // Additionally, the new element isn't in any list yet, isn't moved, and outlives
            // the list.
            unsafe { list.insert_after(v[i].as_ref().into(), extra.as_ref().into()) };
            v.insert(i + 1, extra);
        });
    }
}