1 | /* |
---|
2 | * xlist.h - Trans-cluster double circular linked list, using extended pointers. |
---|
3 | * |
---|
4 | * Author : Alain Greiner (2016,2017,2018,2019,2020) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef _XLIST_H_ |
---|
25 | #define _XLIST_H_ |
---|
26 | |
---|
27 | #include <kernel_config.h> |
---|
28 | #include <hal_kernel_types.h> |
---|
29 | #include <hal_remote.h> |
---|
30 | #include <printk.h> |
---|
31 | |
---|
32 | /**** global variables ***/ |
---|
33 | |
---|
34 | cxy_t local_cxy; |
---|
35 | |
---|
36 | /*************************************************************************** |
---|
37 | * This structure defines an Extended Double Circular Linked List entry. |
---|
38 | * Note (1) The list root is an extra xlist_entry_t, that is NOT part |
---|
39 | * of the set of linked elements. |
---|
40 | * Note (2) Do NOT change the fields order in this struct. |
---|
41 | **************************************************************************/ |
---|
42 | |
---|
43 | typedef struct xlist_entry_s |
---|
44 | { |
---|
45 | xptr_t next; // extended pointer on next xlist_entry_t |
---|
46 | xptr_t pred; // extended pointer on previous xlist_entry_t |
---|
47 | } |
---|
48 | xlist_entry_t; |
---|
49 | |
---|
50 | /*************************************************************************** |
---|
51 | * This macro returns the offset (in bytes) of a field in a structure. |
---|
52 | *************************************************************************** |
---|
53 | * @ type : structure type |
---|
54 | * @ member : name of the field |
---|
55 | **************************************************************************/ |
---|
56 | |
---|
57 | #ifndef OFFSETOF |
---|
58 | #define OFFSETOF( type , member ) ((intptr_t) & ((type *)0)->member) |
---|
59 | #endif |
---|
60 | |
---|
61 | /*************************************************************************** |
---|
62 | * This macro returns an extended pointer on the structure containing an |
---|
63 | * embedded xlist_entry_t field. |
---|
64 | *************************************************************************** |
---|
65 | * @ xlist_xp : extended pointer on the xlist_entry_t field |
---|
66 | * @ type : type of the structure containing the xlist_entry_t |
---|
67 | * @ member : name of the xlist_entry_t field |
---|
68 | **************************************************************************/ |
---|
69 | |
---|
70 | #define XLIST_ELEMENT( xlist_xp , type , member ) \ |
---|
71 | (xlist_xp - OFFSETOF( type , member )) |
---|
72 | |
---|
73 | /*************************************************************************** |
---|
74 | * This macro returns an extended pointer on the first element of an |
---|
75 | * extended double linked list, identified by the extended pointer on |
---|
76 | * the root xlist_entry_t. |
---|
77 | * WARNING : check list non empty before using this macro. |
---|
78 | *************************************************************************** |
---|
79 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
80 | * @ type : type of the linked elements |
---|
81 | * @ member : name of the xlist_entry_t field |
---|
82 | **************************************************************************/ |
---|
83 | |
---|
84 | #define XLIST_FIRST( root_xp , type , member ) \ |
---|
85 | ({ xptr_t __first = hal_remote_l64( root_xp ); \ |
---|
86 | XLIST_ELEMENT( __first , type , member ); }) |
---|
87 | |
---|
88 | /*************************************************************************** |
---|
89 | * This macro returns an extended pointer on the last element of an |
---|
90 | * extended double linked list, identified by the extended pointer on |
---|
91 | * the root xlist_entry_t. |
---|
92 | * WARNING : check list non empty before using this macro. |
---|
93 | *************************************************************************** |
---|
94 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
95 | * @ type : type of the linked elements |
---|
96 | * @ member : name of the xlist_entry_t field |
---|
97 | **************************************************************************/ |
---|
98 | |
---|
99 | #define XLIST_LAST( root_xp , type , member ) \ |
---|
100 | ({ xptr_t __last = hal_remote_l64( root_xp + sizeof(xptr_t) ); \ |
---|
101 | XLIST_ELEMENT( __last , type , member ); }) |
---|
102 | |
---|
103 | /*************************************************************************** |
---|
104 | * This macro traverses an extended double linked list in forward order. |
---|
105 | * WARNING : the iter variable should NOT be deleted during traversal. |
---|
106 | *************************************************************************** |
---|
107 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
108 | * @ iter_xp : current extended pointer on a xlist_entry_t |
---|
109 | **************************************************************************/ |
---|
110 | |
---|
111 | #define XLIST_FOREACH( root_xp , iter_xp ) \ |
---|
112 | for( (iter_xp) = hal_remote_l64( root_xp ) ; \ |
---|
113 | (iter_xp) != (root_xp) ; \ |
---|
114 | (iter_xp) = hal_remote_l64( iter_xp ) ) |
---|
115 | |
---|
116 | /*************************************************************************** |
---|
117 | * This macro traverses an extended double linked list in backward order. |
---|
118 | * WARNING : the iter variable should NOT be deleted during traversal. |
---|
119 | *************************************************************************** |
---|
120 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
121 | * @ iter_xp : current extended pointer on a xlist_entry_t |
---|
122 | **************************************************************************/ |
---|
123 | |
---|
124 | #define XLIST_FOREACH_BACKWARD( root_xp , iter_xp ) \ |
---|
125 | for( (iter_xp) = hal_remote_l64( (root_xp) + sizeof(xptr_t) ) ; \ |
---|
126 | (iter_xp) != (root_xp) ; \ |
---|
127 | (iter_xp) = hal_remote_l64( (iter_xp) + sizeof(xptr_t) ) ) |
---|
128 | |
---|
129 | /*************************************************************************** |
---|
130 | * This function returns an extended pointer on the next xlist_entry_t, |
---|
131 | * from an extended pointer on a reference xlist_entry_t. |
---|
132 | *************************************************************************** |
---|
133 | * @ root : extended pointer on the root xlist_entry_t |
---|
134 | * @ ref : extended pointer on the reference xlist_entry_t |
---|
135 | **************************************************************************/ |
---|
136 | static inline xptr_t xlist_next( xptr_t root, |
---|
137 | xptr_t ref ) |
---|
138 | { |
---|
139 | // get root->next |
---|
140 | xptr_t root_next = (xptr_t)hal_remote_l64( root ); |
---|
141 | |
---|
142 | // get ref->next |
---|
143 | xptr_t ref_next = (xptr_t)hal_remote_l64( ref ); |
---|
144 | |
---|
145 | // test if list is empty or ref is the last element |
---|
146 | if( (root_next == root) || (ref_next == root) ) return XPTR_NULL; |
---|
147 | |
---|
148 | return ref_next; |
---|
149 | } |
---|
150 | |
---|
151 | /*************************************************************************** |
---|
152 | * This function returns an extended pointer on the previous xlist_entry_t. |
---|
153 | *************************************************************************** |
---|
154 | * @ root : extended pointer on the root xlist_entry_t |
---|
155 | * @ ref : extended pointer on the reference xlist_entry_t |
---|
156 | **************************************************************************/ |
---|
157 | static inline xptr_t xlist_pred( xptr_t root, |
---|
158 | xptr_t ref ) |
---|
159 | { |
---|
160 | // get root->next |
---|
161 | xptr_t root_next = (xptr_t)hal_remote_l64( root ); |
---|
162 | |
---|
163 | // get ref->pred |
---|
164 | xptr_t ref_pred = (xptr_t)hal_remote_l64( ref + sizeof(xptr_t) ); |
---|
165 | |
---|
166 | // test if list is empty or ref is the first element |
---|
167 | if( (root_next == root) || (ref_pred == root) ) return XPTR_NULL; |
---|
168 | |
---|
169 | return ref_pred; |
---|
170 | } |
---|
171 | |
---|
172 | /*************************************************************************** |
---|
173 | * This function initialises the root of an extended double linked list. |
---|
174 | * The root can be located in any cluster. |
---|
175 | *************************************************************************** |
---|
176 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
177 | **************************************************************************/ |
---|
178 | static inline void xlist_root_init( xptr_t root_xp ) |
---|
179 | { |
---|
180 | hal_remote_s64( root_xp , root_xp ); |
---|
181 | hal_remote_s64( root_xp + sizeof(xptr_t) , root_xp ); |
---|
182 | } |
---|
183 | |
---|
184 | /*************************************************************************** |
---|
185 | * This function initialises an entry of an extended double linked list. |
---|
186 | * The entry can be located in any cluster. |
---|
187 | *************************************************************************** |
---|
188 | * @ entry_xp : extended pointer on the xlist_entry_t |
---|
189 | **************************************************************************/ |
---|
190 | static inline void xlist_entry_init( xptr_t entry_xp ) |
---|
191 | { |
---|
192 | hal_remote_s64( entry_xp , 0 ); |
---|
193 | hal_remote_s64( entry_xp + sizeof(xptr_t) , 0 ); |
---|
194 | } |
---|
195 | |
---|
196 | /*************************************************************************** |
---|
197 | * This function inserts a new entry in the first place of an extended |
---|
198 | * double linked list. Four extended pointers must be modified. |
---|
199 | * The lock protecting the list should have been previously taken. |
---|
200 | *************************************************************************** |
---|
201 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
202 | * @ entry_xp : extended pointer on the xlist_entry_t to be inserted |
---|
203 | **************************************************************************/ |
---|
204 | static inline void xlist_add_first( xptr_t root_xp, |
---|
205 | xptr_t entry_xp ) |
---|
206 | { |
---|
207 | // get the extended pointer on the first element in list |
---|
208 | xptr_t first_xp = hal_remote_l64( root_xp ); |
---|
209 | |
---|
210 | // update root_xp->next <= entry_xp |
---|
211 | hal_remote_s64( root_xp , entry_xp ); |
---|
212 | |
---|
213 | // update entry_xp->next <= first_xp |
---|
214 | hal_remote_s64( entry_xp , first_xp ); |
---|
215 | |
---|
216 | // update entry_xp->pred <= root_xp |
---|
217 | hal_remote_s64( entry_xp + sizeof(xptr_t) , root_xp ); |
---|
218 | |
---|
219 | // update first_xp->pred <= entry_xp |
---|
220 | hal_remote_s64( first_xp + sizeof(xptr_t) , entry_xp ); |
---|
221 | } |
---|
222 | |
---|
223 | /*************************************************************************** |
---|
224 | * This function inserts a new entry in the last place of an extended |
---|
225 | * double linked list. Four extended pointers must be modified. |
---|
226 | * The lock protecting the list should have been previously taken. |
---|
227 | *************************************************************************** |
---|
228 | * @ root_xp : extended pointer on the root xlist_entry_t |
---|
229 | * @ entry_xp : extended pointer on the xlist_entry_t to be inserted |
---|
230 | **************************************************************************/ |
---|
231 | static inline void xlist_add_last( xptr_t root_xp, |
---|
232 | xptr_t entry_xp ) |
---|
233 | { |
---|
234 | // get the extended pointer on the last element in list |
---|
235 | xptr_t last_xp = hal_remote_l64( root_xp + sizeof(xptr_t) ); |
---|
236 | |
---|
237 | // update root_xp->pred <= entry_xp |
---|
238 | hal_remote_s64( root_xp + sizeof(xptr_t) , entry_xp ); |
---|
239 | |
---|
240 | // update entry_xp->pred <= last_xp |
---|
241 | hal_remote_s64( entry_xp + sizeof(xptr_t) , last_xp ); |
---|
242 | |
---|
243 | // update entry_xp->next <= root_xp |
---|
244 | hal_remote_s64( entry_xp , root_xp ); |
---|
245 | |
---|
246 | // update last_xp->next <= entry_xp |
---|
247 | hal_remote_s64( last_xp , entry_xp ); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | /*************************************************************************** |
---|
252 | * This function returns true if the list is empty. |
---|
253 | *************************************************************************** |
---|
254 | * @ root_xp : extended pointer on the root xlist_entry_t. |
---|
255 | **************************************************************************/ |
---|
256 | static inline bool_t xlist_is_empty( xptr_t root_xp ) |
---|
257 | { |
---|
258 | // get the extended pointer root.next value |
---|
259 | xptr_t next = (xptr_t)hal_remote_l64( root_xp ); |
---|
260 | |
---|
261 | return ( root_xp == next ); |
---|
262 | } |
---|
263 | |
---|
264 | /*************************************************************************** |
---|
265 | * This function removes an entry from an extended double linked list. |
---|
266 | * The memory allocated to the removed entry is not released. |
---|
267 | *************************************************************************** |
---|
268 | * @ xp : extended pointer on the xlist_entry_t to be removed. |
---|
269 | **************************************************************************/ |
---|
270 | static inline void xlist_unlink( xptr_t xp ) |
---|
271 | { |
---|
272 | // get a local copy of the xlist_entry_t to be removed |
---|
273 | xlist_entry_t entry; |
---|
274 | hal_remote_memcpy( XPTR( local_cxy , &entry ) , |
---|
275 | xp , |
---|
276 | sizeof(xlist_entry_t) ); |
---|
277 | |
---|
278 | xptr_t next = entry.next; |
---|
279 | xptr_t pred = entry.pred; |
---|
280 | |
---|
281 | // update pred.next & next.pred |
---|
282 | hal_remote_s64( pred , (uint64_t)next ); |
---|
283 | hal_remote_s64( next + sizeof(xptr_t) , (uint64_t)pred ); |
---|
284 | |
---|
285 | // reset the removed entry itself |
---|
286 | hal_remote_s64( xp , XPTR_NULL ); |
---|
287 | hal_remote_s64( xp + sizeof(xptr_t) , XPTR_NULL ); |
---|
288 | } |
---|
289 | |
---|
290 | /*************************************************************************** |
---|
291 | * This function replaces an entry in an extended double linked list. |
---|
292 | * Four extended pointers must be modified. |
---|
293 | * The memory allocated to the removed entry is not released. |
---|
294 | *************************************************************************** |
---|
295 | * @ old : extended pointer on the xlist_entry_t to be removed. |
---|
296 | * @ new : extended pointer on the xlist_entry_t to be inserted. |
---|
297 | **************************************************************************/ |
---|
298 | static inline void xlist_replace( xptr_t old, |
---|
299 | xptr_t new ) |
---|
300 | { |
---|
301 | // get a local copy of the xlist_entry_t to be removed |
---|
302 | xlist_entry_t entry; |
---|
303 | hal_remote_memcpy( XPTR( local_cxy , &entry ) , |
---|
304 | old , |
---|
305 | sizeof(xlist_entry_t) ); |
---|
306 | |
---|
307 | xptr_t next = entry.next; |
---|
308 | xptr_t pred = entry.pred; |
---|
309 | |
---|
310 | // update new.next <= next |
---|
311 | hal_remote_s64( new , (uint64_t)next ); |
---|
312 | |
---|
313 | // update new.pred <= pred |
---|
314 | hal_remote_s64( new + sizeof(xptr_t) , (uint64_t)pred ); |
---|
315 | |
---|
316 | // update pred.next <= new |
---|
317 | hal_remote_s64( pred , (uint64_t)new ); |
---|
318 | |
---|
319 | // update next.pred <= new |
---|
320 | hal_remote_s64( next + sizeof(xptr_t) , (uint64_t)new ); |
---|
321 | } |
---|
322 | |
---|
323 | /*************************************************************************** |
---|
324 | * This debug function displays <max> entries of an xlist. |
---|
325 | *************************************************************************** |
---|
326 | * @ root_xp : extended pointer on the root xlist_entry_t. |
---|
327 | * @ string : list identifier displayed in header. |
---|
328 | * @ max : max number of éléments to display. |
---|
329 | **************************************************************************/ |
---|
330 | static inline void xlist_display( xptr_t root_xp, |
---|
331 | char * string, |
---|
332 | uint32_t max ) |
---|
333 | { |
---|
334 | cxy_t root_cxy; |
---|
335 | xlist_entry_t * root_ptr; |
---|
336 | |
---|
337 | xptr_t iter_xp; |
---|
338 | cxy_t iter_cxy; |
---|
339 | xlist_entry_t * iter_ptr; |
---|
340 | |
---|
341 | xptr_t next_xp; |
---|
342 | cxy_t next_cxy; |
---|
343 | xlist_entry_t * next_ptr; |
---|
344 | |
---|
345 | xptr_t pred_xp; |
---|
346 | cxy_t pred_cxy; |
---|
347 | xlist_entry_t * pred_ptr; |
---|
348 | |
---|
349 | uint32_t index; |
---|
350 | |
---|
351 | root_cxy = GET_CXY( root_xp ); |
---|
352 | root_ptr = GET_PTR( root_xp ); |
---|
353 | |
---|
354 | next_xp = hal_remote_l64( XPTR( root_cxy , &root_ptr->next ) ); |
---|
355 | next_cxy = GET_CXY( next_xp ); |
---|
356 | next_ptr = GET_PTR( next_xp ); |
---|
357 | |
---|
358 | pred_xp = hal_remote_l64( XPTR( root_cxy , &root_ptr->pred ) ); |
---|
359 | pred_cxy = GET_CXY( pred_xp ); |
---|
360 | pred_ptr = GET_PTR( pred_xp ); |
---|
361 | |
---|
362 | printk("\n***** root (%x,%x) / next (%x,%x) / pred (%x,%x) / %s *****\n", |
---|
363 | root_cxy, root_ptr, next_cxy, next_ptr, pred_cxy, pred_ptr, string ); |
---|
364 | |
---|
365 | if( xlist_is_empty( root_xp ) == false ) |
---|
366 | { |
---|
367 | for( iter_xp = hal_remote_l64( XPTR( root_cxy , &root_ptr->next) ) , index = 0 ; |
---|
368 | (iter_xp != root_xp) && (index < max) ; |
---|
369 | iter_xp = next_xp , index++ ) |
---|
370 | { |
---|
371 | iter_cxy = GET_CXY( iter_xp ); |
---|
372 | iter_ptr = GET_PTR( iter_xp ); |
---|
373 | |
---|
374 | next_xp = hal_remote_l64( XPTR( iter_cxy , &iter_ptr->next ) ); |
---|
375 | next_cxy = GET_CXY( next_xp ); |
---|
376 | next_ptr = GET_PTR( next_xp ); |
---|
377 | |
---|
378 | pred_xp = hal_remote_l64( XPTR( iter_cxy , &iter_ptr->pred ) ); |
---|
379 | pred_cxy = GET_CXY( pred_xp ); |
---|
380 | pred_ptr = GET_PTR( pred_xp ); |
---|
381 | |
---|
382 | printk(" - %d : iter (%x,%x) / next (%x,%x) / pred (%x,%x)\n", |
---|
383 | index, iter_cxy, iter_ptr, next_cxy, next_ptr, pred_cxy, pred_ptr ); |
---|
384 | } |
---|
385 | } |
---|
386 | } // end xlist_display() |
---|
387 | |
---|
388 | #endif /* _XLIST_H_ */ |
---|