Frédéric Lécaille | c6bc185 | 2021-06-30 14:25:10 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | DECLARE_POOL(pool_head_cbuf, "cbuf_pool", sizeof(struct cbuf)); |
| 26 | |
Frédéric Lécaille | a2e954a | 2021-08-04 14:53:06 +0200 | [diff] [blame] | 27 | /* Allocate and return a new circular buffer with <buf> as <sz> byte internal buffer |
| 28 | * if succeeded, NULL if not. |
| 29 | */ |
| 30 | struct cbuf *cbuf_new(unsigned char *buf, size_t sz) |
Frédéric Lécaille | c6bc185 | 2021-06-30 14:25:10 +0200 | [diff] [blame] | 31 | { |
| 32 | struct cbuf *cbuf; |
| 33 | |
| 34 | cbuf = pool_alloc(pool_head_cbuf); |
| 35 | if (cbuf) { |
Frédéric Lécaille | a2e954a | 2021-08-04 14:53:06 +0200 | [diff] [blame] | 36 | cbuf->sz = sz; |
| 37 | cbuf->buf = buf; |
Frédéric Lécaille | c6bc185 | 2021-06-30 14:25:10 +0200 | [diff] [blame] | 38 | cbuf->wr = 0; |
| 39 | cbuf->rd = 0; |
| 40 | } |
| 41 | |
| 42 | return cbuf; |
| 43 | } |
| 44 | |
| 45 | /* Free QUIC ring <cbuf> */ |
| 46 | void 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 | */ |