1 #ifndef CYGONCE_INFRA_CLIST_HXX
2 #define CYGONCE_INFRA_CLIST_HXX
4 //==========================================================================
8 // Standard types, and some useful coding macros.
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
47 // Contributors: nickg
49 // Purpose: Simple circular list implementation
50 // Description: A simple implementation of circular lists.
51 // Usage: #include "cyg/infra/clist.hxx"
54 //####DESCRIPTIONEND####
56 //==========================================================================
58 #include <cyg/infra/cyg_type.h>
60 // -------------------------------------------------------------------------
61 // Class and structure conversion macros.
62 // CYG_CLASSFROMFIELD translates a pointer to a field of a struct or
63 // class into a pointer to the class.
64 // CYG_OFFSETOFBASE yields the offset of a base class of a derived
66 // CYG_CLASSFROMBASE translates a pointer to a base class into a pointer
67 // to a selected derived class. The base class object _must_ be part of
68 // the specified derived class. This is essentially a poor mans version
69 // of the RTTI dynamic_cast operator.
70 // Caveat: These macros do not work for virtual base classes.
72 // Note: These definitions are exact duplicates of definitions in
73 // ktypes.h. If either definition is changed, the other should be too
74 // to avoid compiler messages.
76 #define CYG_CLASSFROMFIELD(_type_,_member_,_ptr_)\
77 ((_type_ *)((char *)(_ptr_)-((char *)&(((_type_ *)0)->_member_))))
79 #define CYG_OFFSETOFBASE(_type_,_base_)\
80 ((char *)((_base_ *)((_type_ *)4)) - (char *)4)
82 # define CYG_CLASSFROMBASE(_class_,_base_,_ptr_)\
83 ((_class_ *)((char *)(_ptr_) - CYG_OFFSETOFBASE(_class_,_base_)))
86 // -------------------------------------------------------------------------
88 // This simply represents a double linked node that is intended to
89 // be a base member of the class that is being managed.
93 friend class Cyg_CList;
102 // Initialize pointers to point here
106 // Accessor and test functions
107 Cyg_DNode *get_next() { return next; };
108 Cyg_DNode *get_prev() { return prev; };
109 cyg_bool in_list() { return next != this; };
111 // Insert a node into the list before this one,
112 // so that it becomes this nodes predecessor in
114 void insert( Cyg_DNode *node )
122 // Append a node after this one so that it become
123 // this nodes sucessor in the list.
124 void append( Cyg_DNode *node )
132 // Unlink this node from it's list. It is safe to apply this to an
133 // already unlinked node.
143 // If this node is still linked, unlink it.
150 // -------------------------------------------------------------------------
153 // This is a simple class that manages a circular list of DNodes. This
154 // object points to the head of the list and provides functions to
155 // manipulate the head and tail of the list.
159 Cyg_DNode *head; // list head pointer
168 // Accessor and test functions
169 Cyg_DNode *get_head() { return head; };
170 Cyg_DNode *get_tail() { return head?head->prev:NULL; };
171 cyg_bool empty() { return head == NULL; };
173 // Add a node at the head of the list
174 void add_head( Cyg_DNode *node )
180 head->insert( node );
185 // Remove the node at the head of the list
186 Cyg_DNode *rem_head()
188 Cyg_DNode *node = head;
191 // There is a node available
192 Cyg_DNode *next = node->next;
200 // remove head node and move head to next.
209 // Add a node at the tail of the list
210 void add_tail( Cyg_DNode *node )
215 head->insert( node );
218 // Remove the node at the tail of the list
219 Cyg_DNode *rem_tail()
224 Cyg_DNode *node = head->prev;
233 // Merge the supplied list into this one, at the tail.
234 void merge( Cyg_CList& list )
236 if( list.head == NULL )
237 return; // Nothing to do
238 else if( head == NULL )
239 head = list.head; // this is empty, just move it
242 // We have a real merge to do. Adjust the pointers
243 // on the two lists so that they become one.
245 Cyg_DNode *lh = list.head;
246 Cyg_DNode *lt = lh->prev;
247 Cyg_DNode *tail = head->prev;
257 // General removal. Deals with what happend if this is only
258 // object on list, or is the head.
259 void remove( Cyg_DNode *node )
266 // Rotation - move the head to the next node in the list.
273 // Move a node to the head of the list. Assumes that the
274 // node is in this list.
275 void to_head( Cyg_DNode *node )
280 // Insert a node before one in this list, and deal with what
281 // happens if the node happens to be at the head of the list.
282 void insert( Cyg_DNode *list_node, Cyg_DNode *node )
284 if( list_node == head )
286 head->insert( node );
289 else list_node->insert( node );
294 while( head != NULL )
300 // -------------------------------------------------------------------------
302 // Template class that allows us to make use of the CList class in a
303 // type-specific way.
305 template <class T> class Cyg_CList_T
315 Cyg_DNode *node = Cyg_CList::get_head();
316 if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
321 Cyg_DNode *node = Cyg_CList::get_tail();
322 if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
328 Cyg_DNode *node = Cyg_CList::rem_head();
329 if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
335 Cyg_DNode *node = Cyg_CList::rem_tail();
336 if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
340 // The rest just default to the Cyg_CList class operations.
343 // -------------------------------------------------------------------------
345 // Template class that allows us to make use of the DNode class in a
346 // type-specific way.
348 template <class T> class Cyg_DNode_T
356 T *get_next() { return CYG_CLASSFROMBASE( T, Cyg_DNode, Cyg_DNode::get_next() ); };
357 T *get_prev() { return CYG_CLASSFROMBASE( T, Cyg_DNode, Cyg_DNode::get_prev() ); };
359 // The rest just default to the Cyg_DNode class operations.
362 // -------------------------------------------------------------------------
363 #endif // CYGONCE_INFRA_CLIST_HXX multiple inclusion protection