From 488c3588d95c120d281f253db5ac4ae5d859f13f Mon Sep 17 00:00:00 2001 From: Wenfeng Liu Date: Wed, 17 Aug 2016 03:22:10 +0000 Subject: [PATCH] lib: Malloc nghttp2_buf_chain array once in nghttp2_bufs_wrap_init2() to simplify logic --- lib/nghttp2_buf.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/nghttp2_buf.c b/lib/nghttp2_buf.c index b28aaf85..bea16eb5 100644 --- a/lib/nghttp2_buf.c +++ b/lib/nghttp2_buf.c @@ -227,22 +227,17 @@ int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, uint8_t *const *bufs_in, size_t in_len, size_t buf_len, nghttp2_mem *mem) { size_t i = 0; nghttp2_buf_chain *cur_chain; - nghttp2_buf_chain *head_chain = NULL; + nghttp2_buf_chain *head_chain; nghttp2_buf_chain **dst_chain = &head_chain; + head_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain) * in_len); + if (head_chain == NULL) { + return NGHTTP2_ERR_NOMEM; + } + for (i = 0; i < in_len; ++i) { uint8_t *begin = bufs_in[i]; - - cur_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain)); - if (cur_chain == NULL) { - while (head_chain) { - nghttp2_buf_chain *tmp_chain = head_chain->next; - nghttp2_mem_free(mem, head_chain); - head_chain = tmp_chain; - } - return NGHTTP2_ERR_NOMEM; - } - + cur_chain = &head_chain[i]; cur_chain->next = NULL; nghttp2_buf_wrap_init(&cur_chain->buf, begin, buf_len); @@ -269,10 +264,9 @@ void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) { return; } - while (bufs->head) { - nghttp2_buf_chain *tmp_chain = bufs->head->next; + if (bufs->head) { nghttp2_mem_free(bufs->mem, bufs->head); - bufs->head = tmp_chain; + bufs->head = NULL; } }