• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ngtcp2_balloc_init(ngtcp2_balloc * balloc,size_t blklen,const ngtcp2_mem * mem)31 void ngtcp2_balloc_init(ngtcp2_balloc *balloc, size_t blklen,
32                         const ngtcp2_mem *mem) {
33   assert((blklen & 0xfu) == 0);
34 
35   balloc->mem = mem;
36   balloc->blklen = blklen;
37   balloc->head = NULL;
38   ngtcp2_buf_init(&balloc->buf, (void *)"", 0);
39 }
40 
ngtcp2_balloc_free(ngtcp2_balloc * balloc)41 void ngtcp2_balloc_free(ngtcp2_balloc *balloc) {
42   if (balloc == NULL) {
43     return;
44   }
45 
46   ngtcp2_balloc_clear(balloc);
47 }
48 
ngtcp2_balloc_clear(ngtcp2_balloc * balloc)49 void ngtcp2_balloc_clear(ngtcp2_balloc *balloc) {
50   ngtcp2_memblock_hd *p, *next;
51 
52   for (p = balloc->head; p; p = next) {
53     next = p->next;
54     ngtcp2_mem_free(balloc->mem, p);
55   }
56 
57   balloc->head = NULL;
58   ngtcp2_buf_init(&balloc->buf, (void *)"", 0);
59 }
60 
ngtcp2_balloc_get(ngtcp2_balloc * balloc,void ** pbuf,size_t n)61 int ngtcp2_balloc_get(ngtcp2_balloc *balloc, void **pbuf, size_t n) {
62   uint8_t *p;
63   ngtcp2_memblock_hd *hd;
64 
65   assert(n <= balloc->blklen);
66 
67   if (ngtcp2_buf_left(&balloc->buf) < n) {
68     p = ngtcp2_mem_malloc(balloc->mem,
69                           sizeof(ngtcp2_memblock_hd) + 0x10u + balloc->blklen);
70     if (p == NULL) {
71       return NGTCP2_ERR_NOMEM;
72     }
73 
74     hd = (ngtcp2_memblock_hd *)(void *)p;
75     hd->next = balloc->head;
76     balloc->head = hd;
77     ngtcp2_buf_init(
78         &balloc->buf,
79         (uint8_t *)(((uintptr_t)p + sizeof(ngtcp2_memblock_hd) + 0xfu) &
80                     ~(uintptr_t)0xfu),
81         balloc->blklen);
82   }
83 
84   assert(((uintptr_t)balloc->buf.last & 0xfu) == 0);
85 
86   *pbuf = balloc->buf.last;
87   balloc->buf.last += (n + 0xfu) & ~(uintptr_t)0xfu;
88 
89   return 0;
90 }
91