[ee7736e] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[ee7736e] | 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 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[ee7736e] | 35 | /*
|
---|
| 36 | * This implementation of FIFO stores values in an array
|
---|
| 37 | * (static or dynamic). As such, these FIFOs have upper bound
|
---|
| 38 | * on number of values they can store. Push and pop operations
|
---|
| 39 | * are done via accessing the array through head and tail indices.
|
---|
| 40 | * Because of better operation ordering in fifo_pop(), the access
|
---|
| 41 | * policy for these two indices is to 'increment (mod size of FIFO)
|
---|
| 42 | * and use'.
|
---|
| 43 | */
|
---|
| 44 |
|
---|
[fadd381] | 45 | #ifndef LIBC_FIFO_H_
|
---|
| 46 | #define LIBC_FIFO_H_
|
---|
[ee7736e] | 47 |
|
---|
| 48 | #include <malloc.h>
|
---|
| 49 |
|
---|
| 50 | typedef unsigned long fifo_count_t;
|
---|
| 51 | typedef unsigned long fifo_index_t;
|
---|
| 52 |
|
---|
[cf28036c] | 53 | #define FIFO_CREATE_STATIC(name, t, itms) \
|
---|
| 54 | struct { \
|
---|
| 55 | t fifo[(itms)]; \
|
---|
| 56 | fifo_count_t items; \
|
---|
| 57 | fifo_index_t head; \
|
---|
| 58 | fifo_index_t tail; \
|
---|
| 59 | } name
|
---|
| 60 |
|
---|
[ee7736e] | 61 | /** Create and initialize static FIFO.
|
---|
| 62 | *
|
---|
| 63 | * FIFO is allocated statically.
|
---|
| 64 | * This macro is suitable for creating smaller FIFOs.
|
---|
| 65 | *
|
---|
| 66 | * @param name Name of FIFO.
|
---|
| 67 | * @param t Type of values stored in FIFO.
|
---|
| 68 | * @param itms Number of items that can be stored in FIFO.
|
---|
| 69 | */
|
---|
| 70 | #define FIFO_INITIALIZE_STATIC(name, t, itms) \
|
---|
[cf28036c] | 71 | FIFO_CREATE_STATIC(name, t, itms) = { \
|
---|
[ee7736e] | 72 | .items = (itms), \
|
---|
| 73 | .head = 0, \
|
---|
| 74 | .tail = 0 \
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /** Create and prepare dynamic FIFO.
|
---|
| 78 | *
|
---|
| 79 | * FIFO is allocated dynamically.
|
---|
| 80 | * This macro is suitable for creating larger FIFOs.
|
---|
| 81 | *
|
---|
| 82 | * @param name Name of FIFO.
|
---|
| 83 | * @param t Type of values stored in FIFO.
|
---|
| 84 | * @param itms Number of items that can be stored in FIFO.
|
---|
| 85 | */
|
---|
| 86 | #define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \
|
---|
| 87 | struct { \
|
---|
| 88 | t *fifo; \
|
---|
| 89 | fifo_count_t items; \
|
---|
| 90 | fifo_index_t head; \
|
---|
| 91 | fifo_index_t tail; \
|
---|
| 92 | } name = { \
|
---|
| 93 | .fifo = NULL, \
|
---|
| 94 | .items = (itms), \
|
---|
| 95 | .head = 0, \
|
---|
| 96 | .tail = 0 \
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /** Pop value from head of FIFO.
|
---|
| 100 | *
|
---|
| 101 | * @param name FIFO name.
|
---|
| 102 | *
|
---|
| 103 | * @return Leading value in FIFO.
|
---|
| 104 | */
|
---|
| 105 | #define fifo_pop(name) \
|
---|
| 106 | name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0]
|
---|
| 107 |
|
---|
| 108 | /** Push value to tail of FIFO.
|
---|
| 109 | *
|
---|
| 110 | * @param name FIFO name.
|
---|
| 111 | * @param value Value to be appended to FIFO.
|
---|
| 112 | *
|
---|
| 113 | */
|
---|
| 114 | #define fifo_push(name, value) \
|
---|
| 115 | name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value)
|
---|
| 116 |
|
---|
| 117 | /** Allocate memory for dynamic FIFO.
|
---|
| 118 | *
|
---|
| 119 | * @param name FIFO name.
|
---|
| 120 | */
|
---|
| 121 | #define fifo_create(name) \
|
---|
| 122 | name.fifo = malloc(sizeof(*name.fifo) * name.items)
|
---|
| 123 |
|
---|
| 124 | #endif
|
---|
[b2951e2] | 125 |
|
---|
[fadd381] | 126 | /** @}
|
---|
[b2951e2] | 127 | */
|
---|