2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
35 #include <xtypes/xpool.h>
37 static inline int __validate_idx(struct xpool *xp, xpool_index idx)
39 struct xpool_node *node = XPTR(&xp->mem)+idx;
40 return (idx < xp->size && node->prev != NoIndex);
43 void __xpool_clear(struct xpool *xp)
46 uint64_t size = xp->size;
47 struct xpool_node *mem = XPTR(&xp->mem);
50 for (i = 1; i < size; i++) {
51 mem[i-1].prev = NoIndex;
54 mem[size-1].prev = NoIndex;
55 mem[size-1].next = NoIndex;
59 void xpool_clear(struct xpool *xp, uint32_t who)
61 xlock_acquire(&xp->lock, who);
63 xlock_release(&xp->lock);
66 void xpool_init(struct xpool *xp, uint64_t size, struct xpool_node* mem)
69 XPTRSET(&xp->mem, mem);
70 xlock_release(&xp->lock);
74 xpool_index __xpool_add(struct xpool *xp, xpool_data data)
76 struct xpool_node *new, *list, *prev;
83 new = XPTR(&xp->mem) + idx;
87 if (xp->list != NoIndex) {
88 list = XPTR(&xp->mem) + xp->list;
90 new->prev = list->prev;
92 prev = XPTR(&xp->mem) + list->prev;
102 list = XPTR(&xp->mem) + idx;
103 printf("xpool data: %llu(%llu), ", xp->list, list->data );
106 list = XPTR(&xp->mem) + idx;
107 printf("%llu(%llu), ", idx, list->data);
108 }while(idx != xp->list);
115 xpool_index xpool_add(struct xpool *xp, xpool_data data, uint32_t who)
118 xlock_acquire(&xp->lock, who);
119 idx = __xpool_add(xp, data);
120 xlock_release(&xp->lock);
124 xpool_index xpool_add(struct xpool *xp, xpool_data data)
126 struct xpool_node *new, *list, *free, *next, *prev;
128 xlock_acquire(&xp->lock, 1);
129 free = XPTR(&xp->free);
130 list = XPTR(&xp->list);
133 xlock_release(&xp->lock);
136 free = XPTR(&new->next);
137 XPTRSET(&xp->free, free);
139 //new->next = xp->list;
140 XPTRSET(&new->next, list);
142 prev = XPTR(&list->prev);
143 XPTRSET(&new->prev, prev);
144 //new->prev = xp->list->prev;
146 next = XPTR(&prev->next);
147 XPTRSET(&prev->next, new);
148 //xp->list->prev->next = new;
150 XPTRSET(&list->prev, new);
151 //xp->list->prev = new;
153 XPTRSET(&new->next ,new);
155 XPTRSET(&new->prev, new);
157 XPTRSET(&xp->list, new);
162 xlock_release(&xp->lock);
163 return (new - XPTR(&xp->mem));
167 xpool_index xpool_remove(struct xpool *xp, xpool_index idx, xpool_data *data)
169 struct xpool_node *node, *list, *free, *prev, *next;
171 xlock_acquire(&xp->lock, 1);
172 if (!__validate_idx(xp, idx)){ // idx < xp->size && node->prev != NULL
173 xlock_release(&xp->lock);
176 node = XPTR(&xp->mem) + idx;
178 list = XPTR(&xp->list);
179 free = XPTR(&xp->free);
180 next = XPTR(&node->next);
181 prev = XPTR(&node->prev);
184 XPTRSET(&xp->list, NULL);
187 XPTRSET(&xp->list, next);
188 //xp->list = node->next;
190 XPTRSET(&prev->next, next);
191 //node->prev->next = node->next;
192 XPTRSET(&next->prev, prev);
193 //node->next->prev = node->prev;
194 XPTRSET(&node->prev, NULL);
196 XPTRSET(&node->next, free);
197 //node->next = xp->free;
199 XPTRSET(&xp->free, free);
203 xlock_release(&xp->lock);
207 xpool_index __xpool_remove(struct xpool *xp, xpool_index idx, xpool_data *data)
209 struct xpool_node *node, *prev, *next;
210 if (!__validate_idx(xp, idx)){ // idx < xp->size && node->prev != NULL
213 node = XPTR(&xp->mem) + idx;
216 if (idx == xp->list){
217 if ( idx == node->next)
220 xp->list = node->next;
222 prev = XPTR(&xp->mem) + node->prev;
223 prev->next = node->next;
225 next = XPTR(&xp->mem) + node->next;
226 next->prev = node->prev;
228 node->prev = NoIndex;
229 node->next = xp->free;
234 xpool_index xpool_remove(struct xpool *xp, xpool_index idx, xpool_data *data, uint32_t who)
237 xlock_acquire(&xp->lock, who);
238 ret = __xpool_remove(xp, idx, data);
239 xlock_release(&xp->lock);
243 xpool_index __xpool_peek(struct xpool *xp, xpool_data *data)
245 struct xpool_node *list;
247 if (xp->list == NoIndex){
251 list = XPTR(&xp->mem) + xp->list;
256 xpool_index xpool_peek(struct xpool *xp, xpool_data *data, uint32_t who)
259 xlock_acquire(&xp->lock, who);
260 ret = __xpool_peek(xp, data);
261 xlock_release(&xp->lock);
265 xpool_index __xpool_peek_idx(struct xpool *xp, xpool_index idx, xpool_data *data)
267 struct xpool_node *node;
268 if (!__validate_idx(xp, idx)){
271 node = XPTR(&xp->mem) + idx;
276 xpool_index xpool_peek_idx(struct xpool *xp, xpool_index idx, xpool_data *data, uint32_t who)
279 xlock_acquire(&xp->lock, who);
280 ret = __xpool_peek_idx(xp,idx,data);
281 xlock_release(&xp->lock);
285 xpool_index __xpool_peek_and_fwd(struct xpool *xp, xpool_data *data)
287 struct xpool_node *list;
289 if (xp->list == NoIndex){
293 list = XPTR(&xp->mem) + xp->list;
295 xp->list = list->next;
299 xpool_index xpool_peek_and_fwd(struct xpool *xp, xpool_data *data, uint32_t who)
302 xlock_acquire(&xp->lock, who);
303 ret = __xpool_peek_and_fwd(xp,data);
304 xlock_release(&xp->lock);
309 xpool_index xpool_peek_and_fwd(struct xpool *xp, xpool_data *data)
311 struct xpool_node *list, *next;
313 xlock_acquire(&xp->lock, 1);
314 list = XPTR(&xp->list);
316 xlock_release(&xp->lock);
320 next = XPTR(&list->next);
321 XPTRSET(&xp->list, next);
322 //xp->list = xp->list->next;
324 xlock_release(&xp->lock);
325 return (list - XPTR(&xp->mem));
329 xpool_index __xpool_set_idx(struct xpool *xp, xpool_index idx, xpool_data data)
331 struct xpool_node *node;
332 if (!__validate_idx(xp, idx)){
335 node = XPTR(&xp->mem) + idx;
340 xpool_index xpool_set_idx(struct xpool *xp, xpool_index idx, xpool_data data, uint32_t who)
343 xlock_acquire(&xp->lock, who);
344 ret = __xpool_set_idx(xp, idx, data);
345 xlock_release(&xp->lock);
351 #include <linux/module.h>
352 #include <xtypes/xpool_exports.h>