OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [infra/] [current/] [include/] [clist.hxx] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
#ifndef CYGONCE_INFRA_CLIST_HXX
2
#define CYGONCE_INFRA_CLIST_HXX
3
 
4
//==========================================================================
5
//
6
//      clist.hxx
7
//
8
//      Standard types, and some useful coding macros.
9
//
10
//==========================================================================
11
// ####ECOSGPLCOPYRIGHTBEGIN####
12
// -------------------------------------------
13
// This file is part of eCos, the Embedded Configurable Operating System.
14
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
15
//
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
19
// version.
20
//
21
// eCos is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License
27
// along with eCos; if not, write to the Free Software Foundation, Inc.,
28
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
29
//
30
// As a special exception, if other files instantiate templates or use
31
// macros or inline functions from this file, or you compile this file
32
// and link it with other works to produce a work based on this file,
33
// this file does not by itself cause the resulting work to be covered by
34
// the GNU General Public License. However the source code for this file
35
// must still be made available in accordance with section (3) of the GNU
36
// General Public License v2.
37
//
38
// This exception does not invalidate any other reasons why a work based
39
// on this file might be covered by the GNU General Public License.
40
// -------------------------------------------
41
// ####ECOSGPLCOPYRIGHTEND####
42
//==========================================================================
43
//#####DESCRIPTIONBEGIN####
44
//
45
// Author(s):     nickg
46
// Contributors:  nickg
47
// Date:        2000-11-08
48
// Purpose:     Simple circular list implementation
49
// Description: A simple implementation of circular lists.
50
// Usage:       #include "cyg/infra/clist.hxx"
51
//              ...
52
//
53
//####DESCRIPTIONEND####
54
//
55
//==========================================================================
56
 
57
#include 
58
 
59
// -------------------------------------------------------------------------
60
// Class and structure conversion macros.
61
// CYG_CLASSFROMFIELD translates a pointer to a field of a struct or
62
// class into a pointer to the class.
63
// CYG_OFFSETOFBASE yields the offset of a base class of a derived
64
// class.
65
// CYG_CLASSFROMBASE translates a pointer to a base class into a pointer
66
// to a selected derived class. The base class object _must_ be part of
67
// the specified derived class. This is essentially a poor mans version
68
// of the RTTI dynamic_cast operator.
69
// Caveat: These macros do not work for virtual base classes.
70
 
71
// Note: These definitions are exact duplicates of definitions in
72
// ktypes.h. If either definition is changed, the other should be too
73
// to avoid compiler messages.
74
 
75
#define CYG_CLASSFROMFIELD(_type_,_member_,_ptr_)\
76
    ((_type_ *)((char *)(_ptr_)-((char *)&(((_type_ *)0)->_member_))))
77
 
78
#define CYG_OFFSETOFBASE(_type_,_base_)\
79
    ((char *)((_base_ *)((_type_ *)4)) - (char *)4)
80
 
81
# define CYG_CLASSFROMBASE(_class_,_base_,_ptr_)\
82
    ((_class_ *)((char *)(_ptr_) - CYG_OFFSETOFBASE(_class_,_base_)))
83
 
84
 
85
// -------------------------------------------------------------------------
86
// Cyg_DNode class.
87
// This simply represents a double linked node that is intended to
88
// be a base member of the class that is being managed.
89
 
90
class Cyg_DNode
91
{
92
    friend class Cyg_CList;
93
 
94
    Cyg_DNode   *next;
95
    Cyg_DNode   *prev;
96
 
97
public:
98
 
99
    Cyg_DNode()
100
    {
101
        // Initialize pointers to point here
102
        next = prev = this;
103
    };
104
 
105
    // Accessor and test functions
106
    Cyg_DNode *get_next() { return next; };
107
    Cyg_DNode *get_prev() { return prev; };
108
    cyg_bool  in_list() { return next != this; };
109
 
110
    // Insert a node into the list before this one,
111
    // so that it becomes this nodes predecessor in
112
    // the list.
113
    void insert( Cyg_DNode *node )
114
    {
115
        node->next = this;
116
        node->prev = prev;
117
        prev->next = node;
118
        prev = node;
119
    };
120
 
121
    // Append a node after this one so that it become
122
    // this nodes sucessor in the list.
123
    void append( Cyg_DNode *node )
124
    {
125
        node->prev = this;
126
        node->next = next;
127
        next->prev = node;
128
        next = node;
129
    };
130
 
131
    // Unlink this node from it's list. It is safe to apply this to an
132
    // already unlinked node.
133
    void unlink()
134
    {
135
        next->prev = prev;
136
        prev->next = next;
137
        next = prev = this;
138
    };
139
 
140
    ~Cyg_DNode()
141
    {
142
        // If this node is still linked, unlink it.
143
        if( next != this )
144
            unlink();
145
    };
146
 
147
};
148
 
149
// -------------------------------------------------------------------------
150
// Cyg_CList class.
151
 
152
// This is a simple class that manages a circular list of DNodes. This
153
// object points to the head of the list and provides functions to
154
// manipulate the head and tail of the list.
155
 
