Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added memory pools to make clean up of temporary buffers simpler.

  • Loading branch information...
commit 8047cbb53abe9b17c4bc65e3d3ba176caffdddc4 1 parent d67f1b7
Arnar Mar Hrafnkelsson authored
Showing with 45 additions and 0 deletions.
  1. +14 −0 imio.h
  2. +31 −0 io.c
View
14 imio.h
@@ -36,6 +36,20 @@ void* myrealloc(void *p, size_t newsize);
#endif /* IMAGER_MALLOC_DEBUG */
+
+typedef struct i_mempool {
+ void **p;
+ unsigned int alloc;
+ unsigned int used;
+} i_mempool;
+
+void i_mempool_init(i_mempool *mp);
+void i_mempool_extend(i_mempool *mp);
+void *i_mempool_alloc(i_mempool *mp, size_t size);
+void i_mempool_destroy(i_mempool *mp);
+
+
+
#ifdef _MSC_VER
#undef min
#undef max
View
31 io.c
@@ -260,6 +260,37 @@ myrealloc(void *block, size_t size) {
+/* memory pool implementation */
+
+void
+i_mempool_init(i_mempool *mp) {
+ mp->alloc = 10;
+ mp->used = 0;
+ mp->p = mymalloc(sizeof(void*)*mp->alloc);
+}
+
+void
+i_mempool_extend(i_mempool *mp) {
+ mp->p = myrealloc(mp->p, mp->alloc * 2);
+ mp->alloc *=2;
+}
+
+void *
+i_mempool_alloc(i_mempool *mp, size_t size) {
+ if (mp->used == mp->alloc) i_mempool_extend(mp);
+ mp->p[mp->used] = mymalloc(size);
+ mp->used++;
+ return mp->p[mp->used-1];
+}
+
+
+void
+i_mempool_destroy(i_mempool *mp) {
+ unsigned int i;
+ for(i=0; i<mp->used; i++) myfree(mp->p[i]);
+ myfree(mp->p);
+}
+
/* Should these really be here? */
Please sign in to comment.
Something went wrong with that request. Please try again.