1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/celt/stack_alloc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,178 @@ 1.4 +/* Copyright (C) 2002-2003 Jean-Marc Valin 1.5 + Copyright (C) 2007-2009 Xiph.Org Foundation */ 1.6 +/** 1.7 + @file stack_alloc.h 1.8 + @brief Temporary memory allocation on stack 1.9 +*/ 1.10 +/* 1.11 + Redistribution and use in source and binary forms, with or without 1.12 + modification, are permitted provided that the following conditions 1.13 + are met: 1.14 + 1.15 + - Redistributions of source code must retain the above copyright 1.16 + notice, this list of conditions and the following disclaimer. 1.17 + 1.18 + - Redistributions in binary form must reproduce the above copyright 1.19 + notice, this list of conditions and the following disclaimer in the 1.20 + documentation and/or other materials provided with the distribution. 1.21 + 1.22 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.23 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.24 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.25 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.26 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.27 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.28 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.29 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.30 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.31 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.32 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 +*/ 1.34 + 1.35 +#ifndef STACK_ALLOC_H 1.36 +#define STACK_ALLOC_H 1.37 + 1.38 +#include "opus_types.h" 1.39 +#include "opus_defines.h" 1.40 + 1.41 +#if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK)) 1.42 +#error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode." 1.43 +#endif 1.44 + 1.45 +#ifdef USE_ALLOCA 1.46 +# ifdef WIN32 1.47 +# include <malloc.h> 1.48 +# else 1.49 +# ifdef HAVE_ALLOCA_H 1.50 +# include <alloca.h> 1.51 +# else 1.52 +# include <stdlib.h> 1.53 +# endif 1.54 +# endif 1.55 +#endif 1.56 + 1.57 +/** 1.58 + * @def ALIGN(stack, size) 1.59 + * 1.60 + * Aligns the stack to a 'size' boundary 1.61 + * 1.62 + * @param stack Stack 1.63 + * @param size New size boundary 1.64 + */ 1.65 + 1.66 +/** 1.67 + * @def PUSH(stack, size, type) 1.68 + * 1.69 + * Allocates 'size' elements of type 'type' on the stack 1.70 + * 1.71 + * @param stack Stack 1.72 + * @param size Number of elements 1.73 + * @param type Type of element 1.74 + */ 1.75 + 1.76 +/** 1.77 + * @def VARDECL(var) 1.78 + * 1.79 + * Declare variable on stack 1.80 + * 1.81 + * @param var Variable to declare 1.82 + */ 1.83 + 1.84 +/** 1.85 + * @def ALLOC(var, size, type) 1.86 + * 1.87 + * Allocate 'size' elements of 'type' on stack 1.88 + * 1.89 + * @param var Name of variable to allocate 1.90 + * @param size Number of elements 1.91 + * @param type Type of element 1.92 + */ 1.93 + 1.94 +#if defined(VAR_ARRAYS) 1.95 + 1.96 +#define VARDECL(type, var) 1.97 +#define ALLOC(var, size, type) type var[size] 1.98 +#define SAVE_STACK 1.99 +#define RESTORE_STACK 1.100 +#define ALLOC_STACK 1.101 +/* C99 does not allow VLAs of size zero */ 1.102 +#define ALLOC_NONE 1 1.103 + 1.104 +#elif defined(USE_ALLOCA) 1.105 + 1.106 +#define VARDECL(type, var) type *var 1.107 + 1.108 +# ifdef WIN32 1.109 +# define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size))) 1.110 +# else 1.111 +# define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size))) 1.112 +# endif 1.113 + 1.114 +#define SAVE_STACK 1.115 +#define RESTORE_STACK 1.116 +#define ALLOC_STACK 1.117 +#define ALLOC_NONE 0 1.118 + 1.119 +#else 1.120 + 1.121 +#ifdef CELT_C 1.122 +char *global_stack=0; 1.123 +#else 1.124 +extern char *global_stack; 1.125 +#endif /* CELT_C */ 1.126 + 1.127 +#ifdef ENABLE_VALGRIND 1.128 + 1.129 +#include <valgrind/memcheck.h> 1.130 + 1.131 +#ifdef CELT_C 1.132 +char *global_stack_top=0; 1.133 +#else 1.134 +extern char *global_stack_top; 1.135 +#endif /* CELT_C */ 1.136 + 1.137 +#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) 1.138 +#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)))) 1.139 +#define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)) 1.140 +#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; 1.141 + 1.142 +#else 1.143 + 1.144 +#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) 1.145 +#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char)))) 1.146 +#define RESTORE_STACK (global_stack = _saved_stack) 1.147 +#define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack; 1.148 + 1.149 +#endif /* ENABLE_VALGRIND */ 1.150 + 1.151 +#include "os_support.h" 1.152 +#define VARDECL(type, var) type *var 1.153 +#define ALLOC(var, size, type) var = PUSH(global_stack, size, type) 1.154 +#define SAVE_STACK char *_saved_stack = global_stack; 1.155 +#define ALLOC_NONE 0 1.156 + 1.157 +#endif /* VAR_ARRAYS */ 1.158 + 1.159 + 1.160 +#ifdef ENABLE_VALGRIND 1.161 + 1.162 +#include <valgrind/memcheck.h> 1.163 +#define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr)) 1.164 +#define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value) 1.165 +#define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr)) 1.166 +#define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value) 1.167 +#define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0) 1.168 +#define OPUS_FPRINTF fprintf 1.169 + 1.170 +#else 1.171 + 1.172 +static OPUS_INLINE int _opus_false(void) {return 0;} 1.173 +#define OPUS_CHECK_ARRAY(ptr, len) _opus_false() 1.174 +#define OPUS_CHECK_VALUE(value) _opus_false() 1.175 +#define OPUS_PRINT_INT(value) do{}while(0) 1.176 +#define OPUS_FPRINTF (void) 1.177 + 1.178 +#endif 1.179 + 1.180 + 1.181 +#endif /* STACK_ALLOC_H */