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.
This commit is contained in:
Tatsuhiro Tsujikawa 2014-12-07 23:07:13 +09:00
parent 21b48d24e4
commit c0ffed7788
31 changed files with 993 additions and 443 deletions

View File

@ -44,7 +44,8 @@ OBJECTS = nghttp2_pq.c nghttp2_map.c nghttp2_queue.c \
nghttp2_version.c \ nghttp2_version.c \
nghttp2_priority_spec.c \ nghttp2_priority_spec.c \
nghttp2_option.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 \ HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \
nghttp2_frame.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_hd.h nghttp2_hd_huffman.h \
nghttp2_priority_spec.h \ nghttp2_priority_spec.h \
nghttp2_option.h \ nghttp2_option.h \
nghttp2_callbacks.h nghttp2_callbacks.h \
nghttp2_mem.h
libnghttp2_la_SOURCES = $(HFILES) $(OBJECTS) libnghttp2_la_SOURCES = $(HFILES) $(OBJECTS)
libnghttp2_la_LDFLAGS = -no-undefined \ libnghttp2_la_LDFLAGS = -no-undefined \

View File

@ -1687,6 +1687,69 @@ void nghttp2_session_callbacks_set_on_begin_frame_callback(
nghttp2_session_callbacks *cbs, nghttp2_session_callbacks *cbs,
nghttp2_on_begin_frame_callback on_begin_frame_callback); 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; struct nghttp2_option;
/** /**
@ -1869,6 +1932,58 @@ int nghttp2_session_server_new2(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks, const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option); 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 * @function
* *
@ -3079,6 +3194,25 @@ typedef struct nghttp2_hd_deflater nghttp2_hd_deflater;
int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr, int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
size_t deflate_hd_table_bufsize_max); 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 * @function
* *
@ -3176,6 +3310,24 @@ typedef struct nghttp2_hd_inflater nghttp2_hd_inflater;
*/ */
int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr); 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 * @function
* *

View File

