source: trunk/kernel/libk/slist.h @ 457

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

First import

File size: 5.1 KB
Line 
1/*
2 * list.h - Simple linked list.
3 *
4 * Authors Ghassan Almaless  (2008,2009,2010,2011,2012)
5 *         Mohamed Lamine Karaoui (2015)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH     
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#ifndef _ALMOS_SLIST_H_
26#define _ALMOS_SLIST_H_
27
28#ifndef NULL
29#define NULL (void *) 0
30#endif
31
32
33/***************************************************************************
34 * This macro return an uint32_t that is the offset (number of bytes)
35 * of a field in a structure.
36 * @ type   : structure type
37 * @ member : name of the field
38 **************************************************************************/
39
40#ifndef OFFSETOF
41#define OFFSETOF( type , member ) ((unsigned int) & ((type *)0)->member)
42#endif
43
44
45////////////////////////////////////////////////////////////////////////////
46////////////////////////////////////////////////////////////////////////////
47//          Simple linked list functions & macros
48////////////////////////////////////////////////////////////////////////////
49////////////////////////////////////////////////////////////////////////////
50
51/***************************************************************************
52 * This structure defines a simple linked list, managed as a stack.
53 * Note : The list root is an extra list-entry, that is NOT part of the
54 *        set of linked elements.
55 **************************************************************************/
56
57typedef struct slist_entry_s
58{
59        struct slist_entry_s * next;
60}
61slist_entry_t;
62
63/***************************************************************************
64 * This macro returns a pointer on a structure containing an
65 * embedded linked list (i.e. a list-entry or slist_entry field).
66 * @ list_ptr       : pointer on the list_entry
67 * @ container_type : type of the structure containing the list-entry
68 * @ member_name    : name of the list_entry field
69 * It can be used for both list_entry & slist_entry
70 **************************************************************************/
71
72#define SLIST_ELEMENT( list_ptr , container_type , member_name)         \
73        ({const typeof(((container_type*)0)->member_name) *__member_ptr = (list_ptr); \
74        (container_type *)( (char*)__member_ptr - offsetof(container_type,member_name));})
75
76/***************************************************************************
77 * This function initialises the root of a simple linked list.
78 **************************************************************************/
79static inline void slist_root_init( slist_entry_t * root )
80{
81        root->next = root;
82}
83
84/***************************************************************************
85 * This function initialises an entry of a simple linked list.
86 **************************************************************************/
87static inline void slist_entry_init( slist_entry_t * entry )
88{
89        entry->next = NULL;
90}
91
92/***************************************************************************
93 * This macro returns a pointer on the first element of a simple list.
94 * @ root    : pointer on the list root
95 * @ type    : type of the container
96 * @ member  : name of the slist_entry field
97 **************************************************************************/
98
99#define SLIST_HEAD( root , type , member )              \
100        SLIST_ELEMENT( root->next , type , member )
101
102/***************************************************************************
103 * This function push a new element in a simple linked list.
104 * @ root    : pointer on the list root
105 * @ entry   : entry to be inserted
106 **************************************************************************/
107static inline void slist_push( slist_entry_t * root,
108                               slist_entry_t * entry )
109{
110        entry->next = root->next;
111        root->next  = entry;            \
112}
113
114/***************************************************************************
115 * This function returns true if the list is empty.
116 **************************************************************************/
117static inline bool_t slist_empty( slist_entry_t * root )
118{
119    return ( root == root->next );
120}
121
122/***************************************************************************
123 * This function pop anw element from a simple linked list.
124 * It returns NULL if the list is empty.
125 * @ root    : pointer on the list root
126 **************************************************************************/
127static inline slist_entry_t * list_pop( slist_entry_t * root )
128{
129        slist_entry_t * entry;
130 
131        if( slist_empty(root) ) return NULL;
132
133        entry = root->next;
134        root->next = entry->next;
135
136        return entry;
137}
138
139
140#endif  /* _ALMOS_SLIST_H_ */
Note: See TracBrowser for help on using the repository browser.