blob: f719435e1deaab9acb80e9256d88eeb56e7c1999 [file] [log] [blame]
Frédéric Lécaillec6bc1852021-06-30 14:25:10 +02001/*
2 * Circular buffer management
3 *
4 * Copyright 2021 HAProxy Technologies, Frédéric Lécaille <flecaill@haproxy.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <haproxy/list.h>
22#include <haproxy/pool.h>
23#include <haproxy/cbuf-t.h>
24
25DECLARE_POOL(pool_head_cbuf, "cbuf_pool", sizeof(struct cbuf));
26
Frédéric Lécaillea2e954a2021-08-04 14:53:06 +020027/* Allocate and return a new circular buffer with <buf> as <sz> byte internal buffer
28 * if succeeded, NULL if not.
29 */
30struct cbuf *cbuf_new(unsigned char *buf, size_t sz)
Frédéric Lécaillec6bc1852021-06-30 14:25:10 +020031{
32 struct cbuf *cbuf;
33
34 cbuf = pool_alloc(pool_head_cbuf);
35 if (cbuf) {
Frédéric Lécaillea2e954a2021-08-04 14:53:06 +020036 cbuf->sz = sz;
37 cbuf->buf = buf;
Frédéric Lécaillec6bc1852021-06-30 14:25:10 +020038 cbuf->wr = 0;
39 cbuf->rd = 0;
40 }
41
42 return cbuf;
43}
44
45/* Free QUIC ring <cbuf> */
46void cbuf_free(struct cbuf *cbuf)
47{
48 if (!cbuf)
49 return;
50
51 pool_free(pool_head_cbuf, cbuf);
52}
53
54/*
55 * Local variables:
56 * c-indent-level: 8
57 * c-basic-offset: 8
58 * End:
59 */