summaryrefslogtreecommitdiffstats
path: root/include/haproxy/cbuf.h
blob: b217a5c2de3c3d96a0d6d67ad4ebbae5b4f3ac82 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * include/haprox/cbuf.h
 * This file contains definitions and prototypes for circular buffers.
 * Inspired from Linux circular buffers (include/linux/circ_buf.h).
 *
 * Copyright 2021 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, version 2.1
 * exclusively.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _HAPROXY_CBUF_H
#define _HAPROXY_CBUF_H
#ifdef USE_QUIC
#ifndef USE_OPENSSL
#error "Must define USE_OPENSSL"
#endif
#endif

#include <haproxy/atomic.h>
#include <haproxy/list.h>
#include <haproxy/cbuf-t.h>

struct cbuf *cbuf_new(unsigned char *buf, size_t sz);
void cbuf_free(struct cbuf *cbuf);

/* Amount of data between <rd> and <wr> */
#define CBUF_DATA(wr, rd, size)  (((wr) - (rd)) & ((size) - 1))

/* Return the writer position in <cbuf>.
 * To be used only by the writer!
 */
static inline unsigned char *cb_wr(struct cbuf *cbuf)
{
	return cbuf->buf + cbuf->wr;
}

/* Reset the reader index.
 * To be used by a reader!
 */
static inline void cb_rd_reset(struct cbuf *cbuf)
{
	cbuf->rd = 0;
}

/* Reset the writer index.
 * To be used by a writer!
 */
static inline void cb_wr_reset(struct cbuf *cbuf)
{
	cbuf->wr = 0;
}

/* Increase <cbuf> circular buffer data by <count>.
 * To be used by a writer!
 */
static inline void cb_add(struct cbuf *cbuf, size_t count)
{
	cbuf->wr = (cbuf->wr + count) & (cbuf->sz - 1);
}

/* Return the reader position in <cbuf>.
 * To be used only by the reader!
 */
static inline unsigned char *cb_rd(struct cbuf *cbuf)
{
	return cbuf->buf + cbuf->rd;
}

/* Skip <count> byte in <cbuf> circular buffer.
 * To be used by a reader!
 */
static inline void cb_del(struct cbuf *cbuf, size_t count)
{
	cbuf->rd = (cbuf->rd + count) & (cbuf->sz - 1);
}

/* Return the amount of data left in <cbuf>.
 * To be used only by the writer!
 */
static inline size_t cb_data(struct cbuf *cbuf)
{
	size_t rd;

	rd = HA_ATOMIC_LOAD(&cbuf->rd);
	return CBUF_DATA(cbuf->wr, rd, cbuf->sz);
}

/* Return the amount of room left in <cbuf> minus 1 to distinguish
 * the case where the buffer is full from the case where is is empty
 * To be used only by the write!
 */
static inline size_t cb_room(struct cbuf *cbuf)
{
	size_t rd;

	rd = HA_ATOMIC_LOAD(&cbuf->rd);
	return CBUF_DATA(rd, cbuf->wr + 1, cbuf->sz);
}

/* Return the amount of contiguous data left in <cbuf>.
 * To be used only by the reader!
 */
static inline size_t cb_contig_data(struct cbuf *cbuf)
{
	size_t end, n;

	end = cbuf->sz - cbuf->rd;
	n = (HA_ATOMIC_LOAD(&cbuf->wr) + end) & (cbuf->sz - 1);
	return n < end ? n : end;
}

/* Return the amount of contiguous space left in <cbuf>.
 * To be used only by the writer!
 */
static inline size_t cb_contig_space(struct cbuf *cbuf)
{
	size_t end, n;

	end = cbuf->sz - 1 - cbuf->wr;
	n = (HA_ATOMIC_LOAD(&cbuf->rd) + end) & (cbuf->sz - 1);
	return n <= end ? n : end + 1;
}

#endif /* _HAPROXY_CBUF_H */