From 84d09f0d259d317aaddf552c9a4d8f2b0b640ef4 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Mon, 2 Jul 2012 14:43:20 +0200 Subject: [PATCH] core: Add simple temporary buffer allocator. --- include/core/GP_TempAlloc.h | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 include/core/GP_TempAlloc.h diff --git a/include/core/GP_TempAlloc.h b/include/core/GP_TempAlloc.h new file mode 100644 index 00000000..925b112e --- /dev/null +++ b/include/core/GP_TempAlloc.h @@ -0,0 +1,85 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2012 Cyril Hrubis * + * * + *****************************************************************************/ + +/* + + Temporary block allocator implementation. + + Creates pool for block allocation (small ones are done on the stack, bigger + using malloc). + + The usage is: + + // Creates new allocation pool + GP_TempAllocCrate(buf, 3 * 1024); + + // Make use of it + int *R = GP_TempAllocGet(buf, 1024); + int *G = GP_TempAllocGet(buf, 1024); + int *B = GP_TempAllocGet(buf, 1024); + + ... + + // Free it + GP_TempAllocDestroy(buf); + + */ + +#ifndef CORE_GP_TEMP_ALLOC_H +#define CORE_GP_TEMP_ALLOC_H + +#include +#include + +#include "core/GP_Common.h" + +#ifndef GP_ALLOCA_THRESHOLD +# define GP_ALLOCA_THRESHOLD 2048 +#endif + +struct GP_TempAlloc { + void *buffer; + size_t pos; + size_t size; +}; + +#define GP_TEMP_ALLOC(size) ({ \ + ((size) > GP_ALLOCA_THRESHOLD) ? malloc(size) : alloca(size); \ +}) + +#define GP_TempAllocCreate(name, bsize) \ + struct GP_TempAlloc name = {.size = (bsize), .pos = 0, \ + .buffer = GP_TEMP_ALLOC(bsize)}; + +#define GP_TempAllocGet(self, bsize) ({ \ + GP_ASSERT(self.pos + bsize <= self.size); \ + size_t _pos = self.pos; \ + self.pos += bsize; \ + (void*)(((char*)(self.buffer)) + _pos); \ +}) + +#define GP_TempAllocFree(self) do { \ + if (self.size > GP_ALLOCA_THRESHOLD) \ + free(self.buffer); \ +} while (0) + +#endif /* CORE_GP_TEMP_ALLOC_H */ -- 2.11.4.GIT