From c1f4d263ec1004512cdd6b38b351eb2fe2321c22 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 5 Mar 2016 22:27:04 +0100 Subject: Add memory pool, also let the user know that libzahl is not designed for cryptography MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/zfree.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/zfree.c') diff --git a/src/zfree.c b/src/zfree.c index 95eb084..2a1680a 100644 --- a/src/zfree.c +++ b/src/zfree.c @@ -5,5 +5,31 @@ void zfree(z_t a) { - free(a->chars); + size_t i = 0, x, j; + zahl_char_t **new; + + if (!a->chars) + return; + + for (x = a->alloced; x; x >>= 1) + i += 1; + + j = libzahl_pool_n[i]++; + + if (j == libzahl_pool_alloc[i]) { + x = j ? ((j * 3) >> 1) : 128; + new = realloc(libzahl_pool[i], x * sizeof(zahl_char_t *)); + if (!new) { + free(a->chars); + free(libzahl_pool[i]); + libzahl_pool_n[i] = 0; + libzahl_pool[i] = 0; + libzahl_pool_alloc[i] = 0; + return; + } + libzahl_pool[i] = new; + libzahl_pool_alloc[i] = x; + } + + libzahl_pool[i][j] = a->chars; } -- cgit v1.2.3-70-g09d2