1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef List_h
27#define List_h
28
29namespace bmalloc {
30
31template<typename T>
32struct ListNode {
33 ListNode<T>* prev { nullptr };
34 ListNode<T>* next { nullptr };
35};
36
37template<typename T>
38class List {
39 static_assert(std::is_trivially_destructible<T>::value, "List must have a trivial destructor.");
40
41 struct iterator {
42 T* operator*() { return static_cast<T*>(m_node); }
43 T* operator->() { return static_cast<T*>(m_node); }
44
45 bool operator!=(const iterator& other) { return m_node != other.m_node; }
46
47 iterator& operator++()
48 {
49 m_node = m_node->next;
50 return *this;
51 }
52
53 ListNode<T>* m_node { nullptr };
54 };
55
56public:
57 List() { }
58
59 bool isEmpty() { return m_root.next == &m_root; }
60
61 T* head() { return static_cast<T*>(m_root.next); }
62 T* tail() { return static_cast<T*>(m_root.prev); }
63
64 iterator begin() { return iterator { m_root.next }; }
65 iterator end() { return iterator { &m_root }; }
66
67 void push(T* node)
68 {
69 ListNode<T>* it = tail();
70 insertAfter(it, node);
71 }
72
73 void pushFront(T* node)
74 {
75 ListNode<T>* it = &m_root;
76 insertAfter(it, node);
77 }
78
79 T* pop()
80 {
81 ListNode<T>* result = tail();
82 remove(result);
83 return static_cast<T*>(result);
84 }
85
86 T* popFront()
87 {
88 ListNode<T>* result = head();
89 remove(result);
90 return static_cast<T*>(result);
91 }
92
93 static void insertAfter(ListNode<T>* it, ListNode<T>* node)
94 {
95 ListNode<T>* prev = it;
96 ListNode<T>* next = it->next;
97
98 node->next = next;
99 next->prev = node;
100
101 node->prev = prev;
102 prev->next = node;
103 }
104
105 static void remove(ListNode<T>* node)
106 {
107 ListNode<T>* next = node->next;
108 ListNode<T>* prev = node->prev;
109
110 next->prev = prev;
111 prev->next = next;
112
113 node->prev = nullptr;
114 node->next = nullptr;
115 }
116
117private:
118 ListNode<T> m_root { &m_root, &m_root };
119};
120
121} // namespace bmalloc
122
123#endif // List_h
124