From 9893ae81af7fcb7ed53fda17d8d9534cce086c2d Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 24 Aug 2014 15:34:55 +0900 Subject: [PATCH] Add nghttp2_bufs_realloc --- lib/nghttp2_buf.c | 27 +++++++++++++++++++++++++++ lib/nghttp2_buf.h | 16 ++++++++++++++++ tests/main.c | 3 ++- tests/nghttp2_buf_test.c | 24 ++++++++++++++++++++++++ tests/nghttp2_buf_test.h | 1 + 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/lib/nghttp2_buf.c b/lib/nghttp2_buf.c index d51e84fd..87382518 100644 --- a/lib/nghttp2_buf.c +++ b/lib/nghttp2_buf.c @@ -169,6 +169,33 @@ int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length, return 0; } +int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) +{ + int rv; + nghttp2_buf_chain *chain; + + if(chunk_length < bufs->offset) { + return NGHTTP2_ERR_INVALID_ARGUMENT; + } + + rv = buf_chain_new(&chain, chunk_length); + if(rv != 0) { + return rv; + } + + nghttp2_bufs_free(bufs); + + bufs->head = chain; + bufs->cur = bufs->head; + + nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset); + + bufs->chunk_length = chunk_length; + bufs->chunk_used = 1; + + return 0; +} + void nghttp2_bufs_free(nghttp2_bufs *bufs) { nghttp2_buf_chain *chain, *next_chain; diff --git a/lib/nghttp2_buf.h b/lib/nghttp2_buf.h index c43362c8..39300a55 100644 --- a/lib/nghttp2_buf.h +++ b/lib/nghttp2_buf.h @@ -228,6 +228,22 @@ int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len); */ void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs); +/* + * Reallocates internal buffer using |chunk_length|. The max_chunk, + * chunk_keep and offset do not change. After successful allocation + * of new buffer, previous buffers are deallocated without copying + * anything into new buffers. chunk_used is reset to 1. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * NGHTTP2_ERR_NOMEM + * Out of memory. + * NGHTTP2_ERR_INVALID_ARGUMENT + * chunk_length < offset + */ +int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length); + /* * Appends the |data| of length |len| to the |bufs|. The write starts * at bufs->cur->buf.last. A new buffers will be allocated to store diff --git a/tests/main.c b/tests/main.c index d5c5cb0c..4ae260a8 100644 --- a/tests/main.c +++ b/tests/main.c @@ -302,7 +302,8 @@ int main(int argc, char* argv[]) !CU_add_test(pSuite, "bufs_seek_present", test_nghttp2_bufs_seek_last_present) || !CU_add_test(pSuite, "bufs_next_present", - test_nghttp2_bufs_next_present) + test_nghttp2_bufs_next_present) || + !CU_add_test(pSuite, "bufs_realloc", test_nghttp2_bufs_realloc) ) { CU_cleanup_registry(); return CU_get_error(); diff --git a/tests/nghttp2_buf_test.c b/tests/nghttp2_buf_test.c index 9367645e..f04f9c01 100644 --- a/tests/nghttp2_buf_test.c +++ b/tests/nghttp2_buf_test.c @@ -317,3 +317,27 @@ void test_nghttp2_bufs_next_present(void) nghttp2_bufs_free(&bufs); } + +void test_nghttp2_bufs_realloc(void) +{ + int rv; + nghttp2_bufs bufs; + + rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10); + CU_ASSERT(0 == rv); + + /* Create new buffer to see that these buffers are deallocated on + realloc */ + rv = nghttp2_bufs_advance(&bufs); + CU_ASSERT(0 == rv); + + rv = nghttp2_bufs_realloc(&bufs, 522); + CU_ASSERT(0 == rv); + + CU_ASSERT(512 == nghttp2_bufs_cur_avail(&bufs)); + + rv = nghttp2_bufs_realloc(&bufs, 9); + CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv); + + nghttp2_bufs_free(&bufs); +} diff --git a/tests/nghttp2_buf_test.h b/tests/nghttp2_buf_test.h index 9b7d5687..8a42ecad 100644 --- a/tests/nghttp2_buf_test.h +++ b/tests/nghttp2_buf_test.h @@ -33,5 +33,6 @@ void test_nghttp2_bufs_reset(void); void test_nghttp2_bufs_advance(void); void test_nghttp2_bufs_seek_last_present(void); void test_nghttp2_bufs_next_present(void); +void test_nghttp2_bufs_realloc(void); #endif /* NGHTTP2_BUF_TEST_H */