1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 Temporary block allocator implementation.
27 Creates pool for block allocation (small ones are done on the stack, bigger
32 // Creates new allocation pool
33 GP_TempAllocCrate(buf, 3 * 1024);
36 int *R = GP_TempAllocGet(buf, 1024);
37 int *G = GP_TempAllocGet(buf, 1024);
38 int *B = GP_TempAllocGet(buf, 1024);
43 GP_TempAllocDestroy(buf);
47 #ifndef CORE_GP_TEMP_ALLOC_H
48 #define CORE_GP_TEMP_ALLOC_H
53 #include "core/GP_Common.h"
55 #ifndef GP_ALLOCA_THRESHOLD
56 # define GP_ALLOCA_THRESHOLD 2048
65 #define GP_TEMP_ALLOC(size) ({ \
66 ((size) > GP_ALLOCA_THRESHOLD) ? malloc(size) : alloca(size); \
69 #define GP_TempAllocCreate(name, bsize) \
70 struct GP_TempAlloc name = {.size = (bsize), .pos = 0, \
71 .buffer = GP_TEMP_ALLOC(bsize)};
73 #define GP_TempAllocGet(self, bsize) ({ \
74 GP_ASSERT(self.pos + bsize <= self.size); \
75 size_t _pos = self.pos; \
77 (void*)(((char*)(self.buffer)) + _pos); \
80 #define GP_TempAllocFree(self) do { \
81 if (self.size > GP_ALLOCA_THRESHOLD) \
85 #endif /* CORE_GP_TEMP_ALLOC_H */