source: mainline/uspace/lib/c/include/adt/fifo.h@ d7f7a4a

Last change on this file since d7f7a4a was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2006 Jakub Jermar
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/** @addtogroup libc
8 * @{
9 */
10/** @file
11 */
12
13/*
14 * This implementation of FIFO stores values in an array
15 * (static or dynamic). As such, these FIFOs have upper bound
16 * on number of values they can store. Push and pop operations
17 * are done via accessing the array through head and tail indices.
18 * Because of better operation ordering in fifo_pop(), the access
19 * policy for these two indices is to 'increment (mod size of FIFO)
20 * and use'.
21 */
22
23#ifndef _LIBC_FIFO_H_
24#define _LIBC_FIFO_H_
25
26#include <stdlib.h>
27
28typedef unsigned long fifo_count_t;
29typedef unsigned long fifo_index_t;
30
31#define FIFO_CREATE_STATIC(name, t, itms) \
32 struct { \
33 t fifo[(itms)]; \
34 fifo_count_t items; \
35 fifo_index_t head; \
36 fifo_index_t tail; \
37 } name
38
39/** Create and initialize static FIFO.
40 *
41 * FIFO is allocated statically.
42 * This macro is suitable for creating smaller FIFOs.
43 *
44 * @param name Name of FIFO.
45 * @param t Type of values stored in FIFO.
46 * @param itms Number of items that can be stored in FIFO.
47 */
48#define FIFO_INITIALIZE_STATIC(name, t, itms) \
49 FIFO_CREATE_STATIC(name, t, itms) = { \
50 .items = (itms), \
51 .head = 0, \
52 .tail = 0 \
53 }
54
55/** Create and prepare dynamic FIFO.
56 *
57 * FIFO is allocated dynamically.
58 * This macro is suitable for creating larger FIFOs.
59 *
60 * @param name Name of FIFO.
61 * @param t Type of values stored in FIFO.
62 * @param itms Number of items that can be stored in FIFO.
63 */
64#define FIFO_INITIALIZE_DYNAMIC(name, t, itms) \
65 struct { \
66 t *fifo; \
67 fifo_count_t items; \
68 fifo_index_t head; \
69 fifo_index_t tail; \
70 } name = { \
71 .fifo = NULL, \
72 .items = (itms), \
73 .head = 0, \
74 .tail = 0 \
75 }
76
77/** Pop value from head of FIFO.
78 *
79 * @param name FIFO name.
80 *
81 * @return Leading value in FIFO.
82 */
83#define fifo_pop(name) \
84 name.fifo[name.head = (name.head + 1) < name.items ? (name.head + 1) : 0]
85
86/** Push value to tail of FIFO.
87 *
88 * @param name FIFO name.
89 * @param value Value to be appended to FIFO.
90 *
91 */
92#define fifo_push(name, value) \
93 name.fifo[name.tail = (name.tail + 1) < name.items ? (name.tail + 1) : 0] = (value)
94
95/** Allocate memory for dynamic FIFO.
96 *
97 * @param name FIFO name.
98 */
99#define fifo_create(name) \
100 name.fifo = malloc(sizeof(*name.fifo) * name.items)
101
102#endif
103
104/** @}
105 */
Note: See TracBrowser for help on using the repository browser.