nghttp3_objalloc.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2022 nghttp3 contributors
  5. * Copyright (c) 2022 ngtcp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef NGHTTP3_OBJALLOC_H
  27. #define NGHTTP3_OBJALLOC_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <nghttp3/nghttp3.h>
  32. #include "nghttp3_balloc.h"
  33. #include "nghttp3_opl.h"
  34. #include "nghttp3_macro.h"
  35. #include "nghttp3_mem.h"
  36. /*
  37. * nghttp3_objalloc combines nghttp3_balloc and nghttp3_opl, and
  38. * provides an object pool with the custom allocator to reduce the
  39. * allocation and deallocation overheads for small objects.
  40. */
  41. typedef struct nghttp3_objalloc {
  42. nghttp3_balloc balloc;
  43. nghttp3_opl opl;
  44. } nghttp3_objalloc;
  45. /*
  46. * nghttp3_objalloc_init initializes |objalloc|. |blklen| is directly
  47. * passed to nghttp3_balloc_init.
  48. */
  49. void nghttp3_objalloc_init(nghttp3_objalloc *objalloc, size_t blklen,
  50. const nghttp3_mem *mem);
  51. /*
  52. * nghttp3_objalloc_free releases all allocated resources.
  53. */
  54. void nghttp3_objalloc_free(nghttp3_objalloc *objalloc);
  55. /*
  56. * nghttp3_objalloc_clear releases all allocated resources and
  57. * initializes its state.
  58. */
  59. void nghttp3_objalloc_clear(nghttp3_objalloc *objalloc);
  60. #ifndef NOMEMPOOL
  61. # define nghttp3_objalloc_decl(NAME, TYPE, OPLENTFIELD) \
  62. inline static void nghttp3_objalloc_##NAME##_init( \
  63. nghttp3_objalloc *objalloc, size_t nmemb, const nghttp3_mem *mem) { \
  64. nghttp3_objalloc_init( \
  65. objalloc, ((sizeof(TYPE) + 0xfu) & ~(uintptr_t)0xfu) * nmemb, mem); \
  66. } \
  67. \
  68. TYPE *nghttp3_objalloc_##NAME##_get(nghttp3_objalloc *objalloc); \
  69. \
  70. TYPE *nghttp3_objalloc_##NAME##_len_get(nghttp3_objalloc *objalloc, \
  71. size_t len); \
  72. \
  73. inline static void nghttp3_objalloc_##NAME##_release( \
  74. nghttp3_objalloc *objalloc, TYPE *obj) { \
  75. nghttp3_opl_push(&objalloc->opl, &obj->OPLENTFIELD); \
  76. }
  77. # define nghttp3_objalloc_def(NAME, TYPE, OPLENTFIELD) \
  78. TYPE *nghttp3_objalloc_##NAME##_get(nghttp3_objalloc *objalloc) { \
  79. nghttp3_opl_entry *oplent = nghttp3_opl_pop(&objalloc->opl); \
  80. TYPE *obj; \
  81. int rv; \
  82. \
  83. if (!oplent) { \
  84. rv = \
  85. nghttp3_balloc_get(&objalloc->balloc, (void **)&obj, sizeof(TYPE)); \
  86. if (rv != 0) { \
  87. return NULL; \
  88. } \
  89. \
  90. return obj; \
  91. } \
  92. \
  93. return nghttp3_struct_of(oplent, TYPE, OPLENTFIELD); \
  94. } \
  95. \
  96. TYPE *nghttp3_objalloc_##NAME##_len_get(nghttp3_objalloc *objalloc, \
  97. size_t len) { \
  98. nghttp3_opl_entry *oplent = nghttp3_opl_pop(&objalloc->opl); \
  99. TYPE *obj; \
  100. int rv; \
  101. \
  102. if (!oplent) { \
  103. rv = nghttp3_balloc_get(&objalloc->balloc, (void **)&obj, len); \
  104. if (rv != 0) { \
  105. return NULL; \
  106. } \
  107. \
  108. return obj; \
  109. } \
  110. \
  111. return nghttp3_struct_of(oplent, TYPE, OPLENTFIELD); \
  112. }
  113. #else /* defined(NOMEMPOOL) */
  114. # define nghttp3_objalloc_decl(NAME, TYPE, OPLENTFIELD) \
  115. inline static void nghttp3_objalloc_##NAME##_init( \
  116. nghttp3_objalloc *objalloc, size_t nmemb, const nghttp3_mem *mem) { \
  117. nghttp3_objalloc_init( \
  118. objalloc, ((sizeof(TYPE) + 0xfu) & ~(uintptr_t)0xfu) * nmemb, mem); \
  119. } \
  120. \
  121. inline static TYPE *nghttp3_objalloc_##NAME##_get( \
  122. nghttp3_objalloc *objalloc) { \
  123. return nghttp3_mem_malloc(objalloc->balloc.mem, sizeof(TYPE)); \
  124. } \
  125. \
  126. inline static TYPE *nghttp3_objalloc_##NAME##_len_get( \
  127. nghttp3_objalloc *objalloc, size_t len) { \
  128. return nghttp3_mem_malloc(objalloc->balloc.mem, len); \
  129. } \
  130. \
  131. inline static void nghttp3_objalloc_##NAME##_release( \
  132. nghttp3_objalloc *objalloc, TYPE *obj) { \
  133. nghttp3_mem_free(objalloc->balloc.mem, obj); \
  134. }
  135. # define nghttp3_objalloc_def(NAME, TYPE, OPLENTFIELD)
  136. #endif /* defined(NOMEMPOOL) */
  137. #endif /* !defined(NGHTTP3_OBJALLOC_H) */