diff options
author | Pixel <pixel@nobis-crew.org> | 2009-02-17 02:28:08 +0100 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2009-02-17 02:28:08 +0100 |
commit | 2661812e3fc3f7385a5f8343172afc173f594efe (patch) | |
tree | 714f07eee0a74b1644df67f7c618ff7e0dc168f0 | |
parent | 5b99194c58385215cad973281a6f162d187ab971 (diff) |
Adding stalloc system.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | mpq-fs.c | 8 | ||||
-rw-r--r-- | stalloc.c | 63 | ||||
-rw-r--r-- | stalloc.h | 8 |
4 files changed, 78 insertions, 2 deletions
@@ -17,6 +17,7 @@ mpq-errors.c \ mpq-file.c \ mpq-fs.c \ mpq-misc.c \ +stalloc.c \ TARGET = mpqlib.a @@ -5,6 +5,7 @@ #include "hashtab.h" #include "mpq-fs.h" #include "mpq-misc.h" +#include "stalloc.h" #define MAX_FNAME 2048 @@ -38,10 +39,10 @@ static void add_file(struct mpq_archive_t * mpq_a, char * fname) { if (!hash_table) hash_table = hcreate(INITIAL_HASH_SIZE); - nfname = strnormalize(strdup(fname)); + nfname = strnormalize(st_strdup(fname)); hadd(hash_table, (uint8_t *) nfname, strlen(nfname), NULL); if (!hstuff(hash_table)) { - hstuff(hash_table) = malloc(sizeof(hash_entry)); + hstuff(hash_table) = st_alloc(sizeof(hash_entry)); } ((hash_entry *) hstuff(hash_table))->mpq_a = mpq_a; ((hash_entry *) hstuff(hash_table))->entry = entry; @@ -154,11 +155,14 @@ int mpqlib_fs_filelist(char * buffer) { void mpqlib_fs_shutdown() { if (!hash_table) return; +/* if (hfirst(hash_table)) do { free(hstuff(hash_table)); free(hkey(hash_table)); } while (hnext(hash_table)); +*/ + st_shutdown(); hdestroy(hash_table); hash_table = 0; } diff --git a/stalloc.c b/stalloc.c new file mode 100644 index 0000000..f8abb22 --- /dev/null +++ b/stalloc.c @@ -0,0 +1,63 @@ +#include <stdlib.h> + +static int nb_pools = 0; +static char ** mem_pools = 0; +static size_t * pools_size = 0; + +#define POOL_SIZE 16384 + +static int create_pool() { + nb_pools++; + mem_pools = (char **) realloc(mem_pools, sizeof(char *) * nb_pools); + pools_size = (size_t *) realloc(pools_size, sizeof(size_t) * nb_pools); + mem_pools[nb_pools - 1] = malloc(POOL_SIZE); + pools_size[nb_pools - 1] = 0; + + return nb_pools - 1; +} + +int search_pool(size_t s) { + int i; + + // This algorithm could use a little bit more of cleverness, but... *shrug* + // the whole purpose is to be used for small static strings, so... + for (i = 0; i < nb_pools; i++) { + if (s <= (POOL_SIZE - pools_size[i])) + return i; + } + + return -1; +} + +void * st_alloc(size_t s) { + int pool; + + if (s > POOL_SIZE) + return malloc(s); + + pool = search_pool(s); + if (pool < 0) + pool = create_pool(); + + pools_size[pool] += s; + + return mem_pools[pool] + pool_size[pool] - s; +} + +char * st_strdup(const char * src) { + int s = strlen(src) + 1; + char * r = (char *) st_alloc(s) + memcpy(r, src); + + return r; +} + +void st_shutdown() { + int i; + + for (i = 0; i < nb_pools; i++) { + free(mem_pools[i]); + } + free(mem_pools); + free(pools_size); +} diff --git a/stalloc.h b/stalloc.h new file mode 100644 index 0000000..4d5cbce --- /dev/null +++ b/stalloc.h @@ -0,0 +1,8 @@ +#ifndef __STALLOC_H__ +#define __STALLOC_H__ + +void * st_alloc(size_t s); +char * st_strdup(const char * src); +void st_shutdown(); + +#endif |