@ -36,21 +36,21 @@ void nghttp2_buf_init(nghttp2_buf *buf) {
buf->mark = NULL; 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); 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) { if (buf == NULL) {
return; return;
} }
free(buf->begin); nghttp2_mem_free(mem, buf->begin);
buf->begin = NULL; 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; uint8_t *ptr;
size_t cap; 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); 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) { if (ptr == NULL) {
return NGHTTP2_ERR_NOMEM; 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; 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; int rv;
*chain = malloc(sizeof(nghttp2_buf_chain)); *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
if (*chain == NULL) { if (*chain == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
(*chain)->next = NULL; (*chain)->next = NULL;
rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length); rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
if (rv != 0) { if (rv != 0) {
free(*chain); nghttp2_mem_free(mem, *chain);
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
return 0; return 0;
} }
static void buf_chain_del(nghttp2_buf_chain *chain) { static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
nghttp2_buf_free(&chain->buf); nghttp2_buf_free(&chain->buf, mem);
free(chain); nghttp2_mem_free(mem, chain);
} }
int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
size_t max_chunk) { nghttp2_mem *mem) {
return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0); return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
} }
int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length, 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) {
return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset); return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
mem);
} }
int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length, 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; int rv;
nghttp2_buf_chain *chain; nghttp2_buf_chain *chain;
@ -128,11 +131,12 @@ int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
rv = buf_chain_new(&chain, chunk_length); rv = buf_chain_new(&chain, chunk_length, mem);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
bufs->mem = mem;
bufs->offset = offset; bufs->offset = offset;
bufs->head = chain; bufs->head = chain;
@ -156,7 +160,7 @@ int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
rv = buf_chain_new(&chain, chunk_length); rv = buf_chain_new(&chain, chunk_length, bufs->mem);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
@ -184,7 +188,7 @@ void nghttp2_bufs_free(nghttp2_bufs *bufs) {
for (chain = bufs->head; chain;) { for (chain = bufs->head; chain;) {
next_chain = chain->next; next_chain = chain->next;
buf_chain_del(chain); buf_chain_del(chain, bufs->mem);
chain = next_chain; chain = next_chain;
} }
@ -192,10 +196,11 @@ void nghttp2_bufs_free(nghttp2_bufs *bufs) {
bufs->head = NULL; 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; nghttp2_buf_chain *chain;
chain = malloc(sizeof(nghttp2_buf_chain)); chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
if (chain == NULL) { if (chain == NULL) {
return NGHTTP2_ERR_NOMEM; 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); nghttp2_buf_wrap_init(&chain->buf, begin, len);
bufs->mem = mem;
bufs->offset = 0; bufs->offset = 0;
bufs->head = chain; bufs->head = chain;
@ -222,7 +228,7 @@ void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
return; return;
} }
free(bufs->head); nghttp2_mem_free(bufs->mem, bufs->head);
bufs->head = NULL; bufs->head = NULL;
} }
@ -270,7 +276,7 @@ static int bufs_alloc_chain(nghttp2_bufs *bufs) {
return NGHTTP2_ERR_BUFFER_ERROR; 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) { if (rv != 0) {
return rv; return rv;
} }
@ -407,7 +413,7 @@ ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
if (!len) { if (!len) {
res = NULL; res = NULL;
} else { } else {
res = malloc(len); res = nghttp2_mem_malloc(bufs->mem, len);
if (res == NULL) { if (res == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
@ -456,7 +462,7 @@ void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
for (ci = chain; ci;) { for (ci = chain; ci;) {
chain = ci->next; chain = ci->next;
buf_chain_del(ci); buf_chain_del(ci, bufs->mem);
ci = chain; ci = chain;
} }

View File

@ -32,6 +32,7 @@
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "nghttp2_int.h" #include "nghttp2_int.h"
#include "nghttp2_mem.h"
typedef struct { typedef struct {
/* This points to the beginning of the buffer. The effective range /* 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 * NGHTTP2_ERR_NOMEM
* Out of memory * 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|. * 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 * Extends buffer so that nghttp2_buf_cap() returns at least
@ -104,7 +105,7 @@ void nghttp2_buf_free(nghttp2_buf *buf);
* NGHTTP2_ERR_NOMEM * NGHTTP2_ERR_NOMEM
* Out of memory * 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. * Resets pos, last, mark member of |buf| to buf->begin.
@ -135,6 +136,8 @@ typedef struct {
nghttp2_buf_chain *head; nghttp2_buf_chain *head;
/* Buffer pointer where write occurs. */ /* Buffer pointer where write occurs. */
nghttp2_buf_chain *cur; nghttp2_buf_chain *cur;
/* Memory allocator */
nghttp2_mem *mem;
/* The buffer capacity of each buf */ /* The buffer capacity of each buf */
size_t chunk_length; size_t chunk_length;
/* The maximum number of nghttp2_buf_chain */ /* 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 * This is the same as calling nghttp2_bufs_init2 with the given
* arguments and offset = 0. * arguments and offset = 0.
*/ */
int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
size_t max_chunk); nghttp2_mem *mem);
/* /*
* This is the same as calling nghttp2_bufs_init3 with the given * This is the same as calling nghttp2_bufs_init3 with the given
* arguments and chunk_keep = max_chunk. * arguments and chunk_keep = max_chunk.
*/ */
int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length, 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 * 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. * long.
*/ */
int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length, 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|. * Frees any related resources to the |bufs|.
@ -203,7 +207,8 @@ void nghttp2_bufs_free(nghttp2_bufs *bufs);
* NGHTTP2_ERR_NOMEM * NGHTTP2_ERR_NOMEM
* Out of memory. * 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 * Frees any related resource to the |bufs|. This function does not

View File

@ -75,8 +75,8 @@ void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
} }
} }
void nghttp2_frame_headers_free(nghttp2_headers *frame) { void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
nghttp2_nv_array_del(frame->nva); nghttp2_nv_array_del(frame->nva, mem);
} }
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id, 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; 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, void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
int32_t stream_id, int32_t stream_id,
@ -119,8 +121,9 @@ void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
frame->reserved = 0; frame->reserved = 0;
} }
void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame) { void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
nghttp2_nv_array_del(frame->nva); nghttp2_mem *mem) {
nghttp2_nv_array_del(frame->nva, mem);
} }
void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags, 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; frame->reserved = 0;
} }
void nghttp2_frame_goaway_free(nghttp2_goaway *frame) { void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
free(frame->opaque_data); nghttp2_mem_free(mem, frame->opaque_data);
} }
void nghttp2_frame_window_update_init(nghttp2_window_update *frame, 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, int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
nghttp2_settings_entry *iv, nghttp2_settings_entry *iv,
size_t niv) { size_t niv, nghttp2_mem *mem) {
size_t payloadlen = niv * sizeof(nghttp2_settings_entry); size_t payloadlen = niv * sizeof(nghttp2_settings_entry);
if (niv == 0) { if (niv == 0) {
frame->iv = NULL; frame->iv = NULL;
} else { } else {
frame->iv = malloc(payloadlen); frame->iv = nghttp2_mem_malloc(mem, payloadlen);
if (frame->iv == NULL) { if (frame->iv == NULL) {
return NGHTTP2_ERR_NOMEM; 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, int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
size_t *niv_ptr, size_t *niv_ptr,
const uint8_t *payload, const uint8_t *payload,
size_t payloadlen) { size_t payloadlen,
nghttp2_mem *mem) {
size_t i; size_t i;
*niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH; *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; 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) { if (*iv_ptr == NULL) {
return NGHTTP2_ERR_NOMEM; 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, int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
const uint8_t *payload, const uint8_t *payload,
size_t payloadlen) { size_t payloadlen, nghttp2_mem *mem) {
uint8_t *var_gift_payload; uint8_t *var_gift_payload;
size_t var_gift_payloadlen; size_t var_gift_payloadlen;
@ -605,7 +610,7 @@ int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
if (!var_gift_payloadlen) { if (!var_gift_payloadlen) {
var_gift_payload = NULL; var_gift_payload = NULL;
} else { } else {
var_gift_payload = malloc(var_gift_payloadlen); var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
if (var_gift_payload == NULL) { if (var_gift_payload == NULL) {
return NGHTTP2_ERR_NOMEM; 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, 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; nghttp2_settings_entry *iv_copy;
size_t len = niv * sizeof(nghttp2_settings_entry); 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; return NULL;
} }
iv_copy = malloc(len); iv_copy = nghttp2_mem_malloc(mem, len);
if (iv_copy == NULL) { if (iv_copy == NULL) {
return 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; 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, static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
size_t blen) { 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, 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; size_t i;
uint8_t *data; uint8_t *data;
size_t buflen = 0; 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; buflen += sizeof(nghttp2_nv) * nvlen;
*nva_ptr = malloc(buflen); *nva_ptr = nghttp2_mem_malloc(mem, buflen);
if (*nva_ptr == NULL) { if (*nva_ptr == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;

View File

@ -231,7 +231,7 @@ void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
*/ */
int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame, int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
nghttp2_settings_entry *iv, nghttp2_settings_entry *iv,
size_t niv); size_t niv, nghttp2_mem *mem);
/* /*
* Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are * 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, int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
size_t *niv_ptr, size_t *niv_ptr,
const uint8_t *payload, 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 * 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, int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
const uint8_t *payload, 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 * 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, const nghttp2_priority_spec *pri_spec,
nghttp2_nv *nva, size_t nvlen); 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, void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
const nghttp2_priority_spec *pri_spec); 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, int32_t promised_stream_id,
nghttp2_nv *nva, size_t nvlen); 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 * 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, void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
nghttp2_settings_entry *iv, size_t niv); 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 * 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, uint32_t error_code, uint8_t *opaque_data,
size_t opaque_data_len); 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, void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
uint8_t flags, int32_t stream_id, uint8_t flags, int32_t stream_id,
@ -461,7 +462,7 @@ void nghttp2_frame_data_free(nghttp2_data *frame);
* it succeeds, or NULL. * it succeeds, or NULL.
*/ */
nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv, 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 * 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. * Out of memory.
*/ */
int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva, 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 * 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|. * 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 * Checks that the |iv|, which includes |niv| entries, does not have

View File

@ -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, int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name,
size_t namelen, uint8_t *value, size_t valuelen, 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; int rv = 0;
/* Since nghttp2_hd_entry is used for indexing, ent->nv.flags always /* 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 */ /* We should not allow empty header field name */
ent->nv.name = NULL; ent->nv.name = NULL;
} else { } else {
ent->nv.name = nghttp2_memdup(name, namelen); ent->nv.name = nghttp2_memdup(name, namelen, mem);
if (ent->nv.name == NULL) { if (ent->nv.name == NULL) {
rv = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail; goto fail;
@ -168,7 +169,7 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name,
if (valuelen == 0) { if (valuelen == 0) {
ent->nv.value = NULL; ent->nv.value = NULL;
} else { } else {
ent->nv.value = nghttp2_memdup(value, valuelen); ent->nv.value = nghttp2_memdup(value, valuelen, mem);
if (ent->nv.value == NULL) { if (ent->nv.value == NULL) {
rv = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail2; goto fail2;
@ -189,27 +190,28 @@ int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name,
fail2: fail2:
if (flags & NGHTTP2_HD_FLAG_NAME_ALLOC) { if (flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
free(ent->nv.name); nghttp2_mem_free(mem, ent->nv.name);
} }
fail: fail:
return rv; 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); assert(ent->ref == 0);
if (ent->flags & NGHTTP2_HD_FLAG_NAME_ALLOC) { 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) { 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; size_t size;
for (size = 1; size < bufsize; size <<= 1) 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) { if (ringbuf->buffer == NULL) {
return NGHTTP2_ERR_NOMEM; 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]; 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 i;
size_t size; size_t size;
nghttp2_hd_entry **buffer; 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) 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) { if (buffer == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
for (i = 0; i < ringbuf->len; ++i) { for (i = 0; i < ringbuf->len; ++i) {
buffer[i] = hd_ringbuf_get(ringbuf, i); buffer[i] = hd_ringbuf_get(ringbuf, i);
} }
free(ringbuf->buffer); nghttp2_mem_free(mem, ringbuf->buffer);
ringbuf->buffer = buffer; ringbuf->buffer = buffer;
ringbuf->mask = size - 1; ringbuf->mask = size - 1;
ringbuf->first = 0; ringbuf->first = 0;
return 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; size_t i;
if (ringbuf == NULL) { if (ringbuf == NULL) {
return; return;
@ -257,17 +260,17 @@ static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf) {
for (i = 0; i < ringbuf->len; ++i) { for (i = 0; i < ringbuf->len; ++i) {
nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i); nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
--ent->ref; --ent->ref;
nghttp2_hd_entry_free(ent); nghttp2_hd_entry_free(ent, mem);
free(ent); nghttp2_mem_free(mem, ent);
} }
free(ringbuf->buffer); nghttp2_mem_free(mem, ringbuf->buffer);
} }
static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf, static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
nghttp2_hd_entry *ent) { nghttp2_hd_entry *ent, nghttp2_mem *mem) {
int rv; int rv;
rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1); rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1, mem);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
@ -284,12 +287,14 @@ static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf) {
--ringbuf->len; --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; int rv;
context->mem = mem;
context->bad = 0; context->bad = 0;
context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE; context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
rv = hd_ringbuf_init(&context->hd_table, context->hd_table_bufsize_max / rv = hd_ringbuf_init(&context->hd_table, context->hd_table_bufsize_max /
NGHTTP2_HD_ENTRY_OVERHEAD); NGHTTP2_HD_ENTRY_OVERHEAD,
mem);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
@ -299,18 +304,19 @@ static int hd_context_init(nghttp2_hd_context *context) {
} }
static void hd_context_free(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) { int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem) {
return nghttp2_hd_deflate_init2(deflater, return nghttp2_hd_deflate_init2(
NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE); deflater, NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE, mem);
} }
int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater, 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; int rv;
rv = hd_context_init(&deflater->ctx); rv = hd_context_init(&deflater->ctx, mem);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
@ -328,10 +334,10 @@ int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
return 0; 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; int rv;
rv = hd_context_init(&inflater->ctx); rv = hd_context_init(&inflater->ctx, mem);
if (rv != 0) { if (rv != 0) {
goto fail; goto fail;
} }
@ -344,7 +350,8 @@ int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater) {
inflater->opcode = NGHTTP2_HD_OPCODE_NONE; inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
inflater->state = NGHTTP2_HD_STATE_OPCODE; 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) { if (rv != 0) {
goto nvbufs_fail; goto nvbufs_fail;
@ -367,15 +374,18 @@ fail:
} }
static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater) { 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) {
if (inflater->ent_keep->ref == 0) { if (inflater->ent_keep->ref == 0) {
nghttp2_hd_entry_free(inflater->ent_keep); nghttp2_hd_entry_free(inflater->ent_keep, mem);
free(inflater->ent_keep); nghttp2_mem_free(mem, inflater->ent_keep);
} }
inflater->ent_keep = NULL; inflater->ent_keep = NULL;
} }
free(inflater->nv_keep); nghttp2_mem_free(mem, inflater->nv_keep);
inflater->nv_keep = NULL; inflater->nv_keep = NULL;
} }
@ -727,7 +737,9 @@ static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context,
int rv; int rv;
nghttp2_hd_entry *new_ent; nghttp2_hd_entry *new_ent;
size_t room; size_t room;
nghttp2_mem *mem;
mem = context->mem;
room = entry_room(nv->namelen, nv->valuelen); room = entry_room(nv->namelen, nv->valuelen);
while (context->hd_table_bufsize + room > context->hd_table_bufsize_max && 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")); DEBUGF(fprintf(stderr, "\n"));
hd_ringbuf_pop_back(&context->hd_table); hd_ringbuf_pop_back(&context->hd_table);
if (--ent->ref == 0) { if (--ent->ref == 0) {
nghttp2_hd_entry_free(ent); nghttp2_hd_entry_free(ent, mem);
free(ent); 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) { if (new_ent == NULL) {
return NULL; return NULL;
} }
rv = nghttp2_hd_entry_init(new_ent, entry_flags, nv->name, nv->namelen, 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) { if (rv != 0) {
free(new_ent); nghttp2_mem_free(mem, new_ent);
return NULL; return NULL;
} }
@ -767,7 +780,7 @@ static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context,
immediately evicted. */ immediately evicted. */
--new_ent->ref; --new_ent->ref;
} else { } 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) { if (rv != 0) {
--new_ent->ref; --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.value = NULL;
new_ent->nv.valuelen = 0; new_ent->nv.valuelen = 0;
nghttp2_hd_entry_free(new_ent); nghttp2_hd_entry_free(new_ent, mem);
free(new_ent); nghttp2_mem_free(mem, new_ent);
return NULL; 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) { 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 && while (context->hd_table_bufsize > context->hd_table_bufsize_max &&
context->hd_table.len > 0) { context->hd_table.len > 0) {
size_t idx = context->hd_table.len - 1; 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); context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
hd_ringbuf_pop_back(&context->hd_table); hd_ringbuf_pop_back(&context->hd_table);
if (--ent->ref == 0) { if (--ent->ref == 0) {
nghttp2_hd_entry_free(ent); nghttp2_hd_entry_free(ent, mem);
free(ent); nghttp2_mem_free(mem, ent);
} }
} }
} }
@ -949,6 +966,7 @@ static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
int incidx = 0; int incidx = 0;
uint32_t name_hash = hash(nv->name, nv->namelen); uint32_t name_hash = hash(nv->name, nv->namelen);
uint32_t value_hash = hash(nv->value, nv->valuelen); uint32_t value_hash = hash(nv->value, nv->valuelen);
nghttp2_mem *mem;
DEBUGF(fprintf(stderr, "deflatehd: deflating ")); DEBUGF(fprintf(stderr, "deflatehd: deflating "));
DEBUGF(fwrite(nv->name, nv->namelen, 1, stderr)); 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(fwrite(nv->value, nv->valuelen, 1, stderr));
DEBUGF(fprintf(stderr, "\n")); DEBUGF(fprintf(stderr, "\n"));
mem = deflater->ctx.mem;
res = search_hd_table(&deflater->ctx, nv, name_hash, value_hash); res = search_hd_table(&deflater->ctx, nv, name_hash, value_hash);
idx = res.index; idx = res.index;
@ -994,8 +1014,8 @@ static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
return NGHTTP2_ERR_HEADER_COMP; return NGHTTP2_ERR_HEADER_COMP;
} }
if (new_ent->ref == 0) { if (new_ent->ref == 0) {
nghttp2_hd_entry_free(new_ent); nghttp2_hd_entry_free(new_ent, mem);
free(new_ent); nghttp2_mem_free(mem, new_ent);
} }
incidx = 1; incidx = 1;
} }
@ -1068,8 +1088,11 @@ ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf,
size_t nvlen) { size_t nvlen) {
nghttp2_bufs bufs; nghttp2_bufs bufs;
int rv; 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) { if (rv != 0) {
return rv; 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, int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
size_t deflate_hd_table_bufsize_max) { 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; int rv;
nghttp2_hd_deflater *deflater; 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) { if (deflater == NULL) {
return NGHTTP2_ERR_NOMEM; 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) { if (rv != 0) {
free(deflater); nghttp2_mem_free(mem, deflater);
return rv; 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) { void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater) {
nghttp2_mem *mem;
mem = deflater->ctx.mem;
nghttp2_hd_deflate_free(deflater); nghttp2_hd_deflate_free(deflater);
free(deflater); nghttp2_mem_free(mem, deflater);
} }
static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater, 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) { nghttp2_nv *nv_out) {
int rv; int rv;
nghttp2_nv nv; nghttp2_nv nv;
nghttp2_mem *mem;
mem = inflater->ctx.mem;
rv = hd_inflate_remove_bufs(inflater, &nv, 0 /* name and value */); rv = hd_inflate_remove_bufs(inflater, &nv, 0 /* name and value */);
if (rv != 0) { if (rv != 0) {
@ -1378,7 +1419,7 @@ static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater,
return 0; return 0;
} }
free(nv.name); nghttp2_mem_free(mem, nv.name);
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
@ -1408,6 +1449,9 @@ static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
int rv; int rv;
nghttp2_nv nv; nghttp2_nv nv;
nghttp2_hd_entry *ent_name; nghttp2_hd_entry *ent_name;
nghttp2_mem *mem;
mem = inflater->ctx.mem;
rv = hd_inflate_remove_bufs(inflater, &nv, 1 /* value only */); rv = hd_inflate_remove_bufs(inflater, &nv, 1 /* value only */);
if (rv != 0) { if (rv != 0) {
@ -1444,8 +1488,8 @@ static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
hash(nv.value, nv.valuelen), ent_flags); hash(nv.value, nv.valuelen), ent_flags);
if (!static_name && --ent_name->ref == 0) { if (!static_name && --ent_name->ref == 0) {
nghttp2_hd_entry_free(ent_name); nghttp2_hd_entry_free(ent_name, mem);
free(ent_name); nghttp2_mem_free(mem, ent_name);
} }
if (new_ent) { if (new_ent) {
@ -1456,7 +1500,7 @@ static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
return 0; return 0;
} }
free(nv.value); nghttp2_mem_free(mem, nv.value);
return NGHTTP2_ERR_NOMEM; 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) { 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; int rv;
nghttp2_hd_inflater *inflater; 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) { if (inflater == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
rv = nghttp2_hd_inflate_init(inflater); rv = nghttp2_hd_inflate_init(inflater, mem);
if (rv != 0) { if (rv != 0) {
free(inflater); nghttp2_mem_free(mem, inflater);
return rv; 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) { void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater) {
nghttp2_mem *mem;
mem = inflater->ctx.mem;
nghttp2_hd_inflate_free(inflater); nghttp2_hd_inflate_free(inflater);
free(inflater); nghttp2_mem_free(mem, inflater);
} }
int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx, int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx,

View File

@ -33,6 +33,7 @@
#include "nghttp2_hd_huffman.h" #include "nghttp2_hd_huffman.h"
#include "nghttp2_buf.h" #include "nghttp2_buf.h"
#include "nghttp2_mem.h"
#define NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE NGHTTP2_DEFAULT_HEADER_TABLE_SIZE #define NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE NGHTTP2_DEFAULT_HEADER_TABLE_SIZE
#define NGHTTP2_HD_ENTRY_OVERHEAD 32 #define NGHTTP2_HD_ENTRY_OVERHEAD 32
@ -109,6 +110,8 @@ typedef enum {
typedef struct { typedef struct {
/* dynamic header table */ /* dynamic header table */
nghttp2_hd_ringbuf hd_table; nghttp2_hd_ringbuf hd_table;
/* Memory allocator */
nghttp2_mem *mem;
/* Abstract buffer size of hd_table as described in the spec. This /* Abstract buffer size of hd_table as described in the spec. This
is the sum of length of name/value in hd_table + is the sum of length of name/value in hd_table +
NGHTTP2_HD_ENTRY_OVERHEAD bytes overhead per each entry. */ 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, int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name,
size_t namelen, uint8_t *value, size_t valuelen, 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. * Initializes |deflater| for deflating name/values pairs.
@ -203,7 +207,7 @@ void nghttp2_hd_entry_free(nghttp2_hd_entry *ent);
* NGHTTP2_ERR_NOMEM * NGHTTP2_ERR_NOMEM
* Out of memory. * 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. * Initializes |deflater| for deflating name/values pairs.
@ -219,7 +223,8 @@ int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater);
* Out of memory. * Out of memory.
*/ */
int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater, 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|. * Deallocates any resources allocated for |deflater|.
@ -259,7 +264,7 @@ int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
* :enum:`NGHTTP2_ERR_NOMEM` * :enum:`NGHTTP2_ERR_NOMEM`
* Out of memory. * 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|. * Deallocates any resources allocated for |inflater|.

View File

@ -51,14 +51,14 @@ uint32_t nghttp2_get_uint32(const uint8_t *data) {
return ntohl(n); 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; void *dest;
if (n == 0) { if (n == 0) {
return NULL; return NULL;
} }
dest = malloc(n); dest = nghttp2_mem_malloc(mem, n);
if (dest == NULL) { if (dest == NULL) {
return 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 */ /* Generated by gennmchartbl.py */
static int VALID_HD_NAME_CHARS[] = { static int VALID_HD_NAME_CHARS[] = {
0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */, 0 /* EOT */, 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */, 0 /* EOT */,

View File

@ -30,6 +30,7 @@
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "nghttp2_mem.h"
#define nghttp2_min(A, B) ((A) < (B) ? (A) : (B)) #define nghttp2_min(A, B) ((A) < (B) ? (A) : (B))
#define nghttp2_max(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 * NGHTTP2_ERR_NOMEM
* Out of memory. * 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); 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, int nghttp2_should_send_window_update(int32_t local_window_size,
int32_t recv_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 * Copies the buffer |src| of length |len| to the destination pointed
* by the |dest|, assuming that the |dest| is at lest |len| bytes long * by the |dest|, assuming that the |dest| is at lest |len| bytes long

View File

@ -28,9 +28,11 @@
#define INITIAL_TABLE_LENGTH 256 #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->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) { if (map->table == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
@ -40,7 +42,9 @@ int nghttp2_map_init(nghttp2_map *map) {
return 0; 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, void nghttp2_map_each_free(nghttp2_map *map,
int (*func)(nghttp2_map_entry *entry, void *ptr), 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) { static int resize(nghttp2_map *map, size_t new_tablelen) {
size_t i; size_t i;
nghttp2_map_entry **new_table; 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) { if (new_table == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
@ -125,7 +131,7 @@ static int resize(nghttp2_map *map, size_t new_tablelen) {
entry = next; entry = next;
} }
} }
free(map->table); nghttp2_mem_free(map->mem, map->table);
map->tablelen = new_tablelen; map->tablelen = new_tablelen;
map->table = new_table; map->table = new_table;

View File

@ -31,6 +31,7 @@
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "nghttp2_int.h" #include "nghttp2_int.h"
#include "nghttp2_mem.h"
/* Implementation of unordered map */ /* Implementation of unordered map */
@ -43,6 +44,7 @@ typedef struct nghttp2_map_entry {
typedef struct { typedef struct {
nghttp2_map_entry **table; nghttp2_map_entry **table;
nghttp2_mem *mem;
size_t tablelen; size_t tablelen;
size_t size; size_t size;
} nghttp2_map; } nghttp2_map;
@ -56,7 +58,7 @@ typedef struct {
* NGHTTP2_ERR_NOMEM * NGHTTP2_ERR_NOMEM
* Out of memory * 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 * Deallocates any resources allocated for |map|. The stored entries

61
lib/nghttp2_mem.c Normal file
View File

@ -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);
}

44
lib/nghttp2_mem.h Normal file
View File

@ -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 <config.h>
#endif /* HAVE_CONFIG_H */
#include <nghttp2/nghttp2.h>
/* 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 */

View File

@ -26,7 +26,7 @@
#include <assert.h> #include <assert.h>
void nghttp2_outbound_item_free(nghttp2_outbound_item *item) { void nghttp2_outbound_item_free(nghttp2_outbound_item *item, nghttp2_mem *mem) {
nghttp2_frame *frame; nghttp2_frame *frame;
if (item == NULL) { if (item == NULL) {
@ -40,7 +40,7 @@ void nghttp2_outbound_item_free(nghttp2_outbound_item *item) {
nghttp2_frame_data_free(&frame->data); nghttp2_frame_data_free(&frame->data);
break; break;
case NGHTTP2_HEADERS: case NGHTTP2_HEADERS:
nghttp2_frame_headers_free(&frame->headers); nghttp2_frame_headers_free(&frame->headers, mem);
break; break;
case NGHTTP2_PRIORITY: case NGHTTP2_PRIORITY:
nghttp2_frame_priority_free(&frame->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); nghttp2_frame_rst_stream_free(&frame->rst_stream);
break; break;
case NGHTTP2_SETTINGS: case NGHTTP2_SETTINGS:
nghttp2_frame_settings_free(&frame->settings); nghttp2_frame_settings_free(&frame->settings, mem);
break; break;
case NGHTTP2_PUSH_PROMISE: case NGHTTP2_PUSH_PROMISE:
nghttp2_frame_push_promise_free(&frame->push_promise); nghttp2_frame_push_promise_free(&frame->push_promise, mem);
break; break;
case NGHTTP2_PING: case NGHTTP2_PING:
nghttp2_frame_ping_free(&frame->ping); nghttp2_frame_ping_free(&frame->ping);
break; break;
case NGHTTP2_GOAWAY: case NGHTTP2_GOAWAY:
nghttp2_frame_goaway_free(&frame->goaway); nghttp2_frame_goaway_free(&frame->goaway, mem);
break; break;
case NGHTTP2_WINDOW_UPDATE: case NGHTTP2_WINDOW_UPDATE:
nghttp2_frame_window_update_free(&frame->window_update); nghttp2_frame_window_update_free(&frame->window_update);

View File

@ -31,6 +31,7 @@
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "nghttp2_frame.h" #include "nghttp2_frame.h"
#include "nghttp2_mem.h"
/* A bit higher weight for non-DATA frames */ /* A bit higher weight for non-DATA frames */
#define NGHTTP2_OB_EX_WEIGHT 300 #define NGHTTP2_OB_EX_WEIGHT 300
@ -101,6 +102,6 @@ typedef struct {
* Deallocates resource for |item|. If |item| is NULL, this function * Deallocates resource for |item|. If |item| is NULL, this function
* does nothing. * 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 */ #endif /* NGHTTP2_OUTBOUND_ITEM_H */

View File

@ -24,9 +24,10 @@
*/ */
#include "nghttp2_pq.h" #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->capacity = 128;
pq->q = malloc(pq->capacity * sizeof(void *)); pq->q = nghttp2_mem_malloc(mem, pq->capacity * sizeof(void *));
if (pq->q == NULL) { if (pq->q == NULL) {
return NGHTTP2_ERR_NOMEM; 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) { void nghttp2_pq_free(nghttp2_pq *pq) {
free(pq->q); nghttp2_mem_free(pq->mem, pq->q);
pq->q = NULL; 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) { int nghttp2_pq_push(nghttp2_pq *pq, void *item) {
if (pq->capacity <= pq->length) { if (pq->capacity <= pq->length) {
void *nq; 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) { if (nq == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }

View File

@ -31,12 +31,15 @@
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "nghttp2_int.h" #include "nghttp2_int.h"
#include "nghttp2_mem.h"
/* Implementation of priority queue */ /* Implementation of priority queue */
typedef struct { typedef struct {
/* The pointer to the pointer to the item stored */ /* The pointer to the pointer to the item stored */
void **q; void **q;
/* Memory allocator */
nghttp2_mem *mem;
/* The number of items sotred */ /* The number of items sotred */
size_t length; size_t length;
/* The maximum number of items this pq can store. This is /* The maximum number of items this pq can store. This is
@ -55,7 +58,7 @@ typedef struct {
* NGHTTP2_ERR_NOMEM * NGHTTP2_ERR_NOMEM
* Out of memory. * 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 * Deallocates any resources allocated for |pq|. The stored items are

View File

@ -218,12 +218,13 @@ static int outbound_item_compar(const void *lhsx, const void *rhsx) {
static void session_inbound_frame_reset(nghttp2_session *session) { static void session_inbound_frame_reset(nghttp2_session *session) {
nghttp2_inbound_frame *iframe = &session->iframe; nghttp2_inbound_frame *iframe = &session->iframe;
nghttp2_mem *mem = &session->mem;
/* A bit risky code, since if this function is called from /* A bit risky code, since if this function is called from
nghttp2_session_new(), we rely on the fact that nghttp2_session_new(), we rely on the fact that
iframe->frame.hd.type is 0, so that no free is performed. */ iframe->frame.hd.type is 0, so that no free is performed. */
switch (iframe->frame.hd.type) { switch (iframe->frame.hd.type) {
case NGHTTP2_HEADERS: case NGHTTP2_HEADERS:
nghttp2_frame_headers_free(&iframe->frame.headers); nghttp2_frame_headers_free(&iframe->frame.headers, mem);
break; break;
case NGHTTP2_PRIORITY: case NGHTTP2_PRIORITY:
nghttp2_frame_priority_free(&iframe->frame.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); nghttp2_frame_rst_stream_free(&iframe->frame.rst_stream);
break; break;
case NGHTTP2_SETTINGS: case NGHTTP2_SETTINGS:
nghttp2_frame_settings_free(&iframe->frame.settings); nghttp2_frame_settings_free(&iframe->frame.settings, mem);
break; break;
case NGHTTP2_PUSH_PROMISE: case NGHTTP2_PUSH_PROMISE:
nghttp2_frame_push_promise_free(&iframe->frame.push_promise); nghttp2_frame_push_promise_free(&iframe->frame.push_promise, mem);
break; break;
case NGHTTP2_PING: case NGHTTP2_PING:
nghttp2_frame_ping_free(&iframe->frame.ping); nghttp2_frame_ping_free(&iframe->frame.ping);
break; break;
case NGHTTP2_GOAWAY: case NGHTTP2_GOAWAY:
nghttp2_frame_goaway_free(&iframe->frame.goaway); nghttp2_frame_goaway_free(&iframe->frame.goaway, mem);
break; break;
case NGHTTP2_WINDOW_UPDATE: case NGHTTP2_WINDOW_UPDATE:
nghttp2_frame_window_update_free(&iframe->frame.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)); sizeof(iframe->raw_sbuf));
iframe->sbuf.mark += NGHTTP2_FRAME_HDLEN; 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); nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
iframe->niv = 0; iframe->niv = 0;
@ -277,11 +278,12 @@ static void init_settings(nghttp2_settings_storage *settings) {
settings->max_header_list_size = UINT32_MAX; 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: reset nghttp2_active_outbound_item\n"));
DEBUGF(fprintf(stderr, "send: aob->item = %p\n", aob->item)); DEBUGF(fprintf(stderr, "send: aob->item = %p\n", aob->item));
nghttp2_outbound_item_free(aob->item); nghttp2_outbound_item_free(aob->item, mem);
free(aob->item); nghttp2_mem_free(mem, aob->item);
aob->item = NULL; aob->item = NULL;
nghttp2_bufs_reset(&aob->framebufs); nghttp2_bufs_reset(&aob->framebufs);
aob->state = NGHTTP2_OB_POP_ITEM; 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, static int session_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks, const nghttp2_session_callbacks *callbacks,
void *user_data, int server, void *user_data, int server,
const nghttp2_option *option) { const nghttp2_option *option, nghttp2_mem *mem) {
int rv; 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) { if (*session_ptr == NULL) {
rv = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail_session; goto fail_session;
} }
(*session_ptr)->mem = *mem;
/* next_stream_id is initialized in either /* next_stream_id is initialized in either
nghttp2_session_client_new2 or nghttp2_session_server_new2 */ 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) { if (rv != 0) {
goto fail_ob_pq; 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) { if (rv != 0) {
goto fail_ob_ss_pq; 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) { if (rv != 0) {
goto fail_ob_da_pq; 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) { if (rv != 0) {
goto fail_hd_deflater; 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) { if (rv != 0) {
goto fail_hd_inflater; goto fail_hd_inflater;
} }
rv = nghttp2_map_init(&(*session_ptr)->streams); rv = nghttp2_map_init(&(*session_ptr)->streams, mem);
if (rv != 0) { if (rv != 0) {
goto fail_map; goto fail_map;
} }
@ -359,12 +367,12 @@ static int session_new(nghttp2_session **session_ptr,
/* 1 for Pad Field. */ /* 1 for Pad Field. */
rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs, rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs,
NGHTTP2_FRAMEBUF_CHUNKLEN, NGHTTP2_FRAMEBUF_MAX_NUM, NGHTTP2_FRAMEBUF_CHUNKLEN, NGHTTP2_FRAMEBUF_MAX_NUM,
1, NGHTTP2_FRAME_HDLEN + 1); 1, NGHTTP2_FRAME_HDLEN + 1, mem);
if (rv != 0) { if (rv != 0) {
goto fail_aob_framebuf; 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)->remote_settings);
init_settings(&(*session_ptr)->local_settings); init_settings(&(*session_ptr)->local_settings);
@ -421,7 +429,7 @@ fail_ob_da_pq:
fail_ob_ss_pq: fail_ob_ss_pq:
nghttp2_pq_free(&(*session_ptr)->ob_pq); nghttp2_pq_free(&(*session_ptr)->ob_pq);
fail_ob_pq: fail_ob_pq:
free(*session_ptr); nghttp2_mem_free(mem, *session_ptr);
fail_session: fail_session:
return rv; return rv;
} }
@ -429,16 +437,25 @@ fail_session:
int nghttp2_session_client_new(nghttp2_session **session_ptr, int nghttp2_session_client_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks, const nghttp2_session_callbacks *callbacks,
void *user_data) { 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, int nghttp2_session_client_new2(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks, const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option) { 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; int rv;
nghttp2_session *session; 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) { if (rv != 0) {
return rv; return rv;
@ -454,16 +471,25 @@ int nghttp2_session_client_new2(nghttp2_session **session_ptr,
int nghttp2_session_server_new(nghttp2_session **session_ptr, int nghttp2_session_server_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks, const nghttp2_session_callbacks *callbacks,
void *user_data) { 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, int nghttp2_session_server_new2(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks, const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option) { 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; int rv;
nghttp2_session *session; 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) { if (rv != 0) {
return rv; return rv;
@ -480,37 +506,44 @@ static int free_streams(nghttp2_map_entry *entry, void *ptr) {
nghttp2_session *session; nghttp2_session *session;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_mem *mem;
session = (nghttp2_session *)ptr; session = (nghttp2_session *)ptr;
mem = &session->mem;
stream = (nghttp2_stream *)entry; stream = (nghttp2_stream *)entry;
item = stream->data_item; item = stream->data_item;
if (item && !item->queued && item != session->aob.item) { if (item && !item->queued && item != session->aob.item) {
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); nghttp2_mem_free(mem, item);
} }
nghttp2_stream_free(stream); nghttp2_stream_free(stream);
free(stream); nghttp2_mem_free(mem, stream);
return 0; 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)) { while (!nghttp2_pq_empty(pq)) {
nghttp2_outbound_item *item = (nghttp2_outbound_item *)nghttp2_pq_top(pq); nghttp2_outbound_item *item = (nghttp2_outbound_item *)nghttp2_pq_top(pq);
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); nghttp2_mem_free(mem, item);
nghttp2_pq_pop(pq); nghttp2_pq_pop(pq);
} }
nghttp2_pq_free(pq); nghttp2_pq_free(pq);
} }
void nghttp2_session_del(nghttp2_session *session) { void nghttp2_session_del(nghttp2_session *session) {
nghttp2_mem *mem;
if (session == NULL) { if (session == NULL) {
return; return;
} }
free(session->inflight_iv);
mem = &session->mem;
nghttp2_mem_free(mem, session->inflight_iv);
nghttp2_stream_roots_free(&session->roots); 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_each_free(&session->streams, free_streams, session);
nghttp2_map_free(&session->streams); nghttp2_map_free(&session->streams);
ob_pq_free(&session->ob_pq); ob_pq_free(&session->ob_pq, mem);
ob_pq_free(&session->ob_ss_pq); ob_pq_free(&session->ob_ss_pq, mem);
ob_pq_free(&session->ob_da_pq); ob_pq_free(&session->ob_da_pq, mem);
active_outbound_item_reset(&session->aob); active_outbound_item_reset(&session->aob, mem);
session_inbound_frame_reset(session); session_inbound_frame_reset(session);
nghttp2_hd_deflate_free(&session->hd_deflater); nghttp2_hd_deflate_free(&session->hd_deflater);
nghttp2_hd_inflate_free(&session->hd_inflater); nghttp2_hd_inflate_free(&session->hd_inflater);
nghttp2_bufs_free(&session->aob.framebufs); nghttp2_bufs_free(&session->aob.framebufs);
free(session); nghttp2_mem_free(mem, session);
} }
int int
@ -758,13 +791,15 @@ int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_mem *mem;
mem = &session->mem;
stream = nghttp2_session_get_stream(session, stream_id); stream = nghttp2_session_get_stream(session, stream_id);
if (stream && stream->state == NGHTTP2_STREAM_CLOSING) { if (stream && stream->state == NGHTTP2_STREAM_CLOSING) {
return 0; return 0;
} }
item = malloc(sizeof(nghttp2_outbound_item)); item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
if (item == NULL) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; 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); rv = nghttp2_session_add_item(session, item);
if (rv != 0) { if (rv != 0) {
nghttp2_frame_rst_stream_free(&frame->rst_stream); nghttp2_frame_rst_stream_free(&frame->rst_stream);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
return 0; 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_default;
nghttp2_priority_spec *pri_spec = pri_spec_in; nghttp2_priority_spec *pri_spec = pri_spec_in;
ssize_t num_adjust_closed = 0; ssize_t num_adjust_closed = 0;
nghttp2_mem *mem;
mem = &session->mem;
stream = nghttp2_session_get_stream_raw(session, stream_id); stream = nghttp2_session_get_stream_raw(session, stream_id);
if (stream) { if (stream) {
@ -812,7 +849,7 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
nghttp2_session_adjust_closed_stream(session, 1); nghttp2_session_adjust_closed_stream(session, 1);
} }
stream = malloc(sizeof(nghttp2_stream)); stream = nghttp2_mem_malloc(mem, sizeof(nghttp2_stream));
if (stream == NULL) { if (stream == NULL) {
return NULL; return NULL;
} }
@ -838,7 +875,7 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
if (dep_stream == NULL) { if (dep_stream == NULL) {
if (stream_alloc) { if (stream_alloc) {
free(stream); nghttp2_mem_free(mem, stream);
} }
return NULL; return NULL;
@ -862,7 +899,7 @@ nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
if (stream_alloc) { if (stream_alloc) {
rv = nghttp2_map_insert(&session->streams, &stream->map_entry); rv = nghttp2_map_insert(&session->streams, &stream->map_entry);
if (rv != 0) { if (rv != 0) {
free(stream); nghttp2_mem_free(mem, stream);
return NULL; return NULL;
} }
} }
@ -943,7 +980,9 @@ int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id,
uint32_t error_code) { uint32_t error_code) {
int rv; int rv;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_mem *mem;
mem = &session->mem;
stream = nghttp2_session_get_stream(session, stream_id); stream = nghttp2_session_get_stream(session, stream_id);
if (!stream) { 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() points to this item, let active_outbound_item_reset()
free the item. */ free the item. */
if (!item->queued && item != session->aob.item) { if (!item->queued && item != session->aob.item) {
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); 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, void nghttp2_session_destroy_stream(nghttp2_session *session,
nghttp2_stream *stream) { nghttp2_stream *stream) {
nghttp2_mem *mem;
DEBUGF(fprintf(stderr, "stream: destroy closed stream(%p)=%d\n", stream, DEBUGF(fprintf(stderr, "stream: destroy closed stream(%p)=%d\n", stream,
stream->stream_id)); stream->stream_id));
mem = &session->mem;
nghttp2_stream_dep_remove(stream); nghttp2_stream_dep_remove(stream);
nghttp2_map_remove(&session->streams, stream->stream_id); nghttp2_map_remove(&session->streams, stream->stream_id);
nghttp2_stream_free(stream); nghttp2_stream_free(stream);
free(stream); nghttp2_mem_free(mem, stream);
} }
void nghttp2_session_keep_closed_stream(nghttp2_session *session, 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 framerv = 0;
int rv; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_mem *mem;
mem = &session->mem;
frame = &item->frame; frame = &item->frame;
if (frame->hd.type != NGHTTP2_DATA) { if (frame->hd.type != NGHTTP2_DATA) {
@ -1842,7 +1887,7 @@ static int session_prep_frame(nghttp2_session *session,
} }
session->aob.item = NULL; session->aob.item = NULL;
active_outbound_item_reset(&session->aob); active_outbound_item_reset(&session->aob, mem);
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;
} }
@ -1858,7 +1903,7 @@ static int session_prep_frame(nghttp2_session *session,
} }
session->aob.item = NULL; session->aob.item = NULL;
active_outbound_item_reset(&session->aob); active_outbound_item_reset(&session->aob, mem);
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;
} }
if (framerv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { 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_outbound_item *item = aob->item;
nghttp2_bufs *framebufs = &aob->framebufs; nghttp2_bufs *framebufs = &aob->framebufs;
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_mem *mem;
mem = &session->mem;
frame = &item->frame; frame = &item->frame;
if (frame->hd.type != NGHTTP2_DATA) { if (frame->hd.type != NGHTTP2_DATA) {
@ -2284,7 +2331,7 @@ static int session_after_frame_sent(nghttp2_session *session) {
default: default:
break; break;
} }
active_outbound_item_reset(&session->aob); active_outbound_item_reset(&session->aob, mem);
return 0; return 0;
} else { } else {
nghttp2_outbound_item *next_item; 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 on_frame_send_callback, which attach data to stream. We don't
want to detach it. */ want to detach it. */
if (aux_data->eof) { if (aux_data->eof) {
active_outbound_item_reset(aob); active_outbound_item_reset(aob, mem);
return 0; 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; return 0;
} }
@ -2413,7 +2460,7 @@ static int session_after_frame_sent(nghttp2_session *session) {
} }
aob->item = NULL; aob->item = NULL;
active_outbound_item_reset(aob); active_outbound_item_reset(aob, mem);
return 0; return 0;
} }
@ -2434,7 +2481,7 @@ static int session_after_frame_sent(nghttp2_session *session) {
} }
aob->item = NULL; aob->item = NULL;
active_outbound_item_reset(aob); active_outbound_item_reset(aob, mem);
return 0; return 0;
} }
@ -2455,7 +2502,7 @@ static int session_after_frame_sent(nghttp2_session *session) {
return rv; return rv;
} }
active_outbound_item_reset(aob); active_outbound_item_reset(aob, mem);
return 0; return 0;
} }
@ -2475,7 +2522,7 @@ static int session_after_frame_sent(nghttp2_session *session) {
} }
aob->item = NULL; aob->item = NULL;
active_outbound_item_reset(&session->aob); active_outbound_item_reset(&session->aob, mem);
return 0; return 0;
} }
/* Unreachable */ /* Unreachable */
@ -2488,7 +2535,9 @@ ssize_t nghttp2_session_mem_send(nghttp2_session *session,
int rv; int rv;
nghttp2_active_outbound_item *aob; nghttp2_active_outbound_item *aob;
nghttp2_bufs *framebufs; nghttp2_bufs *framebufs;
nghttp2_mem *mem;
mem = &session->mem;
aob = &session->aob; aob = &session->aob;
framebufs = &aob->framebufs; framebufs = &aob->framebufs;
@ -2539,16 +2588,16 @@ ssize_t nghttp2_session_mem_send(nghttp2_session *session,
if (session->callbacks.on_frame_not_send_callback( if (session->callbacks.on_frame_not_send_callback(
session, frame, rv, session->user_data) != 0) { session, frame, rv, session->user_data) != 0) {
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); nghttp2_mem_free(mem, item);
return NGHTTP2_ERR_CALLBACK_FAILURE; return NGHTTP2_ERR_CALLBACK_FAILURE;
} }
} }
} }
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); nghttp2_mem_free(mem, item);
active_outbound_item_reset(aob); active_outbound_item_reset(aob, mem);
if (rv == NGHTTP2_ERR_HEADER_COMP) { if (rv == NGHTTP2_ERR_HEADER_COMP) {
/* If header compression error occurred, should terminiate /* 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) { nghttp2_frame *frame, int noack) {
int rv; int rv;
size_t i; size_t i;
nghttp2_mem *mem;
mem = &session->mem;
if (frame->hd.stream_id != 0) { if (frame->hd.stream_id != 0) {
return session_handle_invalid_connection( 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, rv = nghttp2_session_update_local_settings(session, session->inflight_iv,
session->inflight_niv); session->inflight_niv);
free(session->inflight_iv); nghttp2_mem_free(mem, session->inflight_iv);
session->inflight_iv = NULL; session->inflight_iv = NULL;
session->inflight_niv = -1; session->inflight_niv = -1;
if (rv != 0) { if (rv != 0) {
@ -3683,7 +3735,9 @@ static int session_process_settings_frame(nghttp2_session *session) {
nghttp2_frame *frame = &iframe->frame; nghttp2_frame *frame = &iframe->frame;
size_t i; size_t i;
nghttp2_settings_entry min_header_size_entry; nghttp2_settings_entry min_header_size_entry;
nghttp2_mem *mem;
mem = &session->mem;
min_header_size_entry = iframe->iv[NGHTTP2_INBOUND_NUM_IV - 1]; min_header_size_entry = iframe->iv[NGHTTP2_INBOUND_NUM_IV - 1];
if (min_header_size_entry.value < UINT32_MAX) { 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, rv = nghttp2_frame_unpack_settings_payload(&frame->settings, iframe->iv,
iframe->niv); iframe->niv, mem);
if (rv != 0) { if (rv != 0) {
assert(nghttp2_is_fatal(rv)); assert(nghttp2_is_fatal(rv));
return 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_frame_hd cont_hd;
nghttp2_stream *stream; nghttp2_stream *stream;
size_t pri_fieldlen; size_t pri_fieldlen;
nghttp2_mem *mem;
DEBUGF(fprintf(stderr, DEBUGF(fprintf(stderr,
"recv: connection recv_window_size=%d, local_window=%d\n", "recv: connection recv_window_size=%d, local_window=%d\n",
session->recv_window_size, session->local_window_size)); session->recv_window_size, session->local_window_size));
mem = &session->mem;
for (;;) { for (;;) {
switch (iframe->state) { switch (iframe->state) {
case NGHTTP2_IB_READ_CLIENT_PREFACE: 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; debuglen = iframe->frame.hd.length - 8;
if (debuglen > 0) { if (debuglen > 0) {
iframe->raw_lbuf = malloc(debuglen); iframe->raw_lbuf = nghttp2_mem_malloc(mem, debuglen);
if (iframe->raw_lbuf == NULL) { if (iframe->raw_lbuf == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
@ -5512,8 +5569,10 @@ int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
int rv; int rv;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_frame *frame; 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) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
@ -5528,7 +5587,7 @@ int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
if (rv != 0) { if (rv != 0) {
nghttp2_frame_ping_free(&frame->ping); nghttp2_frame_ping_free(&frame->ping);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
return 0; return 0;
@ -5542,6 +5601,9 @@ int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id,
nghttp2_frame *frame; nghttp2_frame *frame;
uint8_t *opaque_data_copy = NULL; uint8_t *opaque_data_copy = NULL;
nghttp2_goaway_aux_data *aux_data; nghttp2_goaway_aux_data *aux_data;
nghttp2_mem *mem;
mem = &session->mem;
if (nghttp2_session_is_my_stream_id(session, last_stream_id)) { if (nghttp2_session_is_my_stream_id(session, last_stream_id)) {
return NGHTTP2_ERR_INVALID_ARGUMENT; 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) { if (opaque_data_len + 8 > NGHTTP2_MAX_PAYLOADLEN) {
return NGHTTP2_ERR_INVALID_ARGUMENT; 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) { if (opaque_data_copy == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
memcpy(opaque_data_copy, opaque_data, opaque_data_len); 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) { if (item == NULL) {
free(opaque_data_copy); nghttp2_mem_free(mem, opaque_data_copy);
return NGHTTP2_ERR_NOMEM; 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); rv = nghttp2_session_add_item(session, item);
if (rv != 0) { if (rv != 0) {
nghttp2_frame_goaway_free(&frame->goaway); nghttp2_frame_goaway_free(&frame->goaway, mem);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
return 0; return 0;
@ -5593,8 +5655,10 @@ int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
int rv; int rv;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_frame *frame; 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) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
@ -5610,7 +5674,7 @@ int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
if (rv != 0) { if (rv != 0) {
nghttp2_frame_window_update_free(&frame->window_update); nghttp2_frame_window_update_free(&frame->window_update);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
return 0; return 0;
@ -5623,6 +5687,9 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
nghttp2_settings_entry *iv_copy; nghttp2_settings_entry *iv_copy;
size_t i; size_t i;
int rv; int rv;
nghttp2_mem *mem;
mem = &session->mem;
if (flags & NGHTTP2_FLAG_ACK) { if (flags & NGHTTP2_FLAG_ACK) {
if (niv != 0) { if (niv != 0) {
@ -5636,15 +5703,15 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
item = malloc(sizeof(nghttp2_outbound_item)); item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
if (item == NULL) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
if (niv > 0) { if (niv > 0) {
iv_copy = nghttp2_frame_iv_copy(iv, niv); iv_copy = nghttp2_frame_iv_copy(iv, niv, mem);
if (iv_copy == NULL) { if (iv_copy == NULL) {
free(item); nghttp2_mem_free(mem, item);
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
} else { } else {
@ -5653,11 +5720,11 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
if ((flags & NGHTTP2_FLAG_ACK) == 0) { if ((flags & NGHTTP2_FLAG_ACK) == 0) {
if (niv > 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) { if (session->inflight_iv == NULL) {
free(iv_copy); nghttp2_mem_free(mem, iv_copy);
free(item); nghttp2_mem_free(mem, item);
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
} else { } else {
@ -5678,13 +5745,13 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
assert(nghttp2_is_fatal(rv)); assert(nghttp2_is_fatal(rv));
if ((flags & NGHTTP2_FLAG_ACK) == 0) { if ((flags & NGHTTP2_FLAG_ACK) == 0) {
free(session->inflight_iv); nghttp2_mem_free(mem, session->inflight_iv);
session->inflight_iv = NULL; session->inflight_iv = NULL;
session->inflight_niv = -1; session->inflight_niv = -1;
} }
nghttp2_frame_settings_free(&frame->settings); nghttp2_frame_settings_free(&frame->settings, mem);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
@ -5950,6 +6017,9 @@ int nghttp2_session_upgrade(nghttp2_session *session,
size_t niv; size_t niv;
int rv; int rv;
nghttp2_priority_spec pri_spec; nghttp2_priority_spec pri_spec;
nghttp2_mem *mem;
mem = &session->mem;
if ((!session->server && session->next_stream_id != 1) || if ((!session->server && session->next_stream_id != 1) ||
(session->server && session->last_recv_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; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload, rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload,
settings_payloadlen); settings_payloadlen, mem);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
@ -5973,7 +6043,7 @@ int nghttp2_session_upgrade(nghttp2_session *session,
} else { } else {
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv); rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
} }
free(iv); nghttp2_mem_free(mem, iv);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }

View File

@ -39,6 +39,7 @@
#include "nghttp2_int.h" #include "nghttp2_int.h"
#include "nghttp2_buf.h" #include "nghttp2_buf.h"
#include "nghttp2_callbacks.h" #include "nghttp2_callbacks.h"
#include "nghttp2_mem.h"
/* /*
* Option flags. * Option flags.
@ -146,6 +147,8 @@ struct nghttp2_session {
nghttp2_hd_deflater hd_deflater; nghttp2_hd_deflater hd_deflater;
nghttp2_hd_inflater hd_inflater; nghttp2_hd_inflater hd_inflater;
nghttp2_session_callbacks callbacks; nghttp2_session_callbacks callbacks;
/* Memory allocator */
nghttp2_mem mem;
/* Sequence number of outbound frame to maintain the order of /* Sequence number of outbound frame to maintain the order of
enqueue if priority is equal. */ enqueue if priority is equal. */
int64_t next_seq; int64_t next_seq;

View File

@ -47,13 +47,16 @@ static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
nghttp2_outbound_item *item = NULL; nghttp2_outbound_item *item = NULL;
nghttp2_frame *frame = NULL; nghttp2_frame *frame = NULL;
nghttp2_headers_category hcat; nghttp2_headers_category hcat;
nghttp2_mem *mem;
mem = &session->mem;
if (stream_id == 0) { if (stream_id == 0) {
rv = NGHTTP2_ERR_INVALID_ARGUMENT; rv = NGHTTP2_ERR_INVALID_ARGUMENT;
goto fail; goto fail;
} }
item = malloc(sizeof(nghttp2_outbound_item)); item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
if (item == NULL) { if (item == NULL) {
rv = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail; 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); rv = nghttp2_session_add_item(session, item);
if (rv != 0) { if (rv != 0) {
nghttp2_frame_headers_free(&frame->headers); nghttp2_frame_headers_free(&frame->headers, mem);
goto fail2; goto fail2;
} }
@ -106,9 +109,9 @@ static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
fail: fail:
/* nghttp2_frame_headers_init() takes ownership of nva_copy. */ /* nghttp2_frame_headers_init() takes ownership of nva_copy. */
nghttp2_nv_array_del(nva_copy); nghttp2_nv_array_del(nva_copy, mem);
fail2: fail2:
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
@ -131,6 +134,9 @@ static int32_t submit_headers_shared_nva(nghttp2_session *session,
int rv; int rv;
nghttp2_nv *nva_copy; nghttp2_nv *nva_copy;
nghttp2_priority_spec copy_pri_spec; nghttp2_priority_spec copy_pri_spec;
nghttp2_mem *mem;
mem = &session->mem;
if (pri_spec) { if (pri_spec) {
copy_pri_spec = *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(&copy_pri_spec); nghttp2_priority_spec_default_init(&copy_pri_spec);
} }
rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen); rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
if (rv < 0) { if (rv < 0) {
return rv; return rv;
} }
@ -178,6 +184,9 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_,
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_priority_spec copy_pri_spec; nghttp2_priority_spec copy_pri_spec;
nghttp2_mem *mem;
mem = &session->mem;
if (stream_id == 0 || pri_spec == NULL) { if (stream_id == 0 || pri_spec == NULL) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
@ -191,7 +200,7 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_,
adjust_priority_spec_weight(&copy_pri_spec); adjust_priority_spec_weight(&copy_pri_spec);
item = malloc(sizeof(nghttp2_outbound_item)); item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
if (item == NULL) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
@ -207,7 +216,7 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_,
if (rv != 0) { if (rv != 0) {
nghttp2_frame_priority_free(&frame->priority); nghttp2_frame_priority_free(&frame->priority);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
@ -246,6 +255,9 @@ int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_,
uint8_t flags_copy; uint8_t flags_copy;
int32_t promised_stream_id; int32_t promised_stream_id;
int rv; int rv;
nghttp2_mem *mem;
mem = &session->mem;
if (stream_id == 0 || nghttp2_session_is_my_stream_id(session, stream_id)) { if (stream_id == 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
return NGHTTP2_ERR_INVALID_ARGUMENT; 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; 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) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
@ -271,9 +283,9 @@ int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_,
frame = &item->frame; 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) { if (rv < 0) {
free(item); nghttp2_mem_free(mem, item);
return rv; 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); rv = nghttp2_session_add_item(session, item);
if (rv != 0) { if (rv != 0) {
nghttp2_frame_push_promise_free(&frame->push_promise); nghttp2_frame_push_promise_free(&frame->push_promise, mem);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
@ -406,12 +418,15 @@ int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_data_aux_data *aux_data; nghttp2_data_aux_data *aux_data;
uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM; uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
nghttp2_mem *mem;
mem = &session->mem;
if (stream_id == 0) { if (stream_id == 0) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
item = malloc(sizeof(nghttp2_outbound_item)); item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
if (item == NULL) { if (item == NULL) {
return NGHTTP2_ERR_NOMEM; 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); rv = nghttp2_session_add_item(session, item);
if (rv != 0) { if (rv != 0) {
nghttp2_frame_data_free(&frame->data); nghttp2_frame_data_free(&frame->data);
free(item); nghttp2_mem_free(mem, item);
return rv; return rv;
} }
return 0; return 0;

View File

@ -312,14 +312,6 @@ cdef extern from 'nghttp2/nghttp2.h':
int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) 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': cdef extern from 'nghttp2_hd.h':
# This is macro # This is macro
@ -347,15 +339,3 @@ cdef extern from 'nghttp2_hd.h':
nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context, nghttp2_hd_entry* nghttp2_hd_table_get(nghttp2_hd_context *context,
size_t index) 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)

View File

@ -120,7 +120,7 @@ static void deflate_hd(nghttp2_hd_deflater *deflater,
ssize_t rv; ssize_t rv;
nghttp2_bufs bufs; 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(), rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, (nghttp2_nv *)nva.data(),
nva.size()); 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) { 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); nghttp2_hd_deflate_change_table_size(deflater, config.table_size);
} }
static void deinit_deflater(nghttp2_hd_deflater *deflater) { static void deinit_deflater(nghttp2_hd_deflater *deflater) {
nghttp2_hd_deflate_free(deflater); nghttp2_hd_deflate_del(deflater);
} }
static int perform(void) { static int perform(void) {
json_error_t error; json_error_t error;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater *deflater = NULL;
auto json = json_loadf(stdin, 0, &error); auto json = json_loadf(stdin, 0, &error);
@ -218,7 +218,7 @@ static int perform(void) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
init_deflater(&deflater); init_deflater(deflater);
output_json_header(); output_json_header();
auto len = json_array_size(cases); 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); fprintf(stderr, "Unexpected JSON type at %zu. It should be object.\n", i);
continue; continue;
} }
if (deflate_hd_json(obj, &deflater, i) != 0) { if (deflate_hd_json(obj, deflater, i) != 0) {
continue; continue;
} }
if (i + 1 < len) { if (i + 1 < len) {
@ -236,7 +236,7 @@ static int perform(void) {
} }
} }
output_json_footer(); output_json_footer();
deinit_deflater(&deflater); deinit_deflater(deflater);
json_decref(json); json_decref(json);
return 0; return 0;
} }
@ -244,8 +244,8 @@ static int perform(void) {
static int perform_from_http1text(void) { static int perform_from_http1text(void) {
char line[1 << 14]; char line[1 << 14];
int seq = 0; int seq = 0;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater *deflater = NULL;
init_deflater(&deflater); init_deflater(deflater);
output_json_header(); output_json_header();
for (;;) { for (;;) {
std::vector<nghttp2_nv> nva; std::vector<nghttp2_nv> nva;
@ -292,7 +292,7 @@ static int perform_from_http1text(void) {
if (seq > 0) { if (seq > 0) {
printf(",\n"); printf(",\n");
} }
deflate_hd(&deflater, nva, inputlen, seq); deflate_hd(deflater, nva, inputlen, seq);
} }
for (auto &nv : nva) { for (auto &nv : nva) {
@ -305,7 +305,7 @@ static int perform_from_http1text(void) {
++seq; ++seq;
} }
output_json_footer(); output_json_footer();
deinit_deflater(&deflater); deinit_deflater(deflater);
return 0; return 0;
} }

View File

@ -159,7 +159,7 @@ static int inflate_hd(json_t *obj, nghttp2_hd_inflater *inflater, int seq) {
} }
static int perform(void) { static int perform(void) {
nghttp2_hd_inflater inflater; nghttp2_hd_inflater *inflater = NULL;
json_error_t error; json_error_t error;
auto json = json_loadf(stdin, 0, &error); auto json = json_loadf(stdin, 0, &error);
@ -181,7 +181,7 @@ static int perform(void) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_new(&inflater);
output_json_header(); output_json_header();
auto len = json_array_size(cases); 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); fprintf(stderr, "Unexpected JSON type at %zu. It should be object.\n", i);
continue; continue;
} }
if (inflate_hd(obj, &inflater, i) != 0) { if (inflate_hd(obj, inflater, i) != 0) {
continue; continue;
} }
if (i + 1 < len) { if (i + 1 < len) {
@ -199,7 +199,7 @@ static int perform(void) {
} }
} }
output_json_footer(); output_json_footer();
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_del(inflater);
json_decref(json); json_decref(json);
return 0; return 0;

View File

@ -33,8 +33,11 @@ void test_nghttp2_bufs_add(void) {
int rv; int rv;
nghttp2_bufs bufs; nghttp2_bufs bufs;
uint8_t data[2048]; 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(0 == rv);
CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last); CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last);
@ -64,8 +67,11 @@ void test_nghttp2_bufs_addb(void) {
int rv; int rv;
nghttp2_bufs bufs; nghttp2_bufs bufs;
ssize_t i; 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); CU_ASSERT(0 == rv);
rv = nghttp2_bufs_addb(&bufs, 14); rv = nghttp2_bufs_addb(&bufs, 14);
@ -126,8 +132,11 @@ void test_nghttp2_bufs_addb(void) {
void test_nghttp2_bufs_orb(void) { void test_nghttp2_bufs_orb(void) {
int rv; int rv;
nghttp2_bufs bufs; 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); CU_ASSERT(0 == rv);
*(bufs.cur->buf.last) = 0; *(bufs.cur->buf.last) = 0;
@ -161,8 +170,11 @@ void test_nghttp2_bufs_remove(void) {
int i; int i;
uint8_t *out; uint8_t *out;
ssize_t outlen; 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); CU_ASSERT(0 == rv);
nghttp2_buf_shift_right(&bufs.cur->buf, 10); nghttp2_buf_shift_right(&bufs.cur->buf, 10);
@ -198,8 +210,11 @@ void test_nghttp2_bufs_reset(void) {
nghttp2_bufs bufs; nghttp2_bufs bufs;
nghttp2_buf_chain *ci; nghttp2_buf_chain *ci;
ssize_t offset = 9; 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); CU_ASSERT(0 == rv);
rv = nghttp2_bufs_add(&bufs, "foo", 3); rv = nghttp2_bufs_add(&bufs, "foo", 3);
@ -232,8 +247,11 @@ void test_nghttp2_bufs_advance(void) {
int rv; int rv;
nghttp2_bufs bufs; nghttp2_bufs bufs;
int i; 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); CU_ASSERT(0 == rv);
for (i = 0; i < 2; ++i) { for (i = 0; i < 2; ++i) {
@ -250,8 +268,11 @@ void test_nghttp2_bufs_advance(void) {
void test_nghttp2_bufs_next_present(void) { void test_nghttp2_bufs_next_present(void) {
int rv; int rv;
nghttp2_bufs bufs; 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 == rv);
CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs)); 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) { void test_nghttp2_bufs_realloc(void) {
int rv; int rv;
nghttp2_bufs bufs; 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); CU_ASSERT(0 == rv);
/* Create new buffer to see that these buffers are deallocated on /* Create new buffer to see that these buffers are deallocated on

View File

@ -79,12 +79,14 @@ void test_nghttp2_frame_pack_headers() {
nva_out out; nva_out out;
ssize_t hdblocklen; ssize_t hdblocklen;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
nva = headers(); nva = headers();
nvlen = HEADERS_LENGTH; nvlen = HEADERS_LENGTH;
@ -117,7 +119,7 @@ void test_nghttp2_frame_pack_headers() {
CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvnameeq("method", &out.nva[0]));
CU_ASSERT(nvvalueeq("GET", &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); nva_out_reset(&out);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
@ -152,12 +154,12 @@ void test_nghttp2_frame_pack_headers() {
nghttp2_nv_array_sort(out.nva, out.nvlen); nghttp2_nv_array_sort(out.nva, out.nvlen);
CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvnameeq("method", &out.nva[0]));
nghttp2_frame_headers_free(&oframe); nghttp2_frame_headers_free(&oframe, mem);
nva_out_reset(&out); nva_out_reset(&out);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_headers_free(&frame); nghttp2_frame_headers_free(&frame, mem);
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_free(&inflater);
nghttp2_hd_deflate_free(&deflater); 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 big_hdslen = ARRLEN(big_hds);
size_t i; size_t i;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
for (i = 0; i < big_hdslen; ++i) { 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; big_hds[i].flags = NGHTTP2_NV_FLAG_NONE;
} }
nghttp2_nv_array_copy(&nva, big_hds, big_hdslen); nghttp2_nv_array_copy(&nva, big_hds, big_hdslen, mem);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_frame_headers_init( nghttp2_frame_headers_init(
&frame, NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS, 1000000007, &frame, NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS, 1000000007,
NGHTTP2_HCAT_REQUEST, NULL, nva, big_hdslen); NGHTTP2_HCAT_REQUEST, NULL, nva, big_hdslen);
rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater); rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);
CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == rv); CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == rv);
nghttp2_frame_headers_free(&frame); nghttp2_frame_headers_free(&frame, mem);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
for (i = 0; i < big_hdslen; ++i) { for (i = 0; i < big_hdslen; ++i) {
free(big_hds[i].value); 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_entry iv[] = {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 256},
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384}, {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384},
{NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096}}; {NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096}};
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nghttp2_frame_settings_init(&frame, NGHTTP2_FLAG_NONE, 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); rv = nghttp2_frame_pack_settings(&bufs, &frame);
CU_ASSERT(0 == rv); CU_ASSERT(0 == rv);
@ -300,8 +306,8 @@ void test_nghttp2_frame_pack_settings() {
} }
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_settings_free(&frame); nghttp2_frame_settings_free(&frame, mem);
nghttp2_frame_settings_free(&oframe); nghttp2_frame_settings_free(&oframe, mem);
} }
void test_nghttp2_frame_pack_push_promise() { void test_nghttp2_frame_pack_push_promise() {
@ -314,12 +320,14 @@ void test_nghttp2_frame_pack_push_promise() {
nva_out out; nva_out out;
ssize_t hdblocklen; ssize_t hdblocklen;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
nva = headers(); nva = headers();
nvlen = HEADERS_LENGTH; nvlen = HEADERS_LENGTH;
@ -346,8 +354,8 @@ void test_nghttp2_frame_pack_push_promise() {
nva_out_reset(&out); nva_out_reset(&out);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_push_promise_free(&oframe); nghttp2_frame_push_promise_free(&oframe, mem);
nghttp2_frame_push_promise_free(&frame); nghttp2_frame_push_promise_free(&frame, mem);
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_free(&inflater);
nghttp2_hd_deflate_free(&deflater); nghttp2_hd_deflate_free(&deflater);
} }
@ -381,7 +389,9 @@ void test_nghttp2_frame_pack_goaway() {
size_t opaque_data_len = 16; size_t opaque_data_len = 16;
uint8_t *opaque_data = malloc(opaque_data_len); uint8_t *opaque_data = malloc(opaque_data_len);
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memcpy(opaque_data, "0123456789abcdef", opaque_data_len); 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(opaque_data_len == oframe.opaque_data_len);
CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, opaque_data_len) == 0); 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); nghttp2_bufs_reset(&bufs);
/* Unknown error code is passed to callback as is */ /* 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); check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
CU_ASSERT(1000000009 == oframe.error_code); 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); nghttp2_bufs_free(&bufs);
} }
@ -448,6 +458,9 @@ void test_nghttp2_nv_array_copy(void) {
nghttp2_nv emptynv[] = {MAKE_NV("", ""), MAKE_NV("", "")}; nghttp2_nv emptynv[] = {MAKE_NV("", ""), MAKE_NV("", "")};
nghttp2_nv nv[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")}; nghttp2_nv nv[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
nghttp2_nv bignv; nghttp2_nv bignv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
bignv.name = (uint8_t *)"echo"; bignv.name = (uint8_t *)"echo";
bignv.namelen = strlen("echo"); bignv.namelen = strlen("echo");
@ -455,20 +468,20 @@ void test_nghttp2_nv_array_copy(void) {
bignv.value = malloc(bignv.valuelen); bignv.value = malloc(bignv.valuelen);
memset(bignv.value, '0', 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(0 == rv);
CU_ASSERT(NULL == nva); 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(0 == rv);
CU_ASSERT(nva[0].namelen == 0); CU_ASSERT(nva[0].namelen == 0);
CU_ASSERT(nva[0].valuelen == 0); CU_ASSERT(nva[0].valuelen == 0);
CU_ASSERT(nva[1].namelen == 0); CU_ASSERT(nva[1].namelen == 0);
CU_ASSERT(nva[1].valuelen == 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(0 == rv);
CU_ASSERT(nva[0].namelen == 5); CU_ASSERT(nva[0].namelen == 5);
CU_ASSERT(0 == memcmp("alpha", nva[0].name, 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(nva[1].valuelen == 5);
CU_ASSERT(0 == memcmp("delta", nva[1].value, 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 */ /* 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); CU_ASSERT(0 == rv);
nghttp2_nv_array_del(nva); nghttp2_nv_array_del(nva, mem);
free(bignv.value); free(bignv.value);
} }

View File

@ -52,12 +52,14 @@ void test_nghttp2_hd_deflate(void) {
ssize_t blocklen; ssize_t blocklen;
nva_out out; nva_out out;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater)); CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem));
CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater)); CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem));
rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1)); rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1));
blocklen = nghttp2_bufs_len(&bufs); blocklen = nghttp2_bufs_len(&bufs);
@ -145,12 +147,14 @@ void test_nghttp2_hd_deflate_same_indexed_repr(void) {
ssize_t blocklen; ssize_t blocklen;
nva_out out; nva_out out;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater)); CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem));
CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater)); CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem));
/* Encode 2 same headers. Emit 1 literal reprs and 1 index repr. */ /* Encode 2 same headers. Emit 1 literal reprs and 1 index repr. */
rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1)); 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; ssize_t blocklen;
nghttp2_nv nv = MAKE_NV(":path", "/"); nghttp2_nv nv = MAKE_NV(":path", "/");
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
nghttp2_bufs_addb(&bufs, (1 << 7) | 4); nghttp2_bufs_addb(&bufs, (1 << 7) | 4);
@ -234,11 +240,13 @@ void test_nghttp2_hd_inflate_indname_noinc(void) {
MAKE_NV("user-agent", "x")}; MAKE_NV("user-agent", "x")};
size_t i; size_t i;
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
for (i = 0; i < ARRLEN(nv); ++i) { for (i = 0; i < ARRLEN(nv); ++i) {
CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 57, &nv[i], 0)); 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; ssize_t blocklen;
nghttp2_nv nv = MAKE_NV("user-agent", "nghttp2"); nghttp2_nv nv = MAKE_NV("user-agent", "nghttp2");
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); 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)); 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]; uint8_t value[1024];
nva_out out; nva_out out;
nghttp2_nv nv; nghttp2_nv nv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
memset(value, '0', sizeof(value)); memset(value, '0', sizeof(value));
nv.value = value; nv.value = value;
@ -350,11 +362,13 @@ void test_nghttp2_hd_inflate_newname_noinc(void) {
MAKE_NV("x", "nghttp2")}; MAKE_NV("x", "nghttp2")};
size_t i; size_t i;
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
for (i = 0; i < ARRLEN(nv); ++i) { for (i = 0; i < ARRLEN(nv); ++i) {
CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv[i], 0)); 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; ssize_t blocklen;
nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2"); nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2");
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); 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)); 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; nghttp2_nv nv;
uint8_t value[4060]; uint8_t value[4060];
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
bufs_large_init(&bufs, 8192); bufs_large_init(&bufs, 8192);
nva_out_init(&out); nva_out_init(&out);
@ -427,7 +445,7 @@ void test_nghttp2_hd_inflate_clearall_inc(void) {
nv.flags = NGHTTP2_NV_FLAG_NONE; 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)); 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 */ /* Literal header without indexing - new name */
uint8_t data[] = {0x40, 0x01, 0x78 /* 'x' */, 0x80}; uint8_t data[] = {0x40, 0x01, 0x78 /* 'x' */, 0x80};
nva_out out; nva_out out;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
@ -493,7 +513,7 @@ void test_nghttp2_hd_inflate_zero_length_huffman(void) {
/* ptr[2] = 'x'; */ /* ptr[2] = 'x'; */
/* ptr[3] = 0x80; */ /* 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(4 == inflate_hd(&inflater, &out, &bufs, 0));
CU_ASSERT(1 == out.nvlen); CU_ASSERT(1 == out.nvlen);
@ -516,7 +536,9 @@ void test_nghttp2_hd_ringbuf_reserve(void) {
int i; int i;
ssize_t rv; ssize_t rv;
ssize_t blocklen; ssize_t blocklen;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
@ -527,8 +549,8 @@ void test_nghttp2_hd_ringbuf_reserve(void) {
nv.value = malloc(nv.valuelen); nv.value = malloc(nv.valuelen);
memset(nv.value, 0, nv.valuelen); memset(nv.value, 0, nv.valuelen);
nghttp2_hd_deflate_init2(&deflater, 8000); nghttp2_hd_deflate_init2(&deflater, 8000, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
nghttp2_hd_inflate_change_table_size(&inflater, 8000); nghttp2_hd_inflate_change_table_size(&inflater, 8000);
nghttp2_hd_deflate_change_table_size(&deflater, 8000); nghttp2_hd_deflate_change_table_size(&deflater, 8000);
@ -566,13 +588,15 @@ void test_nghttp2_hd_change_table_size(void) {
ssize_t rv; ssize_t rv;
nva_out out; nva_out out;
ssize_t blocklen; ssize_t blocklen;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
/* inflater changes notifies 8000 max header table size */ /* inflater changes notifies 8000 max header table size */
CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000)); 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 */ /* Check table buffer is expanded */
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nghttp2_hd_deflate_init2(&deflater, 8192); nghttp2_hd_deflate_init2(&deflater, 8192, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
/* First inflater changes header table size to 8000 */ /* First inflater changes header table size to 8000 */
CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 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 /* Check that encoder can handle the case where its allowable buffer
size is less than default size, 4096 */ size is less than default size, 4096 */
nghttp2_hd_deflate_init2(&deflater, 1024); nghttp2_hd_deflate_init2(&deflater, 1024, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max); 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); nghttp2_hd_deflate_free(&deflater);
/* Check that table size UINT32_MAX can be received */ /* Check that table size UINT32_MAX can be received */
nghttp2_hd_deflate_init2(&deflater, UINT32_MAX); nghttp2_hd_deflate_init2(&deflater, UINT32_MAX, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, UINT32_MAX)); CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, UINT32_MAX));
CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 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); nghttp2_hd_deflate_free(&deflater);
/* Check that context update emitted twice */ /* Check that context update emitted twice */
nghttp2_hd_deflate_init2(&deflater, 4096); nghttp2_hd_deflate_init2(&deflater, 4096, mem);
nghttp2_hd_inflate_init(&inflater); 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, 0));
CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 3000)); 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", "HIT from alphabravo"),
MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"), MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
}; };
nghttp2_mem *mem;
nghttp2_hd_deflate_init(&deflater); mem = nghttp2_mem_default();
nghttp2_hd_inflate_init(&inflater);
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, nv1, ARRLEN(nv1));
check_deflate_inflate(&deflater, &inflater, nv2, ARRLEN(nv2)); check_deflate_inflate(&deflater, &inflater, nv2, ARRLEN(nv2));
@ -1000,6 +1027,9 @@ void test_nghttp2_hd_no_index(void) {
size_t i; size_t i;
nva_out out; nva_out out;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
/* 1st :method: GET can be indexable, last one is not */ /* 1st :method: GET can be indexable, last one is not */
for (i = 1; i < ARRLEN(nva); ++i) { for (i = 1; i < ARRLEN(nva); ++i) {
@ -1010,8 +1040,8 @@ void test_nghttp2_hd_no_index(void) {
nva_out_init(&out); nva_out_init(&out);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_hd_inflate_init(&inflater); nghttp2_hd_inflate_init(&inflater, mem);
rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva)); rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva));
blocklen = nghttp2_bufs_len(&bufs); 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_nv nva[] = {MAKE_NV(":method", "GET"), MAKE_NV("alpha", "bravo")};
nghttp2_bufs bufs; nghttp2_bufs bufs;
size_t bound, bound2; size_t bound, bound2;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); 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)); bound = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva));
@ -1071,6 +1103,9 @@ void test_nghttp2_hd_public_api(void) {
size_t buflen; size_t buflen;
ssize_t blocklen; ssize_t blocklen;
nghttp2_bufs bufs; nghttp2_bufs bufs;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096)); CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096));
CU_ASSERT(0 == nghttp2_hd_inflate_new(&inflater)); CU_ASSERT(0 == nghttp2_hd_inflate_new(&inflater));
@ -1081,7 +1116,7 @@ void test_nghttp2_hd_public_api(void) {
CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen > 0);
nghttp2_bufs_wrap_init(&bufs, buf, blocklen); nghttp2_bufs_wrap_init(&bufs, buf, blocklen, mem);
bufs.head->buf.last += blocklen; bufs.head->buf.last += blocklen;
CU_ASSERT(blocklen == inflate_hd(inflater, NULL, &bufs, 0)); CU_ASSERT(blocklen == inflate_hd(inflater, NULL, &bufs, 0));

