Subversion Repositories HelenOS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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.  
  40. #include "packet.h"
  41. #include "packet_queue.h"
  42.  
  43. #define PQ_MAGIC_VALUE  0x23465968;
  44.  
  45. static inline int   pq_is_valid( pq_item_ref item );
  46.  
  47. pq_item_ref pq_add( pq_head * queue, packet_t packet, int order, size_t metric ){
  48.     pq_item_ref item;
  49.     pq_item_ref tmp;
  50.  
  51.     tmp = pq_create( packet, order, metric );
  52.     if( ! tmp ) return NULL;
  53.     item = queue;
  54.     while( pq_is_valid( item )){
  55.         if( item->order < order ){
  56.             if( item->next && pq_is_valid( item->next )){
  57.                 item = item->next;
  58.             }else{
  59.                 item->next = tmp;
  60.                 tmp->previous = item;
  61.                 return tmp;
  62.             }
  63.         }else{
  64.             tmp->previous = item->previous;
  65.             tmp->next = item;
  66.             item->previous = tmp;
  67.             if( tmp->previous ){
  68.                 tmp->previous->next = tmp;
  69.             }else{
  70.                 * queue = tmp;
  71.             }
  72.             return tmp;
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78. pq_head pq_create( packet_t packet, int order, size_t metric ){
  79.     pq_item_ref item;
  80.  
  81.     item = ( pq_item_ref ) malloc( sizeof( pq_item_t ));
  82.     if( ! item ) return NULL;
  83.     item->order = order;
  84.     item->metric = metric;
  85.     item->packet = packet;
  86.     item->previous = NULL;
  87.     item->next = NULL;
  88.     item->magic_value = PQ_MAGIC_VALUE;
  89.     return item;
  90. }
  91.  
  92. void pq_destroy( pq_head queue ){
  93.     pq_item_ref actual;
  94.     pq_item_ref next;
  95.  
  96.     actual = queue;
  97.     while( pq_is_valid( actual )){
  98.         next = actual->next;
  99.         actual->magic_value = 0;
  100.         free( actual );
  101.         actual = next;
  102.     }
  103. }
  104.  
  105. static inline int pq_is_valid( pq_item_ref item ){
  106.     return item && ( item->magic_value == PQ_MAGIC_VALUE );
  107. }
  108.  
  109. /** @}
  110.  */
  111.