diff options
| author | Mattias Andrée <maandree@kth.se> | 2016-03-05 22:27:04 +0100 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2016-03-05 22:28:33 +0100 |
| commit | c1f4d263ec1004512cdd6b38b351eb2fe2321c22 (patch) | |
| tree | ff884393424694c79eb87d16f13b151ba0233ec7 /src/zfree.c | |
| parent | zinit is now an inline function (diff) | |
| download | libzahl-c1f4d263ec1004512cdd6b38b351eb2fe2321c22.tar.gz libzahl-c1f4d263ec1004512cdd6b38b351eb2fe2321c22.tar.bz2 libzahl-c1f4d263ec1004512cdd6b38b351eb2fe2321c22.tar.xz | |
Add memory pool, also let the user know that libzahl is not designed for cryptography
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/zfree.c')
| -rw-r--r-- | src/zfree.c | 28 |
1 files changed, 27 insertions, 1 deletions
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; } |
