From c0ffed778805e982208259faa6c48e93e05b4744 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 7 Dec 2014 23:07:13 +0900 Subject: [PATCH] Support custom memory allocator nghttp2_mem structure is introduced to hold custom memory allocator functions and user supplied pointer. nghttp2_mem object can be passed to nghttp2_session_client_new3(), nghttp2_session_server_new3(), nghttp2_hd_deflate_new2() and nghttp2_hd_inflate_new2() to replace standard malloc(), free(), calloc() and realloc(). nghttp2_mem structure has user supplied pointer mem_user_data which can be used as per session/object memory pool. --- lib/Makefile.am | 6 +- lib/includes/nghttp2/nghttp2.h | 152 ++++++++++++++++++++++ lib/nghttp2_buf.c | 62 +++++---- lib/nghttp2_buf.h | 21 +-- lib/nghttp2_frame.c | 43 +++--- lib/nghttp2_frame.h | 21 +-- lib/nghttp2_hd.c | 170 ++++++++++++++++-------- lib/nghttp2_hd.h | 15 ++- lib/nghttp2_helper.c | 6 +- lib/nghttp2_helper.h | 11 +- lib/nghttp2_map.c | 16 ++- lib/nghttp2_map.h | 4 +- lib/nghttp2_mem.c | 61 +++++++++ lib/nghttp2_mem.h | 44 +++++++ lib/nghttp2_outbound_item.c | 10 +- lib/nghttp2_outbound_item.h | 3 +- lib/nghttp2_pq.c | 10 +- lib/nghttp2_pq.h | 5 +- lib/nghttp2_session.c | 230 +++++++++++++++++++++------------ lib/nghttp2_session.h | 3 + lib/nghttp2_submit.c | 43 ++++-- python/cnghttp2.pxd | 22 +--- src/deflatehd.cc | 22 ++-- src/inflatehd.cc | 8 +- tests/nghttp2_buf_test.c | 40 ++++-- tests/nghttp2_frame_test.c | 63 +++++---- tests/nghttp2_hd_test.c | 95 +++++++++----- tests/nghttp2_map_test.c | 6 +- tests/nghttp2_pq_test.c | 4 +- tests/nghttp2_session_test.c | 226 ++++++++++++++++++++------------ tests/nghttp2_test_helper.c | 14 +- 31 files changed, 993 insertions(+), 443 deletions(-) create mode 100644 lib/nghttp2_mem.c create mode 100644 lib/nghttp2_mem.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 74a6344b..35a05954 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -44,7 +44,8 @@ OBJECTS = nghttp2_pq.c nghttp2_map.c nghttp2_queue.c \ nghttp2_version.c \ nghttp2_priority_spec.c \ nghttp2_option.c \ - nghttp2_callbacks.c + nghttp2_callbacks.c \ + nghttp2_mem.c HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \ nghttp2_frame.h \ @@ -56,7 +57,8 @@ HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \ nghttp2_hd.h nghttp2_hd_huffman.h \ nghttp2_priority_spec.h \ nghttp2_option.h \ - nghttp2_callbacks.h + nghttp2_callbacks.h \ + nghttp2_mem.h libnghttp2_la_SOURCES = $(HFILES) $(OBJECTS) libnghttp2_la_LDFLAGS = -no-undefined \ diff --git a/lib/includes/nghttp2/nghttp2.h b/lib/includes/nghttp2/nghttp2.h index 6f1a914c..a76e9655 100644 --- a/lib/includes/nghttp2/nghttp2.h +++ b/lib/includes/nghttp2/nghttp2.h @@ -1687,6 +1687,69 @@ void nghttp2_session_callbacks_set_on_begin_frame_callback( nghttp2_session_callbacks *cbs, nghttp2_on_begin_frame_callback on_begin_frame_callback); +/** + * @functypedef + * + * Custom memory allocator to replace malloc(). The |mem_user_data| + * is the mem_user_data member of :type:`nghttp2_mem` structure. + */ +typedef void *(*nghttp2_malloc)(size_t size, void *mem_user_data); + +/** + * @functypedef + * + * Custom memory allocator to replace free(). The |mem_user_data| is + * the mem_user_data member of :type:`nghttp2_mem` structure. + */ +typedef void (*nghttp2_free)(void *ptr, void *mem_user_data); + +/** + * @functypedef + * + * Custom memory allocator to replace calloc(). The |mem_user_data| + * is the mem_user_data member of :type:`nghttp2_mem` structure. + */ +typedef void *(*nghttp2_calloc)(size_t nmemb, size_t size, void *mem_user_data); + +/** + * @functypedef + * + * Custom memory allocator to replace realloc(). The |mem_user_data| + * is the mem_user_data member of :type:`nghttp2_mem` structure. + */ +typedef void *(*nghttp2_realloc)(void *ptr, size_t size, void *mem_user_data); + +/** + * @struct + * + * Custom memory allocator functions and user defined pointer. The + * |mem_user_data| member is passed to each allocator function. This + * can be used, for example, to achieve per-session memory pool. + */ +typedef struct { + /** + * An arbitrary user supplied data. This is passed to each + * allocator function. + */ + void *mem_user_data; + /** + * Custom allocator function to replace malloc(). + */ + nghttp2_malloc malloc; + /** + * Custom allocator function to replace free(). + */ + nghttp2_free free; + /** + * Custom allocator function to replace calloc(). + */ + nghttp2_calloc calloc; + /** + * Custom allocator function to replace realloc(). + */ + nghttp2_realloc realloc; +} nghttp2_mem; + struct nghttp2_option; /** @@ -1869,6 +1932,58 @@ int nghttp2_session_server_new2(nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks, void *user_data, const nghttp2_option *option); +/** + * @function + * + * Like `nghttp2_session_client_new2()`, but with additional custom + * memory allocator specified in the |mem|. + * + * The |mem| can be ``NULL`` and the call is equivalent to + * `nghttp2_session_client_new2()`. + * + * This function does not take ownership |mem|. The application is + * responsible for freeing |mem|. + * + * The library code does not refer to |mem| pointer after this + * function returns, so the application can safely free it. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * :enum:`NGHTTP2_ERR_NOMEM` + * Out of memory. + */ +int nghttp2_session_client_new3(nghttp2_session **session_ptr, + const nghttp2_session_callbacks *callbacks, + void *user_data, const nghttp2_option *option, + nghttp2_mem *mem); + +/** + * @function + * + * Like `nghttp2_session_server_new2()`, but with additional custom + * memory allocator specified in the |mem|. + * + * The |mem| can be ``NULL`` and the call is equivalent to + * `nghttp2_session_server_new2()`. + * + * This function does not take ownership |mem|. The application is + * responsible for freeing |mem|. + * + * The library code does not refer to |mem| pointer after this + * function returns, so the application can safely free it. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * :enum:`NGHTTP2_ERR_NOMEM` + * Out of memory. + */ +int nghttp2_session_server_new3(nghttp2_session **session_ptr, + const nghttp2_session_callbacks *callbacks, + void *user_data, const nghttp2_option *option, + nghttp2_mem *mem); + /** * @function * @@ -3079,6 +3194,25 @@ typedef struct nghttp2_hd_deflater nghttp2_hd_deflater; int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr, size_t deflate_hd_table_bufsize_max); +/** + * @function + * + * Like `nghttp2_hd_deflate_new()`, but with additional custom memory + * allocator specified in the |mem|. + * + * The |mem| can be ``NULL`` and the call is equivalent to + * `nghttp2_hd_deflate_new()`. + * + * This function does not take ownership |mem|. The application is + * responsible for freeing |mem|. + * + * The library code does not refer to |mem| pointer after this + * function returns, so the application can safely free it. + */ +int nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr, + size_t deflate_hd_table_bufsize_max, + nghttp2_mem *mem); + /** * @function * @@ -3176,6 +3310,24 @@ typedef struct nghttp2_hd_inflater nghttp2_hd_inflater; */ int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr); +/** + * @function + * + * Like `nghttp2_hd_inflate_new()`, but with additional custom memory + * allocator specified in the |mem|. + * + * The |mem| can be ``NULL`` and the call is equivalent to + * `nghttp2_hd_inflate_new()`. + * + * This function does not take ownership |mem|. The application is + * responsible for freeing |mem|. + * + * The library code does not refer to |mem| pointer after this + * function returns, so the application can safely free it. + */ +int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr, + nghttp2_mem *mem); + /** * @function * diff --git a/lib/nghttp2_buf.c b/lib/nghttp2_buf.c index 35c3fb93..a9fd3f84 100644 --- a/lib/nghttp2_buf.c +++ b/lib/nghttp2_buf.c @@ -36,21 +36,21 @@ void nghttp2_buf_init(nghttp2_buf *buf) { buf->mark = NULL; } -int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial) { +int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) { nghttp2_buf_init(buf); - return nghttp2_buf_reserve(buf, initial); + return nghttp2_buf_reserve(buf, initial, mem); } -void nghttp2_buf_free(nghttp2_buf *buf) { +void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) { if (buf == NULL) { return; } - free(buf->begin); + nghttp2_mem_free(mem, buf->begin); buf->begin = NULL; } -int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap) { +int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) { uint8_t *ptr; size_t cap; @@ -62,7 +62,7 @@ int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap) { new_cap = nghttp2_max(new_cap, cap * 2); - ptr = realloc(buf->begin, new_cap); + ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap); if (ptr == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -85,42 +85,45 @@ void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) { buf->end = begin + len; } -static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length) { +static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length, + nghttp2_mem *mem) { int rv; - *chain = malloc(sizeof(nghttp2_buf_chain)); + *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain)); if (*chain == NULL) { return NGHTTP2_ERR_NOMEM; } (*chain)->next = NULL; - rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length); + rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem); if (rv != 0) { - free(*chain); + nghttp2_mem_free(mem, *chain); return NGHTTP2_ERR_NOMEM; } return 0; } -static void buf_chain_del(nghttp2_buf_chain *chain) { - nghttp2_buf_free(&chain->buf); - free(chain); +static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) { + nghttp2_buf_free(&chain->buf, mem); + nghttp2_mem_free(mem, chain); } -int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, - size_t max_chunk) { - return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0); +int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk, + nghttp2_mem *mem) { + return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem); } int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length, - size_t max_chunk, size_t offset) { - return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset); + size_t max_chunk, size_t offset, nghttp2_mem *mem) { + return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset, + mem); } int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length, - size_t max_chunk, size_t chunk_keep, size_t offset) { + size_t max_chunk, size_t chunk_keep, size_t offset, + nghttp2_mem *mem) { int rv; nghttp2_buf_chain *chain; @@ -128,11 +131,12 @@ int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length, return NGHTTP2_ERR_INVALID_ARGUMENT; } - rv = buf_chain_new(&chain, chunk_length); + rv = buf_chain_new(&chain, chunk_length, mem); if (rv != 0) { return rv; } + bufs->mem = mem; bufs->offset = offset; bufs->head = chain; @@ -156,7 +160,7 @@ int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) { return NGHTTP2_ERR_INVALID_ARGUMENT; } - rv = buf_chain_new(&chain, chunk_length); + rv = buf_chain_new(&chain, chunk_length, bufs->mem); if (rv != 0) { return rv; } @@ -184,7 +188,7 @@ void nghttp2_bufs_free(nghttp2_bufs *bufs) { for (chain = bufs->head; chain;) { next_chain = chain->next; - buf_chain_del(chain); + buf_chain_del(chain, bufs->mem); chain = next_chain; } @@ -192,10 +196,11 @@ void nghttp2_bufs_free(nghttp2_bufs *bufs) { bufs->head = NULL; } -int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len) { +int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len, + nghttp2_mem *mem) { nghttp2_buf_chain *chain; - chain = malloc(sizeof(nghttp2_buf_chain)); + chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain)); if (chain == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -204,6 +209,7 @@ int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len) { nghttp2_buf_wrap_init(&chain->buf, begin, len); + bufs->mem = mem; bufs->offset = 0; bufs->head = chain; @@ -222,7 +228,7 @@ void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) { return; } - free(bufs->head); + nghttp2_mem_free(bufs->mem, bufs->head); bufs->head = NULL; } @@ -270,7 +276,7 @@ static int bufs_alloc_chain(nghttp2_bufs *bufs) { return NGHTTP2_ERR_BUFFER_ERROR; } - rv = buf_chain_new(&chain, bufs->chunk_length); + rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem); if (rv != 0) { return rv; } @@ -407,7 +413,7 @@ ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) { if (!len) { res = NULL; } else { - res = malloc(len); + res = nghttp2_mem_malloc(bufs->mem, len); if (res == NULL) { return NGHTTP2_ERR_NOMEM; @@ -456,7 +462,7 @@ void nghttp2_bufs_reset(nghttp2_bufs *bufs) { for (ci = chain; ci;) { chain = ci->next; - buf_chain_del(ci); + buf_chain_del(ci, bufs->mem); ci = chain; } diff --git a/lib/nghttp2_buf.h b/lib/nghttp2_buf.h index 2fc3acbe..c0455181 100644 --- a/lib/nghttp2_buf.h +++ b/lib/nghttp2_buf.h @@ -32,6 +32,7 @@ #include #include "nghttp2_int.h" +#include "nghttp2_mem.h" typedef struct { /* This points to the beginning of the buffer. The effective range @@ -86,12 +87,12 @@ void nghttp2_buf_init(nghttp2_buf *buf); * NGHTTP2_ERR_NOMEM * Out of memory */ -int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial); +int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem); /* * Frees buffer in |buf|. */ -void nghttp2_buf_free(nghttp2_buf *buf); +void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem); /* * Extends buffer so that nghttp2_buf_cap() returns at least @@ -104,7 +105,7 @@ void nghttp2_buf_free(nghttp2_buf *buf); * NGHTTP2_ERR_NOMEM * Out of memory */ -int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap); +int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem); /* * Resets pos, last, mark member of |buf| to buf->begin. @@ -135,6 +136,8 @@ typedef struct { nghttp2_buf_chain *head; /* Buffer pointer where write occurs. */ nghttp2_buf_chain *cur; + /* Memory allocator */ + nghttp2_mem *mem; /* The buffer capacity of each buf */ size_t chunk_length; /* The maximum number of nghttp2_buf_chain */ @@ -153,15 +156,15 @@ typedef struct { * This is the same as calling nghttp2_bufs_init2 with the given * arguments and offset = 0. */ -int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, - size_t max_chunk); +int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk, + nghttp2_mem *mem); /* * This is the same as calling nghttp2_bufs_init3 with the given * arguments and chunk_keep = max_chunk. */ int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length, - size_t max_chunk, size_t offset); + size_t max_chunk, size_t offset, nghttp2_mem *mem); /* * Initializes |bufs|. Each buffer size is given in the @@ -183,7 +186,8 @@ int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length, * long. */ int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length, - size_t max_chunk, size_t chunk_keep, size_t offset); + size_t max_chunk, size_t chunk_keep, size_t offset, + nghttp2_mem *mem); /* * Frees any related resources to the |bufs|. @@ -203,7 +207,8 @@ void nghttp2_bufs_free(nghttp2_bufs *bufs); * NGHTTP2_ERR_NOMEM * Out of memory. */ -int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len); +int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len, + nghttp2_mem *mem); /* * Frees any related resource to the |bufs|. This function does not diff --git a/lib/nghttp2_frame.c b/lib/nghttp2_frame.c index 2eb2ea06..d97194b2 100644 --- a/lib/nghttp2_frame.c +++ b/lib/nghttp2_frame.c @@ -75,8 +75,8 @@ void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags, } } -void nghttp2_frame_headers_free(nghttp2_headers *frame) { - nghttp2_nv_array_del(frame->nva); +void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) { + nghttp2_nv_array_del(frame->nva, mem); } void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id, @@ -105,7 +105,9 @@ void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags, frame->iv = iv; } -void nghttp2_frame_settings_free(nghttp2_settings *frame) { free(frame->iv); } +void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) { + nghttp2_mem_free(mem, frame->iv); +} void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags, int32_t stream_id, @@ -119,8 +121,9 @@ void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags, frame->reserved = 0; } -void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame) { - nghttp2_nv_array_del(frame->nva); +void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame, + nghttp2_mem *mem) { + nghttp2_nv_array_del(frame->nva, mem); } void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags, @@ -147,8 +150,8 @@ void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id, frame->reserved = 0; } -void nghttp2_frame_goaway_free(nghttp2_goaway *frame) { - free(frame->opaque_data); +void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) { + nghttp2_mem_free(mem, frame->opaque_data); } void nghttp2_frame_window_update_init(nghttp2_window_update *frame, @@ -422,13 +425,13 @@ size_t nghttp2_frame_pack_settings_payload(uint8_t *buf, int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame, nghttp2_settings_entry *iv, - size_t niv) { + size_t niv, nghttp2_mem *mem) { size_t payloadlen = niv * sizeof(nghttp2_settings_entry); if (niv == 0) { frame->iv = NULL; } else { - frame->iv = malloc(payloadlen); + frame->iv = nghttp2_mem_malloc(mem, payloadlen); if (frame->iv == NULL) { return NGHTTP2_ERR_NOMEM; @@ -450,7 +453,8 @@ void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv, int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr, size_t *niv_ptr, const uint8_t *payload, - size_t payloadlen) { + size_t payloadlen, + nghttp2_mem *mem) { size_t i; *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH; @@ -461,7 +465,8 @@ int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr, return 0; } - *iv_ptr = malloc((*niv_ptr) * sizeof(nghttp2_settings_entry)); + *iv_ptr = + nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry)); if (*iv_ptr == NULL) { return NGHTTP2_ERR_NOMEM; @@ -590,7 +595,7 @@ void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame, int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame, const uint8_t *payload, - size_t payloadlen) { + size_t payloadlen, nghttp2_mem *mem) { uint8_t *var_gift_payload; size_t var_gift_payloadlen; @@ -605,7 +610,7 @@ int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame, if (!var_gift_payloadlen) { var_gift_payload = NULL; } else { - var_gift_payload = malloc(var_gift_payloadlen); + var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen); if (var_gift_payload == NULL) { return NGHTTP2_ERR_NOMEM; @@ -648,7 +653,7 @@ void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame, } nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv, - size_t niv) { + size_t niv, nghttp2_mem *mem) { nghttp2_settings_entry *iv_copy; size_t len = niv * sizeof(nghttp2_settings_entry); @@ -656,7 +661,7 @@ nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv, return NULL; } - iv_copy = malloc(len); + iv_copy = nghttp2_mem_malloc(mem, len); if (iv_copy == NULL) { return NULL; @@ -673,7 +678,9 @@ int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) { memcmp(a->value, b->value, a->valuelen) == 0; } -void nghttp2_nv_array_del(nghttp2_nv *nva) { free(nva); } +void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) { + nghttp2_mem_free(mem, nva); +} static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen) { @@ -725,7 +732,7 @@ void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) { } int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva, - size_t nvlen) { + size_t nvlen, nghttp2_mem *mem) { size_t i; uint8_t *data; size_t buflen = 0; @@ -743,7 +750,7 @@ int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva, buflen += sizeof(nghttp2_nv) * nvlen; - *nva_ptr = malloc(buflen); + *nva_ptr = nghttp2_mem_malloc(mem, buflen); if (*nva_ptr == NULL) { return NGHTTP2_ERR_NOMEM; diff --git a/lib/nghttp2_frame.h b/lib/nghttp2_frame.h index 80c28c1e..4304b199 100644 --- a/lib/nghttp2_frame.h +++ b/lib/nghttp2_frame.h @@ -231,7 +231,7 @@ void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv, */ int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame, nghttp2_settings_entry *iv, - size_t niv); + size_t niv, nghttp2_mem *mem); /* * Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are @@ -248,7 +248,7 @@ int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame, int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr, size_t *niv_ptr, const uint8_t *payload, - size_t payloadlen); + size_t payloadlen, nghttp2_mem *mem); /* * Packs PUSH_PROMISE frame |frame| in wire format and store it in @@ -351,7 +351,7 @@ void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame, */ int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame, const uint8_t *payload, - size_t payloadlen); + size_t payloadlen, nghttp2_mem *mem); /* * Packs WINDOW_UPDATE frame |frame| in wire frame format and store it @@ -382,7 +382,7 @@ void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags, const nghttp2_priority_spec *pri_spec, nghttp2_nv *nva, size_t nvlen); -void nghttp2_frame_headers_free(nghttp2_headers *frame); +void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem); void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id, const nghttp2_priority_spec *pri_spec); @@ -403,7 +403,8 @@ void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags, int32_t promised_stream_id, nghttp2_nv *nva, size_t nvlen); -void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame); +void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame, + nghttp2_mem *mem); /* * Initializes SETTINGS frame |frame| with given values. |frame| takes @@ -413,7 +414,7 @@ void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame); void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags, nghttp2_settings_entry *iv, size_t niv); -void nghttp2_frame_settings_free(nghttp2_settings *frame); +void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem); /* * Initializes PING frame |frame| with given values. If the @@ -435,7 +436,7 @@ void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id, uint32_t error_code, uint8_t *opaque_data, size_t opaque_data_len); -void nghttp2_frame_goaway_free(nghttp2_goaway *frame); +void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem); void nghttp2_frame_window_update_init(nghttp2_window_update *frame, uint8_t flags, int32_t stream_id, @@ -461,7 +462,7 @@ void nghttp2_frame_data_free(nghttp2_data *frame); * it succeeds, or NULL. */ nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv, - size_t niv); + size_t niv, nghttp2_mem *mem); /* * Sorts the |nva| in ascending order of name and value. If names are @@ -483,7 +484,7 @@ void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen); * Out of memory. */ int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva, - size_t nvlen); + size_t nvlen, nghttp2_mem *mem); /* * Returns nonzero if the name/value pair |a| equals to |b|. The name @@ -495,7 +496,7 @@ int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b); /* * Frees |nva|. */ -void nghttp2_nv_array_del(nghttp2_nv *nva); +void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem); /* * Checks that the |iv|, which includes |niv| entries, does not have diff --git a/lib/nghttp2_hd.c b/lib/nghttp2_hd.c index c4807fa3..a7c6ab74 100644 --- a/lib/nghttp2_hd.c +++ b/lib/nghttp2_hd.c @@ -141,7 +141,8 @@ static uint32_t hash(const uint8_t *s, size_t n) { int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name, size_t namelen, uint8_t *value, size_t valuelen, - uint32_t name_hash, uint32_t value_hash) { + uint32_t name_hash, uint32_t value_hash, + nghttp2_mem *mem) { int rv = 0; /* Since nghttp2_hd_entry is used for indexing, ent->nv.flags always @@ -154,7 +155,7 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name, /* We should not allow empty header field name */ ent->nv.name = NULL; } else { - ent->nv.name = nghttp2_memdup(name, namelen); + ent->nv.name = nghttp2_memdup(name, namelen, mem); if (ent->nv.name == NULL) { rv = NGHTTP2_ERR_NOMEM; goto fail; @@ -168,7 +169,7 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name, if (valuelen == 0) { ent->nv.value = NULL; } else { - ent->nv.value = nghttp2_memdup(value, valuelen); + ent->nv.value = nghttp2_memdup(value, valuelen, mem); if (ent->nv.value == NULL) { rv = NGHTTP2_ERR_NOMEM; goto fail2; @@ -189,27 +190,28 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name, fail2: if (flags & NGHTTP2_HD_FLAG_NAME_ALLOC) { - free(ent->nv.name); + nghttp2_mem_free(mem, ent->nv.name); } fail: return rv; } -void nghttp2_hd_entry_free(nghttp2_hd_entry *ent) { +void nghttp2_hd_entry_free(nghttp2_hd_entry *ent, nghttp2_mem *mem) { assert(ent->ref == 0); if (ent->flags & NGHTTP2_HD_FLAG_NAME_ALLOC) { - free(ent->nv.name); + nghttp2_mem_free(mem, ent->nv.name); } if (ent->flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) { - free(ent->nv.value); + nghttp2_mem_free(mem, ent->nv.value); } } -static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize) { +static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize, + nghttp2_mem *mem) { size_t size; for (size = 1; size < bufsize; size <<= 1) ; - ringbuf->buffer = malloc(sizeof(nghttp2_hd_entry *) * size); + ringbuf->buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size); if (ringbuf->buffer == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -225,7 +227,8 @@ static nghttp2_hd_entry *hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf, return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask]; } -static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize) { +static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize, + nghttp2_mem *mem) { size_t i; size_t size; nghttp2_hd_entry **buffer; @@ -235,21 +238,21 @@ static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize) { } for (size = 1; size < bufsize; size <<= 1) ; - buffer = malloc(sizeof(nghttp2_hd_entry *) * size); + buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size); if (buffer == NULL) { return NGHTTP2_ERR_NOMEM; } for (i = 0; i < ringbuf->len; ++i) { buffer[i] = hd_ringbuf_get(ringbuf, i); } - free(ringbuf->buffer); + nghttp2_mem_free(mem, ringbuf->buffer); ringbuf->buffer = buffer; ringbuf->mask = size - 1; ringbuf->first = 0; return 0; } -static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf) { +static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf, nghttp2_mem *mem) { size_t i; if (ringbuf == NULL) { return; @@ -257,17 +260,17 @@ static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf) { for (i = 0; i < ringbuf->len; ++i) { nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i); --ent->ref; - nghttp2_hd_entry_free(ent); - free(ent); + nghttp2_hd_entry_free(ent, mem); + nghttp2_mem_free(mem, ent); } - free(ringbuf->buffer); + nghttp2_mem_free(mem, ringbuf->buffer); } static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf, - nghttp2_hd_entry *ent) { + nghttp2_hd_entry *ent, nghttp2_mem *mem) { int rv; - rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1); + rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1, mem); if (rv != 0) { return rv; @@ -284,12 +287,14 @@ static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf) { --ringbuf->len; } -static int hd_context_init(nghttp2_hd_context *context) { +static int hd_context_init(nghttp2_hd_context *context, nghttp2_mem *mem) { int rv; + context->mem = mem; context->bad = 0; context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE; rv = hd_ringbuf_init(&context->hd_table, context->hd_table_bufsize_max / - NGHTTP2_HD_ENTRY_OVERHEAD); + NGHTTP2_HD_ENTRY_OVERHEAD, + mem); if (rv != 0) { return rv; } @@ -299,18 +304,19 @@ static int hd_context_init(nghttp2_hd_context *context) { } static void hd_context_free(nghttp2_hd_context *context) { - hd_ringbuf_free(&context->hd_table); + hd_ringbuf_free(&context->hd_table, context->mem); } -int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater) { - return nghttp2_hd_deflate_init2(deflater, - NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE); +int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem) { + return nghttp2_hd_deflate_init2( + deflater, NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE, mem); } int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater, - size_t deflate_hd_table_bufsize_max) { + size_t deflate_hd_table_bufsize_max, + nghttp2_mem *mem) { int rv; - rv = hd_context_init(&deflater->ctx); + rv = hd_context_init(&deflater->ctx, mem); if (rv != 0) { return rv; } @@ -328,10 +334,10 @@ int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater, return 0; } -int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater) { +int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem) { int rv; - rv = hd_context_init(&inflater->ctx); + rv = hd_context_init(&inflater->ctx, mem); if (rv != 0) { goto fail; } @@ -344,7 +350,8 @@ int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater) { inflater->opcode = NGHTTP2_HD_OPCODE_NONE; inflater->state = NGHTTP2_HD_STATE_OPCODE; - rv = nghttp2_bufs_init3(&inflater->nvbufs, NGHTTP2_HD_MAX_NV / 8, 8, 1, 0); + rv = nghttp2_bufs_init3(&inflater->nvbufs, NGHTTP2_HD_MAX_NV / 8, 8, 1, 0, + mem); if (rv != 0) { goto nvbufs_fail; @@ -367,15 +374,18 @@ fail: } static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater) { + nghttp2_mem *mem; + + mem = inflater->ctx.mem; if (inflater->ent_keep) { if (inflater->ent_keep->ref == 0) { - nghttp2_hd_entry_free(inflater->ent_keep); - free(inflater->ent_keep); + nghttp2_hd_entry_free(inflater->ent_keep, mem); + nghttp2_mem_free(mem, inflater->ent_keep); } inflater->ent_keep = NULL; } - free(inflater->nv_keep); + nghttp2_mem_free(mem, inflater->nv_keep); inflater->nv_keep = NULL; } @@ -727,7 +737,9 @@ static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context, int rv; nghttp2_hd_entry *new_ent; size_t room; + nghttp2_mem *mem; + mem = context->mem; room = entry_room(nv->namelen, nv->valuelen); while (context->hd_table_bufsize + room > context->hd_table_bufsize_max && @@ -745,20 +757,21 @@ static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context, DEBUGF(fprintf(stderr, "\n")); hd_ringbuf_pop_back(&context->hd_table); if (--ent->ref == 0) { - nghttp2_hd_entry_free(ent); - free(ent); + nghttp2_hd_entry_free(ent, mem); + nghttp2_mem_free(mem, ent); } } - new_ent = malloc(sizeof(nghttp2_hd_entry)); + new_ent = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry)); if (new_ent == NULL) { return NULL; } rv = nghttp2_hd_entry_init(new_ent, entry_flags, nv->name, nv->namelen, - nv->value, nv->valuelen, name_hash, value_hash); + nv->value, nv->valuelen, name_hash, value_hash, + mem); if (rv != 0) { - free(new_ent); + nghttp2_mem_free(mem, new_ent); return NULL; } @@ -767,7 +780,7 @@ static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context, immediately evicted. */ --new_ent->ref; } else { - rv = hd_ringbuf_push_front(&context->hd_table, new_ent); + rv = hd_ringbuf_push_front(&context->hd_table, new_ent, mem); if (rv != 0) { --new_ent->ref; @@ -778,8 +791,8 @@ static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context, new_ent->nv.value = NULL; new_ent->nv.valuelen = 0; - nghttp2_hd_entry_free(new_ent); - free(new_ent); + nghttp2_hd_entry_free(new_ent, mem); + nghttp2_mem_free(mem, new_ent); return NULL; } @@ -865,6 +878,10 @@ static search_result search_hd_table(nghttp2_hd_context *context, } static void hd_context_shrink_table_size(nghttp2_hd_context *context) { + nghttp2_mem *mem; + + mem = context->mem; + while (context->hd_table_bufsize > context->hd_table_bufsize_max && context->hd_table.len > 0) { size_t idx = context->hd_table.len - 1; @@ -872,8 +889,8 @@ static void hd_context_shrink_table_size(nghttp2_hd_context *context) { context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen); hd_ringbuf_pop_back(&context->hd_table); if (--ent->ref == 0) { - nghttp2_hd_entry_free(ent); - free(ent); + nghttp2_hd_entry_free(ent, mem); + nghttp2_mem_free(mem, ent); } } } @@ -949,6 +966,7 @@ static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs, int incidx = 0; uint32_t name_hash = hash(nv->name, nv->namelen); uint32_t value_hash = hash(nv->value, nv->valuelen); + nghttp2_mem *mem; DEBUGF(fprintf(stderr, "deflatehd: deflating ")); DEBUGF(fwrite(nv->name, nv->namelen, 1, stderr)); @@ -956,6 +974,8 @@ static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs, DEBUGF(fwrite(nv->value, nv->valuelen, 1, stderr)); DEBUGF(fprintf(stderr, "\n")); + mem = deflater->ctx.mem; + res = search_hd_table(&deflater->ctx, nv, name_hash, value_hash); idx = res.index; @@ -994,8 +1014,8 @@ static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs, return NGHTTP2_ERR_HEADER_COMP; } if (new_ent->ref == 0) { - nghttp2_hd_entry_free(new_ent); - free(new_ent); + nghttp2_hd_entry_free(new_ent, mem); + nghttp2_mem_free(mem, new_ent); } incidx = 1; } @@ -1068,8 +1088,11 @@ ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf, size_t nvlen) { nghttp2_bufs bufs; int rv; + nghttp2_mem *mem; - rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen); + mem = deflater->ctx.mem; + + rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen, mem); if (rv != 0) { return rv; @@ -1120,19 +1143,30 @@ size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater _U_, int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr, size_t deflate_hd_table_bufsize_max) { + return nghttp2_hd_deflate_new2(deflater_ptr, deflate_hd_table_bufsize_max, + NULL); +} + +int nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr, + size_t deflate_hd_table_bufsize_max, + nghttp2_mem *mem) { int rv; nghttp2_hd_deflater *deflater; - deflater = malloc(sizeof(nghttp2_hd_deflater)); + if (mem == NULL) { + mem = nghttp2_mem_default(); + } + + deflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_deflater)); if (deflater == NULL) { return NGHTTP2_ERR_NOMEM; } - rv = nghttp2_hd_deflate_init2(deflater, deflate_hd_table_bufsize_max); + rv = nghttp2_hd_deflate_init2(deflater, deflate_hd_table_bufsize_max, mem); if (rv != 0) { - free(deflater); + nghttp2_mem_free(mem, deflater); return rv; } @@ -1143,9 +1177,13 @@ int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr, } void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater) { + nghttp2_mem *mem; + + mem = deflater->ctx.mem; + nghttp2_hd_deflate_free(deflater); - free(deflater); + nghttp2_mem_free(mem, deflater); } static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater, @@ -1346,6 +1384,9 @@ static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater, nghttp2_nv *nv_out) { int rv; nghttp2_nv nv; + nghttp2_mem *mem; + + mem = inflater->ctx.mem; rv = hd_inflate_remove_bufs(inflater, &nv, 0 /* name and value */); if (rv != 0) { @@ -1378,7 +1419,7 @@ static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater, return 0; } - free(nv.name); + nghttp2_mem_free(mem, nv.name); return NGHTTP2_ERR_NOMEM; } @@ -1408,6 +1449,9 @@ static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater, int rv; nghttp2_nv nv; nghttp2_hd_entry *ent_name; + nghttp2_mem *mem; + + mem = inflater->ctx.mem; rv = hd_inflate_remove_bufs(inflater, &nv, 1 /* value only */); if (rv != 0) { @@ -1444,8 +1488,8 @@ static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater, hash(nv.value, nv.valuelen), ent_flags); if (!static_name && --ent_name->ref == 0) { - nghttp2_hd_entry_free(ent_name); - free(ent_name); + nghttp2_hd_entry_free(ent_name, mem); + nghttp2_mem_free(mem, ent_name); } if (new_ent) { @@ -1456,7 +1500,7 @@ static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater, return 0; } - free(nv.value); + nghttp2_mem_free(mem, nv.value); return NGHTTP2_ERR_NOMEM; } @@ -1812,19 +1856,28 @@ int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) { } int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr) { + return nghttp2_hd_inflate_new2(inflater_ptr, NULL); +} + +int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr, + nghttp2_mem *mem) { int rv; nghttp2_hd_inflater *inflater; - inflater = malloc(sizeof(nghttp2_hd_inflater)); + if (mem == NULL) { + mem = nghttp2_mem_default(); + } + + inflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_inflater)); if (inflater == NULL) { return NGHTTP2_ERR_NOMEM; } - rv = nghttp2_hd_inflate_init(inflater); + rv = nghttp2_hd_inflate_init(inflater, mem); if (rv != 0) { - free(inflater); + nghttp2_mem_free(mem, inflater); return rv; } @@ -1835,9 +1888,12 @@ int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr) { } void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater) { + nghttp2_mem *mem; + + mem = inflater->ctx.mem; nghttp2_hd_inflate_free(inflater); - free(inflater); + nghttp2_mem_free(mem, inflater); } int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx, diff --git a/lib/nghttp2_hd.h b/lib/nghttp2_hd.h index c88ac931..178abe36 100644 --- a/lib/nghttp2_hd.h +++ b/lib/nghttp2_hd.h @@ -33,6 +33,7 @@ #include "nghttp2_hd_huffman.h" #include "nghttp2_buf.h" +#include "nghttp2_mem.h" #define NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE NGHTTP2_DEFAULT_HEADER_TABLE_SIZE #define NGHTTP2_HD_ENTRY_OVERHEAD 32 @@ -109,6 +110,8 @@ typedef enum { typedef struct { /* dynamic header table */ nghttp2_hd_ringbuf hd_table; + /* Memory allocator */ + nghttp2_mem *mem; /* Abstract buffer size of hd_table as described in the spec. This is the sum of length of name/value in hd_table + NGHTTP2_HD_ENTRY_OVERHEAD bytes overhead per each entry. */ @@ -185,9 +188,10 @@ struct nghttp2_hd_inflater { */ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name, size_t namelen, uint8_t *value, size_t valuelen, - uint32_t name_hash, uint32_t value_hash); + uint32_t name_hash, uint32_t value_hash, + nghttp2_mem *mem); -void nghttp2_hd_entry_free(nghttp2_hd_entry *ent); +void nghttp2_hd_entry_free(nghttp2_hd_entry *ent, nghttp2_mem *mem); /* * Initializes |deflater| for deflating name/values pairs. @@ -203,7 +207,7 @@ void nghttp2_hd_entry_free(nghttp2_hd_entry *ent); * NGHTTP2_ERR_NOMEM * Out of memory. */ -int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater); +int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem); /* * Initializes |deflater| for deflating name/values pairs. @@ -219,7 +223,8 @@ int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater); * Out of memory. */ int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater, - size_t deflate_hd_table_bufsize_max); + size_t deflate_hd_table_bufsize_max, + nghttp2_mem *mem); /* * Deallocates any resources allocated for |deflater|. @@ -259,7 +264,7 @@ int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater, * :enum:`NGHTTP2_ERR_NOMEM` * Out of memory. */ -int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater); +int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem); /* * Deallocates any resources allocated for |inflater|. diff --git a/lib/nghttp2_helper.c b/lib/nghttp2_helper.c index 038001e0..1be8d0dc 100644 --- a/lib/nghttp2_helper.c +++ b/lib/nghttp2_helper.c @@ -51,14 +51,14 @@ uint32_t nghttp2_get_uint32(const uint8_t *data) { return ntohl(n); } -void *nghttp2_memdup(const void *src, size_t n) { +void *nghttp2_memdup(const void *src, size_t n, nghttp2_mem *mem) { void *dest; if (n == 0) { return NULL; } - dest = malloc(n); + dest = nghttp2_mem_malloc(mem, n); if (dest == NULL) { return NULL; } @@ -304,8 +304,6 @@ const char *nghttp2_strerror(int error_code) { } } -void nghttp2_free(void *ptr) { free(ptr); } - /* Generated by gennmchartbl.py */ static int VALID_HD_NAME_CHARS[] = { 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */, 0 /* EOT */, diff --git a/lib/nghttp2_helper.h b/lib/nghttp2_helper.h index 049d7365..54422a8f 100644 --- a/lib/nghttp2_helper.h +++ b/lib/nghttp2_helper.h @@ -30,6 +30,7 @@ #endif /* HAVE_CONFIG_H */ #include +#include "nghttp2_mem.h" #define nghttp2_min(A, B) ((A) < (B) ? (A) : (B)) #define nghttp2_max(A, B) ((A) > (B) ? (A) : (B)) @@ -68,7 +69,7 @@ uint32_t nghttp2_get_uint32(const uint8_t *data); * NGHTTP2_ERR_NOMEM * Out of memory. */ -void *nghttp2_memdup(const void *src, size_t n); +void *nghttp2_memdup(const void *src, size_t n, nghttp2_mem *mem); void nghttp2_downcase(uint8_t *s, size_t len); @@ -99,14 +100,6 @@ int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr, int nghttp2_should_send_window_update(int32_t local_window_size, int32_t recv_window_size); -/* - * Deallocates memory space pointed by |ptr|. This function exists for - * the application to free the memory space allocated by the library - * functions. Currently this function is hidden from the public API, - * but may be exposed as public API. - */ -void nghttp2_free(void *ptr); - /* * Copies the buffer |src| of length |len| to the destination pointed * by the |dest|, assuming that the |dest| is at lest |len| bytes long diff --git a/lib/nghttp2_map.c b/lib/nghttp2_map.c index ace2e089..5a38ee2c 100644 --- a/lib/nghttp2_map.c +++ b/lib/nghttp2_map.c @@ -28,9 +28,11 @@ #define INITIAL_TABLE_LENGTH 256 -int nghttp2_map_init(nghttp2_map *map) { +int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) { + map->mem = mem; map->tablelen = INITIAL_TABLE_LENGTH; - map->table = calloc(map->tablelen, sizeof(nghttp2_map_entry *)); + map->table = + nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_entry *)); if (map->table == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -40,7 +42,9 @@ int nghttp2_map_init(nghttp2_map *map) { return 0; } -void nghttp2_map_free(nghttp2_map *map) { free(map->table); } +void nghttp2_map_free(nghttp2_map *map) { + nghttp2_mem_free(map->mem, map->table); +} void nghttp2_map_each_free(nghttp2_map *map, int (*func)(nghttp2_map_entry *entry, void *ptr), @@ -110,7 +114,9 @@ static int insert(nghttp2_map_entry **table, size_t tablelen, static int resize(nghttp2_map *map, size_t new_tablelen) { size_t i; nghttp2_map_entry **new_table; - new_table = calloc(new_tablelen, sizeof(nghttp2_map_entry *)); + + new_table = + nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_entry *)); if (new_table == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -125,7 +131,7 @@ static int resize(nghttp2_map *map, size_t new_tablelen) { entry = next; } } - free(map->table); + nghttp2_mem_free(map->mem, map->table); map->tablelen = new_tablelen; map->table = new_table; diff --git a/lib/nghttp2_map.h b/lib/nghttp2_map.h index 4639602c..87744272 100644 --- a/lib/nghttp2_map.h +++ b/lib/nghttp2_map.h @@ -31,6 +31,7 @@ #include #include "nghttp2_int.h" +#include "nghttp2_mem.h" /* Implementation of unordered map */ @@ -43,6 +44,7 @@ typedef struct nghttp2_map_entry { typedef struct { nghttp2_map_entry **table; + nghttp2_mem *mem; size_t tablelen; size_t size; } nghttp2_map; @@ -56,7 +58,7 @@ typedef struct { * NGHTTP2_ERR_NOMEM * Out of memory */ -int nghttp2_map_init(nghttp2_map *map); +int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem); /* * Deallocates any resources allocated for |map|. The stored entries diff --git a/lib/nghttp2_mem.c b/lib/nghttp2_mem.c new file mode 100644 index 00000000..e554a200 --- /dev/null +++ b/lib/nghttp2_mem.c @@ -0,0 +1,61 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2014 Tatsuhiro Tsujikawa + * + * 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. + */ +#include "nghttp2_mem.h" + +static void *default_malloc(size_t size, void *mem_user_data _U_) { + return malloc(size); +} + +static void default_free(void *ptr, void *mem_user_data _U_) { free(ptr); } + +static void *default_calloc(size_t nmemb, size_t size, + void *mem_user_data _U_) { + return calloc(nmemb, size); +} + +static void *default_realloc(void *ptr, size_t size, void *mem_user_data _U_) { + return realloc(ptr, size); +} + +static nghttp2_mem mem_default = {NULL, default_malloc, default_free, + default_calloc, default_realloc}; + +nghttp2_mem *nghttp2_mem_default(void) { return &mem_default; } + +void *nghttp2_mem_malloc(nghttp2_mem *mem, size_t size) { + return mem->malloc(size, mem->mem_user_data); +} + +void nghttp2_mem_free(nghttp2_mem *mem, void *ptr) { + return mem->free(ptr, mem->mem_user_data); +} + +void *nghttp2_mem_calloc(nghttp2_mem *mem, size_t nmemb, size_t size) { + return mem->calloc(nmemb, size, mem->mem_user_data); +} + +void *nghttp2_mem_realloc(nghttp2_mem *mem, void *ptr, size_t size) { + return mem->realloc(ptr, size, mem->mem_user_data); +} diff --git a/lib/nghttp2_mem.h b/lib/nghttp2_mem.h new file mode 100644 index 00000000..d1fded4f --- /dev/null +++ b/lib/nghttp2_mem.h @@ -0,0 +1,44 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2014 Tatsuhiro Tsujikawa + * + * 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 NGHTTP2_MEM_H +#define NGHTTP2_MEM_H + +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include + +/* The default, system standard memory allocator */ +nghttp2_mem *nghttp2_mem_default(void); + +/* Convenient wrapper functions to call allocator function in + |mem|. */ +void *nghttp2_mem_malloc(nghttp2_mem *mem, size_t size); +void nghttp2_mem_free(nghttp2_mem *mem, void *ptr); +void *nghttp2_mem_calloc(nghttp2_mem *mem, size_t nmemb, size_t size); +void *nghttp2_mem_realloc(nghttp2_mem *mem, void *ptr, size_t size); + +#endif /* NGHTTP2_MEM_H */ diff --git a/lib/nghttp2_outbound_item.c b/lib/nghttp2_outbound_item.c index ea7c1942..2aaff377 100644 --- a/lib/nghttp2_outbound_item.c +++ b/lib/nghttp2_outbound_item.c @@ -26,7 +26,7 @@ #include -void nghttp2_outbound_item_free(nghttp2_outbound_item *item) { +void nghttp2_outbound_item_free(nghttp2_outbound_item *item, nghttp2_mem *mem) { nghttp2_frame *frame; if (item == NULL) { @@ -40,7 +40,7 @@ void nghttp2_outbound_item_free(nghttp2_outbound_item *item) { nghttp2_frame_data_free(&frame->data); break; case NGHTTP2_HEADERS: - nghttp2_frame_headers_free(&frame->headers); + nghttp2_frame_headers_free(&frame->headers, mem); break; case NGHTTP2_PRIORITY: nghttp2_frame_priority_free(&frame->priority); @@ -49,16 +49,16 @@ void nghttp2_outbound_item_free(nghttp2_outbound_item *item) { nghttp2_frame_rst_stream_free(&frame->rst_stream); break; case NGHTTP2_SETTINGS: - nghttp2_frame_settings_free(&frame->settings); + nghttp2_frame_settings_free(&frame->settings, mem); break; case NGHTTP2_PUSH_PROMISE: - nghttp2_frame_push_promise_free(&frame->push_promise); + nghttp2_frame_push_promise_free(&frame->push_promise, mem); break; case NGHTTP2_PING: nghttp2_frame_ping_free(&frame->ping); break; case NGHTTP2_GOAWAY: - nghttp2_frame_goaway_free(&frame->goaway); + nghttp2_frame_goaway_free(&frame->goaway, mem); break; case NGHTTP2_WINDOW_UPDATE: nghttp2_frame_window_update_free(&frame->window_update); diff --git a/lib/nghttp2_outbound_item.h b/lib/nghttp2_outbound_item.h index aff98cac..4dda2dbe 100644 --- a/lib/nghttp2_outbound_item.h +++ b/lib/nghttp2_outbound_item.h @@ -31,6 +31,7 @@ #include #include "nghttp2_frame.h" +#include "nghttp2_mem.h" /* A bit higher weight for non-DATA frames */ #define NGHTTP2_OB_EX_WEIGHT 300 @@ -101,6 +102,6 @@ typedef struct { * Deallocates resource for |item|. If |item| is NULL, this function * does nothing. */ -void nghttp2_outbound_item_free(nghttp2_outbound_item *item); +void nghttp2_outbound_item_free(nghttp2_outbound_item *item, nghttp2_mem *mem); #endif /* NGHTTP2_OUTBOUND_ITEM_H */ diff --git a/lib/nghttp2_pq.c b/lib/nghttp2_pq.c index 5ba362b5..4ad37c08 100644 --- a/lib/nghttp2_pq.c +++ b/lib/nghttp2_pq.c @@ -24,9 +24,10 @@ */ #include "nghttp2_pq.h" -int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_compar compar) { +int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_compar compar, nghttp2_mem *mem) { + pq->mem = mem; pq->capacity = 128; - pq->q = malloc(pq->capacity * sizeof(void *)); + pq->q = nghttp2_mem_malloc(mem, pq->capacity * sizeof(void *)); if (pq->q == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -36,7 +37,7 @@ int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_compar compar) { } void nghttp2_pq_free(nghttp2_pq *pq) { - free(pq->q); + nghttp2_mem_free(pq->mem, pq->q); pq->q = NULL; } @@ -61,7 +62,8 @@ static void bubble_up(nghttp2_pq *pq, size_t index) { int nghttp2_pq_push(nghttp2_pq *pq, void *item) { if (pq->capacity <= pq->length) { void *nq; - nq = realloc(pq->q, (pq->capacity * 2) * sizeof(void *)); + nq = nghttp2_mem_realloc(pq->mem, pq->q, + (pq->capacity * 2) * sizeof(void *)); if (nq == NULL) { return NGHTTP2_ERR_NOMEM; } diff --git a/lib/nghttp2_pq.h b/lib/nghttp2_pq.h index c22fa7fd..e23dd496 100644 --- a/lib/nghttp2_pq.h +++ b/lib/nghttp2_pq.h @@ -31,12 +31,15 @@ #include #include "nghttp2_int.h" +#include "nghttp2_mem.h" /* Implementation of priority queue */ typedef struct { /* The pointer to the pointer to the item stored */ void **q; + /* Memory allocator */ + nghttp2_mem *mem; /* The number of items sotred */ size_t length; /* The maximum number of items this pq can store. This is @@ -55,7 +58,7 @@ typedef struct { * NGHTTP2_ERR_NOMEM * Out of memory. */ -int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_compar cmp); +int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_compar cmp, nghttp2_mem *mem); /* * Deallocates any resources allocated for |pq|. The stored items are diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c index ab8f608b..e482e8c7 100644 --- a/lib/nghttp2_session.c +++ b/lib/nghttp2_session.c @@ -218,12 +218,13 @@ static int outbound_item_compar(const void *lhsx, const void *rhsx) { static void session_inbound_frame_reset(nghttp2_session *session) { nghttp2_inbound_frame *iframe = &session->iframe; + nghttp2_mem *mem = &session->mem; /* A bit risky code, since if this function is called from nghttp2_session_new(), we rely on the fact that iframe->frame.hd.type is 0, so that no free is performed. */ switch (iframe->frame.hd.type) { case NGHTTP2_HEADERS: - nghttp2_frame_headers_free(&iframe->frame.headers); + nghttp2_frame_headers_free(&iframe->frame.headers, mem); break; case NGHTTP2_PRIORITY: nghttp2_frame_priority_free(&iframe->frame.priority); @@ -232,16 +233,16 @@ static void session_inbound_frame_reset(nghttp2_session *session) { nghttp2_frame_rst_stream_free(&iframe->frame.rst_stream); break; case NGHTTP2_SETTINGS: - nghttp2_frame_settings_free(&iframe->frame.settings); + nghttp2_frame_settings_free(&iframe->frame.settings, mem); break; case NGHTTP2_PUSH_PROMISE: - nghttp2_frame_push_promise_free(&iframe->frame.push_promise); + nghttp2_frame_push_promise_free(&iframe->frame.push_promise, mem); break; case NGHTTP2_PING: nghttp2_frame_ping_free(&iframe->frame.ping); break; case NGHTTP2_GOAWAY: - nghttp2_frame_goaway_free(&iframe->frame.goaway); + nghttp2_frame_goaway_free(&iframe->frame.goaway, mem); break; case NGHTTP2_WINDOW_UPDATE: nghttp2_frame_window_update_free(&iframe->frame.window_update); @@ -257,7 +258,7 @@ static void session_inbound_frame_reset(nghttp2_session *session) { sizeof(iframe->raw_sbuf)); iframe->sbuf.mark += NGHTTP2_FRAME_HDLEN; - nghttp2_buf_free(&iframe->lbuf); + nghttp2_buf_free(&iframe->lbuf, mem); nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0); iframe->niv = 0; @@ -277,11 +278,12 @@ static void init_settings(nghttp2_settings_storage *settings) { settings->max_header_list_size = UINT32_MAX; } -static void active_outbound_item_reset(nghttp2_active_outbound_item *aob) { +static void active_outbound_item_reset(nghttp2_active_outbound_item *aob, + nghttp2_mem *mem) { DEBUGF(fprintf(stderr, "send: reset nghttp2_active_outbound_item\n")); DEBUGF(fprintf(stderr, "send: aob->item = %p\n", aob->item)); - nghttp2_outbound_item_free(aob->item); - free(aob->item); + nghttp2_outbound_item_free(aob->item, mem); + nghttp2_mem_free(mem, aob->item); aob->item = NULL; nghttp2_bufs_reset(&aob->framebufs); aob->state = NGHTTP2_OB_POP_ITEM; @@ -294,40 +296,46 @@ int nghttp2_enable_strict_first_settings_check = 1; static int session_new(nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks, void *user_data, int server, - const nghttp2_option *option) { + const nghttp2_option *option, nghttp2_mem *mem) { int rv; - *session_ptr = calloc(1, sizeof(nghttp2_session)); + if (mem == NULL) { + mem = nghttp2_mem_default(); + } + + *session_ptr = nghttp2_mem_calloc(mem, 1, sizeof(nghttp2_session)); if (*session_ptr == NULL) { rv = NGHTTP2_ERR_NOMEM; goto fail_session; } + (*session_ptr)->mem = *mem; + /* next_stream_id is initialized in either nghttp2_session_client_new2 or nghttp2_session_server_new2 */ - rv = nghttp2_pq_init(&(*session_ptr)->ob_pq, outbound_item_compar); + rv = nghttp2_pq_init(&(*session_ptr)->ob_pq, outbound_item_compar, mem); if (rv != 0) { goto fail_ob_pq; } - rv = nghttp2_pq_init(&(*session_ptr)->ob_ss_pq, outbound_item_compar); + rv = nghttp2_pq_init(&(*session_ptr)->ob_ss_pq, outbound_item_compar, mem); if (rv != 0) { goto fail_ob_ss_pq; } - rv = nghttp2_pq_init(&(*session_ptr)->ob_da_pq, outbound_item_compar); + rv = nghttp2_pq_init(&(*session_ptr)->ob_da_pq, outbound_item_compar, mem); if (rv != 0) { goto fail_ob_da_pq; } - rv = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater); + rv = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater, mem); if (rv != 0) { goto fail_hd_deflater; } - rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater); + rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, mem); if (rv != 0) { goto fail_hd_inflater; } - rv = nghttp2_map_init(&(*session_ptr)->streams); + rv = nghttp2_map_init(&(*session_ptr)->streams, mem); if (rv != 0) { goto fail_map; } @@ -359,12 +367,12 @@ static int session_new(nghttp2_session **session_ptr, /* 1 for Pad Field. */ rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs, NGHTTP2_FRAMEBUF_CHUNKLEN, NGHTTP2_FRAMEBUF_MAX_NUM, - 1, NGHTTP2_FRAME_HDLEN + 1); + 1, NGHTTP2_FRAME_HDLEN + 1, mem); if (rv != 0) { goto fail_aob_framebuf; } - active_outbound_item_reset(&(*session_ptr)->aob); + active_outbound_item_reset(&(*session_ptr)->aob, mem); init_settings(&(*session_ptr)->remote_settings); init_settings(&(*session_ptr)->local_settings); @@ -421,7 +429,7 @@ fail_ob_da_pq: fail_ob_ss_pq: nghttp2_pq_free(&(*session_ptr)->ob_pq); fail_ob_pq: - free(*session_ptr); + nghttp2_mem_free(mem, *session_ptr); fail_session: return rv; } @@ -429,16 +437,25 @@ fail_session: int nghttp2_session_client_new(nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks, void *user_data) { - return nghttp2_session_client_new2(session_ptr, callbacks, user_data, NULL); + return nghttp2_session_client_new3(session_ptr, callbacks, user_data, NULL, + NULL); } int nghttp2_session_client_new2(nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks, void *user_data, const nghttp2_option *option) { + return nghttp2_session_client_new3(session_ptr, callbacks, user_data, option, + NULL); +} + +int nghttp2_session_client_new3(nghttp2_session **session_ptr, + const nghttp2_session_callbacks *callbacks, + void *user_data, const nghttp2_option *option, + nghttp2_mem *mem) { int rv; nghttp2_session *session; - rv = session_new(&session, callbacks, user_data, 0, option); + rv = session_new(&session, callbacks, user_data, 0, option, mem); if (rv != 0) { return rv; @@ -454,16 +471,25 @@ int nghttp2_session_client_new2(nghttp2_session **session_ptr, int nghttp2_session_server_new(nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks, void *user_data) { - return nghttp2_session_server_new2(session_ptr, callbacks, user_data, NULL); + return nghttp2_session_server_new3(session_ptr, callbacks, user_data, NULL, + NULL); } int nghttp2_session_server_new2(nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks, void *user_data, const nghttp2_option *option) { + return nghttp2_session_server_new3(session_ptr, callbacks, user_data, option, + NULL); +} + +int nghttp2_session_server_new3(nghttp2_session **session_ptr, + const nghttp2_session_callbacks *callbacks, + void *user_data, const nghttp2_option *option, + nghttp2_mem *mem) { int rv; nghttp2_session *session; - rv = session_new(&session, callbacks, user_data, 1, option); + rv = session_new(&session, callbacks, user_data, 1, option, mem); if (rv != 0) { return rv; @@ -480,37 +506,44 @@ static int free_streams(nghttp2_map_entry *entry, void *ptr) { nghttp2_session *session; nghttp2_stream *stream; nghttp2_outbound_item *item; + nghttp2_mem *mem; session = (nghttp2_session *)ptr; + mem = &session->mem; stream = (nghttp2_stream *)entry; item = stream->data_item; if (item && !item->queued && item != session->aob.item) { - nghttp2_outbound_item_free(item); - free(item); + nghttp2_outbound_item_free(item, mem); + nghttp2_mem_free(mem, item); } nghttp2_stream_free(stream); - free(stream); + nghttp2_mem_free(mem, stream); return 0; } -static void ob_pq_free(nghttp2_pq *pq) { +static void ob_pq_free(nghttp2_pq *pq, nghttp2_mem *mem) { while (!nghttp2_pq_empty(pq)) { nghttp2_outbound_item *item = (nghttp2_outbound_item *)nghttp2_pq_top(pq); - nghttp2_outbound_item_free(item); - free(item); + nghttp2_outbound_item_free(item, mem); + nghttp2_mem_free(mem, item); nghttp2_pq_pop(pq); } nghttp2_pq_free(pq); } void nghttp2_session_del(nghttp2_session *session) { + nghttp2_mem *mem; + if (session == NULL) { return; } - free(session->inflight_iv); + + mem = &session->mem; + + nghttp2_mem_free(mem, session->inflight_iv); nghttp2_stream_roots_free(&session->roots); @@ -519,15 +552,15 @@ void nghttp2_session_del(nghttp2_session *session) { nghttp2_map_each_free(&session->streams, free_streams, session); nghttp2_map_free(&session->streams); - ob_pq_free(&session->ob_pq); - ob_pq_free(&session->ob_ss_pq); - ob_pq_free(&session->ob_da_pq); - active_outbound_item_reset(&session->aob); + ob_pq_free(&session->ob_pq, mem); + ob_pq_free(&session->ob_ss_pq, mem); + ob_pq_free(&session->ob_da_pq, mem); + active_outbound_item_reset(&session->aob, mem); session_inbound_frame_reset(session); nghttp2_hd_deflate_free(&session->hd_deflater); nghttp2_hd_inflate_free(&session->hd_inflater); nghttp2_bufs_free(&session->aob.framebufs); - free(session); + nghttp2_mem_free(mem, session); } int @@ -758,13 +791,15 @@ int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id, nghttp2_outbound_item *item; nghttp2_frame *frame; nghttp2_stream *stream; + nghttp2_mem *mem; + mem = &session->mem; stream = nghttp2_session_get_stream(session, stream_id); if (stream && stream->state == NGHTTP2_STREAM_CLOSING) { return 0; } - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -777,7 +812,7 @@ int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id, rv = nghttp2_session_add_item(session, item); if (rv != 0) { nghttp2_frame_rst_stream_free(&frame->rst_stream); - free(item); + nghttp2_mem_free(mem, item); return rv; } return 0; @@ -796,7 +831,9 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session, nghttp2_priority_spec pri_spec_default; nghttp2_priority_spec *pri_spec = pri_spec_in; ssize_t num_adjust_closed = 0; + nghttp2_mem *mem; + mem = &session->mem; stream = nghttp2_session_get_stream_raw(session, stream_id); if (stream) { @@ -812,7 +849,7 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session, nghttp2_session_adjust_closed_stream(session, 1); } - stream = malloc(sizeof(nghttp2_stream)); + stream = nghttp2_mem_malloc(mem, sizeof(nghttp2_stream)); if (stream == NULL) { return NULL; } @@ -838,7 +875,7 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session, if (dep_stream == NULL) { if (stream_alloc) { - free(stream); + nghttp2_mem_free(mem, stream); } return NULL; @@ -862,7 +899,7 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session, if (stream_alloc) { rv = nghttp2_map_insert(&session->streams, &stream->map_entry); if (rv != 0) { - free(stream); + nghttp2_mem_free(mem, stream); return NULL; } } @@ -943,7 +980,9 @@ int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id, uint32_t error_code) { int rv; nghttp2_stream *stream; + nghttp2_mem *mem; + mem = &session->mem; stream = nghttp2_session_get_stream(session, stream_id); if (!stream) { @@ -969,8 +1008,8 @@ int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id, points to this item, let active_outbound_item_reset() free the item. */ if (!item->queued && item != session->aob.item) { - nghttp2_outbound_item_free(item); - free(item); + nghttp2_outbound_item_free(item, mem); + nghttp2_mem_free(mem, item); } } @@ -1019,14 +1058,18 @@ int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id, void nghttp2_session_destroy_stream(nghttp2_session *session, nghttp2_stream *stream) { + nghttp2_mem *mem; + DEBUGF(fprintf(stderr, "stream: destroy closed stream(%p)=%d\n", stream, stream->stream_id)); + mem = &session->mem; + nghttp2_stream_dep_remove(stream); nghttp2_map_remove(&session->streams, stream->stream_id); nghttp2_stream_free(stream); - free(stream); + nghttp2_mem_free(mem, stream); } void nghttp2_session_keep_closed_stream(nghttp2_session *session, @@ -1578,7 +1621,9 @@ static int session_prep_frame(nghttp2_session *session, int framerv = 0; int rv; nghttp2_frame *frame; + nghttp2_mem *mem; + mem = &session->mem; frame = &item->frame; if (frame->hd.type != NGHTTP2_DATA) { @@ -1842,7 +1887,7 @@ static int session_prep_frame(nghttp2_session *session, } session->aob.item = NULL; - active_outbound_item_reset(&session->aob); + active_outbound_item_reset(&session->aob, mem); return NGHTTP2_ERR_DEFERRED; } @@ -1858,7 +1903,7 @@ static int session_prep_frame(nghttp2_session *session, } session->aob.item = NULL; - active_outbound_item_reset(&session->aob); + active_outbound_item_reset(&session->aob, mem); return NGHTTP2_ERR_DEFERRED; } if (framerv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { @@ -2143,7 +2188,9 @@ static int session_after_frame_sent(nghttp2_session *session) { nghttp2_outbound_item *item = aob->item; nghttp2_bufs *framebufs = &aob->framebufs; nghttp2_frame *frame; + nghttp2_mem *mem; + mem = &session->mem; frame = &item->frame; if (frame->hd.type != NGHTTP2_DATA) { @@ -2284,7 +2331,7 @@ static int session_after_frame_sent(nghttp2_session *session) { default: break; } - active_outbound_item_reset(&session->aob); + active_outbound_item_reset(&session->aob, mem); return 0; } else { nghttp2_outbound_item *next_item; @@ -2351,7 +2398,7 @@ static int session_after_frame_sent(nghttp2_session *session) { on_frame_send_callback, which attach data to stream. We don't want to detach it. */ if (aux_data->eof) { - active_outbound_item_reset(aob); + active_outbound_item_reset(aob, mem); return 0; } @@ -2367,7 +2414,7 @@ static int session_after_frame_sent(nghttp2_session *session) { } } - active_outbound_item_reset(aob); + active_outbound_item_reset(aob, mem); return 0; } @@ -2413,7 +2460,7 @@ static int session_after_frame_sent(nghttp2_session *session) { } aob->item = NULL; - active_outbound_item_reset(aob); + active_outbound_item_reset(aob, mem); return 0; } @@ -2434,7 +2481,7 @@ static int session_after_frame_sent(nghttp2_session *session) { } aob->item = NULL; - active_outbound_item_reset(aob); + active_outbound_item_reset(aob, mem); return 0; } @@ -2455,7 +2502,7 @@ static int session_after_frame_sent(nghttp2_session *session) { return rv; } - active_outbound_item_reset(aob); + active_outbound_item_reset(aob, mem); return 0; } @@ -2475,7 +2522,7 @@ static int session_after_frame_sent(nghttp2_session *session) { } aob->item = NULL; - active_outbound_item_reset(&session->aob); + active_outbound_item_reset(&session->aob, mem); return 0; } /* Unreachable */ @@ -2488,7 +2535,9 @@ ssize_t nghttp2_session_mem_send(nghttp2_session *session, int rv; nghttp2_active_outbound_item *aob; nghttp2_bufs *framebufs; + nghttp2_mem *mem; + mem = &session->mem; aob = &session->aob; framebufs = &aob->framebufs; @@ -2539,16 +2588,16 @@ ssize_t nghttp2_session_mem_send(nghttp2_session *session, if (session->callbacks.on_frame_not_send_callback( session, frame, rv, session->user_data) != 0) { - nghttp2_outbound_item_free(item); - free(item); + nghttp2_outbound_item_free(item, mem); + nghttp2_mem_free(mem, item); return NGHTTP2_ERR_CALLBACK_FAILURE; } } } - nghttp2_outbound_item_free(item); - free(item); - active_outbound_item_reset(aob); + nghttp2_outbound_item_free(item, mem); + nghttp2_mem_free(mem, item); + active_outbound_item_reset(aob, mem); if (rv == NGHTTP2_ERR_HEADER_COMP) { /* If header compression error occurred, should terminiate @@ -3535,6 +3584,9 @@ int nghttp2_session_on_settings_received(nghttp2_session *session, nghttp2_frame *frame, int noack) { int rv; size_t i; + nghttp2_mem *mem; + + mem = &session->mem; if (frame->hd.stream_id != 0) { return session_handle_invalid_connection( @@ -3552,7 +3604,7 @@ int nghttp2_session_on_settings_received(nghttp2_session *session, } rv = nghttp2_session_update_local_settings(session, session->inflight_iv, session->inflight_niv); - free(session->inflight_iv); + nghttp2_mem_free(mem, session->inflight_iv); session->inflight_iv = NULL; session->inflight_niv = -1; if (rv != 0) { @@ -3683,7 +3735,9 @@ static int session_process_settings_frame(nghttp2_session *session) { nghttp2_frame *frame = &iframe->frame; size_t i; nghttp2_settings_entry min_header_size_entry; + nghttp2_mem *mem; + mem = &session->mem; min_header_size_entry = iframe->iv[NGHTTP2_INBOUND_NUM_IV - 1]; if (min_header_size_entry.value < UINT32_MAX) { @@ -3704,7 +3758,7 @@ static int session_process_settings_frame(nghttp2_session *session) { } rv = nghttp2_frame_unpack_settings_payload(&frame->settings, iframe->iv, - iframe->niv); + iframe->niv, mem); if (rv != 0) { assert(nghttp2_is_fatal(rv)); return rv; @@ -4386,11 +4440,14 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in, nghttp2_frame_hd cont_hd; nghttp2_stream *stream; size_t pri_fieldlen; + nghttp2_mem *mem; DEBUGF(fprintf(stderr, "recv: connection recv_window_size=%d, local_window=%d\n", session->recv_window_size, session->local_window_size)); + mem = &session->mem; + for (;;) { switch (iframe->state) { case NGHTTP2_IB_READ_CLIENT_PREFACE: @@ -4919,7 +4976,7 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in, debuglen = iframe->frame.hd.length - 8; if (debuglen > 0) { - iframe->raw_lbuf = malloc(debuglen); + iframe->raw_lbuf = nghttp2_mem_malloc(mem, debuglen); if (iframe->raw_lbuf == NULL) { return NGHTTP2_ERR_NOMEM; @@ -5512,8 +5569,10 @@ int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags, int rv; nghttp2_outbound_item *item; nghttp2_frame *frame; + nghttp2_mem *mem; - item = malloc(sizeof(nghttp2_outbound_item)); + mem = &session->mem; + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -5528,7 +5587,7 @@ int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags, if (rv != 0) { nghttp2_frame_ping_free(&frame->ping); - free(item); + nghttp2_mem_free(mem, item); return rv; } return 0; @@ -5542,6 +5601,9 @@ int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id, nghttp2_frame *frame; uint8_t *opaque_data_copy = NULL; nghttp2_goaway_aux_data *aux_data; + nghttp2_mem *mem; + + mem = &session->mem; if (nghttp2_session_is_my_stream_id(session, last_stream_id)) { return NGHTTP2_ERR_INVALID_ARGUMENT; @@ -5551,16 +5613,16 @@ int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id, if (opaque_data_len + 8 > NGHTTP2_MAX_PAYLOADLEN) { return NGHTTP2_ERR_INVALID_ARGUMENT; } - opaque_data_copy = malloc(opaque_data_len); + opaque_data_copy = nghttp2_mem_malloc(mem, opaque_data_len); if (opaque_data_copy == NULL) { return NGHTTP2_ERR_NOMEM; } memcpy(opaque_data_copy, opaque_data, opaque_data_len); } - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { - free(opaque_data_copy); + nghttp2_mem_free(mem, opaque_data_copy); return NGHTTP2_ERR_NOMEM; } @@ -5580,8 +5642,8 @@ int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id, rv = nghttp2_session_add_item(session, item); if (rv != 0) { - nghttp2_frame_goaway_free(&frame->goaway); - free(item); + nghttp2_frame_goaway_free(&frame->goaway, mem); + nghttp2_mem_free(mem, item); return rv; } return 0; @@ -5593,8 +5655,10 @@ int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags, int rv; nghttp2_outbound_item *item; nghttp2_frame *frame; + nghttp2_mem *mem; - item = malloc(sizeof(nghttp2_outbound_item)); + mem = &session->mem; + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -5610,7 +5674,7 @@ int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags, if (rv != 0) { nghttp2_frame_window_update_free(&frame->window_update); - free(item); + nghttp2_mem_free(mem, item); return rv; } return 0; @@ -5623,6 +5687,9 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags, nghttp2_settings_entry *iv_copy; size_t i; int rv; + nghttp2_mem *mem; + + mem = &session->mem; if (flags & NGHTTP2_FLAG_ACK) { if (niv != 0) { @@ -5636,15 +5703,15 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags, return NGHTTP2_ERR_INVALID_ARGUMENT; } - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; } if (niv > 0) { - iv_copy = nghttp2_frame_iv_copy(iv, niv); + iv_copy = nghttp2_frame_iv_copy(iv, niv, mem); if (iv_copy == NULL) { - free(item); + nghttp2_mem_free(mem, item); return NGHTTP2_ERR_NOMEM; } } else { @@ -5653,11 +5720,11 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags, if ((flags & NGHTTP2_FLAG_ACK) == 0) { if (niv > 0) { - session->inflight_iv = nghttp2_frame_iv_copy(iv, niv); + session->inflight_iv = nghttp2_frame_iv_copy(iv, niv, mem); if (session->inflight_iv == NULL) { - free(iv_copy); - free(item); + nghttp2_mem_free(mem, iv_copy); + nghttp2_mem_free(mem, item); return NGHTTP2_ERR_NOMEM; } } else { @@ -5678,13 +5745,13 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags, assert(nghttp2_is_fatal(rv)); if ((flags & NGHTTP2_FLAG_ACK) == 0) { - free(session->inflight_iv); + nghttp2_mem_free(mem, session->inflight_iv); session->inflight_iv = NULL; session->inflight_niv = -1; } - nghttp2_frame_settings_free(&frame->settings); - free(item); + nghttp2_frame_settings_free(&frame->settings, mem); + nghttp2_mem_free(mem, item); return rv; } @@ -5950,6 +6017,9 @@ int nghttp2_session_upgrade(nghttp2_session *session, size_t niv; int rv; nghttp2_priority_spec pri_spec; + nghttp2_mem *mem; + + mem = &session->mem; if ((!session->server && session->next_stream_id != 1) || (session->server && session->last_recv_stream_id >= 1)) { @@ -5959,7 +6029,7 @@ int nghttp2_session_upgrade(nghttp2_session *session, return NGHTTP2_ERR_INVALID_ARGUMENT; } rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload, - settings_payloadlen); + settings_payloadlen, mem); if (rv != 0) { return rv; } @@ -5973,7 +6043,7 @@ int nghttp2_session_upgrade(nghttp2_session *session, } else { rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv); } - free(iv); + nghttp2_mem_free(mem, iv); if (rv != 0) { return rv; } diff --git a/lib/nghttp2_session.h b/lib/nghttp2_session.h index 0ea567d5..7009b7b7 100644 --- a/lib/nghttp2_session.h +++ b/lib/nghttp2_session.h @@ -39,6 +39,7 @@ #include "nghttp2_int.h" #include "nghttp2_buf.h" #include "nghttp2_callbacks.h" +#include "nghttp2_mem.h" /* * Option flags. @@ -146,6 +147,8 @@ struct nghttp2_session { nghttp2_hd_deflater hd_deflater; nghttp2_hd_inflater hd_inflater; nghttp2_session_callbacks callbacks; + /* Memory allocator */ + nghttp2_mem mem; /* Sequence number of outbound frame to maintain the order of enqueue if priority is equal. */ int64_t next_seq; diff --git a/lib/nghttp2_submit.c b/lib/nghttp2_submit.c index a8298254..262dc8a3 100644 --- a/lib/nghttp2_submit.c +++ b/lib/nghttp2_submit.c @@ -47,13 +47,16 @@ static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags, nghttp2_outbound_item *item = NULL; nghttp2_frame *frame = NULL; nghttp2_headers_category hcat; + nghttp2_mem *mem; + + mem = &session->mem; if (stream_id == 0) { rv = NGHTTP2_ERR_INVALID_ARGUMENT; goto fail; } - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { rv = NGHTTP2_ERR_NOMEM; goto fail; @@ -94,7 +97,7 @@ static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags, rv = nghttp2_session_add_item(session, item); if (rv != 0) { - nghttp2_frame_headers_free(&frame->headers); + nghttp2_frame_headers_free(&frame->headers, mem); goto fail2; } @@ -106,9 +109,9 @@ static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags, fail: /* nghttp2_frame_headers_init() takes ownership of nva_copy. */ - nghttp2_nv_array_del(nva_copy); + nghttp2_nv_array_del(nva_copy, mem); fail2: - free(item); + nghttp2_mem_free(mem, item); return rv; } @@ -131,6 +134,9 @@ static int32_t submit_headers_shared_nva(nghttp2_session *session, int rv; nghttp2_nv *nva_copy; nghttp2_priority_spec copy_pri_spec; + nghttp2_mem *mem; + + mem = &session->mem; if (pri_spec) { copy_pri_spec = *pri_spec; @@ -139,7 +145,7 @@ static int32_t submit_headers_shared_nva(nghttp2_session *session, nghttp2_priority_spec_default_init(©_pri_spec); } - rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen); + rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem); if (rv < 0) { return rv; } @@ -178,6 +184,9 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_, nghttp2_outbound_item *item; nghttp2_frame *frame; nghttp2_priority_spec copy_pri_spec; + nghttp2_mem *mem; + + mem = &session->mem; if (stream_id == 0 || pri_spec == NULL) { return NGHTTP2_ERR_INVALID_ARGUMENT; @@ -191,7 +200,7 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_, adjust_priority_spec_weight(©_pri_spec); - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; @@ -207,7 +216,7 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_, if (rv != 0) { nghttp2_frame_priority_free(&frame->priority); - free(item); + nghttp2_mem_free(mem, item); return rv; } @@ -246,6 +255,9 @@ int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_, uint8_t flags_copy; int32_t promised_stream_id; int rv; + nghttp2_mem *mem; + + mem = &session->mem; if (stream_id == 0 || nghttp2_session_is_my_stream_id(session, stream_id)) { return NGHTTP2_ERR_INVALID_ARGUMENT; @@ -260,7 +272,7 @@ int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_, return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE; } - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -271,9 +283,9 @@ int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_, frame = &item->frame; - rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen); + rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem); if (rv < 0) { - free(item); + nghttp2_mem_free(mem, item); return rv; } @@ -288,8 +300,8 @@ int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_, rv = nghttp2_session_add_item(session, item); if (rv != 0) { - nghttp2_frame_push_promise_free(&frame->push_promise); - free(item); + nghttp2_frame_push_promise_free(&frame->push_promise, mem); + nghttp2_mem_free(mem, item); return rv; } @@ -406,12 +418,15 @@ int nghttp2_submit_data(nghttp2_session *session, uint8_t flags, nghttp2_frame *frame; nghttp2_data_aux_data *aux_data; uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM; + nghttp2_mem *mem; + + mem = &session->mem; if (stream_id == 0) { return NGHTTP2_ERR_INVALID_ARGUMENT; } - item = malloc(sizeof(nghttp2_outbound_item)); + item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item)); if (item == NULL) { return NGHTTP2_ERR_NOMEM; } @@ -430,7 +445,7 @@ int nghttp2_submit_data(nghttp2_session *session, uint8_t flags, rv = nghttp2_session_add_item(session, item); if (rv != 0) { nghttp2_frame_data_free(&frame->data); - free(item); + nghttp2_mem_free(mem, item); return rv; } return 0; diff --git a/python/cnghttp2.pxd b/python/cnghttp2.pxd index e3ac95c3..a5d23259 100644 --- a/python/cnghttp2.pxd +++ b/python/cnghttp2.pxd @@ -249,7 +249,7 @@ cdef extern from 'nghttp2/nghttp2.h': int32_t stream_id int32_t weight uint8_t exclusive - + int nghttp2_submit_request(nghttp2_session *session, const nghttp2_priority_spec *pri_spec, const nghttp2_nv *nva, size_t nvlen, const nghttp2_data_provider *data_prd, @@ -312,14 +312,6 @@ cdef extern from 'nghttp2/nghttp2.h': int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) -cdef extern from 'nghttp2_helper.h': - - void nghttp2_free(void *ptr) - -cdef extern from 'nghttp2_frame.h': - - void nghttp2_nv_array_del(nghttp2_nv *nva) - cdef extern from 'nghttp2_hd.h': # This is macro @@ -347,15 +339,3 @@ cdef extern from 'nghttp2_hd.h': nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context, size_t index) - -cdef extern from 'nghttp2_buf.h': - - ctypedef struct nghttp2_bufs: - pass - - void nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_size, - size_t max_chunk) - - void nghttp2_bufs_free(nghttp2_bufs *bufs) - - ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) diff --git a/src/deflatehd.cc b/src/deflatehd.cc index b435bb08..07d7197d 100644 --- a/src/deflatehd.cc +++ b/src/deflatehd.cc @@ -120,7 +120,7 @@ static void deflate_hd(nghttp2_hd_deflater *deflater, ssize_t rv; nghttp2_bufs bufs; - nghttp2_bufs_init2(&bufs, 4096, 16, 0); + nghttp2_bufs_init2(&bufs, 4096, 16, 0, nghttp2_mem_default()); rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, (nghttp2_nv *)nva.data(), nva.size()); @@ -187,17 +187,17 @@ static int deflate_hd_json(json_t *obj, nghttp2_hd_deflater *deflater, } static void init_deflater(nghttp2_hd_deflater *deflater) { - nghttp2_hd_deflate_init2(deflater, config.deflate_table_size); + nghttp2_hd_deflate_new(&deflater, config.deflate_table_size); nghttp2_hd_deflate_change_table_size(deflater, config.table_size); } static void deinit_deflater(nghttp2_hd_deflater *deflater) { - nghttp2_hd_deflate_free(deflater); + nghttp2_hd_deflate_del(deflater); } static int perform(void) { json_error_t error; - nghttp2_hd_deflater deflater; + nghttp2_hd_deflater *deflater = NULL; auto json = json_loadf(stdin, 0, &error); @@ -218,7 +218,7 @@ static int perform(void) { exit(EXIT_FAILURE); } - init_deflater(&deflater); + init_deflater(deflater); output_json_header(); auto len = json_array_size(cases); @@ -228,7 +228,7 @@ static int perform(void) { fprintf(stderr, "Unexpected JSON type at %zu. It should be object.\n", i); continue; } - if (deflate_hd_json(obj, &deflater, i) != 0) { + if (deflate_hd_json(obj, deflater, i) != 0) { continue; } if (i + 1 < len) { @@ -236,7 +236,7 @@ static int perform(void) { } } output_json_footer(); - deinit_deflater(&deflater); + deinit_deflater(deflater); json_decref(json); return 0; } @@ -244,8 +244,8 @@ static int perform(void) { static int perform_from_http1text(void) { char line[1 << 14]; int seq = 0; - nghttp2_hd_deflater deflater; - init_deflater(&deflater); + nghttp2_hd_deflater *deflater = NULL; + init_deflater(deflater); output_json_header(); for (;;) { std::vector nva; @@ -292,7 +292,7 @@ static int perform_from_http1text(void) { if (seq > 0) { printf(",\n"); } - deflate_hd(&deflater, nva, inputlen, seq); + deflate_hd(deflater, nva, inputlen, seq); } for (auto &nv : nva) { @@ -305,7 +305,7 @@ static int perform_from_http1text(void) { ++seq; } output_json_footer(); - deinit_deflater(&deflater); + deinit_deflater(deflater); return 0; } diff --git a/src/inflatehd.cc b/src/inflatehd.cc index 7122821f..353941af 100644 --- a/src/inflatehd.cc +++ b/src/inflatehd.cc @@ -159,7 +159,7 @@ static int inflate_hd(json_t *obj, nghttp2_hd_inflater *inflater, int seq) { } static int perform(void) { - nghttp2_hd_inflater inflater; + nghttp2_hd_inflater *inflater = NULL; json_error_t error; auto json = json_loadf(stdin, 0, &error); @@ -181,7 +181,7 @@ static int perform(void) { exit(EXIT_FAILURE); } - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_new(&inflater); output_json_header(); auto len = json_array_size(cases); @@ -191,7 +191,7 @@ static int perform(void) { fprintf(stderr, "Unexpected JSON type at %zu. It should be object.\n", i); continue; } - if (inflate_hd(obj, &inflater, i) != 0) { + if (inflate_hd(obj, inflater, i) != 0) { continue; } if (i + 1 < len) { @@ -199,7 +199,7 @@ static int perform(void) { } } output_json_footer(); - nghttp2_hd_inflate_free(&inflater); + nghttp2_hd_inflate_del(inflater); json_decref(json); return 0; diff --git a/tests/nghttp2_buf_test.c b/tests/nghttp2_buf_test.c index a2e4fad5..e6765f27 100644 --- a/tests/nghttp2_buf_test.c +++ b/tests/nghttp2_buf_test.c @@ -33,8 +33,11 @@ void test_nghttp2_bufs_add(void) { int rv; nghttp2_bufs bufs; uint8_t data[2048]; + nghttp2_mem *mem; - rv = nghttp2_bufs_init(&bufs, 1000, 3); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init(&bufs, 1000, 3, mem); CU_ASSERT(0 == rv); CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last); @@ -64,8 +67,11 @@ void test_nghttp2_bufs_addb(void) { int rv; nghttp2_bufs bufs; ssize_t i; + nghttp2_mem *mem; - rv = nghttp2_bufs_init(&bufs, 1000, 3); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init(&bufs, 1000, 3, mem); CU_ASSERT(0 == rv); rv = nghttp2_bufs_addb(&bufs, 14); @@ -126,8 +132,11 @@ void test_nghttp2_bufs_addb(void) { void test_nghttp2_bufs_orb(void) { int rv; nghttp2_bufs bufs; + nghttp2_mem *mem; - rv = nghttp2_bufs_init(&bufs, 1000, 3); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init(&bufs, 1000, 3, mem); CU_ASSERT(0 == rv); *(bufs.cur->buf.last) = 0; @@ -161,8 +170,11 @@ void test_nghttp2_bufs_remove(void) { int i; uint8_t *out; ssize_t outlen; + nghttp2_mem *mem; - rv = nghttp2_bufs_init(&bufs, 1000, 3); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init(&bufs, 1000, 3, mem); CU_ASSERT(0 == rv); nghttp2_buf_shift_right(&bufs.cur->buf, 10); @@ -198,8 +210,11 @@ void test_nghttp2_bufs_reset(void) { nghttp2_bufs bufs; nghttp2_buf_chain *ci; ssize_t offset = 9; + nghttp2_mem *mem; - rv = nghttp2_bufs_init3(&bufs, 250, 3, 1, offset); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init3(&bufs, 250, 3, 1, offset, mem); CU_ASSERT(0 == rv); rv = nghttp2_bufs_add(&bufs, "foo", 3); @@ -232,8 +247,11 @@ void test_nghttp2_bufs_advance(void) { int rv; nghttp2_bufs bufs; int i; + nghttp2_mem *mem; - rv = nghttp2_bufs_init(&bufs, 250, 3); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init(&bufs, 250, 3, mem); CU_ASSERT(0 == rv); for (i = 0; i < 2; ++i) { @@ -250,8 +268,11 @@ void test_nghttp2_bufs_advance(void) { void test_nghttp2_bufs_next_present(void) { int rv; nghttp2_bufs bufs; + nghttp2_mem *mem; - rv = nghttp2_bufs_init(&bufs, 250, 3); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init(&bufs, 250, 3, mem); CU_ASSERT(0 == rv); CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs)); @@ -278,8 +299,11 @@ void test_nghttp2_bufs_next_present(void) { void test_nghttp2_bufs_realloc(void) { int rv; nghttp2_bufs bufs; + nghttp2_mem *mem; - rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10); + mem = nghttp2_mem_default(); + + rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10, mem); CU_ASSERT(0 == rv); /* Create new buffer to see that these buffers are deallocated on diff --git a/tests/nghttp2_frame_test.c b/tests/nghttp2_frame_test.c index 8929554a..2f501f71 100644 --- a/tests/nghttp2_frame_test.c +++ b/tests/nghttp2_frame_test.c @@ -79,12 +79,14 @@ void test_nghttp2_frame_pack_headers() { nva_out out; ssize_t hdblocklen; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_deflate_init(&deflater); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_hd_inflate_init(&inflater, mem); nva = headers(); nvlen = HEADERS_LENGTH; @@ -117,7 +119,7 @@ void test_nghttp2_frame_pack_headers() { CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvvalueeq("GET", &out.nva[0])); - nghttp2_frame_headers_free(&oframe); + nghttp2_frame_headers_free(&oframe, mem); nva_out_reset(&out); nghttp2_bufs_reset(&bufs); @@ -152,12 +154,12 @@ void test_nghttp2_frame_pack_headers() { nghttp2_nv_array_sort(out.nva, out.nvlen); CU_ASSERT(nvnameeq("method", &out.nva[0])); - nghttp2_frame_headers_free(&oframe); + nghttp2_frame_headers_free(&oframe, mem); nva_out_reset(&out); nghttp2_bufs_reset(&bufs); nghttp2_bufs_free(&bufs); - nghttp2_frame_headers_free(&frame); + nghttp2_frame_headers_free(&frame, mem); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater); } @@ -172,7 +174,9 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void) { size_t big_hdslen = ARRLEN(big_hds); size_t i; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); for (i = 0; i < big_hdslen; ++i) { @@ -185,15 +189,15 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void) { big_hds[i].flags = NGHTTP2_NV_FLAG_NONE; } - nghttp2_nv_array_copy(&nva, big_hds, big_hdslen); - nghttp2_hd_deflate_init(&deflater); + nghttp2_nv_array_copy(&nva, big_hds, big_hdslen, mem); + nghttp2_hd_deflate_init(&deflater, mem); nghttp2_frame_headers_init( &frame, NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS, 1000000007, NGHTTP2_HCAT_REQUEST, NULL, nva, big_hdslen); rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater); CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == rv); - nghttp2_frame_headers_free(&frame); + nghttp2_frame_headers_free(&frame, mem); nghttp2_bufs_free(&bufs); for (i = 0; i < big_hdslen; ++i) { free(big_hds[i].value); @@ -279,11 +283,13 @@ void test_nghttp2_frame_pack_settings() { nghttp2_settings_entry iv[] = {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 256}, {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384}, {NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096}}; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nghttp2_frame_settings_init(&frame, NGHTTP2_FLAG_NONE, - nghttp2_frame_iv_copy(iv, 3), 3); + nghttp2_frame_iv_copy(iv, 3, mem), 3); rv = nghttp2_frame_pack_settings(&bufs, &frame); CU_ASSERT(0 == rv); @@ -300,8 +306,8 @@ void test_nghttp2_frame_pack_settings() { } nghttp2_bufs_free(&bufs); - nghttp2_frame_settings_free(&frame); - nghttp2_frame_settings_free(&oframe); + nghttp2_frame_settings_free(&frame, mem); + nghttp2_frame_settings_free(&oframe, mem); } void test_nghttp2_frame_pack_push_promise() { @@ -314,12 +320,14 @@ void test_nghttp2_frame_pack_push_promise() { nva_out out; ssize_t hdblocklen; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_deflate_init(&deflater); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_hd_inflate_init(&inflater, mem); nva = headers(); nvlen = HEADERS_LENGTH; @@ -346,8 +354,8 @@ void test_nghttp2_frame_pack_push_promise() { nva_out_reset(&out); nghttp2_bufs_free(&bufs); - nghttp2_frame_push_promise_free(&oframe); - nghttp2_frame_push_promise_free(&frame); + nghttp2_frame_push_promise_free(&oframe, mem); + nghttp2_frame_push_promise_free(&frame, mem); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater); } @@ -381,7 +389,9 @@ void test_nghttp2_frame_pack_goaway() { size_t opaque_data_len = 16; uint8_t *opaque_data = malloc(opaque_data_len); int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memcpy(opaque_data, "0123456789abcdef", opaque_data_len); @@ -400,7 +410,7 @@ void test_nghttp2_frame_pack_goaway() { CU_ASSERT(opaque_data_len == oframe.opaque_data_len); CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, opaque_data_len) == 0); - nghttp2_frame_goaway_free(&oframe); + nghttp2_frame_goaway_free(&oframe, mem); nghttp2_bufs_reset(&bufs); /* Unknown error code is passed to callback as is */ @@ -413,9 +423,9 @@ void test_nghttp2_frame_pack_goaway() { check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd); CU_ASSERT(1000000009 == oframe.error_code); - nghttp2_frame_goaway_free(&oframe); + nghttp2_frame_goaway_free(&oframe, mem); - nghttp2_frame_goaway_free(&frame); + nghttp2_frame_goaway_free(&frame, mem); nghttp2_bufs_free(&bufs); } @@ -448,6 +458,9 @@ void test_nghttp2_nv_array_copy(void) { nghttp2_nv emptynv[] = {MAKE_NV("", ""), MAKE_NV("", "")}; nghttp2_nv nv[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")}; nghttp2_nv bignv; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); bignv.name = (uint8_t *)"echo"; bignv.namelen = strlen("echo"); @@ -455,20 +468,20 @@ void test_nghttp2_nv_array_copy(void) { bignv.value = malloc(bignv.valuelen); memset(bignv.value, '0', bignv.valuelen); - rv = nghttp2_nv_array_copy(&nva, NULL, 0); + rv = nghttp2_nv_array_copy(&nva, NULL, 0, mem); CU_ASSERT(0 == rv); CU_ASSERT(NULL == nva); - rv = nghttp2_nv_array_copy(&nva, emptynv, ARRLEN(emptynv)); + rv = nghttp2_nv_array_copy(&nva, emptynv, ARRLEN(emptynv), mem); CU_ASSERT(0 == rv); CU_ASSERT(nva[0].namelen == 0); CU_ASSERT(nva[0].valuelen == 0); CU_ASSERT(nva[1].namelen == 0); CU_ASSERT(nva[1].valuelen == 0); - nghttp2_nv_array_del(nva); + nghttp2_nv_array_del(nva, mem); - rv = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv)); + rv = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv), mem); CU_ASSERT(0 == rv); CU_ASSERT(nva[0].namelen == 5); CU_ASSERT(0 == memcmp("alpha", nva[0].name, 5)); @@ -479,13 +492,13 @@ void test_nghttp2_nv_array_copy(void) { CU_ASSERT(nva[1].valuelen == 5); CU_ASSERT(0 == memcmp("delta", nva[1].value, 5)); - nghttp2_nv_array_del(nva); + nghttp2_nv_array_del(nva, mem); /* Large header field is acceptable */ - rv = nghttp2_nv_array_copy(&nva, &bignv, 1); + rv = nghttp2_nv_array_copy(&nva, &bignv, 1, mem); CU_ASSERT(0 == rv); - nghttp2_nv_array_del(nva); + nghttp2_nv_array_del(nva, mem); free(bignv.value); } diff --git a/tests/nghttp2_hd_test.c b/tests/nghttp2_hd_test.c index d4811807..1374054e 100644 --- a/tests/nghttp2_hd_test.c +++ b/tests/nghttp2_hd_test.c @@ -52,12 +52,14 @@ void test_nghttp2_hd_deflate(void) { ssize_t blocklen; nva_out out; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater)); - CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater)); + CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem)); + CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem)); rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1)); blocklen = nghttp2_bufs_len(&bufs); @@ -145,12 +147,14 @@ void test_nghttp2_hd_deflate_same_indexed_repr(void) { ssize_t blocklen; nva_out out; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater)); - CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater)); + CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem)); + CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem)); /* Encode 2 same headers. Emit 1 literal reprs and 1 index repr. */ rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1)); @@ -192,11 +196,13 @@ void test_nghttp2_hd_inflate_indexed(void) { ssize_t blocklen; nghttp2_nv nv = MAKE_NV(":path", "/"); nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); nghttp2_bufs_addb(&bufs, (1 << 7) | 4); @@ -234,11 +240,13 @@ void test_nghttp2_hd_inflate_indname_noinc(void) { MAKE_NV("user-agent", "x")}; size_t i; nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); for (i = 0; i < ARRLEN(nv); ++i) { CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 57, &nv[i], 0)); @@ -266,11 +274,13 @@ void test_nghttp2_hd_inflate_indname_inc(void) { ssize_t blocklen; nghttp2_nv nv = MAKE_NV("user-agent", "nghttp2"); nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 57, &nv, 1)); @@ -299,11 +309,13 @@ void test_nghttp2_hd_inflate_indname_inc_eviction(void) { uint8_t value[1024]; nva_out out; nghttp2_nv nv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); memset(value, '0', sizeof(value)); nv.value = value; @@ -350,11 +362,13 @@ void test_nghttp2_hd_inflate_newname_noinc(void) { MAKE_NV("x", "nghttp2")}; size_t i; nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); for (i = 0; i < ARRLEN(nv); ++i) { CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv[i], 0)); @@ -381,11 +395,13 @@ void test_nghttp2_hd_inflate_newname_inc(void) { ssize_t blocklen; nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2"); nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv, 1)); @@ -414,7 +430,9 @@ void test_nghttp2_hd_inflate_clearall_inc(void) { nghttp2_nv nv; uint8_t value[4060]; nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); bufs_large_init(&bufs, 8192); nva_out_init(&out); @@ -427,7 +445,7 @@ void test_nghttp2_hd_inflate_clearall_inc(void) { nv.flags = NGHTTP2_NV_FLAG_NONE; - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv, 1)); @@ -480,7 +498,9 @@ void test_nghttp2_hd_inflate_zero_length_huffman(void) { /* Literal header without indexing - new name */ uint8_t data[] = {0x40, 0x01, 0x78 /* 'x' */, 0x80}; nva_out out; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); @@ -493,7 +513,7 @@ void test_nghttp2_hd_inflate_zero_length_huffman(void) { /* ptr[2] = 'x'; */ /* ptr[3] = 0x80; */ - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(4 == inflate_hd(&inflater, &out, &bufs, 0)); CU_ASSERT(1 == out.nvlen); @@ -516,7 +536,9 @@ void test_nghttp2_hd_ringbuf_reserve(void) { int i; ssize_t rv; ssize_t blocklen; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); @@ -527,8 +549,8 @@ void test_nghttp2_hd_ringbuf_reserve(void) { nv.value = malloc(nv.valuelen); memset(nv.value, 0, nv.valuelen); - nghttp2_hd_deflate_init2(&deflater, 8000); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init2(&deflater, 8000, mem); + nghttp2_hd_inflate_init(&inflater, mem); nghttp2_hd_inflate_change_table_size(&inflater, 8000); nghttp2_hd_deflate_change_table_size(&deflater, 8000); @@ -566,13 +588,15 @@ void test_nghttp2_hd_change_table_size(void) { ssize_t rv; nva_out out; ssize_t blocklen; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); - nghttp2_hd_deflate_init(&deflater); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_hd_inflate_init(&inflater, mem); /* inflater changes notifies 8000 max header table size */ CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000)); @@ -659,8 +683,8 @@ void test_nghttp2_hd_change_table_size(void) { /* Check table buffer is expanded */ frame_pack_bufs_init(&bufs); - nghttp2_hd_deflate_init2(&deflater, 8192); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init2(&deflater, 8192, mem); + nghttp2_hd_inflate_init(&inflater, mem); /* First inflater changes header table size to 8000 */ CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000)); @@ -725,8 +749,8 @@ void test_nghttp2_hd_change_table_size(void) { /* Check that encoder can handle the case where its allowable buffer size is less than default size, 4096 */ - nghttp2_hd_deflate_init2(&deflater, 1024); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init2(&deflater, 1024, mem); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max); @@ -751,8 +775,8 @@ void test_nghttp2_hd_change_table_size(void) { nghttp2_hd_deflate_free(&deflater); /* Check that table size UINT32_MAX can be received */ - nghttp2_hd_deflate_init2(&deflater, UINT32_MAX); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init2(&deflater, UINT32_MAX, mem); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, UINT32_MAX)); CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, UINT32_MAX)); @@ -774,8 +798,8 @@ void test_nghttp2_hd_change_table_size(void) { nghttp2_hd_deflate_free(&deflater); /* Check that context update emitted twice */ - nghttp2_hd_deflate_init2(&deflater, 4096); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init2(&deflater, 4096, mem); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0)); CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 3000)); @@ -968,9 +992,12 @@ void test_nghttp2_hd_deflate_inflate(void) { MAKE_NV("x-cache", "HIT from alphabravo"), MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"), }; + nghttp2_mem *mem; - nghttp2_hd_deflate_init(&deflater); - nghttp2_hd_inflate_init(&inflater); + mem = nghttp2_mem_default(); + + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_hd_inflate_init(&inflater, mem); check_deflate_inflate(&deflater, &inflater, nv1, ARRLEN(nv1)); check_deflate_inflate(&deflater, &inflater, nv2, ARRLEN(nv2)); @@ -1000,6 +1027,9 @@ void test_nghttp2_hd_no_index(void) { size_t i; nva_out out; int rv; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); /* 1st :method: GET can be indexable, last one is not */ for (i = 1; i < ARRLEN(nva); ++i) { @@ -1010,8 +1040,8 @@ void test_nghttp2_hd_no_index(void) { nva_out_init(&out); - nghttp2_hd_deflate_init(&deflater); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_deflate_init(&deflater, mem); + nghttp2_hd_inflate_init(&inflater, mem); rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva)); blocklen = nghttp2_bufs_len(&bufs); @@ -1040,10 +1070,12 @@ void test_nghttp2_hd_deflate_bound(void) { nghttp2_nv nva[] = {MAKE_NV(":method", "GET"), MAKE_NV("alpha", "bravo")}; nghttp2_bufs bufs; size_t bound, bound2; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); bound = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva)); @@ -1071,6 +1103,9 @@ void test_nghttp2_hd_public_api(void) { size_t buflen; ssize_t blocklen; nghttp2_bufs bufs; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096)); CU_ASSERT(0 == nghttp2_hd_inflate_new(&inflater)); @@ -1081,7 +1116,7 @@ void test_nghttp2_hd_public_api(void) { CU_ASSERT(blocklen > 0); - nghttp2_bufs_wrap_init(&bufs, buf, blocklen); + nghttp2_bufs_wrap_init(&bufs, buf, blocklen, mem); bufs.head->buf.last += blocklen; CU_ASSERT(blocklen == inflate_hd(inflater, NULL, &bufs, 0)); diff --git a/tests/nghttp2_map_test.c b/tests/nghttp2_map_test.c index 5d38d51f..b244b919 100644 --- a/tests/nghttp2_map_test.c +++ b/tests/nghttp2_map_test.c @@ -41,7 +41,7 @@ static void strentry_init(strentry *entry, key_type key, const char *str) { void test_nghttp2_map(void) { strentry foo, FOO, bar, baz, shrubbery; nghttp2_map map; - nghttp2_map_init(&map); + nghttp2_map_init(&map, nghttp2_mem_default()); strentry_init(&foo, 1, "foo"); strentry_init(&FOO, 1, "FOO"); @@ -110,7 +110,7 @@ void test_nghttp2_map_functional(void) { nghttp2_map map; int i; - nghttp2_map_init(&map); + nghttp2_map_init(&map, nghttp2_mem_default()); for (i = 0; i < NUM_ENT; ++i) { strentry_init(&arr[i], i + 1, "foo"); order[i] = i + 1; @@ -155,7 +155,7 @@ void test_nghttp2_map_each_free(void) { *baz = malloc(sizeof(strentry)), *shrubbery = malloc(sizeof(strentry)); nghttp2_map map; - nghttp2_map_init(&map); + nghttp2_map_init(&map, nghttp2_mem_default()); strentry_init(foo, 1, "foo"); strentry_init(bar, 2, "bar"); diff --git a/tests/nghttp2_pq_test.c b/tests/nghttp2_pq_test.c index acb33ace..1cef4d0e 100644 --- a/tests/nghttp2_pq_test.c +++ b/tests/nghttp2_pq_test.c @@ -35,7 +35,7 @@ static int pq_compar(const void *lhs, const void *rhs) { void test_nghttp2_pq(void) { int i; nghttp2_pq pq; - nghttp2_pq_init(&pq, pq_compar); + nghttp2_pq_init(&pq, pq_compar, nghttp2_mem_default()); CU_ASSERT(nghttp2_pq_empty(&pq)); CU_ASSERT(0 == nghttp2_pq_size(&pq)); CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"foo")); @@ -103,7 +103,7 @@ void test_nghttp2_pq_update(void) { node *nd; int ans[] = {-8, -6, -4, -2, 0, 1, 3, 5, 7, 9}; - nghttp2_pq_init(&pq, node_compar); + nghttp2_pq_init(&pq, node_compar, nghttp2_mem_default()); for (i = 0; i < (int)(sizeof(nodes) / sizeof(nodes[0])); ++i) { nodes[i].key = i; diff --git a/tests/nghttp2_session_test.c b/tests/nghttp2_session_test.c index 096a5c7c..34f1a033 100644 --- a/tests/nghttp2_session_test.c +++ b/tests/nghttp2_session_test.c @@ -357,7 +357,7 @@ static int on_stream_close_callback(nghttp2_session *session _U_, static nghttp2_settings_entry *dup_iv(const nghttp2_settings_entry *iv, size_t niv) { - return nghttp2_frame_iv_copy(iv, niv); + return nghttp2_frame_iv_copy(iv, niv, nghttp2_mem_default()); } static nghttp2_priority_spec pri_spec_default = {0, NGHTTP2_DEFAULT_WEIGHT, 0}; @@ -377,7 +377,9 @@ void test_nghttp2_session_recv(void) { ssize_t nvlen; nghttp2_hd_deflater deflater; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); @@ -389,10 +391,10 @@ void test_nghttp2_session_recv(void) { user_data.df = &df; nghttp2_session_server_new(&session, &callbacks, &user_data); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); nvlen = ARRLEN(nv); - nghttp2_nv_array_copy(&nva, nv, nvlen); + nghttp2_nv_array_copy(&nva, nv, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); @@ -408,7 +410,7 @@ void test_nghttp2_session_recv(void) { df.feedseq[i] = 1; } - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); user_data.frame_recv_cb_called = 0; user_data.begin_frame_cb_called = 0; @@ -428,7 +430,7 @@ void test_nghttp2_session_recv(void) { CU_ASSERT(0 == rv); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); scripted_data_feed_init2(&df, &bufs); user_data.frame_recv_cb_called = 0; @@ -510,7 +512,9 @@ void test_nghttp2_session_recv_invalid_stream_id(void) { nghttp2_frame frame; nghttp2_hd_deflater deflater; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); @@ -520,7 +524,7 @@ void test_nghttp2_session_recv_invalid_stream_id(void) { user_data.df = &df; user_data.invalid_frame_recv_cb_called = 0; nghttp2_session_server_new(&session, &callbacks, &user_data); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 2, NGHTTP2_HCAT_HEADERS, NULL, NULL, 0); @@ -530,7 +534,7 @@ void test_nghttp2_session_recv_invalid_stream_id(void) { CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); scripted_data_feed_init2(&df, &bufs); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); CU_ASSERT(0 == nghttp2_session_recv(session)); CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); @@ -552,7 +556,9 @@ void test_nghttp2_session_recv_invalid_frame(void) { ssize_t nvlen; nghttp2_hd_deflater deflater; int rv; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); @@ -563,9 +569,9 @@ void test_nghttp2_session_recv_invalid_frame(void) { user_data.df = &df; user_data.frame_send_cb_called = 0; nghttp2_session_server_new(&session, &callbacks, &user_data); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); nvlen = ARRLEN(nv); - nghttp2_nv_array_copy(&nva, nv, nvlen); + nghttp2_nv_array_copy(&nva, nv, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); @@ -588,7 +594,7 @@ void test_nghttp2_session_recv_invalid_frame(void) { CU_ASSERT(0 == user_data.frame_send_cb_called); nghttp2_bufs_free(&bufs); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_hd_deflate_free(&deflater); nghttp2_session_del(session); @@ -748,7 +754,9 @@ void test_nghttp2_session_recv_continuation(void) { size_t datalen; nghttp2_frame_hd cont_hd; nghttp2_priority_spec pri_spec; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); @@ -758,11 +766,11 @@ void test_nghttp2_session_recv_continuation(void) { nghttp2_session_server_new(&session, &callbacks, &ud); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); /* Make 1 HEADERS and insert CONTINUATION header */ nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_NONE, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); @@ -774,7 +782,7 @@ void test_nghttp2_session_recv_continuation(void) { buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); /* HEADERS's payload is 1 byte */ memcpy(data, buf->pos, NGHTTP2_FRAME_HDLEN + 1); @@ -821,11 +829,11 @@ void test_nghttp2_session_recv_continuation(void) { /* Expecting CONTINUATION, but get the other frame */ nghttp2_session_server_new(&session, &callbacks, &ud); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); /* HEADERS without END_HEADERS flag */ nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_NONE, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); nghttp2_bufs_reset(&bufs); @@ -834,7 +842,7 @@ void test_nghttp2_session_recv_continuation(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); /* make sure that all data is in the first buf */ buf = &bufs.head->buf; @@ -885,7 +893,9 @@ void test_nghttp2_session_recv_headers_with_priority(void) { nghttp2_outbound_item *item; nghttp2_priority_spec pri_spec; nghttp2_stream *stream; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); @@ -893,13 +903,13 @@ void test_nghttp2_session_recv_headers_with_priority(void) { nghttp2_session_server_new(&session, &callbacks, &ud); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); open_stream(session, 1); /* With NGHTTP2_FLAG_PRIORITY without exclusive flag set */ nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_priority_spec_init(&pri_spec, 1, 99, 0); @@ -912,7 +922,7 @@ void test_nghttp2_session_recv_headers_with_priority(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -934,7 +944,7 @@ void test_nghttp2_session_recv_headers_with_priority(void) { /* With NGHTTP2_FLAG_PRIORITY, but cut last 1 byte to make it invalid. */ nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_priority_spec_init(&pri_spec, 0, 99, 0); @@ -947,7 +957,7 @@ void test_nghttp2_session_recv_headers_with_priority(void) { CU_ASSERT(0 == rv); CU_ASSERT(NGHTTP2_FRAME_HDLEN + 5 + 2 == nghttp2_bufs_len(&bufs)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); buf = &bufs.head->buf; /* Make payload shorter than required length to store priroty @@ -978,10 +988,10 @@ void test_nghttp2_session_recv_headers_with_priority(void) { /* Check dep_stream_id == stream_id */ nghttp2_session_server_new(&session, &callbacks, &ud); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_priority_spec_init(&pri_spec, 1, 0, 0); @@ -994,7 +1004,7 @@ void test_nghttp2_session_recv_headers_with_priority(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1035,17 +1045,19 @@ void test_nghttp2_session_recv_premature_headers(void) { my_user_data ud; nghttp2_hd_deflater deflater; nghttp2_outbound_item *item; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); nghttp2_session_server_new(&session, &callbacks, &ud); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); @@ -1053,7 +1065,7 @@ void test_nghttp2_session_recv_premature_headers(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1154,7 +1166,9 @@ void test_nghttp2_session_recv_settings_header_table_size(void) { my_user_data ud; nghttp2_settings_entry iv[3]; nghttp2_nv nv = MAKE_NV(":authority", "example.org"); + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); @@ -1177,7 +1191,7 @@ void test_nghttp2_session_recv_settings_header_table_size(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1212,7 +1226,7 @@ void test_nghttp2_session_recv_settings_header_table_size(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1254,7 +1268,7 @@ void test_nghttp2_session_recv_settings_header_table_size(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1297,7 +1311,7 @@ void test_nghttp2_session_recv_settings_header_table_size(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1365,7 +1379,9 @@ void test_nghttp2_session_continue(void) { const nghttp2_frame *recv_frame; nghttp2_frame_hd data_hd; nghttp2_hd_deflater deflater; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nghttp2_buf_wrap_init(&databuf, buffer, sizeof(buffer)); @@ -1378,11 +1394,11 @@ void test_nghttp2_session_continue(void) { nghttp2_session_server_new(&session, &callbacks, &user_data); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); /* Make 2 HEADERS frames */ nvlen = ARRLEN(nv1); - nghttp2_nv_array_copy(&nva, nv1, nvlen); + nghttp2_nv_array_copy(&nva, nv1, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); @@ -1390,7 +1406,7 @@ void test_nghttp2_session_continue(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); buf = &bufs.head->buf; assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1399,7 +1415,7 @@ void test_nghttp2_session_continue(void) { databuf.last = nghttp2_cpymem(databuf.last, buf->pos, nghttp2_buf_len(buf)); nvlen = ARRLEN(nv2); - nghttp2_nv_array_copy(&nva, nv2, nvlen); + nghttp2_nv_array_copy(&nva, nv2, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 3, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); nghttp2_bufs_reset(&bufs); @@ -1408,7 +1424,7 @@ void test_nghttp2_session_continue(void) { CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf)); @@ -1541,7 +1557,9 @@ void test_nghttp2_session_add_frame(void) { nghttp2_frame *frame; nghttp2_nv *nva; ssize_t nvlen; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = accumulator_send_callback; @@ -1557,7 +1575,7 @@ void test_nghttp2_session_add_frame(void) { frame = &item->frame; nvlen = ARRLEN(nv); - nghttp2_nv_array_copy(&nva, nv, nvlen); + nghttp2_nv_array_copy(&nva, nv, nvlen, mem); nghttp2_frame_headers_init( &frame->headers, NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY, @@ -1587,7 +1605,9 @@ void test_nghttp2_session_on_request_headers_received(void) { nghttp2_nv *nva; size_t nvlen; nghttp2_priority_spec pri_spec; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; @@ -1609,7 +1629,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(NGHTTP2_STREAM_OPENING == stream->state); CU_ASSERT(255 == stream->weight); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); /* More than un-ACKed max concurrent streams leads REFUSED_STREAM */ session->pending_local_max_concurrent_stream = 1; @@ -1622,7 +1642,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); session->local_settings.max_concurrent_streams = NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS; @@ -1637,7 +1657,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(0 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); /* Stream ID is our side and it is idle stream ID, then treat it as connection error */ @@ -1650,7 +1670,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); @@ -1658,7 +1678,7 @@ void test_nghttp2_session_on_request_headers_received(void) { nghttp2_session_server_new(&session, &callbacks, &user_data); nvlen = ARRLEN(malformed_nva); - nghttp2_nv_array_copy(&nva, malformed_nva, nvlen); + nghttp2_nv_array_copy(&nva, malformed_nva, nvlen, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY, 1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); @@ -1668,7 +1688,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(1 == user_data.begin_headers_cb_called); CU_ASSERT(0 == user_data.invalid_frame_recv_cb_called); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); @@ -1685,7 +1705,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); @@ -1701,7 +1721,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); @@ -1720,7 +1740,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(0 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); @@ -1734,7 +1754,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(0 == nghttp2_session_on_request_headers_received(session, &frame)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); /* Stream ID which is greater than local_last_stream_id is ignored */ @@ -1747,7 +1767,7 @@ void test_nghttp2_session_on_request_headers_received(void) { CU_ASSERT(0 == user_data.invalid_frame_recv_cb_called); CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); } @@ -1758,7 +1778,9 @@ void test_nghttp2_session_on_response_headers_received(void) { my_user_data user_data; nghttp2_frame frame; nghttp2_stream *stream; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; @@ -1778,7 +1800,7 @@ void test_nghttp2_session_on_response_headers_received(void) { CU_ASSERT(1 == user_data.begin_headers_cb_called); CU_ASSERT(NGHTTP2_STREAM_OPENED == stream->state); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); } @@ -1788,7 +1810,9 @@ void test_nghttp2_session_on_headers_received(void) { my_user_data user_data; nghttp2_frame frame; nghttp2_stream *stream; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; @@ -1847,7 +1871,7 @@ void test_nghttp2_session_on_headers_received(void) { nghttp2_session_on_headers_received(session, &frame, stream)); CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); } @@ -1859,7 +1883,9 @@ void test_nghttp2_session_on_push_response_headers_received(void) { nghttp2_frame frame; nghttp2_stream *stream; nghttp2_outbound_item *item; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; callbacks.on_begin_headers_callback = on_begin_headers_callback; @@ -1919,7 +1945,7 @@ void test_nghttp2_session_on_push_response_headers_received(void) { CU_ASSERT(NGHTTP2_ENHANCE_YOUR_CALM == item->frame.goaway.error_code); CU_ASSERT(1 == session->num_incoming_streams); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); } @@ -2028,6 +2054,9 @@ void test_nghttp2_session_on_settings_received(void) { nghttp2_settings_entry iv[255]; nghttp2_outbound_item *item; nghttp2_nv nv = MAKE_NV(":authority", "example.org"); + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[0].value = 50; @@ -2085,7 +2114,7 @@ void test_nghttp2_session_on_settings_received(void) { CU_ASSERT(0 == nghttp2_session_get_stream_remote_window_size( session, stream2->stream_id)); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); nghttp2_session_del(session); @@ -2105,7 +2134,7 @@ void test_nghttp2_session_on_settings_received(void) { session->inflight_iv = NULL; session->inflight_niv = -1; - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); nghttp2_session_del(session); /* Check ACK against no inflight SETTINGS */ @@ -2117,7 +2146,7 @@ void test_nghttp2_session_on_settings_received(void) { CU_ASSERT(item != NULL); CU_ASSERT(NGHTTP2_GOAWAY == item->frame.hd.type); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); nghttp2_session_del(session); /* Check that 2 SETTINGS_HEADER_TABLE_SIZE 0 and 4096 are included @@ -2145,7 +2174,7 @@ void test_nghttp2_session_on_settings_received(void) { CU_ASSERT(2048 == session->hd_deflater.ctx.hd_table_bufsize_max); CU_ASSERT(2048 == session->remote_settings.header_table_size); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); nghttp2_session_del(session); /* Check too large SETTINGS_MAX_FRAME_SIZE */ @@ -2164,7 +2193,7 @@ void test_nghttp2_session_on_settings_received(void) { CU_ASSERT(item != NULL); CU_ASSERT(NGHTTP2_GOAWAY == item->frame.hd.type); - nghttp2_frame_settings_free(&frame.settings); + nghttp2_frame_settings_free(&frame.settings, mem); nghttp2_session_del(session); } @@ -2178,7 +2207,9 @@ void test_nghttp2_session_on_push_promise_received(void) { nghttp2_nv malformed_nva[] = {MAKE_NV(":path", "\x01")}; nghttp2_nv *nva; size_t nvlen; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; callbacks.on_begin_headers_callback = on_begin_headers_callback; @@ -2291,7 +2322,7 @@ void test_nghttp2_session_on_push_promise_received(void) { CU_ASSERT(NULL == nghttp2_session_get_stream(session, 10)); CU_ASSERT(NULL == nghttp2_session_get_next_ob_item(session)); - nghttp2_frame_push_promise_free(&frame.push_promise); + nghttp2_frame_push_promise_free(&frame.push_promise, mem); nghttp2_session_del(session); nghttp2_session_client_new(&session, &callbacks, &user_data); @@ -2311,7 +2342,7 @@ void test_nghttp2_session_on_push_promise_received(void) { CU_ASSERT(0 == user_data.begin_headers_cb_called); CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); - nghttp2_frame_push_promise_free(&frame.push_promise); + nghttp2_frame_push_promise_free(&frame.push_promise, mem); nghttp2_session_del(session); /* Disable PUSH */ @@ -2334,7 +2365,7 @@ void test_nghttp2_session_on_push_promise_received(void) { CU_ASSERT(0 == user_data.begin_headers_cb_called); CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); - nghttp2_frame_push_promise_free(&frame.push_promise); + nghttp2_frame_push_promise_free(&frame.push_promise, mem); nghttp2_session_del(session); /* Check malformed headers. We accept malformed headers */ @@ -2344,7 +2375,7 @@ void test_nghttp2_session_on_push_promise_received(void) { &pri_spec_default, NGHTTP2_STREAM_OPENING, NULL); nvlen = ARRLEN(malformed_nva); - nghttp2_nv_array_copy(&nva, malformed_nva, nvlen); + nghttp2_nv_array_copy(&nva, malformed_nva, nvlen, mem); nghttp2_frame_push_promise_init(&frame.push_promise, NGHTTP2_FLAG_END_HEADERS, 1, 2, nva, nvlen); user_data.begin_headers_cb_called = 0; @@ -2354,7 +2385,7 @@ void test_nghttp2_session_on_push_promise_received(void) { CU_ASSERT(1 == user_data.begin_headers_cb_called); CU_ASSERT(0 == user_data.invalid_frame_recv_cb_called); - nghttp2_frame_push_promise_free(&frame.push_promise); + nghttp2_frame_push_promise_free(&frame.push_promise, mem); nghttp2_session_del(session); } @@ -2403,7 +2434,9 @@ void test_nghttp2_session_on_goaway_received(void) { my_user_data user_data; nghttp2_frame frame; int i; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); user_data.frame_recv_cb_called = 0; user_data.invalid_frame_recv_cb_called = 0; @@ -2437,7 +2470,7 @@ void test_nghttp2_session_on_goaway_received(void) { CU_ASSERT(NULL != nghttp2_session_get_stream(session, 6)); CU_ASSERT(NULL == nghttp2_session_get_stream(session, 7)); - nghttp2_frame_goaway_free(&frame.goaway); + nghttp2_frame_goaway_free(&frame.goaway, mem); nghttp2_session_del(session); } @@ -2645,6 +2678,9 @@ void test_nghttp2_session_send_headers_frame_size_error(void) { size_t nnv = ARRLEN(nv); size_t i; my_user_data ud; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); for (i = 0; i < nnv; ++i) { nv[i].name = (uint8_t *)"header"; @@ -2662,7 +2698,7 @@ void test_nghttp2_session_send_headers_frame_size_error(void) { nghttp2_session_client_new(&session, &callbacks, &ud); nvlen = nnv; - nghttp2_nv_array_copy(&nva, nv, nvlen); + nghttp2_nv_array_copy(&nva, nv, nvlen, mem); item = malloc(sizeof(nghttp2_outbound_item)); @@ -2760,7 +2796,9 @@ void test_nghttp2_session_send_push_promise(void) { nghttp2_stream *stream; nghttp2_settings_entry iv; my_user_data ud; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; callbacks.on_frame_not_send_callback = on_frame_not_send_callback; @@ -2794,7 +2832,7 @@ void test_nghttp2_session_send_push_promise(void) { nghttp2_frame_settings_init(&frame->settings, NGHTTP2_FLAG_NONE, dup_iv(&iv, 1), 1); nghttp2_session_on_settings_received(session, frame, 1); - nghttp2_frame_settings_free(&frame->settings); + nghttp2_frame_settings_free(&frame->settings, mem); free(frame); item = malloc(sizeof(nghttp2_outbound_item)); @@ -3294,7 +3332,9 @@ void test_nghttp2_submit_request_without_data(void) { nghttp2_hd_inflater inflater; nva_out out; nghttp2_bufs bufs; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); @@ -3304,7 +3344,7 @@ void test_nghttp2_submit_request_without_data(void) { callbacks.send_callback = accumulator_send_callback; CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud)); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(1 == nghttp2_submit_request(session, NULL, nva, ARRLEN(nva), &data_prd, NULL)); item = nghttp2_session_get_next_ob_item(session); @@ -3318,7 +3358,7 @@ void test_nghttp2_submit_request_without_data(void) { inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN); CU_ASSERT(nvnameeq(":version", &out.nva[0])); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nva_out_reset(&out); nghttp2_bufs_free(&bufs); @@ -3364,7 +3404,9 @@ void test_nghttp2_submit_response_without_data(void) { nghttp2_hd_inflater inflater; nva_out out; nghttp2_bufs bufs; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); @@ -3374,7 +3416,7 @@ void test_nghttp2_submit_response_without_data(void) { callbacks.send_callback = accumulator_send_callback; CU_ASSERT(0 == nghttp2_session_server_new(&session, &callbacks, &ud)); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_END_STREAM, &pri_spec_default, NGHTTP2_STREAM_OPENING, NULL); CU_ASSERT(0 == @@ -3393,7 +3435,7 @@ void test_nghttp2_submit_response_without_data(void) { nva_out_reset(&out); nghttp2_bufs_free(&bufs); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_hd_inflate_free(&inflater); nghttp2_session_del(session); } @@ -3516,7 +3558,9 @@ void test_nghttp2_submit_headers(void) { nghttp2_hd_inflater inflater; nva_out out; nghttp2_bufs bufs; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); @@ -3528,7 +3572,7 @@ void test_nghttp2_submit_headers(void) { CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud)); - nghttp2_hd_inflate_init(&inflater); + nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_submit_headers(session, NGHTTP2_FLAG_END_STREAM, 1, NULL, nv, ARRLEN(nv), NULL)); item = nghttp2_session_get_next_ob_item(session); @@ -3563,7 +3607,7 @@ void test_nghttp2_submit_headers(void) { nva_out_reset(&out); nghttp2_bufs_free(&bufs); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_hd_inflate_free(&inflater); nghttp2_session_del(session); @@ -3652,6 +3696,9 @@ void test_nghttp2_submit_settings(void) { nghttp2_settings_entry iv[7]; nghttp2_frame ack_frame; const int32_t UNKNOWN_ID = 1000000007; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[0].value = 5; @@ -3713,7 +3760,7 @@ void test_nghttp2_submit_settings(void) { nghttp2_frame_settings_init(&ack_frame.settings, NGHTTP2_FLAG_ACK, NULL, 0); CU_ASSERT(0 == nghttp2_session_on_settings_received(session, &ack_frame, 0)); - nghttp2_frame_settings_free(&ack_frame.settings); + nghttp2_frame_settings_free(&ack_frame.settings, mem); CU_ASSERT(16 * 1024 == session->local_settings.initial_window_size); CU_ASSERT(0 == session->hd_inflater.ctx.hd_table_bufsize_max); @@ -3731,7 +3778,9 @@ void test_nghttp2_submit_settings_update_local_window_size(void) { nghttp2_settings_entry iv[4]; nghttp2_stream *stream; nghttp2_frame ack_frame; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); nghttp2_frame_settings_init(&ack_frame.settings, NGHTTP2_FLAG_ACK, NULL, 0); iv[0].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; @@ -3786,7 +3835,7 @@ void test_nghttp2_submit_settings_update_local_window_size(void) { CU_ASSERT(NGHTTP2_FLOW_CONTROL_ERROR == item->frame.goaway.error_code); nghttp2_session_del(session); - nghttp2_frame_settings_free(&ack_frame.settings); + nghttp2_frame_settings_free(&ack_frame.settings, mem); } void test_nghttp2_submit_push_promise(void) { @@ -4182,7 +4231,9 @@ void test_nghttp2_session_pop_next_ob_item(void) { nghttp2_outbound_item *item; nghttp2_priority_spec pri_spec; nghttp2_stream *stream; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; @@ -4199,12 +4250,12 @@ void test_nghttp2_session_pop_next_ob_item(void) { item = nghttp2_session_pop_next_ob_item(session); CU_ASSERT(NGHTTP2_PING == item->frame.hd.type); - nghttp2_outbound_item_free(item); + nghttp2_outbound_item_free(item, mem); free(item); item = nghttp2_session_pop_next_ob_item(session); CU_ASSERT(NGHTTP2_HEADERS == item->frame.hd.type); - nghttp2_outbound_item_free(item); + nghttp2_outbound_item_free(item, mem); free(item); CU_ASSERT(NULL == nghttp2_session_pop_next_ob_item(session)); @@ -4230,7 +4281,7 @@ void test_nghttp2_session_pop_next_ob_item(void) { nghttp2_stream_detach_data(stream, session); - nghttp2_outbound_item_free(item); + nghttp2_outbound_item_free(item, mem); free(item); CU_ASSERT(NULL == nghttp2_session_pop_next_ob_item(session)); @@ -4239,7 +4290,7 @@ void test_nghttp2_session_pop_next_ob_item(void) { item = nghttp2_session_pop_next_ob_item(session); CU_ASSERT(NGHTTP2_HEADERS == item->frame.hd.type); - nghttp2_outbound_item_free(item); + nghttp2_outbound_item_free(item, mem); free(item); nghttp2_session_del(session); @@ -4280,7 +4331,9 @@ void test_nghttp2_session_max_concurrent_streams(void) { nghttp2_session_callbacks callbacks; nghttp2_frame frame; nghttp2_outbound_item *item; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = null_send_callback; @@ -4313,7 +4366,7 @@ void test_nghttp2_session_max_concurrent_streams(void) { CU_ASSERT(NGHTTP2_GOAWAY == item->frame.hd.type); CU_ASSERT(NGHTTP2_ENHANCE_YOUR_CALM == item->frame.goaway.error_code); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); nghttp2_session_del(session); } @@ -4467,7 +4520,9 @@ void test_nghttp2_session_flow_control(void) { int32_t new_initial_window_size; nghttp2_settings_entry iv[1]; nghttp2_frame settings_frame; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.send_callback = fixed_bytes_send_callback; callbacks.on_frame_send_callback = on_frame_send_callback; @@ -4548,7 +4603,7 @@ void test_nghttp2_session_flow_control(void) { nghttp2_frame_settings_init(&settings_frame.settings, NGHTTP2_FLAG_NONE, dup_iv(iv, 1), 1); nghttp2_session_on_settings_received(session, &settings_frame, 1); - nghttp2_frame_settings_free(&settings_frame.settings); + nghttp2_frame_settings_free(&settings_frame.settings, mem); /* Sends another 8KiB data */ CU_ASSERT(0 == nghttp2_session_send(session)); @@ -5096,6 +5151,9 @@ void test_nghttp2_pack_settings_payload(void) { ssize_t len; nghttp2_settings_entry *resiv; size_t resniv; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE; iv[0].value = 1023; @@ -5104,8 +5162,8 @@ void test_nghttp2_pack_settings_payload(void) { len = nghttp2_pack_settings_payload(buf, sizeof(buf), iv, 2); CU_ASSERT(2 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH == len); - CU_ASSERT(0 == - nghttp2_frame_unpack_settings_payload2(&resiv, &resniv, buf, len)); + CU_ASSERT(0 == nghttp2_frame_unpack_settings_payload2(&resiv, &resniv, buf, + len, mem)); CU_ASSERT(2 == resniv); CU_ASSERT(NGHTTP2_SETTINGS_HEADER_TABLE_SIZE == resiv[0].settings_id); CU_ASSERT(1023 == resiv[0].value); @@ -6339,7 +6397,9 @@ void test_nghttp2_session_on_header_temporal_failure(void) { nghttp2_frame frame; nghttp2_frame_hd hd; nghttp2_outbound_item *item; + nghttp2_mem *mem; + mem = nghttp2_mem_default(); memset(&callbacks, 0, sizeof(callbacks)); callbacks.on_header_callback = temporal_failure_on_header_callback; @@ -6347,14 +6407,14 @@ void test_nghttp2_session_on_header_temporal_failure(void) { frame_pack_bufs_init(&bufs); - nghttp2_hd_deflate_init(&deflater); + nghttp2_hd_deflate_init(&deflater, mem); - nghttp2_nv_array_copy(&nva, nv, 1); + nghttp2_nv_array_copy(&nva, nv, 1, mem); nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1, NGHTTP2_HCAT_REQUEST, NULL, nva, 1); nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); - nghttp2_frame_headers_free(&frame.headers); + nghttp2_frame_headers_free(&frame.headers, mem); /* We are going to create CONTINUATION. First serialize header block, and then frame header. */ diff --git a/tests/nghttp2_test_helper.c b/tests/nghttp2_test_helper.c index 942a6a8e..ba149015 100644 --- a/tests/nghttp2_test_helper.c +++ b/tests/nghttp2_test_helper.c @@ -45,6 +45,9 @@ int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) { const uint8_t *payload = in + NGHTTP2_FRAME_HDLEN; size_t payloadlen = len - NGHTTP2_FRAME_HDLEN; size_t payloadoff; + nghttp2_mem *mem; + + mem = nghttp2_mem_default(); nghttp2_frame_unpack_frame_hd(&frame->hd, in); switch (frame->hd.type) { @@ -63,7 +66,7 @@ int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) { break; case NGHTTP2_SETTINGS: rv = nghttp2_frame_unpack_settings_payload2( - &frame->settings.iv, &frame->settings.niv, payload, payloadlen); + &frame->settings.iv, &frame->settings.niv, payload, payloadlen, mem); break; case NGHTTP2_PUSH_PROMISE: rv = nghttp2_frame_unpack_push_promise_payload(&frame->push_promise, @@ -73,7 +76,8 @@ int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) { nghttp2_frame_unpack_ping_payload(&frame->ping, payload, payloadlen); break; case NGHTTP2_GOAWAY: - nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen); + nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen, + mem); break; case NGHTTP2_WINDOW_UPDATE: nghttp2_frame_unpack_window_update_payload(&frame->window_update, payload, @@ -198,12 +202,14 @@ ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out, int frame_pack_bufs_init(nghttp2_bufs *bufs) { /* 1 for Pad Length */ - return nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 1); + return nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 1, + nghttp2_mem_default()); } void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size) { /* 1 for Pad Length */ - nghttp2_bufs_init2(bufs, chunk_size, 16, NGHTTP2_FRAME_HDLEN + 1); + nghttp2_bufs_init2(bufs, chunk_size, 16, NGHTTP2_FRAME_HDLEN + 1, + nghttp2_mem_default()); } static nghttp2_stream *open_stream_with_all(nghttp2_session *session,