]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.h
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / lib / dlist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2000-2003 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 /* ========================================================================
26  * 
27  *   Doubly linked list  -- dlist
28  *
29  */
30
31 #define OFFSET(item,link) ((char *)link - (char *)item)
32
33 struct dlink {
34    void *next;
35    void *prev;
36 };
37
38 class dlist {
39    void *head;
40    void *tail;
41    int loffset;
42    int num_items;
43 public:
44    dlist(void *item, void *link);
45    void init(void *item, void *link);
46    void prepend(void *item);
47    void append(void *item);
48    void insert_before(void *item, void *where);
49    void insert_after(void *item, void *where);
50    void remove(void *item);
51    bool empty();
52    int  size();
53    void *next(void *item);
54    void *prev(void *item);
55    void destroy();
56    void *first();
57    void *last();
58    void * operator new(size_t);
59    void operator delete(void *);
60 };
61
62 /*                            
63  * This allows us to do explicit initialization,
64  *   allowing us to mix C++ classes inside malloc'ed
65  *   C structures. Define before called in constructor.
66  */
67 inline void dlist::init(void *item, void *link) 
68 {
69    head = tail = NULL;
70    loffset = (char *)link - (char *)item;
71    num_items = 0;
72 }
73
74 /* Constructor */
75 inline dlist::dlist(void *item, void *link)
76 {
77    this->init(item, link);
78 }
79
80 inline bool dlist::empty()
81 {
82    return head == NULL;
83 }
84
85 inline int dlist::size()
86 {
87    return num_items;
88 }
89
90    
91 inline void * dlist::operator new(size_t)
92 {
93    return malloc(sizeof(dlist));
94 }
95
96 inline void dlist::operator delete(void  *item)
97 {
98    ((dlist *)item)->destroy();
99    free(item);
100 }
101  
102
103 inline void * dlist::first()
104 {
105    return head;
106 }
107
108 inline void * dlist::last()
109 {
110    return tail;
111 }