156
class Cyg_CList
157
{
158
    Cyg_DNode   *head;                  // list head pointer
159
 
160
public:
161
 
162
    Cyg_CList()
163
    {
164
        head = NULL;
165
    };
166
 
167
    // Accessor and test functions
168
    Cyg_DNode *get_head() { return head; };
169
    Cyg_DNode *get_tail() { return head?head->prev:NULL; };
170
    cyg_bool empty() { return head == NULL; };
171
 
172
    // Add a node at the head of the list
173
    void add_head( Cyg_DNode *node )
174
    {
175
        if( head == NULL )
176
            head = node;
177
        else
178
        {
179
            head->insert( node );
180
            head = node;
181
        }
182
    };
183
 
184
    // Remove the node at the head of the list
185
    Cyg_DNode *rem_head()
186
    {
187
        Cyg_DNode *node = head;
188
        if( node != NULL )
189
        {
190
            // There is a node available
191
            Cyg_DNode *next = node->next;
192
            if( next == node )
193
            {
194
                // Only node on list
195
                head = NULL;
196
            }
197
            else
198
            {
199
                // remove head node and move head to next.
200
                node->unlink();
201
                head = next;
202
            }
203
        }
204
        return node;
205
    };
206
 
207
 
208
    // Add a node at the tail of the list
209
    void add_tail( Cyg_DNode *node )
210
    {
211
        if( head == NULL )
212
            head = node;
213
        else
214
            head->insert( node );
215
    };
216
 
217
    // Remove the node at the tail of the list
218
    Cyg_DNode *rem_tail()
219
    {
220
        if( head == NULL )
221
            return NULL;
222
 
223
        Cyg_DNode *node = head->prev;
224
 
225
        if( node == head )
226
            head = NULL;
227
        else node->unlink();
228
 
229
        return node;
230
    };
231
 
232
    // Merge the supplied list into this one, at the tail.
233
    void merge( Cyg_CList& list )
234
    {
235
        if( list.head == NULL )
236
            return;                     // Nothing to do
237
        else if( head == NULL )
238
            head = list.head;           // this is empty, just move it
239
        else
240
        {
241
            // We have a real merge to do. Adjust the pointers
242
            // on the two lists so that they become one.
243
 
244
            Cyg_DNode *lh = list.head;
245
            Cyg_DNode *lt = lh->prev;
246
            Cyg_DNode *tail = head->prev;
247
 
248
            head->prev = lt;
249
            lt->next = head;
250
            tail->next = lh;
251
            lh->prev = tail;
252
        }
253
        list.head = NULL;
254
    };
255
 
256
    // General removal. Deals with what happend if this is only
257
    // object on list, or is the head.
258
    void remove( Cyg_DNode *node )
259
    {
260
        if( node == head )
261
            rem_head();
262
        else node->unlink();
263
    };
264
 
265
    // Rotation - move the head to the next node in the list.
266
    void rotate()
267
    {
268
        if( head )
269
            head = head->next;
270
    };
271
 
272
    // Move a node to the head of the list. Assumes that the
273
    // node is in this list.
274
    void to_head( Cyg_DNode *node )
275
    {
276
        head = node;
277
    };
278
 
279
    // Insert a node before one in this list, and deal with what
280
    // happens if the node happens to be at the head of the list.
281
    void insert( Cyg_DNode *list_node, Cyg_DNode *node )
282
    {
283
        if( list_node == head )
284
        {
285
            head->insert( node );
286
            head = node;
287
        }
288
        else list_node->insert( node );
289
    };
290
 
291
    ~Cyg_CList()
292
    {
293
        while( head != NULL )
294
            rem_head();
295
    };
296
 
297
};
298
 
299
// -------------------------------------------------------------------------
300
// Cyg_CList_T
301
// Template class that allows us to make use of the CList class in a
302
// type-specific way.
303
 
304
template  class Cyg_CList_T
305
    : public Cyg_CList
306
{
307
public:
308
 
309
    Cyg_CList_T() {};
310
    ~Cyg_CList_T() {};
311
 
312
    T *get_head()
313
    {
314
        Cyg_DNode *node = Cyg_CList::get_head();
315
        if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
316
        return NULL;
317
    };
318
    T *get_tail()
319
    {
320
        Cyg_DNode *node = Cyg_CList::get_tail();
321
        if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
322
        return NULL;
323
    };
324
 
325
    T *rem_head()
326
    {
327
        Cyg_DNode *node = Cyg_CList::rem_head();
328
        if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
329
        return NULL;
330
    };
331
 
332
    T *rem_tail()
333
    {
334
        Cyg_DNode *node = Cyg_CList::rem_tail();
335
        if( node ) return CYG_CLASSFROMBASE( T, Cyg_DNode, node );
336
        return NULL;
337
    };
338
 
339
    // The rest just default to the Cyg_CList class operations.
340
};
341
 
342
// -------------------------------------------------------------------------
343
// Cyg_DNode_T
344
// Template class that allows us to make use of the DNode class in a
345
// type-specific way.
346
 
347
template  class Cyg_DNode_T
348
    : public Cyg_DNode
349
{
350
public:
351
 
352
    Cyg_DNode_T() {};
353
    ~Cyg_DNode_T() {};
354
 
355
    T *get_next() { return CYG_CLASSFROMBASE( T, Cyg_DNode, Cyg_DNode::get_next() ); };
356
    T *get_prev() { return CYG_CLASSFROMBASE( T, Cyg_DNode, Cyg_DNode::get_prev() ); };
357
 
358
    // The rest just default to the Cyg_DNode class operations.
359
};
360
 
361
// -------------------------------------------------------------------------
362
#endif // CYGONCE_INFRA_CLIST_HXX multiple inclusion protection
363
// EOF clist.hxx
364
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.