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