[1] | 1 | /* Copyright (c) 2007-2009, Stanford University |
---|
| 2 | * All rights reserved. |
---|
| 3 | * |
---|
| 4 | * Redistribution and use in source and binary forms, with or without |
---|
| 5 | * modification, are permitted provided that the following conditions are met: |
---|
| 6 | * * Redistributions of source code must retain the above copyright |
---|
| 7 | * notice, this list of conditions and the following disclaimer. |
---|
| 8 | * * Redistributions in binary form must reproduce the above copyright |
---|
| 9 | * notice, this list of conditions and the following disclaimer in the |
---|
| 10 | * documentation and/or other materials provided with the distribution. |
---|
| 11 | * * Neither the name of Stanford University nor the names of its |
---|
| 12 | * contributors may be used to endorse or promote products derived from |
---|
| 13 | * this software without specific prior written permission. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY |
---|
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 18 | * DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE FOR ANY |
---|
| 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
| 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
| 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #ifndef QUEUE_H_ |
---|
| 28 | #define QUEUE_H_ |
---|
| 29 | |
---|
| 30 | #include "list.h" |
---|
| 31 | |
---|
| 32 | typedef list queue_t; |
---|
| 33 | typedef list_ent queue_elem_t; |
---|
| 34 | |
---|
| 35 | #define queue_entry(QUEUE_ELEM, STRUCT, MEMBER) \ |
---|
| 36 | list_entry(QUEUE_ELEM, STRUCT, MEMBER) |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Initialize queue for use |
---|
| 40 | */ |
---|
| 41 | static inline void queue_init (queue_t *q) |
---|
| 42 | { |
---|
| 43 | assert (q != NULL); |
---|
| 44 | list_init(q); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Add element to end of queue |
---|
| 49 | */ |
---|
| 50 | static inline void queue_push_back (queue_t *q, queue_elem_t *qe) |
---|
| 51 | { |
---|
| 52 | assert (q != NULL); |
---|
| 53 | assert (qe != NULL); |
---|
| 54 | |
---|
| 55 | list_add_tail(q, qe); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Remove element at end of queue |
---|
| 60 | * @return 0 on empty, 1 if found |
---|
| 61 | */ |
---|
| 62 | static inline int queue_pop_back (queue_t *q, queue_elem_t **qe) |
---|
| 63 | { |
---|
| 64 | queue_elem_t *tail; |
---|
| 65 | |
---|
| 66 | assert (q != NULL); |
---|
| 67 | assert (qe != NULL); |
---|
| 68 | |
---|
| 69 | tail = list_remove_tail(q); |
---|
| 70 | if (tail == NULL) |
---|
| 71 | return 0; |
---|
| 72 | |
---|
| 73 | *qe = tail; |
---|
| 74 | |
---|
| 75 | return 1; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Put element at front of queue |
---|
| 80 | */ |
---|
| 81 | static inline void queue_push_front (queue_t *q, queue_elem_t *qe) |
---|
| 82 | { |
---|
| 83 | assert (q != NULL); |
---|
| 84 | assert (qe != NULL); |
---|
| 85 | |
---|
| 86 | list_add_head(q, qe); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Remove element from front of queue |
---|
| 91 | * @return 0 if queue was empty, 1 if successful |
---|
| 92 | */ |
---|
| 93 | static inline int queue_pop_front (queue_t *q, queue_elem_t **qe) |
---|
| 94 | { |
---|
| 95 | queue_elem_t *head; |
---|
| 96 | |
---|
| 97 | assert (q != NULL); |
---|
| 98 | assert (qe != NULL); |
---|
| 99 | |
---|
| 100 | head = list_remove_head(q); |
---|
| 101 | if (head == NULL) |
---|
| 102 | return 0; |
---|
| 103 | |
---|
| 104 | *qe = head; |
---|
| 105 | |
---|
| 106 | return 1; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | #endif /* QUEUE_H_ */ |
---|