View File

@ -41,7 +41,7 @@ static void strentry_init(strentry *entry, key_type key, const char *str) {
void test_nghttp2_map(void) { void test_nghttp2_map(void) {
strentry foo, FOO, bar, baz, shrubbery; strentry foo, FOO, bar, baz, shrubbery;
nghttp2_map map; nghttp2_map map;
nghttp2_map_init(&map); nghttp2_map_init(&map, nghttp2_mem_default());
strentry_init(&foo, 1, "foo"); strentry_init(&foo, 1, "foo");
strentry_init(&FOO, 1, "FOO"); strentry_init(&FOO, 1, "FOO");
@ -110,7 +110,7 @@ void test_nghttp2_map_functional(void) {
nghttp2_map map; nghttp2_map map;
int i; int i;
nghttp2_map_init(&map); nghttp2_map_init(&map, nghttp2_mem_default());
for (i = 0; i < NUM_ENT; ++i) { for (i = 0; i < NUM_ENT; ++i) {
strentry_init(&arr[i], i + 1, "foo"); strentry_init(&arr[i], i + 1, "foo");
order[i] = i + 1; order[i] = i + 1;
@ -155,7 +155,7 @@ void test_nghttp2_map_each_free(void) {
*baz = malloc(sizeof(strentry)), *baz = malloc(sizeof(strentry)),
*shrubbery = malloc(sizeof(strentry)); *shrubbery = malloc(sizeof(strentry));
nghttp2_map map; nghttp2_map map;
nghttp2_map_init(&map); nghttp2_map_init(&map, nghttp2_mem_default());
strentry_init(foo, 1, "foo"); strentry_init(foo, 1, "foo");
strentry_init(bar, 2, "bar"); strentry_init(bar, 2, "bar");

View File

@ -35,7 +35,7 @@ static int pq_compar(const void *lhs, const void *rhs) {
void test_nghttp2_pq(void) { void test_nghttp2_pq(void) {
int i; int i;
nghttp2_pq pq; 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(nghttp2_pq_empty(&pq));
CU_ASSERT(0 == nghttp2_pq_size(&pq)); CU_ASSERT(0 == nghttp2_pq_size(&pq));
CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"foo")); CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"foo"));
@ -103,7 +103,7 @@ void test_nghttp2_pq_update(void) {
node *nd; node *nd;
int ans[] = {-8, -6, -4, -2, 0, 1, 3, 5, 7, 9}; 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) { for (i = 0; i < (int)(sizeof(nodes) / sizeof(nodes[0])); ++i) {
nodes[i].key = i; nodes[i].key = i;

View File

@ -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, static nghttp2_settings_entry *dup_iv(const nghttp2_settings_entry *iv,
size_t niv) { 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}; 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; ssize_t nvlen;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater deflater;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
@ -389,10 +391,10 @@ void test_nghttp2_session_recv(void) {
user_data.df = &df; user_data.df = &df;
nghttp2_session_server_new(&session, &callbacks, &user_data); nghttp2_session_server_new(&session, &callbacks, &user_data);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nvlen = ARRLEN(nv); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
@ -408,7 +410,7 @@ void test_nghttp2_session_recv(void) {
df.feedseq[i] = 1; 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.frame_recv_cb_called = 0;
user_data.begin_frame_cb_called = 0; user_data.begin_frame_cb_called = 0;
@ -428,7 +430,7 @@ void test_nghttp2_session_recv(void) {
CU_ASSERT(0 == rv); CU_ASSERT(0 == rv);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
scripted_data_feed_init2(&df, &bufs); scripted_data_feed_init2(&df, &bufs);
user_data.frame_recv_cb_called = 0; user_data.frame_recv_cb_called = 0;
@ -510,7 +512,9 @@ void test_nghttp2_session_recv_invalid_stream_id(void) {
nghttp2_frame frame; nghttp2_frame frame;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater deflater;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); 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.df = &df;
user_data.invalid_frame_recv_cb_called = 0; user_data.invalid_frame_recv_cb_called = 0;
nghttp2_session_server_new(&session, &callbacks, &user_data); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 2,
NGHTTP2_HCAT_HEADERS, NULL, NULL, 0); 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); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
scripted_data_feed_init2(&df, &bufs); 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(0 == nghttp2_session_recv(session));
CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); 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; ssize_t nvlen;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater deflater;
int rv; int rv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); 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.df = &df;
user_data.frame_send_cb_called = 0; user_data.frame_send_cb_called = 0;
nghttp2_session_server_new(&session, &callbacks, &user_data); nghttp2_session_server_new(&session, &callbacks, &user_data);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nvlen = ARRLEN(nv); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); 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); CU_ASSERT(0 == user_data.frame_send_cb_called);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
nghttp2_hd_deflate_free(&deflater); nghttp2_hd_deflate_free(&deflater);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -748,7 +754,9 @@ void test_nghttp2_session_recv_continuation(void) {
size_t datalen; size_t datalen;
nghttp2_frame_hd cont_hd; nghttp2_frame_hd cont_hd;
nghttp2_priority_spec pri_spec; nghttp2_priority_spec pri_spec;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); 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_session_server_new(&session, &callbacks, &ud);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
/* Make 1 HEADERS and insert CONTINUATION header */ /* Make 1 HEADERS and insert CONTINUATION header */
nvlen = ARRLEN(nv1); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_NONE, 1,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
@ -774,7 +782,7 @@ void test_nghttp2_session_recv_continuation(void) {
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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 */ /* HEADERS's payload is 1 byte */
memcpy(data, buf->pos, NGHTTP2_FRAME_HDLEN + 1); 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 */ /* Expecting CONTINUATION, but get the other frame */
nghttp2_session_server_new(&session, &callbacks, &ud); nghttp2_session_server_new(&session, &callbacks, &ud);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
/* HEADERS without END_HEADERS flag */ /* HEADERS without END_HEADERS flag */
nvlen = ARRLEN(nv1); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_NONE, 1,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
@ -834,7 +842,7 @@ void test_nghttp2_session_recv_continuation(void) {
CU_ASSERT(0 == rv); CU_ASSERT(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); 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 */ /* make sure that all data is in the first buf */
buf = &bufs.head->buf; buf = &bufs.head->buf;
@ -885,7 +893,9 @@ void test_nghttp2_session_recv_headers_with_priority(void) {
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_priority_spec pri_spec; nghttp2_priority_spec pri_spec;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); 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_session_server_new(&session, &callbacks, &ud);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
open_stream(session, 1); open_stream(session, 1);
/* With NGHTTP2_FLAG_PRIORITY without exclusive flag set */ /* With NGHTTP2_FLAG_PRIORITY without exclusive flag set */
nvlen = ARRLEN(nv1); 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); 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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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 /* With NGHTTP2_FLAG_PRIORITY, but cut last 1 byte to make it
invalid. */ invalid. */
nvlen = ARRLEN(nv1); 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); 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(0 == rv);
CU_ASSERT(NGHTTP2_FRAME_HDLEN + 5 + 2 == nghttp2_bufs_len(&bufs)); 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; buf = &bufs.head->buf;
/* Make payload shorter than required length to store priroty /* 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 */ /* Check dep_stream_id == stream_id */
nghttp2_session_server_new(&session, &callbacks, &ud); nghttp2_session_server_new(&session, &callbacks, &ud);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nvlen = ARRLEN(nv1); 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); 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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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; my_user_data ud;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater deflater;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
nghttp2_session_server_new(&session, &callbacks, &ud); nghttp2_session_server_new(&session, &callbacks, &ud);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
nvlen = ARRLEN(nv1); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); 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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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; my_user_data ud;
nghttp2_settings_entry iv[3]; nghttp2_settings_entry iv[3];
nghttp2_nv nv = MAKE_NV(":authority", "example.org"); nghttp2_nv nv = MAKE_NV(":authority", "example.org");
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); 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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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; const nghttp2_frame *recv_frame;
nghttp2_frame_hd data_hd; nghttp2_frame_hd data_hd;
nghttp2_hd_deflater deflater; nghttp2_hd_deflater deflater;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nghttp2_buf_wrap_init(&databuf, buffer, sizeof(buffer)); 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_session_server_new(&session, &callbacks, &user_data);
nghttp2_hd_deflate_init(&deflater); nghttp2_hd_deflate_init(&deflater, mem);
/* Make 2 HEADERS frames */ /* Make 2 HEADERS frames */
nvlen = ARRLEN(nv1); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 1,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); 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(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
buf = &bufs.head->buf; buf = &bufs.head->buf;
assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(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)); databuf.last = nghttp2_cpymem(databuf.last, buf->pos, nghttp2_buf_len(buf));
nvlen = ARRLEN(nv2); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_HEADERS, 3,
NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
@ -1408,7 +1424,7 @@ void test_nghttp2_session_continue(void) {
CU_ASSERT(0 == rv); CU_ASSERT(0 == rv);
CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); 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)); assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf));
@ -1541,7 +1557,9 @@ void test_nghttp2_session_add_frame(void) {
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_nv *nva; nghttp2_nv *nva;
ssize_t nvlen; ssize_t nvlen;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = accumulator_send_callback; callbacks.send_callback = accumulator_send_callback;
@ -1557,7 +1575,7 @@ void test_nghttp2_session_add_frame(void) {
frame = &item->frame; frame = &item->frame;
nvlen = ARRLEN(nv); nvlen = ARRLEN(nv);
nghttp2_nv_array_copy(&nva, nv, nvlen); nghttp2_nv_array_copy(&nva, nv, nvlen, mem);
nghttp2_frame_headers_init( nghttp2_frame_headers_init(
&frame->headers, NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY, &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; nghttp2_nv *nva;
size_t nvlen; size_t nvlen;
nghttp2_priority_spec pri_spec; nghttp2_priority_spec pri_spec;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_begin_headers_callback = on_begin_headers_callback;
callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_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(NGHTTP2_STREAM_OPENING == stream->state);
CU_ASSERT(255 == stream->weight); 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 */ /* More than un-ACKed max concurrent streams leads REFUSED_STREAM */
session->pending_local_max_concurrent_stream = 1; 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(1 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); 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 = session->local_settings.max_concurrent_streams =
NGHTTP2_INITIAL_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 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); 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 /* Stream ID is our side and it is idle stream ID, then treat it as
connection error */ 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(1 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND); 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); 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); nghttp2_session_server_new(&session, &callbacks, &user_data);
nvlen = ARRLEN(malformed_nva); 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_frame_headers_init(&frame.headers,
NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY, NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PRIORITY,
1, NGHTTP2_HCAT_HEADERS, NULL, nva, nvlen); 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(1 == user_data.begin_headers_cb_called);
CU_ASSERT(0 == user_data.invalid_frame_recv_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); 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(1 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND); 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); 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(1 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND); 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); 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 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); 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); 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)); 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 /* Stream ID which is greater than local_last_stream_id is
ignored */ 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 == user_data.invalid_frame_recv_cb_called);
CU_ASSERT(0 == (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND)); 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); nghttp2_session_del(session);
} }
@ -1758,7 +1778,9 @@ void test_nghttp2_session_on_response_headers_received(void) {
my_user_data user_data; my_user_data user_data;
nghttp2_frame frame; nghttp2_frame frame;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_begin_headers_callback = on_begin_headers_callback;
callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_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(1 == user_data.begin_headers_cb_called);
CU_ASSERT(NGHTTP2_STREAM_OPENED == stream->state); CU_ASSERT(NGHTTP2_STREAM_OPENED == stream->state);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
nghttp2_session_del(session); nghttp2_session_del(session);
} }
@ -1788,7 +1810,9 @@ void test_nghttp2_session_on_headers_received(void) {
my_user_data user_data; my_user_data user_data;
nghttp2_frame frame; nghttp2_frame frame;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_begin_headers_callback = on_begin_headers_callback;
callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_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)); nghttp2_session_on_headers_received(session, &frame, stream));
CU_ASSERT(1 == user_data.invalid_frame_recv_cb_called); 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); nghttp2_session_del(session);
} }
@ -1859,7 +1883,9 @@ void test_nghttp2_session_on_push_response_headers_received(void) {
nghttp2_frame frame; nghttp2_frame frame;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = null_send_callback; callbacks.send_callback = null_send_callback;
callbacks.on_begin_headers_callback = on_begin_headers_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(NGHTTP2_ENHANCE_YOUR_CALM == item->frame.goaway.error_code);
CU_ASSERT(1 == session->num_incoming_streams); CU_ASSERT(1 == session->num_incoming_streams);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
nghttp2_session_del(session); nghttp2_session_del(session);
} }
@ -2028,6 +2054,9 @@ void test_nghttp2_session_on_settings_received(void) {
nghttp2_settings_entry iv[255]; nghttp2_settings_entry iv[255];
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_nv nv = MAKE_NV(":authority", "example.org"); 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].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[0].value = 50; 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( CU_ASSERT(0 == nghttp2_session_get_stream_remote_window_size(
session, stream2->stream_id)); session, stream2->stream_id));
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, mem);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -2105,7 +2134,7 @@ void test_nghttp2_session_on_settings_received(void) {
session->inflight_iv = NULL; session->inflight_iv = NULL;
session->inflight_niv = -1; session->inflight_niv = -1;
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings, mem);
nghttp2_session_del(session); nghttp2_session_del(session);
/* Check ACK against no inflight SETTINGS */ /* Check ACK against no inflight SETTINGS */
@ -2117,7 +2146,7 @@ void test_nghttp2_session_on_settings_received(void) {
CU_ASSERT(item != NULL); CU_ASSERT(item != NULL);
CU_ASSERT(NGHTTP2_GOAWAY == item->frame.hd.type); 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); nghttp2_session_del(session);
/* Check that 2 SETTINGS_HEADER_TABLE_SIZE 0 and 4096 are included /* 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->hd_deflater.ctx.hd_table_bufsize_max);
CU_ASSERT(2048 == session->remote_settings.header_table_size); 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); nghttp2_session_del(session);
/* Check too large SETTINGS_MAX_FRAME_SIZE */ /* 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(item != NULL);
CU_ASSERT(NGHTTP2_GOAWAY == item->frame.hd.type); 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); 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 malformed_nva[] = {MAKE_NV(":path", "\x01")};
nghttp2_nv *nva; nghttp2_nv *nva;
size_t nvlen; size_t nvlen;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = null_send_callback; callbacks.send_callback = null_send_callback;
callbacks.on_begin_headers_callback = on_begin_headers_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_stream(session, 10));
CU_ASSERT(NULL == nghttp2_session_get_next_ob_item(session)); 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_del(session);
nghttp2_session_client_new(&session, &callbacks, &user_data); 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(0 == user_data.begin_headers_cb_called);
CU_ASSERT(1 == user_data.invalid_frame_recv_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); nghttp2_session_del(session);
/* Disable PUSH */ /* 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(0 == user_data.begin_headers_cb_called);
CU_ASSERT(1 == user_data.invalid_frame_recv_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); nghttp2_session_del(session);
/* Check malformed headers. We accept malformed headers */ /* Check malformed headers. We accept malformed headers */
@ -2344,7 +2375,7 @@ void test_nghttp2_session_on_push_promise_received(void) {
&pri_spec_default, &pri_spec_default,
NGHTTP2_STREAM_OPENING, NULL); NGHTTP2_STREAM_OPENING, NULL);
nvlen = ARRLEN(malformed_nva); 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, nghttp2_frame_push_promise_init(&frame.push_promise, NGHTTP2_FLAG_END_HEADERS,
1, 2, nva, nvlen); 1, 2, nva, nvlen);
user_data.begin_headers_cb_called = 0; 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(1 == user_data.begin_headers_cb_called);
CU_ASSERT(0 == user_data.invalid_frame_recv_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); nghttp2_session_del(session);
} }
@ -2403,7 +2434,9 @@ void test_nghttp2_session_on_goaway_received(void) {
my_user_data user_data; my_user_data user_data;
nghttp2_frame frame; nghttp2_frame frame;
int i; int i;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
user_data.frame_recv_cb_called = 0; user_data.frame_recv_cb_called = 0;
user_data.invalid_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, 6));
CU_ASSERT(NULL == nghttp2_session_get_stream(session, 7)); 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); 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 nnv = ARRLEN(nv);
size_t i; size_t i;
my_user_data ud; my_user_data ud;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
for (i = 0; i < nnv; ++i) { for (i = 0; i < nnv; ++i) {
nv[i].name = (uint8_t *)"header"; 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); nghttp2_session_client_new(&session, &callbacks, &ud);
nvlen = nnv; nvlen = nnv;
nghttp2_nv_array_copy(&nva, nv, nvlen); nghttp2_nv_array_copy(&nva, nv, nvlen, mem);
item = malloc(sizeof(nghttp2_outbound_item)); item = malloc(sizeof(nghttp2_outbound_item));
@ -2760,7 +2796,9 @@ void test_nghttp2_session_send_push_promise(void) {
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_settings_entry iv; nghttp2_settings_entry iv;
my_user_data ud; my_user_data ud;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = null_send_callback; callbacks.send_callback = null_send_callback;
callbacks.on_frame_not_send_callback = on_frame_not_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, nghttp2_frame_settings_init(&frame->settings, NGHTTP2_FLAG_NONE,
dup_iv(&iv, 1), 1); dup_iv(&iv, 1), 1);
nghttp2_session_on_settings_received(session, frame, 1); nghttp2_session_on_settings_received(session, frame, 1);
nghttp2_frame_settings_free(&frame->settings); nghttp2_frame_settings_free(&frame->settings, mem);
free(frame); free(frame);
item = malloc(sizeof(nghttp2_outbound_item)); item = malloc(sizeof(nghttp2_outbound_item));
@ -3294,7 +3332,9 @@ void test_nghttp2_submit_request_without_data(void) {
nghttp2_hd_inflater inflater; nghttp2_hd_inflater inflater;
nva_out out; nva_out out;
nghttp2_bufs bufs; nghttp2_bufs bufs;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
@ -3304,7 +3344,7 @@ void test_nghttp2_submit_request_without_data(void) {
callbacks.send_callback = accumulator_send_callback; callbacks.send_callback = accumulator_send_callback;
CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud)); 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), CU_ASSERT(1 == nghttp2_submit_request(session, NULL, nva, ARRLEN(nva),
&data_prd, NULL)); &data_prd, NULL));
item = nghttp2_session_get_next_ob_item(session); 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); inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN);
CU_ASSERT(nvnameeq(":version", &out.nva[0])); CU_ASSERT(nvnameeq(":version", &out.nva[0]));
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
nva_out_reset(&out); nva_out_reset(&out);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
@ -3364,7 +3404,9 @@ void test_nghttp2_submit_response_without_data(void) {
nghttp2_hd_inflater inflater; nghttp2_hd_inflater inflater;
nva_out out; nva_out out;
nghttp2_bufs bufs; nghttp2_bufs bufs;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
@ -3374,7 +3416,7 @@ void test_nghttp2_submit_response_without_data(void) {
callbacks.send_callback = accumulator_send_callback; callbacks.send_callback = accumulator_send_callback;
CU_ASSERT(0 == nghttp2_session_server_new(&session, &callbacks, &ud)); 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, nghttp2_session_open_stream(session, 1, NGHTTP2_FLAG_END_STREAM,
&pri_spec_default, NGHTTP2_STREAM_OPENING, NULL); &pri_spec_default, NGHTTP2_STREAM_OPENING, NULL);
CU_ASSERT(0 == CU_ASSERT(0 ==
@ -3393,7 +3435,7 @@ void test_nghttp2_submit_response_without_data(void) {
nva_out_reset(&out); nva_out_reset(&out);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_free(&inflater);
nghttp2_session_del(session); nghttp2_session_del(session);
} }
@ -3516,7 +3558,9 @@ void test_nghttp2_submit_headers(void) {
nghttp2_hd_inflater inflater; nghttp2_hd_inflater inflater;
nva_out out; nva_out out;
nghttp2_bufs bufs; nghttp2_bufs bufs;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
nva_out_init(&out); nva_out_init(&out);
@ -3528,7 +3572,7 @@ void test_nghttp2_submit_headers(void) {
CU_ASSERT(0 == nghttp2_session_client_new(&session, &callbacks, &ud)); 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, CU_ASSERT(0 == nghttp2_submit_headers(session, NGHTTP2_FLAG_END_STREAM, 1,
NULL, nv, ARRLEN(nv), NULL)); NULL, nv, ARRLEN(nv), NULL));
item = nghttp2_session_get_next_ob_item(session); item = nghttp2_session_get_next_ob_item(session);
@ -3563,7 +3607,7 @@ void test_nghttp2_submit_headers(void) {
nva_out_reset(&out); nva_out_reset(&out);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers, mem);
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_free(&inflater);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -3652,6 +3696,9 @@ void test_nghttp2_submit_settings(void) {
nghttp2_settings_entry iv[7]; nghttp2_settings_entry iv[7];
nghttp2_frame ack_frame; nghttp2_frame ack_frame;
const int32_t UNKNOWN_ID = 1000000007; const int32_t UNKNOWN_ID = 1000000007;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[0].value = 5; 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); nghttp2_frame_settings_init(&ack_frame.settings, NGHTTP2_FLAG_ACK, NULL, 0);
CU_ASSERT(0 == nghttp2_session_on_settings_received(session, &ack_frame, 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(16 * 1024 == session->local_settings.initial_window_size);
CU_ASSERT(0 == session->hd_inflater.ctx.hd_table_bufsize_max); 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_settings_entry iv[4];
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_frame ack_frame; nghttp2_frame ack_frame;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
nghttp2_frame_settings_init(&ack_frame.settings, NGHTTP2_FLAG_ACK, NULL, 0); nghttp2_frame_settings_init(&ack_frame.settings, NGHTTP2_FLAG_ACK, NULL, 0);
iv[0].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; 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); CU_ASSERT(NGHTTP2_FLOW_CONTROL_ERROR == item->frame.goaway.error_code);
nghttp2_session_del(session); 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) { 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_outbound_item *item;
nghttp2_priority_spec pri_spec; nghttp2_priority_spec pri_spec;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = null_send_callback; 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); item = nghttp2_session_pop_next_ob_item(session);
CU_ASSERT(NGHTTP2_PING == item->frame.hd.type); CU_ASSERT(NGHTTP2_PING == item->frame.hd.type);
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); free(item);
item = nghttp2_session_pop_next_ob_item(session); item = nghttp2_session_pop_next_ob_item(session);
CU_ASSERT(NGHTTP2_HEADERS == item->frame.hd.type); CU_ASSERT(NGHTTP2_HEADERS == item->frame.hd.type);
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); free(item);
CU_ASSERT(NULL == nghttp2_session_pop_next_ob_item(session)); 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_stream_detach_data(stream, session);
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); free(item);
CU_ASSERT(NULL == nghttp2_session_pop_next_ob_item(session)); 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); item = nghttp2_session_pop_next_ob_item(session);
CU_ASSERT(NGHTTP2_HEADERS == item->frame.hd.type); CU_ASSERT(NGHTTP2_HEADERS == item->frame.hd.type);
nghttp2_outbound_item_free(item); nghttp2_outbound_item_free(item, mem);
free(item); free(item);
nghttp2_session_del(session); nghttp2_session_del(session);
@ -4280,7 +4331,9 @@ void test_nghttp2_session_max_concurrent_streams(void) {
nghttp2_session_callbacks callbacks; nghttp2_session_callbacks callbacks;
nghttp2_frame frame; nghttp2_frame frame;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = null_send_callback; 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_GOAWAY == item->frame.hd.type);
CU_ASSERT(NGHTTP2_ENHANCE_YOUR_CALM == item->frame.goaway.error_code); 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); nghttp2_session_del(session);
} }
@ -4467,7 +4520,9 @@ void test_nghttp2_session_flow_control(void) {
int32_t new_initial_window_size; int32_t new_initial_window_size;
nghttp2_settings_entry iv[1]; nghttp2_settings_entry iv[1];
nghttp2_frame settings_frame; nghttp2_frame settings_frame;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.send_callback = fixed_bytes_send_callback; callbacks.send_callback = fixed_bytes_send_callback;
callbacks.on_frame_send_callback = on_frame_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, nghttp2_frame_settings_init(&settings_frame.settings, NGHTTP2_FLAG_NONE,
dup_iv(iv, 1), 1); dup_iv(iv, 1), 1);
nghttp2_session_on_settings_received(session, &settings_frame, 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 */ /* Sends another 8KiB data */
CU_ASSERT(0 == nghttp2_session_send(session)); CU_ASSERT(0 == nghttp2_session_send(session));
@ -5096,6 +5151,9 @@ void test_nghttp2_pack_settings_payload(void) {
ssize_t len; ssize_t len;
nghttp2_settings_entry *resiv; nghttp2_settings_entry *resiv;
size_t resniv; size_t resniv;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE; iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
iv[0].value = 1023; 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); len = nghttp2_pack_settings_payload(buf, sizeof(buf), iv, 2);
CU_ASSERT(2 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH == len); CU_ASSERT(2 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH == len);
CU_ASSERT(0 == CU_ASSERT(0 == nghttp2_frame_unpack_settings_payload2(&resiv, &resniv, buf,
nghttp2_frame_unpack_settings_payload2(&resiv, &resniv, buf, len)); len, mem));
CU_ASSERT(2 == resniv); CU_ASSERT(2 == resniv);
CU_ASSERT(NGHTTP2_SETTINGS_HEADER_TABLE_SIZE == resiv[0].settings_id); CU_ASSERT(NGHTTP2_SETTINGS_HEADER_TABLE_SIZE == resiv[0].settings_id);
CU_ASSERT(1023 == resiv[0].value); CU_ASSERT(1023 == resiv[0].value);
@ -6339,7 +6397,9 @@ void test_nghttp2_session_on_header_temporal_failure(void) {
nghttp2_frame frame; nghttp2_frame frame;
nghttp2_frame_hd hd; nghttp2_frame_hd hd;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));
callbacks.on_header_callback = temporal_failure_on_header_callback; 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); 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_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
NGHTTP2_HCAT_REQUEST, NULL, nva, 1); NGHTTP2_HCAT_REQUEST, NULL, nva, 1);
nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); 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 /* We are going to create CONTINUATION. First serialize header
block, and then frame header. */ block, and then frame header. */

