annotate src/opus-1.3/celt/stack_alloc.h @ 83:ae30d91d2ffe

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