Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3846 | mejdrech | 1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup net |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** @file |
||
34 | */ |
||
35 | |||
36 | #include <malloc.h> |
||
37 | |||
38 | #include "configuration.h" |
||
39 | #include "packet.h" |
||
40 | #include "packet_queue.h" |
||
41 | |||
42 | #define PQ_MAGIC_VALUE 0x23465968; |
||
43 | |||
44 | static inline int pq_is_valid( pq_item_ref item ); |
||
45 | |||
46 | pq_item_ref pq_add( pq_head * queue, packet_t packet, int order, size_t metric ){ |
||
47 | pq_item_ref item; |
||
48 | pq_item_ref tmp; |
||
49 | |||
50 | tmp = pq_create( packet, order, metric ); |
||
51 | if( ! tmp ) return NULL; |
||
52 | item = queue; |
||
53 | while( pq_is_valid( item )){ |
||
54 | if( item->order < order ){ |
||
55 | if( item->next && pq_is_valid( item->next )){ |
||
56 | item = item->next; |
||
57 | }else{ |
||
58 | item->next = tmp; |
||
59 | tmp->previous = item; |
||
60 | return tmp; |
||
61 | } |
||
62 | }else{ |
||
63 | tmp->previous = item->previous; |
||
64 | tmp->next = item; |
||
65 | item->previous = tmp; |
||
66 | if( tmp->previous ){ |
||
67 | tmp->previous->next = tmp; |
||
68 | }else{ |
||
69 | * queue = tmp; |
||
70 | } |
||
71 | return tmp; |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | |||
77 | pq_head pq_create( packet_t packet, int order, size_t metric ){ |
||
78 | pq_item_ref item; |
||
79 | |||
80 | item = ( pq_item_ref ) malloc( sizeof( pq_item_t )); |
||
81 | if( ! item ) return NULL; |
||
82 | item->order = order; |
||
83 | item->metric = metric; |
||
84 | item->packet = packet; |
||
85 | item->previous = NULL; |
||
86 | item->next = NULL; |
||
87 | item->magic_value = PQ_MAGIC_VALUE; |
||
88 | return item; |
||
89 | } |
||
90 | |||
91 | void pq_destroy( pq_head queue ){ |
||
92 | pq_item_ref actual; |
||
93 | pq_item_ref next; |
||
94 | |||
95 | actual = queue; |
||
96 | while( pq_is_valid( actual )){ |
||
97 | next = actual->next; |
||
98 | actual->magic_value = 0; |
||
99 | free( actual ); |
||
100 | actual = next; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | static inline int pq_is_valid( pq_item_ref item ){ |
||
105 | return item && ( item->magic_value == PQ_MAGIC_VALUE ); |
||
106 | } |
||
107 | |||
108 | /** @} |
||
109 | */ |