| 1 | /* Copyright (C) 2002 Jean-Marc Valin |
| 2 | File: stack_alloc.h |
| 3 | |
| 4 | Temporary memory allocation on stack |
| 5 | |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions |
| 8 | are met: |
| 9 | |
| 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | |
| 13 | - Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the |
| 15 | documentation and/or other materials provided with the distribution. |
| 16 | |
| 17 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 18 | contributors may be used to endorse or promote products derived from |
| 19 | this software without specific prior written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef STACK_ALLOC_H |
| 35 | #define STACK_ALLOC_H |
| 36 | |
| 37 | #ifdef USE_ALLOCA |
| 38 | #include <alloca.h> |
| 39 | #endif |
| 40 | |
| 41 | #ifdef ENABLE_VALGRIND |
| 42 | |
| 43 | #include <valgrind/memcheck.h> |
| 44 | /*Aligns the stack to a 'size' boundary */ |
| 45 | #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) |
| 46 | |
| 47 | /* Allocates 'size' elements of type 'type' on the stack */ |
| 48 | #define PUSH(stack, size, type) (VALGRIND_MAKE_NOACCESS(stack, 1000),ALIGN((stack),sizeof(type)),VALGRIND_MAKE_WRITABLE(stack, ((size)*sizeof(type))),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) |
| 49 | |
| 50 | /* Allocates a struct stack */ |
| 51 | #define PUSHS(stack, type) (VALGRIND_MAKE_NOACCESS(stack, 1000),ALIGN((stack),sizeof(long)),VALGRIND_MAKE_WRITABLE(stack, (sizeof(type))),(stack)+=(sizeof(type)),(type*)((stack)-(sizeof(type)))) |
| 52 | |
| 53 | #else |
| 54 | |
| 55 | |
| 56 | /*Aligns the stack to a 'size' boundary */ |
| 57 | #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) |
| 58 | |
| 59 | /* Allocates 'size' elements of type 'type' on the stack */ |
| 60 | #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) |
| 61 | |
| 62 | /* Allocates a struct stack */ |
| 63 | #define PUSHS(stack, type) (ALIGN((stack),sizeof(long)),(stack)+=(sizeof(type)),(type*)((stack)-(sizeof(type)))) |
| 64 | |
| 65 | #endif |
| 66 | |
| 67 | #if defined(VAR_ARRAYS) |
| 68 | #define VARDECL(var) |
| 69 | #define ALLOC(var, size, type) type var[size] |
| 70 | #elif defined(USE_ALLOCA) |
| 71 | #define VARDECL(var) var |
| 72 | #define ALLOC(var, size, type) var = alloca(sizeof(type)*size) |
| 73 | #else |
| 74 | #define VARDECL(var) var |
| 75 | #define ALLOC(var, size, type) var = PUSH(stack, size, type) |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | #endif |