source: libcfa/src/collections/list.hfa@ b14d0d97

Last change on this file since b14d0d97 was ad41cbd, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

restructure forall clauses for dlist

  • Property mode set to 100644
File size: 13.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// list -- lets a user-defined stuct form intrusive linked lists
8//
9// Author : Michael Brooks
10// Created On : Wed Apr 22 18:00:00 2020
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Aug 7 10:46:08 2025
13// Update Count : 75
14//
15
16#pragma once
17
18#include <assert.h>
19
20forall( Decorator &, T & )
21struct tytagref {
22 inline T &;
23};
24
25forall( tOuter &, tMid &, tInner & )
26trait embedded {
27 tytagref( tMid, tInner ) ?`inner( tOuter & );
28};
29
30// embedded is reflexive, with no info (void) as the type tag
31forall( T & )
32static inline tytagref(void, T) ?`inner ( T & this ) { tytagref( void, T ) ret = {this}; return ret; }
33
34
35//
36// P9_EMBEDDED: Use on every case of plan-9 inheritance, to make "implements embedded" be a closure of plan-9 inheritance.
37//
38// struct foo {
39// int a, b, c;
40// inline (bar);
41// };
42// P9_EMBEDDED( foo, bar )
43//
44
45// usual version, for structs that are top-level declarations
46#define P9_EMBEDDED( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, static ) P9_EMBEDDED_BDY_( immedBase )
47
48// special version, for structs that are declared in functions
49#define P9_EMBEDDED_INFUNC( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, ) P9_EMBEDDED_BDY_( immedBase )
50
51// forward declarations of both the above; generally not needed
52// may help you control where the P9_EMBEEDED cruft goes, in case "right after the stuct" isn't where you want it
53#define P9_EMBEDDED_FWD( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, static ) ;
54#define P9_EMBEDDED_FWD_INFUNC( derived, immedBase ) auto P9_EMBEDDED_DECL_( derived, immedBase, ) ;
55
56// private helpers
57#define P9_EMBEDDED_DECL_( derived, immedBase, STORAGE ) \
58 forall( Tbase &, TdiscardPath & | { tytagref( TdiscardPath, Tbase ) ?`inner( immedBase & ); } ) \
59 STORAGE inline tytagref( immedBase, Tbase ) ?`inner( derived & this )
60
61#define P9_EMBEDDED_BDY_( immedBase ) { \
62 immedBase & ib = this; \
63 Tbase & b = ib`inner; \
64 tytagref( immedBase, Tbase ) result = { b }; \
65 return result; \
66 }
67
68#define EMBEDDED_VIA( OUTER, MID, INNER ) (struct { tytagref( MID, INNER ) ( * ?`inner ) ( OUTER & ); }){ ?`inner }
69
70#define DLINK_VIA( TE, TLINK ) EMBEDDED_VIA( TE, TLINK, dlink( TE ) )
71
72
73// The origin is the position encountered at the start of iteration, signifying, "need to advance to the first element,"
74// and at the end of iteration, signifying, "no more elements." Normal comsumption of an iterator runs "advance" as
75// the first step, and uses the return of "advance" as a guard, before dereferencing the iterator. So normal
76// consumption of an iterator does not dereference an iterator in origin position. The value of a pointer (underlying a
77// refence) that is exposed publicly as an iteraor, and also a pointer stored internally in a link field, is tagged, to
78// indicate "is the origin" (internally, is the list-head sentinel node), or untagged, to indicate "is a regular node."
79// Intent is to help a user who dereferences an iterator in origin position (which would be an API-use error on their
80// part), by failing fast.
81
82#ifdef __EXPERIMENTAL_DISABLE_OTAG__ // Perf experimention alt mode
83
84 // With origin tagging disabled, iteration never reports "no more elements."
85 // In this mode, the list API is buggy.
86 // This mode is used to quantify the cost of the normal tagging scheme.
87
88 #define ORIGIN_TAG_SET(p) (p)
89 #define ORIGIN_TAG_CLEAR(p) (p)
90 #define ORIGIN_TAG_QUERY(p) 0
91 #define ORIGIN_TAG_ASGN(p, v) (p)
92 #define ORIGIN_TAG_EITHER(p, v) (p)
93 #define ORIGIN_TAG_NEQ(v1, v2) 0
94
95#else // Normal
96
97 #if defined( __x86_64 )
98 // Preferred case: tag in the most-significant bit. Dereference
99 // has been shown to segfault consistently. Maintenance should
100 // list more architectures as "ok" here, to let them use the
101 // preferred case, when valid.
102 #define ORIGIN_TAG_BITNO ( 8 * sizeof( size_t ) - 1 )
103 #else
104 // Fallback case: tag in the least-significant bit. Dereference
105 // will often give an alignment error, but may not, e.g. if
106 // accessing a char-typed member. 32-bit x86 uses the most-
107 // significant bit for real room on the heap.
108 #define ORIGIN_TAG_BITNO 0
109 #endif
110
111 #define ORIGIN_TAG_MASK (((size_t)1) << ORIGIN_TAG_BITNO)
112
113 #define ORIGIN_TAG_SET(p) ((p) | ORIGIN_TAG_MASK)
114 #define ORIGIN_TAG_CLEAR(p) ((p) & ~ORIGIN_TAG_MASK)
115 #define ORIGIN_TAG_QUERY(p) ((p) & ORIGIN_TAG_MASK)
116
117 #define ORIGIN_TAG_ASGN(p, v) ( \
118 verify( ! ORIGIN_TAG_QUERY(p) && "p had no tagbit" ), \
119 ORIGIN_TAG_EITHER((p), (v)) \
120 )
121
122 #define ORIGIN_TAG_EITHER(p, v) ( \
123 verify( ! ORIGIN_TAG_CLEAR(v) && "v is a pure tagbit" ), \
124 ( (p) | (v) ) \
125 )
126
127 #define ORIGIN_TAG_NEQ(v1, v2) ( \
128 verify( ! ORIGIN_TAG_CLEAR(v1) && "v1 is a pure tagbit" ), \
129 verify( ! ORIGIN_TAG_CLEAR(v2) && "v2 is a pure tagbit" ), \
130 ( (v1) ^ (v2) ) \
131 )
132
133#endif
134
135
136
137#ifdef __EXPERIMENTAL_LOOSE_SINGLES__ // Perf experimention alt mode
138
139 // In loose-singles mode, the ability to answer an "is listed" query is disabled, as is "to insert an element,
140 // it must not be listed already" checking. The user must know separately whether an element is listed.
141 // Other than inserting it, any list-api action on an unlisted element is undefined. Notably, list iteration
142 // starting from an unlisted element is not defined to respond "no more elements," and may instead continue
143 // iterating from a formerly occupied list position. This mode matches LQ usage.
144
145 #define NOLOOSE(...)
146 #define LOOSEONLY(...) __VA_ARGS__
147
148#else // Normal
149
150 #define NOLOOSE(...) __VA_ARGS__
151 #define LOOSEONLY(...)
152
153#endif
154
155
156forall( tE & ) {
157 struct dlink{
158 tE * next, * prev;
159 };
160
161 static inline void ?{}( dlink( tE ) & this ) {
162 NOLOOSE(
163 this.next = this.prev = 0p;
164 )
165 }
166
167 forall( tLinks & = dlink( tE ) ) {
168 struct dlist {
169 inline dlink( tE );
170 };
171
172 forall( | embedded( tE, tLinks, dlink( tE ) ) ) {
173 static inline tE * $get_list_origin_addr( dlist( tE, tLinks ) & list ) {
174 dlink( tE ) & link_from_null = (*(tE *)0p)`inner;
175 ptrdiff_t link_offset = (ptrdiff_t)&link_from_null;
176 size_t origin_addr = ((size_t)&list) - link_offset;
177 size_t preResult = ORIGIN_TAG_SET( origin_addr );
178 return (tE *)preResult;
179 }
180
181 static inline void ?{}( dlist( tE, tLinks ) & this ) {
182 tE * listOrigin = $get_list_origin_addr( this );
183 ((dlink( tE ) &)this){ listOrigin, listOrigin };
184 }
185 }
186 }
187}
188
189
190static inline forall( tE &, tLinks & | embedded( tE, tLinks, dlink( tE ) ) ) {
191 bool isListed( tE & node ) {
192 NOLOOSE(
193 verify( &node != 0p );
194 dlink( tE ) & node_links = node`inner;
195 return (node_links.prev != 0p) || (node_links.next != 0p);
196 )
197 LOOSEONLY(
198 verify(false && "isListed is undefined");
199 return true;
200 )
201 }
202
203 bool isEmpty( dlist( tE, tLinks ) & list ) {
204 tE * firstPtr = list.next;
205 if ( ORIGIN_TAG_QUERY(( size_t)firstPtr) ) firstPtr = 0p;
206 return firstPtr == 0p;
207 }
208
209 tE & first( dlist( tE, tLinks ) & list ) {
210 tE * firstPtr = list.next;
211 if ( ORIGIN_TAG_QUERY( (size_t)firstPtr ) ) firstPtr = 0p;
212 return *firstPtr;
213 }
214
215 tE & last( dlist( tE, tLinks ) & list ) {
216 tE * lastPtr = list.prev;
217 if ( ORIGIN_TAG_QUERY( (size_t)lastPtr) ) lastPtr = 0p;
218 return *lastPtr;
219 }
220
221 tE & insert_before( tE & before, tE & node ) {
222 verify( &before != 0p );
223 verify( &node != 0p );
224
225 dlink( tE ) & linkToInsert = node`inner;
226 NOLOOSE(
227 verify( linkToInsert.next == 0p );
228 verify( linkToInsert.prev == 0p );
229 )
230
231 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before );
232 dlink( tE ) & list_pos_links = list_pos_elem`inner;
233 asm( "" : : : "memory" );
234 tE & before_raw = *list_pos_links.prev;
235 tE & before_elem = *(tE *) ORIGIN_TAG_CLEAR( (size_t)&before_raw );
236 linkToInsert.next = &before;
237 linkToInsert.prev = &before_raw;
238 dlink( tE ) & beforeLinks = before_elem`inner;
239 beforeLinks.next = &node;
240 list_pos_links.prev = &node;
241 asm( "" : : : "memory" );
242 return node;
243 }
244
245 tE & insert_after( tE & after, tE & node ) {
246 verify( &after != 0p );
247 verify( &node != 0p );
248
249 dlink( tE ) & linkToInsert = node`inner;
250 NOLOOSE(
251 verify( linkToInsert.prev == 0p );
252 verify( linkToInsert.next == 0p );
253 )
254
255 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after );
256 dlink( tE ) & list_pos_links = list_pos_elem`inner;
257 asm( "" : : : "memory" );
258 tE & after_raw = *list_pos_links.next;
259 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
260 linkToInsert.prev = &after;
261 linkToInsert.next = &after_raw;
262 dlink( tE ) & afterLinks = after_elem`inner;
263 afterLinks.prev = &node;
264 list_pos_links.next = &node;
265 asm( "" : : : "memory" );
266 return node;
267 }
268
269 tE & remove( tE & node ) {
270 verify( &node != 0p );
271 verify( ! ORIGIN_TAG_QUERY( (size_t)&node) );
272
273 dlink( tE ) & list_pos_links = node`inner;
274 tE & before_raw = *list_pos_links.prev;
275 tE & before_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before_raw );
276 dlink( tE ) & before_links = before_elem`inner;
277 tE & after_raw = *list_pos_links.next;
278 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
279 dlink( tE ) & after_links = after_elem`inner;
280 before_links.next = &after_raw;
281 after_links.prev = &before_raw;
282
283 NOLOOSE(
284 asm( "" : : : "memory" );
285 list_pos_links.prev = 0p;
286 list_pos_links.next = 0p;
287 asm( "" : : : "memory" );
288 )
289 return node;
290 }
291
292 tE & iter( dlist( tE, tLinks ) & list ) {
293 tE * origin = $get_list_origin_addr( list );
294 return *origin;
295 }
296
297 bool recede( tE && refx ) {
298 tE && ref_inner = refx;
299 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
300 &ref_inner = oldReferent`inner.prev;
301 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
302 }
303
304 bool advance( tE && refx ) {
305 tE && ref_inner = refx;
306 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
307 &ref_inner = oldReferent`inner.next;
308 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
309 }
310
311 bool isFirst( tE & node ) {
312 return recede( node );
313 }
314
315 bool isLast( tE & node ) {
316 return advance( node );
317 }
318
319 tE & prev( tE & node ) {
320 if ( recede( node ) ) return node;
321 return *0p;
322 }
323
324 tE & next( tE & node ) {
325 if ( advance( node ) ) return node;
326 return *0p;
327 }
328
329 tE & insert_first( dlist( tE, tLinks ) & list, tE & node ) {
330 insert_after( iter( list ), node );
331 return node;
332 }
333
334 tE & insert_last( dlist( tE, tLinks ) & list, tE & node ) {
335 insert_before( iter( list ), node );
336 return node;
337 }
338 tE & insert( dlist( tE, tLinks ) & list, tE & node ) { // synonym for insert_last
339 insert_last( list, node );
340 return node;
341 }
342
343 tE & remove_first( dlist( tE, tLinks ) & list ) {
344 tE & first_node = first( list );
345 if ( &first_node ) return remove( first_node );
346 return first_node;
347 }
348
349 tE & remove_last( dlist( tE, tLinks ) & list ) {
350 tE & last_node = last( list );
351 if ( &last_node ) return remove( last_node );
352 return last_node;
353 }
354
355 // Transfer the "from" list to the end of this sequence; the "from" list is empty after the transfer.
356// void transfer( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from ) {
357// if ( isEmpty( from ) ) return; // "from" list empty ?
358// if ( isEmpty( to ) ) { // "to" list empty ?
359// root = from.root;
360// } else { // "to" list not empty
361// T * toEnd = (T *)uBack( root );
362// T * fromEnd = (T *)from.uBack( from.root );
363// uBack( root ) = fromEnd;
364// from.uNext( fromEnd ) = root;
365// from.uBack( from.root ) = toEnd;
366// uNext( toEnd ) = from.root;
367// } // if
368// from.root = nullptr; // mark "from" list empty
369// }
370
371 // Transfer the "from" list up to node "n" to the end of this list; the "from" list becomes the sequence after node "n".
372 // Node "n" must be in the "from" list.
373// void split( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from, tE & node ) {
374// #ifdef __U_DEBUG__
375// if ( ! n->listed() ) abort( "(uSequence &)%p.split( %p ) : Node is not on a list.", this, n );
376// #endif // __U_DEBUG__
377// uSequence<T> to;
378// to.root = from.root; // start of "to" list
379// from.root = (T *)uNext( n ); // start of "from" list
380// if ( to.root == from.root ) { // last node in list ?
381// from.root = nullptr; // mark "from" list empty
382// } else {
383// uBack( from.root ) = (T *)uBack( to.root ); // fix "from" list
384// uNext( uBack( to.root ) ) = from.root;
385// uNext( n ) = to.root; // fix "to" list
386// uBack( to.root ) = n;
387// } // if
388// transfer( to );
389// }
390
391 #if ! defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
392 bool $validate_fwd( dlist( tE, tLinks ) & this ) {
393 if ( ! & first( this ) ) return &last( this ) == 0p;
394
395 tE & lagElem = *0p;
396 while ( tE & it = iter( this ); advance( it ) ) {
397 if ( & lagElem == 0p && &it != & first( this ) ) return false;
398 &lagElem = &it;
399 }
400
401 if ( &lagElem != &last( this ) ) return false;
402
403 // TODO: verify that it is back at iter( this );
404 return true;
405 }
406
407 bool $validate_rev( dlist( tE, tLinks ) & this ) {
408 if ( ! & last( this ) ) return &first( this ) == 0p;
409
410 tE & lagElem = *0p;
411 while ( tE & it = iter( this ); recede( it ) ) {
412 if ( &lagElem == 0p && &it != & last( this ) ) return false;
413 &lagElem = &it;
414 }
415
416 if ( &lagElem != &first( this ) ) return false;
417
418 // TODO: verify that it is back at iter( this );
419 return true;
420 }
421
422 bool validate( dlist( tE, tLinks ) & this ) {
423 return $validate_fwd( this ) && $validate_rev( this );
424 }
425 #endif
426}
427
428// TEMPORARY, until foreach statement created.
429#define FOREACH( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ); )
430#define FOREACH_REV( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); recede( index ); )
431#define FOREACH_COND( list, index, expr ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ) && !(expr); )
432#define FOREACH_REV_COND( list, index, expr ) for ( typeof(iter( list )) & (index) = iter( list ); recede( index ) && !(expr); )
Note: See TracBrowser for help on using the repository browser.