From 3abb513923adb1f5238a62dd7c64c433a08d9d62 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 26 Nov 2018 23:46:55 -0800 Subject: *: initial commit This is a simple allocator derived from the chunker in the sparkler rototiller particle system module. It's useful for pooling allocations of varying size with a lot of churn. It also gives a convenient way of discarding all allocations through a reset mechanism obviating the need for granular freeing, where applicable. --- src/example.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/example.c (limited to 'src/example.c') diff --git a/src/example.c b/src/example.c new file mode 100644 index 0000000..12383fa --- /dev/null +++ b/src/example.c @@ -0,0 +1,35 @@ +#include + +#include "pad.h" + +typedef struct foo_t { + int x, y, z; +} foo_t; + +#define CHUNK_CNT 256 + +int main(int argc, char *argv[]) +{ + foo_t *f[10 * CHUNK_CNT]; + pad_t *p; + + p = pad_new(sizeof(foo_t) * 256); + assert(p); + + for (int n = 0; n < 10; n++) { + for (int i = 0; i < 10 * CHUNK_CNT; i++) + assert(f[i] = pad_get(p, sizeof(foo_t))); + + for (int i = 0; i < 10 * CHUNK_CNT; i++) + pad_put(f[i]); + + for (int i = 0; i < 10 * CHUNK_CNT; i++) + assert(f[i] = pad_get(p, sizeof(foo_t))); + + pad_reset(p); + } + + pad_free(p); + + return 0; +} -- cgit v1.2.3