media/libopus/celt/stack_alloc.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Copyright (C) 2002-2003 Jean-Marc Valin
michael@0 2 Copyright (C) 2007-2009 Xiph.Org Foundation */
michael@0 3 /**
michael@0 4 @file stack_alloc.h
michael@0 5 @brief Temporary memory allocation on stack
michael@0 6 */
michael@0 7 /*
michael@0 8 Redistribution and use in source and binary forms, with or without
michael@0 9 modification, are permitted provided that the following conditions
michael@0 10 are met:
michael@0 11
michael@0 12 - Redistributions of source code must retain the above copyright
michael@0 13 notice, this list of conditions and the following disclaimer.
michael@0 14
michael@0 15 - Redistributions in binary form must reproduce the above copyright
michael@0 16 notice, this list of conditions and the following disclaimer in the
michael@0 17 documentation and/or other materials provided with the distribution.
michael@0 18
michael@0 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
michael@0 23 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 30 */
michael@0 31
michael@0 32 #ifndef STACK_ALLOC_H
michael@0 33 #define STACK_ALLOC_H
michael@0 34
michael@0 35 #include "opus_types.h"
michael@0 36 #include "opus_defines.h"
michael@0 37
michael@0 38 #if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK))
michael@0 39 #error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode."
michael@0 40 #endif
michael@0 41
michael@0 42 #ifdef USE_ALLOCA
michael@0 43 # ifdef WIN32
michael@0 44 # include <malloc.h>
michael@0 45 # else
michael@0 46 # ifdef HAVE_ALLOCA_H
michael@0 47 # include <alloca.h>
michael@0 48 # else
michael@0 49 # include <stdlib.h>
michael@0 50 # endif
michael@0 51 # endif
michael@0 52 #endif
michael@0 53
michael@0 54 /**
michael@0 55 * @def ALIGN(stack, size)
michael@0 56 *
michael@0 57 * Aligns the stack to a 'size' boundary
michael@0 58 *
michael@0 59 * @param stack Stack
michael@0 60 * @param size New size boundary
michael@0 61 */
michael@0 62
michael@0 63 /**
michael@0 64 * @def PUSH(stack, size, type)
michael@0 65 *
michael@0 66 * Allocates 'size' elements of type 'type' on the stack
michael@0 67 *
michael@0 68 * @param stack Stack
michael@0 69 * @param size Number of elements
michael@0 70 * @param type Type of element
michael@0 71 */
michael@0 72
michael@0 73 /**
michael@0 74 * @def VARDECL(var)
michael@0 75 *
michael@0 76 * Declare variable on stack
michael@0 77 *
michael@0 78 * @param var Variable to declare
michael@0 79 */
michael@0 80
michael@0 81 /**
michael@0 82 * @def ALLOC(var, size, type)
michael@0 83 *
michael@0 84 * Allocate 'size' elements of 'type' on stack
michael@0 85 *
michael@0 86 * @param var Name of variable to allocate
michael@0 87 * @param size Number of elements
michael@0 88 * @param type Type of element
michael@0 89 */
michael@0 90
michael@0 91 #if defined(VAR_ARRAYS)
michael@0 92
michael@0 93 #define VARDECL(type, var)
michael@0 94 #define ALLOC(var, size, type) type var[size]
michael@0 95 #define SAVE_STACK
michael@0 96 #define RESTORE_STACK
michael@0 97 #define ALLOC_STACK
michael@0 98 /* C99 does not allow VLAs of size zero */
michael@0 99 #define ALLOC_NONE 1
michael@0 100
michael@0 101 #elif defined(USE_ALLOCA)
michael@0 102
michael@0 103 #define VARDECL(type, var) type *var
michael@0 104
michael@0 105 # ifdef WIN32
michael@0 106 # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
michael@0 107 # else
michael@0 108 # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
michael@0 109 # endif
michael@0 110
michael@0 111 #define SAVE_STACK
michael@0 112 #define RESTORE_STACK
michael@0 113 #define ALLOC_STACK
michael@0 114 #define ALLOC_NONE 0
michael@0 115
michael@0 116 #else
michael@0 117
michael@0 118 #ifdef CELT_C
michael@0 119 char *global_stack=0;
michael@0 120 #else
michael@0 121 extern char *global_stack;
michael@0 122 #endif /* CELT_C */
michael@0 123
michael@0 124 #ifdef ENABLE_VALGRIND
michael@0 125
michael@0 126 #include <valgrind/memcheck.h>
michael@0 127
michael@0 128 #ifdef CELT_C
michael@0 129 char *global_stack_top=0;
michael@0 130 #else
michael@0 131 extern char *global_stack_top;
michael@0 132 #endif /* CELT_C */
michael@0 133
michael@0 134 #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
michael@0 135 #define PUSH(stack, size, type) (VALGRIND_MAKE_MEM_NOACCESS(stack, global_stack_top-stack),ALIGN((stack),sizeof(type)/sizeof(char)),VALGRIND_MAKE_MEM_UNDEFINED(stack, ((size)*sizeof(type)/sizeof(char))),(stack)+=(2*(size)*sizeof(type)/sizeof(char)),(type*)((stack)-(2*(size)*sizeof(type)/sizeof(char))))
michael@0 136 #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
michael@0 137 #define ALLOC_STACK char *_saved_stack; ((global_stack = (global_stack==0) ? ((global_stack_top=opus_alloc_scratch(GLOBAL_STACK_SIZE*2)+(GLOBAL_STACK_SIZE*2))-(GLOBAL_STACK_SIZE*2)) : global_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)); _saved_stack = global_stack;
michael@0 138
michael@0 139 #else
michael@0 140
michael@0 141 #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
michael@0 142 #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
michael@0 143 #define RESTORE_STACK (global_stack = _saved_stack)
michael@0 144 #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack;
michael@0 145
michael@0 146 #endif /* ENABLE_VALGRIND */
michael@0 147
michael@0 148 #include "os_support.h"
michael@0 149 #define VARDECL(type, var) type *var
michael@0 150 #define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
michael@0 151 #define SAVE_STACK char *_saved_stack = global_stack;
michael@0 152 #define ALLOC_NONE 0
michael@0 153
michael@0 154 #endif /* VAR_ARRAYS */
michael@0 155
michael@0 156
michael@0 157 #ifdef ENABLE_VALGRIND
michael@0 158
michael@0 159 #include <valgrind/memcheck.h>
michael@0 160 #define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
michael@0 161 #define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
michael@0 162 #define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
michael@0 163 #define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
michael@0 164 #define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0)
michael@0 165 #define OPUS_FPRINTF fprintf
michael@0 166
michael@0 167 #else
michael@0 168
michael@0 169 static OPUS_INLINE int _opus_false(void) {return 0;}
michael@0 170 #define OPUS_CHECK_ARRAY(ptr, len) _opus_false()
michael@0 171 #define OPUS_CHECK_VALUE(value) _opus_false()
michael@0 172 #define OPUS_PRINT_INT(value) do{}while(0)
michael@0 173 #define OPUS_FPRINTF (void)
michael@0 174
michael@0 175 #endif
michael@0 176
michael@0 177
michael@0 178 #endif /* STACK_ALLOC_H */

mercurial