mmlib/mmat: replace some variables by literal constants
[mmondor.git] / mmsoftware / mmlib / mmlifo.h
1 /* $Id: mmlifo.h,v 1.6 2007/12/05 23:47:56 mmondor Exp $ */
2
3 /*
4 * Copyright (C) 2000-2004, Matthew Mondor
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Matthew Mondor.
18 * 4. The name of Matthew Mondor may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 * 5. Redistribution of source code may not be released under the terms of
22 * any GNU Public License derivate.
23 *
24 * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36
37
38 #ifndef MMLIFO_H
39 #define MMLIFO_H
40
41
42
43 #include <stdbool.h>
44 #include <stdint.h>
45
46 #include <mmtypes.h>
47
48
49
50 /* LIFO_DEFINE(lifotypename, objecttype); */
51 #define LIFO_DEFINE(n, o) typedef struct n { \
52 uint32_t size, elements; \
53 o *buffer, *endbuffer, *head; \
54 } n
55
56 /* Because of the way this is implemented using macros, it would also be
57 * possible to provide lifo types to hold structures.
58 */
59 LIFO_DEFINE(lifo8_t, uint8_t);
60 LIFO_DEFINE(lifo16_t, uint16_t);
61 LIFO_DEFINE(lifo32_t, uint32_t);
62 LIFO_DEFINE(lifo64_t, uint64_t);
63
64
65
66 /* XXX Although it's great to use macros for these operations, it also
67 * prevents assembly functions to be provided to replace them.
68 */
69 /* void LIFO_INIT(lifo*_t *, uint*_t *, uint32_t); */
70 #define LIFO_INIT(f, b, s) do { \
71 (f)->size = (s); \
72 (f)->elements = 0; \
73 (f)->buffer = (f)->endbuffer = (f)->head = (b); \
74 } while (/* CONSTCOND */0)
75
76 /* bool LIFO_FULL(lifo*_t *); */
77 #define LIFO_FULL(f) ((f)->elements == (f)->size)
78
79 /* uint32_t LIFO_STAT(lifo*_t *); */
80 #define LIFO_STAT(f) ((f)->elements)
81
82 /* void LIFO_FLUSH(lifo*_t *); */
83 #define LIFO_FLUSH(f) do { \
84 (f)->head = (f)->buffer; \
85 (f)->elements = 0; \
86 } while (/* CONSTCOND */0)
87
88 /* void LIFO_PUT(lifo*_t *, uint*_t *); */
89 #define LIFO_PUT(s, e) do { \
90 if ((s)->elements < (s)->size) { \
91 *((s)->head++) = *(e); \
92 (s)->elements++; \
93 } \
94 } while (/* CONSTCOND */0)
95
96 /* void LIFO_GET(lifo*_t *, uint*_t *); */
97 #define LIFO_GET(s, e) do { \
98 if ((s)->elements > 0) { \
99 *(e) = *(--(s)->head); \
100 (s)->elements--; \
101 } \
102 } while (/* CONSTCOND */0)
103
104 /* LIFO_ALLOC(lifo*_t *, uint*_t **, size_t *); */
105
106 /* LIFO_FREE(lifo*_t, uint*_t **, size_t *); */
107
108
109
110 #endif