Subversion Repositories HelenOS

Rev

Rev 4704 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 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 <errno.h>
  37. #include <malloc.h>
  38. #include <mem.h>
  39.  
  40. #include "dynamic_fifo.h"
  41.  
  42. #define DYN_FIFO_MAGIC_VALUE    0x58627659
  43.  
  44. #define NEXT_INDEX( fifo, index )   ((( index ) + 1 ) % (( fifo )->size + 1 ))
  45.  
  46. int dyn_fifo_is_valid( dyn_fifo_ref fifo );
  47.  
  48. int dyn_fifo_is_valid( dyn_fifo_ref fifo ){
  49.     return fifo && ( fifo->magic_value == DYN_FIFO_MAGIC_VALUE );
  50. }
  51.  
  52. int dyn_fifo_initialize( dyn_fifo_ref fifo, int size ){
  53.     if( ! fifo ) return EBADMEM;
  54.     if( size <= 0 ) return EINVAL;
  55.     fifo->items = ( int * ) malloc( sizeof( int ) * size + 1 );
  56.     if( ! fifo->items ) return ENOMEM;
  57.     fifo->size = size;
  58.     fifo->head = 0;
  59.     fifo->tail = 0;
  60.     fifo->magic_value = DYN_FIFO_MAGIC_VALUE;
  61.     return EOK;
  62. }
  63.  
  64. int dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size ){
  65.     int *   new_items;
  66.  
  67.     if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
  68.     if( NEXT_INDEX( fifo, fifo->tail ) == fifo->head ){
  69.         if(( max_size > 0 ) && (( fifo->size * 2 ) > max_size )){
  70.             if( fifo->size >= max_size ) return ENOMEM;
  71.         }else{
  72.             max_size = fifo->size * 2;
  73.         }
  74.         new_items = realloc( fifo->items, sizeof( int ) * max_size + 1 );
  75.         if( ! new_items ) return ENOMEM;
  76.         fifo->items = new_items;
  77.         if( fifo->tail < fifo->head ){
  78.             if( fifo->tail < max_size - fifo->size ){
  79.                 memcpy( fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof( int ));
  80.                 fifo->tail += fifo->size + 1;
  81.             }else{
  82.                 memcpy( fifo->items + fifo->size + 1, fifo->items, ( max_size - fifo->size ) * sizeof( int ));
  83.                 memcpy( fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size );
  84.                 fifo->tail -= max_size - fifo->size;
  85.             }
  86.         }
  87.         fifo->size = max_size;
  88.     }
  89.     fifo->items[ fifo->tail ] = value;
  90.     fifo->tail = NEXT_INDEX( fifo, fifo->tail );
  91.     return EOK;
  92. }
  93.  
  94. int dyn_fifo_pop( dyn_fifo_ref fifo ){
  95.     int value;
  96.  
  97.     if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
  98.     if( fifo->head == fifo->tail ) return ENOENT;
  99.     value = fifo->items[ fifo->head ];
  100.     fifo->head = NEXT_INDEX( fifo, fifo->head );
  101.     return value;
  102. }
  103.  
  104. int dyn_fifo_value( dyn_fifo_ref fifo ){
  105.     if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
  106.     if( fifo->head == fifo->tail ) return ENOENT;
  107.     return fifo->items[ fifo->head ];
  108. }
  109.  
  110. int dyn_fifo_destroy( dyn_fifo_ref fifo ){
  111.     if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
  112.     free( fifo->items );
  113.     fifo->magic_value = 0;
  114.     return EOK;
  115. }
  116.  
  117. /** @}
  118.  */
  119.