inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/tcpip/transport/tcp/tcp_endpoint_list.go (about) 1 package tcp 2 3 // ElementMapper provides an identity mapping by default. 4 // 5 // This can be replaced to provide a struct that maps elements to linker 6 // objects, if they are not the same. An ElementMapper is not typically 7 // required if: Linker is left as is, Element is left as is, or Linker and 8 // Element are the same type. 9 type endpointElementMapper struct{} 10 11 // linkerFor maps an Element to a Linker. 12 // 13 // This default implementation should be inlined. 14 // 15 //go:nosplit 16 func (endpointElementMapper) linkerFor(elem *endpoint) *endpoint { return elem } 17 18 // List is an intrusive list. Entries can be added to or removed from the list 19 // in O(1) time and with no additional memory allocations. 20 // 21 // The zero value for List is an empty list ready to use. 22 // 23 // To iterate over a list (where l is a List): 24 // for e := l.Front(); e != nil; e = e.Next() { 25 // // do something with e. 26 // } 27 // 28 // +stateify savable 29 type endpointList struct { 30 head *endpoint 31 tail *endpoint 32 } 33 34 // Reset resets list l to the empty state. 35 func (l *endpointList) Reset() { 36 l.head = nil 37 l.tail = nil 38 } 39 40 // Empty returns true iff the list is empty. 41 // 42 //go:nosplit 43 func (l *endpointList) Empty() bool { 44 return l.head == nil 45 } 46 47 // Front returns the first element of list l or nil. 48 // 49 //go:nosplit 50 func (l *endpointList) Front() *endpoint { 51 return l.head 52 } 53 54 // Back returns the last element of list l or nil. 55 // 56 //go:nosplit 57 func (l *endpointList) Back() *endpoint { 58 return l.tail 59 } 60 61 // Len returns the number of elements in the list. 62 // 63 // NOTE: This is an O(n) operation. 64 // 65 //go:nosplit 66 func (l *endpointList) Len() (count int) { 67 for e := l.Front(); e != nil; e = (endpointElementMapper{}.linkerFor(e)).Next() { 68 count++ 69 } 70 return count 71 } 72 73 // PushFront inserts the element e at the front of list l. 74 // 75 //go:nosplit 76 func (l *endpointList) PushFront(e *endpoint) { 77 linker := endpointElementMapper{}.linkerFor(e) 78 linker.SetNext(l.head) 79 linker.SetPrev(nil) 80 if l.head != nil { 81 endpointElementMapper{}.linkerFor(l.head).SetPrev(e) 82 } else { 83 l.tail = e 84 } 85 86 l.head = e 87 } 88 89 // PushBack inserts the element e at the back of list l. 90 // 91 //go:nosplit 92 func (l *endpointList) PushBack(e *endpoint) { 93 linker := endpointElementMapper{}.linkerFor(e) 94 linker.SetNext(nil) 95 linker.SetPrev(l.tail) 96 if l.tail != nil { 97 endpointElementMapper{}.linkerFor(l.tail).SetNext(e) 98 } else { 99 l.head = e 100 } 101 102 l.tail = e 103 } 104 105 // PushBackList inserts list m at the end of list l, emptying m. 106 // 107 //go:nosplit 108 func (l *endpointList) PushBackList(m *endpointList) { 109 if l.head == nil { 110 l.head = m.head 111 l.tail = m.tail 112 } else if m.head != nil { 113 endpointElementMapper{}.linkerFor(l.tail).SetNext(m.head) 114 endpointElementMapper{}.linkerFor(m.head).SetPrev(l.tail) 115 116 l.tail = m.tail 117 } 118 m.head = nil 119 m.tail = nil 120 } 121 122 // InsertAfter inserts e after b. 123 // 124 //go:nosplit 125 func (l *endpointList) InsertAfter(b, e *endpoint) { 126 bLinker := endpointElementMapper{}.linkerFor(b) 127 eLinker := endpointElementMapper{}.linkerFor(e) 128 129 a := bLinker.Next() 130 131 eLinker.SetNext(a) 132 eLinker.SetPrev(b) 133 bLinker.SetNext(e) 134 135 if a != nil { 136 endpointElementMapper{}.linkerFor(a).SetPrev(e) 137 } else { 138 l.tail = e 139 } 140 } 141 142 // InsertBefore inserts e before a. 143 // 144 //go:nosplit 145 func (l *endpointList) InsertBefore(a, e *endpoint) { 146 aLinker := endpointElementMapper{}.linkerFor(a) 147 eLinker := endpointElementMapper{}.linkerFor(e) 148 149 b := aLinker.Prev() 150 eLinker.SetNext(a) 151 eLinker.SetPrev(b) 152 aLinker.SetPrev(e) 153 154 if b != nil { 155 endpointElementMapper{}.linkerFor(b).SetNext(e) 156 } else { 157 l.head = e 158 } 159 } 160 161 // Remove removes e from l. 162 // 163 //go:nosplit 164 func (l *endpointList) Remove(e *endpoint) { 165 linker := endpointElementMapper{}.linkerFor(e) 166 prev := linker.Prev() 167 next := linker.Next() 168 169 if prev != nil { 170 endpointElementMapper{}.linkerFor(prev).SetNext(next) 171 } else if l.head == e { 172 l.head = next 173 } 174 175 if next != nil { 176 endpointElementMapper{}.linkerFor(next).SetPrev(prev) 177 } else if l.tail == e { 178 l.tail = prev 179 } 180 181 linker.SetNext(nil) 182 linker.SetPrev(nil) 183 } 184 185 // Entry is a default implementation of Linker. Users can add anonymous fields 186 // of this type to their structs to make them automatically implement the 187 // methods needed by List. 188 // 189 // +stateify savable 190 type endpointEntry struct { 191 next *endpoint 192 prev *endpoint 193 } 194 195 // Next returns the entry that follows e in the list. 196 // 197 //go:nosplit 198 func (e *endpointEntry) Next() *endpoint { 199 return e.next 200 } 201 202 // Prev returns the entry that precedes e in the list. 203 // 204 //go:nosplit 205 func (e *endpointEntry) Prev() *endpoint { 206 return e.prev 207 } 208 209 // SetNext assigns 'entry' as the entry that follows e in the list. 210 // 211 //go:nosplit 212 func (e *endpointEntry) SetNext(elem *endpoint) { 213 e.next = elem 214 } 215 216 // SetPrev assigns 'entry' as the entry that precedes e in the list. 217 // 218 //go:nosplit 219 func (e *endpointEntry) SetPrev(elem *endpoint) { 220 e.prev = elem 221 }