View File

@ -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; const uint8_t *payload = in + NGHTTP2_FRAME_HDLEN;
size_t payloadlen = len - NGHTTP2_FRAME_HDLEN; size_t payloadlen = len - NGHTTP2_FRAME_HDLEN;
size_t payloadoff; size_t payloadoff;
nghttp2_mem *mem;
mem = nghttp2_mem_default();
nghttp2_frame_unpack_frame_hd(&frame->hd, in); nghttp2_frame_unpack_frame_hd(&frame->hd, in);
switch (frame->hd.type) { switch (frame->hd.type) {
@ -63,7 +66,7 @@ int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) {
break; break;
case NGHTTP2_SETTINGS: case NGHTTP2_SETTINGS:
rv = nghttp2_frame_unpack_settings_payload2( rv = nghttp2_frame_unpack_settings_payload2(
&frame->settings.iv, &frame->settings.niv, payload, payloadlen); &frame->settings.iv, &frame->settings.niv, payload, payloadlen, mem);
break; break;
case NGHTTP2_PUSH_PROMISE: case NGHTTP2_PUSH_PROMISE:
rv = nghttp2_frame_unpack_push_promise_payload(&frame->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); nghttp2_frame_unpack_ping_payload(&frame->ping, payload, payloadlen);
break; break;
case NGHTTP2_GOAWAY: case NGHTTP2_GOAWAY:
nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen); nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen,
mem);
break; break;
case NGHTTP2_WINDOW_UPDATE: case NGHTTP2_WINDOW_UPDATE:
nghttp2_frame_unpack_window_update_payload(&frame->window_update, payload, 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) { int frame_pack_bufs_init(nghttp2_bufs *bufs) {
/* 1 for Pad Length */ /* 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) { void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size) {
/* 1 for Pad Length */ /* 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, static nghttp2_stream *open_stream_with_all(nghttp2_session *session,