ngtcp2_balloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <assert.h>
  27. #include "ngtcp2_mem.h"
  28. void ngtcp2_balloc_init(ngtcp2_balloc *balloc, size_t blklen,
  29. const ngtcp2_mem *mem) {
  30. assert((blklen & 0xfu) == 0);
  31. balloc->mem = mem;
  32. balloc->blklen = blklen;
  33. balloc->head = NULL;
  34. ngtcp2_buf_init(&balloc->buf, (void *)"", 0);
  35. }
  36. void ngtcp2_balloc_free(ngtcp2_balloc *balloc) {
  37. if (balloc == NULL) {
  38. return;
  39. }
  40. ngtcp2_balloc_clear(balloc);
  41. }
  42. void ngtcp2_balloc_clear(ngtcp2_balloc *balloc) {
  43. ngtcp2_memblock_hd *p, *next;
  44. for (p = balloc->head; p; p = next) {
  45. next = p->next;
  46. ngtcp2_mem_free(balloc->mem, p);
  47. }
  48. balloc->head = NULL;
  49. ngtcp2_buf_init(&balloc->buf, (void *)"", 0);
  50. }
  51. int ngtcp2_balloc_get(ngtcp2_balloc *balloc, void **pbuf, size_t n) {
  52. uint8_t *p;
  53. ngtcp2_memblock_hd *hd;
  54. assert(n <= balloc->blklen);
  55. if (ngtcp2_buf_left(&balloc->buf) < n) {
  56. p = ngtcp2_mem_malloc(balloc->mem,
  57. sizeof(ngtcp2_memblock_hd) + 0x8u + balloc->blklen);
  58. if (p == NULL) {
  59. return NGTCP2_ERR_NOMEM;
  60. }
  61. hd = (ngtcp2_memblock_hd *)(void *)p;
  62. hd->next = balloc->head;
  63. balloc->head = hd;
  64. ngtcp2_buf_init(
  65. &balloc->buf,
  66. (uint8_t *)(((uintptr_t)p + sizeof(ngtcp2_memblock_hd) + 0xfu) &
  67. ~(uintptr_t)0xfu),
  68. balloc->blklen);
  69. }
  70. assert(((uintptr_t)balloc->buf.last & 0xfu) == 0);
  71. *pbuf = balloc->buf.last;
  72. balloc->buf.last += (n + 0xfu) & ~(uintptr_t)0xfu;
  73. return 0;
  74. }