Add nghttp2_bufs_realloc

This commit is contained in:
Tatsuhiro Tsujikawa 2014-08-24 15:34:55 +09:00
parent 7bfa276e96
commit 9893ae81af
5 changed files with 70 additions and 1 deletions

View File

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

View File

@ -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

View File

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

View File

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

View File

@ -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 */