From: Pietro Cerutti Date: Tue, 27 Aug 2019 11:32:30 +0000 (+0000) Subject: Move buffer_new / buffer_free to static functions in pool.c X-Git-Tag: 2019-10-25~64^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=52d04a3bf7c60e9849e2b8d03c28c14bb553b7db;p=neomutt Move buffer_new / buffer_free to static functions in pool.c --- diff --git a/mutt/buffer.c b/mutt/buffer.c index 43c5ae131..0cb146b6a 100644 --- a/mutt/buffer.c +++ b/mutt/buffer.c @@ -36,21 +36,6 @@ #include "memory.h" #include "string2.h" -/** - * mutt_buffer_new - Create and initialise a Buffer - * @retval ptr New Buffer - * - * Call mutt_buffer_free() to release the Buffer. - */ -struct Buffer *mutt_buffer_new(void) -{ - struct Buffer *b = mutt_mem_malloc(sizeof(struct Buffer)); - - mutt_buffer_init(b); - - return b; -} - /** * mutt_buffer_init - Initialise a new Buffer * @param buf Buffer to initialise @@ -107,20 +92,6 @@ size_t mutt_buffer_addstr_n(struct Buffer *buf, const char *s, size_t len) return len; } -/** - * mutt_buffer_free - Release a Buffer and its contents - * @param[out] p Buffer pointer to free and NULL - */ -void mutt_buffer_free(struct Buffer **p) -{ - if (!p || !*p) - return; - - FREE(&(*p)->data); - /* dptr is just an offset to data and shouldn't be freed */ - FREE(p); -} - /** * buffer_printf - Format a string into a Buffer * @param buf Buffer diff --git a/mutt/buffer.h b/mutt/buffer.h index f8bbeacb3..ac2dce56c 100644 --- a/mutt/buffer.h +++ b/mutt/buffer.h @@ -45,11 +45,9 @@ struct Buffer void mutt_buffer_alloc (struct Buffer *buf, size_t size); void mutt_buffer_dealloc (struct Buffer *buf); void mutt_buffer_fix_dptr (struct Buffer *buf); -void mutt_buffer_free (struct Buffer **p); struct Buffer *mutt_buffer_init (struct Buffer *buf); bool mutt_buffer_is_empty (const struct Buffer *buf); size_t mutt_buffer_len (const struct Buffer *buf); -struct Buffer *mutt_buffer_new (void); void mutt_buffer_reset (struct Buffer *buf); // Functions that APPEND to a Buffer diff --git a/mutt/pool.c b/mutt/pool.c index 810d11bdf..3ca1145fc 100644 --- a/mutt/pool.c +++ b/mutt/pool.c @@ -38,6 +38,31 @@ static size_t BufferPoolIncrement = 20; static size_t BufferPoolInitialBufferSize = 1024; static struct Buffer **BufferPool = NULL; +/** + * buffer_new - Allocate a new Buffer on the heap + * @retval buf A newly allocated Buffer + * @note call buffer_free to release the memory + */ +static struct Buffer *buffer_new(void) +{ + struct Buffer *b = mutt_mem_malloc(sizeof(struct Buffer)); + mutt_buffer_init(b); + return b; +} + +/** + * buffer_free - Release a Buffer and its contents + * @param[out] p Buffer pointer to free and NULL + */ +static void buffer_free(struct Buffer **p) +{ + if (!p || !*p) + return; + + mutt_buffer_dealloc(*p); + FREE(p); +} + /** * increase_buffer_pool - Increase the size of the Buffer pool */ @@ -49,7 +74,7 @@ static void increase_buffer_pool(void) mutt_mem_realloc(&BufferPool, BufferPoolLen * sizeof(struct Buffer *)); while (BufferPoolCount < BufferPoolIncrement) { - struct Buffer *newbuf = mutt_buffer_new(); + struct Buffer *newbuf = buffer_new(); mutt_buffer_alloc(newbuf, BufferPoolInitialBufferSize); BufferPool[BufferPoolCount++] = newbuf; } @@ -63,7 +88,7 @@ void mutt_buffer_pool_free(void) mutt_debug(LL_DEBUG1, "%zu of %zu returned to pool\n", BufferPoolCount, BufferPoolLen); while (BufferPoolCount) - mutt_buffer_free(&BufferPool[--BufferPoolCount]); + buffer_free(&BufferPool[--BufferPoolCount]); FREE(&BufferPool); BufferPoolLen = 0; } @@ -91,7 +116,7 @@ void mutt_buffer_pool_release(struct Buffer **pbuf) if (BufferPoolCount >= BufferPoolLen) { mutt_debug(LL_DEBUG1, "Internal buffer pool error\n"); - mutt_buffer_free(pbuf); + buffer_free(pbuf); return; } diff --git a/test/Makefile.autosetup b/test/Makefile.autosetup index cdd1c9597..b42af1d8a 100644 --- a/test/Makefile.autosetup +++ b/test/Makefile.autosetup @@ -45,11 +45,9 @@ BUFFER_OBJS = test/buffer/mutt_buffer_addch.o \ test/buffer/mutt_buffer_alloc.o \ test/buffer/mutt_buffer_concat_path.o \ test/buffer/mutt_buffer_fix_dptr.o \ - test/buffer/mutt_buffer_free.o \ test/buffer/mutt_buffer_init.o \ test/buffer/mutt_buffer_is_empty.o \ test/buffer/mutt_buffer_len.o \ - test/buffer/mutt_buffer_new.o \ test/buffer/mutt_buffer_pool_free.o \ test/buffer/mutt_buffer_pool_get.o \ test/buffer/mutt_buffer_pool_release.o \ diff --git a/test/buffer/mutt_buffer_free.c b/test/buffer/mutt_buffer_free.c deleted file mode 100644 index 8277a0ab8..000000000 --- a/test/buffer/mutt_buffer_free.c +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @file - * Test code for mutt_buffer_free() - * - * @authors - * Copyright (C) 2019 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#define TEST_NO_MAIN -#include "acutest.h" -#include "config.h" -#include "mutt/mutt.h" - -void test_mutt_buffer_free(void) -{ - // void mutt_buffer_free(struct Buffer **p); - - { - mutt_buffer_free(NULL); - TEST_CHECK_(1, "mutt_buffer_free(NULL)"); - } - - { - struct Buffer *buf = NULL; - mutt_buffer_free(&buf); - TEST_CHECK_(1, "mutt_buffer_free(&buf)"); - TEST_CHECK(buf == NULL); - } - - { - struct Buffer *buf = mutt_buffer_new(); - mutt_buffer_free(&buf); - TEST_CHECK_(1, "mutt_buffer_free(&buf)"); - TEST_CHECK(buf == NULL); - } -} diff --git a/test/buffer/mutt_buffer_new.c b/test/buffer/mutt_buffer_new.c deleted file mode 100644 index 941389c3a..000000000 --- a/test/buffer/mutt_buffer_new.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file - * Test code for mutt_buffer_new() - * - * @authors - * Copyright (C) 2019 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#define TEST_NO_MAIN -#include "acutest.h" -#include "config.h" -#include "mutt/mutt.h" - -void test_mutt_buffer_new(void) -{ - // struct Buffer *mutt_buffer_new(void); - - { - struct Buffer *buf = NULL; - TEST_CHECK((buf = mutt_buffer_new()) != NULL); - - mutt_buffer_free(&buf); - } -} diff --git a/test/main.c b/test/main.c index c870f9fbc..eebf2ec89 100644 --- a/test/main.c +++ b/test/main.c @@ -70,11 +70,9 @@ NEOMUTT_TEST_ITEM(test_mutt_buffer_alloc) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_concat_path) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_fix_dptr) \ - NEOMUTT_TEST_ITEM(test_mutt_buffer_free) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_init) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_is_empty) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_len) \ - NEOMUTT_TEST_ITEM(test_mutt_buffer_new) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_pool_free) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_pool_get) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_pool_release) \