Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Copyright (C) 2002 Jean-Marc Valin */ |
michael@0 | 2 | /** |
michael@0 | 3 | @file stack_alloc.h |
michael@0 | 4 | @brief Temporary memory allocation on stack |
michael@0 | 5 | */ |
michael@0 | 6 | /* |
michael@0 | 7 | Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | modification, are permitted provided that the following conditions |
michael@0 | 9 | are met: |
michael@0 | 10 | |
michael@0 | 11 | - Redistributions of source code must retain the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer. |
michael@0 | 13 | |
michael@0 | 14 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 15 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 16 | documentation and/or other materials provided with the distribution. |
michael@0 | 17 | |
michael@0 | 18 | - Neither the name of the Xiph.org Foundation nor the names of its |
michael@0 | 19 | contributors may be used to endorse or promote products derived from |
michael@0 | 20 | this software without specific prior written permission. |
michael@0 | 21 | |
michael@0 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 23 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
michael@0 | 26 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 27 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 28 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 30 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 31 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 33 | */ |
michael@0 | 34 | |
michael@0 | 35 | #ifndef STACK_ALLOC_H |
michael@0 | 36 | #define STACK_ALLOC_H |
michael@0 | 37 | |
michael@0 | 38 | #ifdef WIN32 |
michael@0 | 39 | # include <malloc.h> |
michael@0 | 40 | # ifndef alloca |
michael@0 | 41 | # define alloca(_x) _alloca(_x); |
michael@0 | 42 | # endif |
michael@0 | 43 | #else |
michael@0 | 44 | #ifdef HAVE_ALLOCA_H |
michael@0 | 45 | # include <alloca.h> |
michael@0 | 46 | # else |
michael@0 | 47 | # include <stdlib.h> |
michael@0 | 48 | # endif |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * @def ALIGN(stack, size) |
michael@0 | 53 | * |
michael@0 | 54 | * Aligns the stack to a 'size' boundary |
michael@0 | 55 | * |
michael@0 | 56 | * @param stack Stack |
michael@0 | 57 | * @param size New size boundary |
michael@0 | 58 | */ |
michael@0 | 59 | |
michael@0 | 60 | /** |
michael@0 | 61 | * @def PUSH(stack, size, type) |
michael@0 | 62 | * |
michael@0 | 63 | * Allocates 'size' elements of type 'type' on the stack |
michael@0 | 64 | * |
michael@0 | 65 | * @param stack Stack |
michael@0 | 66 | * @param size Number of elements |
michael@0 | 67 | * @param type Type of element |
michael@0 | 68 | */ |
michael@0 | 69 | |
michael@0 | 70 | /** |
michael@0 | 71 | * @def VARDECL(var) |
michael@0 | 72 | * |
michael@0 | 73 | * Declare variable on stack |
michael@0 | 74 | * |
michael@0 | 75 | * @param var Variable to declare |
michael@0 | 76 | */ |
michael@0 | 77 | |
michael@0 | 78 | /** |
michael@0 | 79 | * @def ALLOC(var, size, type) |
michael@0 | 80 | * |
michael@0 | 81 | * Allocate 'size' elements of 'type' on stack |
michael@0 | 82 | * |
michael@0 | 83 | * @param var Name of variable to allocate |
michael@0 | 84 | * @param size Number of elements |
michael@0 | 85 | * @param type Type of element |
michael@0 | 86 | */ |
michael@0 | 87 | |
michael@0 | 88 | #ifdef ENABLE_VALGRIND |
michael@0 | 89 | |
michael@0 | 90 | #include <valgrind/memcheck.h> |
michael@0 | 91 | |
michael@0 | 92 | #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) |
michael@0 | 93 | |
michael@0 | 94 | #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)))) |
michael@0 | 95 | |
michael@0 | 96 | #else |
michael@0 | 97 | |
michael@0 | 98 | #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) |
michael@0 | 99 | |
michael@0 | 100 | #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) |
michael@0 | 101 | |
michael@0 | 102 | #endif |
michael@0 | 103 | |
michael@0 | 104 | #if defined(VAR_ARRAYS) |
michael@0 | 105 | #define VARDECL(var) |
michael@0 | 106 | #define ALLOC(var, size, type) type var[size] |
michael@0 | 107 | #elif defined(USE_ALLOCA) |
michael@0 | 108 | #define VARDECL(var) var |
michael@0 | 109 | #define ALLOC(var, size, type) var = alloca(sizeof(type)*(size)) |
michael@0 | 110 | #else |
michael@0 | 111 | #define VARDECL(var) var |
michael@0 | 112 | #define ALLOC(var, size, type) var = PUSH(stack, size, type) |
michael@0 | 113 | #endif |
michael@0 | 114 | |
michael@0 | 115 | |
michael@0 | 116 | #endif |