summaryrefslogtreecommitdiff
path: root/inc/common/fifo.h
blob: 410ac990b558428544d30f381bf8ad9d82c0170a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright (C) 2003-2008 Andrey Nazarov

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef FIFO_H
#define FIFO_H

typedef struct {
    byte *data;
    size_t size;
    size_t ax, ay, bs;
} fifo_t;

static inline void *FIFO_Reserve(fifo_t *fifo, size_t *len)
{
    size_t tail;

    if (fifo->bs) {
        *len = fifo->ax - fifo->bs;
        return fifo->data + fifo->bs;
    }

    tail = fifo->size - fifo->ay;
    if (tail) {
        *len = tail;
        return fifo->data + fifo->ay;
    }

    *len = fifo->ax;
    return fifo->data;
}

static inline void FIFO_Commit(fifo_t *fifo, size_t len)
{
    size_t tail;

    if (fifo->bs) {
        fifo->bs += len;
        return;
    }

    tail = fifo->size - fifo->ay;
    if (tail) {
        fifo->ay += len;
        return;
    }

    fifo->bs = len;
}

static inline void *FIFO_Peek(fifo_t *fifo, size_t *len)
{
    *len = fifo->ay - fifo->ax;
    return fifo->data + fifo->ax;
}

static inline void FIFO_Decommit(fifo_t *fifo, size_t len)
{
    if (fifo->ax + len < fifo->ay) {
        fifo->ax += len;
        return;
    }

    fifo->ay = fifo->bs;
    fifo->ax = fifo->bs = 0;
}

static inline size_t FIFO_Usage(fifo_t *fifo)
{
    return fifo->ay - fifo->ax + fifo->bs;
}

static inline int FIFO_Percent(fifo_t *fifo)
{
    if (!fifo->size) {
        return 0;
    }
    return (int)(FIFO_Usage(fifo) * 100 / fifo->size);
}

static inline void FIFO_Clear(fifo_t *fifo)
{
    fifo->ax = fifo->ay = fifo->bs = 0;
}

size_t FIFO_Read(fifo_t *fifo, void *buffer, size_t len);
size_t FIFO_Write(fifo_t *fifo, const void *buffer, size_t len);

static inline qboolean FIFO_TryRead(fifo_t *fifo, void *buffer, size_t len)
{
    if (FIFO_Read(fifo, NULL, len) < len) {
        return qfalse;
    }
    FIFO_Read(fifo, buffer, len);
    return qtrue;
}

static inline qboolean FIFO_TryWrite(fifo_t *fifo, void *buffer, size_t len)
{
    if (FIFO_Write(fifo, NULL, len) < len) {
        return qfalse;
    }
    FIFO_Write(fifo, buffer, len);
    return qtrue;
}

qboolean FIFO_ReadMessage(fifo_t *fifo, size_t msglen);

#endif // FIFO_H