source: trunk/kernel/libk/xlist.h @ 603

Last change on this file since 603 was 603, checked in by alain, 5 years ago

Improve the FAT32 file system to support cat, rm, cp commands.

File size: 12.3 KB
RevLine 
[1]1/*
[563]2    // check calling thread can yield
3    thread_assert_can_yield( this , __FUNCTION__ );
4
[1]5 * xlist.h - Double Circular Linked lists, using extended pointers.
6 *
7 * Author : Alain Greiner (2016)
8 *
9 * Copyright (c) UPMC Sorbonne Universites
10 *
11 * This file is part of ALMOS-MKH.
12 *
13 * ALMOS-kernel is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 2.0 of the License.
16 *
17 * ALMOS-kernel is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
[603]27#ifndef _XLIST_H_
28#define _XLIST_H_
[1]29
[14]30#include <kernel_config.h>
[457]31#include <hal_kernel_types.h>
[1]32#include <hal_remote.h>
33
34/**** global variables ***/
35
36cxy_t  local_cxy;
37
38/***************************************************************************
39 * This structure defines an Extended Double Circular Linked List entry.
40 * Note (1) The list root is an extra xlist_entry_t, that is NOT part
41 *          of the set of linked elements.
42 * Note (2) Do NOT change the fields order in this struct.
43 **************************************************************************/
44
45typedef struct xlist_entry_s
46{
47    xptr_t  next;              // extended pointer on next xlist_entry_t
48    xptr_t  pred;              // extended pointer on previous xlist_entry_t
49}
50xlist_entry_t;
51
52/***************************************************************************
53 * This macro returns the offset (in bytes) of a field in a structure.
54 * @ type   : structure type
55 * @ member : name of the field
56 **************************************************************************/
57
58#ifndef OFFSETOF
[24]59#define OFFSETOF( type , member ) ((intptr_t) & ((type *)0)->member)
[1]60#endif
61
62/***************************************************************************
63 * This macro returns an extended pointer on the structure containing an
64 * embedded xlist_entry_t field.
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.
[435]77 * WARNING : check list non empty before using this macro.
[1]78 * @ root_xp : extended pointer on the root xlist_entry_t
79 * @ type    : type of the linked elements
80 * @ member  : name of the xlist_entry_t field
81 **************************************************************************/
82
[603]83#define XLIST_FIRST( root_xp , type , member )       \
84    ({ xptr_t __first = hal_remote_l64( root_xp );   \
[1]85           XLIST_ELEMENT( __first , type , member ); })
86
87/***************************************************************************
88 * This macro returns an extended pointer on the last element of an
89 * extended double linked list, identified by the extended pointer on
90 * the root xlist_entry_t.
[435]91 * WARNING : check list non empty before using this macro.
[1]92 * @ root_xp : extended pointer on the root xlist_entry_t
93 * @ type    : type of the linked elements
94 * @ member  : name of the xlist_entry_t field
95 **************************************************************************/
96
[603]97#define XLIST_LAST( root_xp , type , member )                       \
98    ({ xptr_t __last = hal_remote_l64( root_xp + sizeof(xptr_t) );  \
[1]99           XLIST_ELEMENT( __last , type , member ); })
100
101/***************************************************************************
102 * This macro traverses an extended double linked list in forward order.
[563]103 * WARNING : the iter variable should NOT be deleted during traversal.
[1]104 * @ root_xp  : extended pointer on the root xlist_entry_t
105 * @ iter_xp  : current extended pointer on a xlist_entry_t
106 **************************************************************************/
107
108#define XLIST_FOREACH( root_xp , iter_xp )    \
[563]109for( (iter_xp) = hal_remote_l64( root_xp ) ;  \
[1]110     (iter_xp) != (root_xp) ;                 \
[563]111     (iter_xp) = hal_remote_l64( iter_xp ) )
[1]112
113/***************************************************************************
114 * This macro traverses an extended double linked list in backward order.
[563]115 * WARNING : the iter variable should NOT be deleted during traversal.
[1]116 * @ root_xp  : extended pointer on the root xlist_entry_t
117 * @ iter_xp  : current extended pointer on a xlist_entry_t
118 **************************************************************************/
119
[603]120#define XLIST_FOREACH_BACKWARD( root_xp , iter_xp )              \
121for( (iter_xp) = hal_remote_l64( (root_xp) + sizeof(xptr_t) ) ;  \
122     (iter_xp) != (root_xp) ;                                    \
123     (iter_xp) = hal_remote_l64( (iter_xp) + sizeof(xptr_t) ) )
[1]124
125/***************************************************************************
126 * This function returns an extended pointer on the next xlist_entry_t,
127 * from an extended pointer on a reference xlist_entry_t.
128 * @ root    : extended pointer on the root xlist_entry_t
129 * @ ref     : extended pointer on the reference xlist_entry_t
130 **************************************************************************/
131static inline xptr_t xlist_next( xptr_t  root,
132                                 xptr_t  ref )
133{
134    // get root->next
[563]135    xptr_t root_next = (xptr_t)hal_remote_l64( root );
[1]136
137    // get ref->next
[563]138    xptr_t ref_next  = (xptr_t)hal_remote_l64( ref );
[1]139
140    // test if list is empty or ref is the last element 
141    if( (root_next == root) || (ref_next == root) )  return XPTR_NULL;
142   
143        return ref_next;
144}
145
146/***************************************************************************
147 * This function returns an extended pointer on the previous xlist_entry_t.
148 * @ root    : extended pointer on the root xlist_entry_t
149 * @ ref     : extended pointer on the reference xlist_entry_t
150 **************************************************************************/
151static inline xptr_t xlist_pred( xptr_t root,
152                                 xptr_t ref )
153{
154    // get root->next
[563]155    xptr_t root_next = (xptr_t)hal_remote_l64( root );
[1]156
157    // get ref->pred
[603]158    xptr_t ref_pred  = (xptr_t)hal_remote_l64( ref + sizeof(xptr_t) );
[1]159
160    // test if list is empty or ref is the first element 
161    if( (root_next == root) || (ref_pred == root) )  return XPTR_NULL;
162   
163        return ref_pred;
164}
165
166/***************************************************************************
167 * This function initialises the root of an extended double linked list.
168 * The root can be located in any cluster.
169 * @ root_xp   :  extended pointer on the root xlist_entry_t
[563]170xixi **************************************************************************/
[1]171static inline void xlist_root_init( xptr_t root_xp )
172{
[603]173    hal_remote_s64( root_xp                  , root_xp );
174    hal_remote_s64( root_xp + sizeof(xptr_t) , root_xp );
[1]175}
176
177/***************************************************************************
178 * This function initialises an entry of an extended double linked list.
179 * The entry can be located in any cluster.
180 * @ entry_xp  : extended pointer on the xlist_entry_t
181 **************************************************************************/
182static inline void xlist_entry_init( xptr_t entry_xp )
183{
[603]184    hal_remote_s64( entry_xp                  , 0 );
185    hal_remote_s64( entry_xp + sizeof(xptr_t) , 0 );
[1]186}
187
188/***************************************************************************
189 * This function inserts a new entry in the first place of an extended 
190 * double linked list. Four extended pointers must be modified.
191 * The lock protecting the list should have been previously taken.
192 * @ root   : extended pointer on the root xlist_entry_t
193 * @ entry  : extended pointer on the xlist_entry_t to be inserted
194 **************************************************************************/
195static inline void xlist_add_first( xptr_t root, 
196                                    xptr_t entry )
197{
198    // get the extended pointer on the first element in list
[563]199    xptr_t first = (xptr_t)hal_remote_l64( root );
[1]200
201    // update root.next <= entry
[563]202    hal_remote_s64( root , (uint64_t)entry );
[1]203
204    // update entry.next <= first
[563]205    hal_remote_s64( entry , (uint64_t)first );
[1]206
207    // entry.pred <= root
[603]208    hal_remote_s64( entry + sizeof(xptr_t) , (uint64_t)root );
[1]209   
210    // first.pred <= new
[603]211    hal_remote_s64( first + sizeof(xptr_t) , (uint64_t)entry );
[1]212}
213
214/***************************************************************************
215 * This function inserts a new entry in the last place of an extended 
216 * double linked list.  Four extended pointers must be modified.
217 * The lock protecting the list should have been previously taken.
218 * @ root   : extended pointer on the root xlist_entry_t
219 * @ entry  : extended pointer on the xlist_entry_t to be inserted
220 **************************************************************************/
221static inline void xlist_add_last( xptr_t root, 
222                                   xptr_t entry )
223{
224    // get the extended pointer on the last element in list
[603]225    xptr_t last = (xptr_t)hal_remote_l64( root + sizeof(xptr_t) );
[1]226
227    // update root.pred <= entry
[603]228    hal_remote_s64( root + sizeof(xptr_t) , (uint64_t)entry );
[1]229
230    // update entry.pred <= last
[603]231    hal_remote_s64( entry + sizeof(xptr_t) , (uint64_t)last );
[1]232
233    // entry.next <= root
[563]234    hal_remote_s64( entry , (uint64_t)root );
[1]235   
236    // last.next <= entry
[563]237    hal_remote_s64( last , (uint64_t)entry );
[1]238}
239
240
241/***************************************************************************
242 * This function returns true if the list is empty.
243 * @ root  : extended pointer on the root xlist_entry_t
244 **************************************************************************/
245static inline bool_t xlist_is_empty( xptr_t root )
246{
247    // get the extended pointer root.next value
[563]248    xptr_t next = (xptr_t)hal_remote_l64( root );
[1]249
250    return ( root == next );
251} 
252
253/***************************************************************************
254 * This function removes an entry from an extended  double linked list.
255 * Two extended pointers must be modified.
256 * The memory allocated to the removed entry is not released.
257 * @ xp : extended pointer on the xlist_entry_t to be removed.
258 **************************************************************************/
259static inline void xlist_unlink( xptr_t xp )
260{
261    // get a local copy of the xlist_entry_t to be removed
262    xlist_entry_t entry;
263    hal_remote_memcpy( XPTR( local_cxy , &entry ) ,
[603]264                       xp , 
265                       sizeof(xlist_entry_t) );
[1]266
267    xptr_t next = entry.next;
268    xptr_t pred = entry.pred;
269
270    // update pred.next <= next
[563]271    hal_remote_s64( pred , (uint64_t)next );
[1]272
273    // update next.pred <= pred
[603]274    hal_remote_s64( next + sizeof(xptr_t) , (uint64_t)pred );
[1]275}
276
277/***************************************************************************
278 * This function replaces an entry in an extended double linked list.
279 * Four extended pointers must be modified.
280 * The memory allocated to the removed entry is not released.
281 * @old      : extended pointer on the xlist_entry_t to be removed.
282 * @new      : extended pointer on the xlist_entry_t to be inserted.
283 **************************************************************************/
284static inline void xlist_replace( xptr_t old,
285                                  xptr_t new )
286{
287    // get a local copy of the xlist_entry_t to be removed
288    xlist_entry_t entry;
289    hal_remote_memcpy( XPTR( local_cxy , &entry ) , 
290                                  old , 
291                                  sizeof(xlist_entry_t) );
292
293    xptr_t next = entry.next;
294    xptr_t pred = entry.pred;
295
296        // update new.next <= next
[563]297    hal_remote_s64( new , (uint64_t)next );
[1]298
299    // update new.pred <= pred
[603]300    hal_remote_s64( new + sizeof(xptr_t) , (uint64_t)pred );
[1]301
302        // update pred.next <= new
[563]303    hal_remote_s64( pred , (uint64_t)new );
[1]304
305    // update next.pred <= new
[603]306    hal_remote_s64( next + sizeof(xptr_t) , (uint64_t)new );
[1]307}
308
[603]309#endif  /* _XLIST_H_ */
Note: See TracBrowser for help on using the repository browser.