]>
git.proxmox.com Git - spiceterm.git/blob - ring.h
1 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 Copyright (C) 2009 Red Hat, Inc.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include <spice/macros.h>
24 //#include "spice_common.h"
28 typedef struct Ring RingItem
;
34 static inline void ring_init(Ring
*ring
)
36 ring
->next
= ring
->prev
= ring
;
39 static inline void ring_item_init(RingItem
*item
)
41 item
->next
= item
->prev
= NULL
;
44 static inline int ring_item_is_linked(RingItem
*item
)
49 static inline int ring_is_empty(Ring
*ring
)
51 g_assert(ring
->next
!= NULL
&& ring
->prev
!= NULL
);
52 return ring
== ring
->next
;
55 static inline void ring_add(Ring
*ring
, RingItem
*item
)
57 g_assert(ring
->next
!= NULL
&& ring
->prev
!= NULL
);
58 g_assert(item
->next
== NULL
&& item
->prev
== NULL
);
60 item
->next
= ring
->next
;
62 ring
->next
= item
->next
->prev
= item
;
65 static inline void ring_add_after(RingItem
*item
, RingItem
*pos
)
70 static inline void ring_add_before(RingItem
*item
, RingItem
*pos
)
72 ring_add(pos
->prev
, item
);
75 static inline void __ring_remove(RingItem
*item
)
77 item
->next
->prev
= item
->prev
;
78 item
->prev
->next
= item
->next
;
79 item
->prev
= item
->next
= 0;
82 static inline void ring_remove(RingItem
*item
)
84 g_assert(item
->next
!= NULL
&& item
->prev
!= NULL
);
85 g_assert(item
->next
!= item
);
90 static inline RingItem
*ring_get_head(Ring
*ring
)
94 g_assert(ring
->next
!= NULL
&& ring
->prev
!= NULL
);
96 if (ring_is_empty(ring
)) {
103 static inline RingItem
*ring_get_tail(Ring
*ring
)
107 g_assert(ring
->next
!= NULL
&& ring
->prev
!= NULL
);
109 if (ring_is_empty(ring
)) {
116 static inline RingItem
*ring_next(Ring
*ring
, RingItem
*pos
)
120 g_assert(ring
->next
!= NULL
&& ring
->prev
!= NULL
);
122 g_assert(pos
->next
!= NULL
&& pos
->prev
!= NULL
);
124 return (ret
== ring
) ? NULL
: ret
;
127 static inline RingItem
*ring_prev(Ring
*ring
, RingItem
*pos
)
131 g_assert(ring
->next
!= NULL
&& ring
->prev
!= NULL
);
133 g_assert(pos
->next
!= NULL
&& pos
->prev
!= NULL
);
135 return (ret
== ring
) ? NULL
: ret
;
138 #define RING_FOREACH_SAFE(var, next, ring) \
139 for ((var) = ring_get_head(ring); \
140 (var) && ((next) = ring_next(ring, (var)), 1); \
144 #define RING_FOREACH(var, ring) \
145 for ((var) = ring_get_head(ring); \
147 (var) = ring_next(ring, var))
149 #define RING_FOREACH_REVERSED(var, ring) \
150 for ((var) = ring_get_tail(ring); \
152 (var) = ring_prev(ring, var))
155 static inline unsigned int ring_get_length(Ring
*ring
)
158 unsigned int ret
= 0;
160 for (i
= ring_get_head(ring
);
162 i
= ring_next(ring
, i
))