/src/samba/third_party/ngtcp2/lib/ngtcp2_balloc.c
Line | Count | Source |
1 | | /* |
2 | | * ngtcp2 |
3 | | * |
4 | | * Copyright (c) 2022 ngtcp2 contributors |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining |
7 | | * a copy of this software and associated documentation files (the |
8 | | * "Software"), to deal in the Software without restriction, including |
9 | | * without limitation the rights to use, copy, modify, merge, publish, |
10 | | * distribute, sublicense, and/or sell copies of the Software, and to |
11 | | * permit persons to whom the Software is furnished to do so, subject to |
12 | | * the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be |
15 | | * included in all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21 | | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22 | | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23 | | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 | | */ |
25 | | #include "ngtcp2_balloc.h" |
26 | | |
27 | | #include <assert.h> |
28 | | |
29 | | #include "ngtcp2_mem.h" |
30 | | |
31 | | void ngtcp2_balloc_init(ngtcp2_balloc *balloc, size_t blklen, |
32 | 0 | const ngtcp2_mem *mem) { |
33 | 0 | assert((blklen & 0xFU) == 0); |
34 | |
|
35 | 0 | balloc->mem = mem; |
36 | 0 | balloc->blklen = blklen; |
37 | 0 | balloc->head = NULL; |
38 | 0 | ngtcp2_buf_init(&balloc->buf, (void *)"", 0); |
39 | 0 | } |
40 | | |
41 | 0 | void ngtcp2_balloc_free(ngtcp2_balloc *balloc) { |
42 | 0 | if (balloc == NULL) { |
43 | 0 | return; |
44 | 0 | } |
45 | | |
46 | 0 | ngtcp2_balloc_clear(balloc); |
47 | 0 | } |
48 | | |
49 | 0 | void ngtcp2_balloc_clear(ngtcp2_balloc *balloc) { |
50 | 0 | ngtcp2_memblock_hd *p, *next; |
51 | |
|
52 | 0 | for (p = balloc->head; p; p = next) { |
53 | 0 | next = p->next; |
54 | 0 | ngtcp2_mem_free(balloc->mem, p); |
55 | 0 | } |
56 | |
|
57 | 0 | balloc->head = NULL; |
58 | 0 | ngtcp2_buf_init(&balloc->buf, (void *)"", 0); |
59 | 0 | } |
60 | | |
61 | 0 | int ngtcp2_balloc_get(ngtcp2_balloc *balloc, void **pbuf, size_t n) { |
62 | 0 | uint8_t *p; |
63 | 0 | ngtcp2_memblock_hd *hd; |
64 | |
|
65 | 0 | assert(n <= balloc->blklen); |
66 | |
|
67 | 0 | if (ngtcp2_buf_left(&balloc->buf) < n) { |
68 | 0 | p = ngtcp2_mem_malloc(balloc->mem, |
69 | 0 | sizeof(ngtcp2_memblock_hd) + 0x8U + balloc->blklen); |
70 | 0 | if (p == NULL) { |
71 | 0 | return NGTCP2_ERR_NOMEM; |
72 | 0 | } |
73 | | |
74 | 0 | hd = (void *)p; |
75 | 0 | hd->next = balloc->head; |
76 | 0 | balloc->head = hd; |
77 | 0 | ngtcp2_buf_init( |
78 | 0 | &balloc->buf, |
79 | 0 | (uint8_t *)(((uintptr_t)p + sizeof(ngtcp2_memblock_hd) + 0xFU) & |
80 | 0 | ~(uintptr_t)0xFU), |
81 | 0 | balloc->blklen); |
82 | 0 | } |
83 | | |
84 | 0 | assert(((uintptr_t)balloc->buf.last & 0xFU) == 0); |
85 | |
|
86 | 0 | *pbuf = balloc->buf.last; |
87 | 0 | balloc->buf.last += (n + 0xFU) & ~(uintptr_t)0xFU; |
88 | |
|
89 | 0 | return 0; |
90 | 0 | } |