1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libspeex_resampler/src/stack_alloc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +/* Copyright (C) 2002 Jean-Marc Valin */ 1.5 +/** 1.6 + @file stack_alloc.h 1.7 + @brief Temporary memory allocation on stack 1.8 +*/ 1.9 +/* 1.10 + Redistribution and use in source and binary forms, with or without 1.11 + modification, are permitted provided that the following conditions 1.12 + are met: 1.13 + 1.14 + - Redistributions of source code must retain the above copyright 1.15 + notice, this list of conditions and the following disclaimer. 1.16 + 1.17 + - Redistributions in binary form must reproduce the above copyright 1.18 + notice, this list of conditions and the following disclaimer in the 1.19 + documentation and/or other materials provided with the distribution. 1.20 + 1.21 + - Neither the name of the Xiph.org Foundation nor the names of its 1.22 + contributors may be used to endorse or promote products derived from 1.23 + this software without specific prior written permission. 1.24 + 1.25 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.26 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.27 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.28 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 1.29 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.30 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.31 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.32 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.33 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.34 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.35 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.36 +*/ 1.37 + 1.38 +#ifndef STACK_ALLOC_H 1.39 +#define STACK_ALLOC_H 1.40 + 1.41 +#ifdef WIN32 1.42 +# include <malloc.h> 1.43 +# ifndef alloca 1.44 +# define alloca(_x) _alloca(_x); 1.45 +# endif 1.46 +#else 1.47 +#ifdef HAVE_ALLOCA_H 1.48 +# include <alloca.h> 1.49 +# else 1.50 +# include <stdlib.h> 1.51 +# endif 1.52 +#endif 1.53 + 1.54 +/** 1.55 + * @def ALIGN(stack, size) 1.56 + * 1.57 + * Aligns the stack to a 'size' boundary 1.58 + * 1.59 + * @param stack Stack 1.60 + * @param size New size boundary 1.61 + */ 1.62 + 1.63 +/** 1.64 + * @def PUSH(stack, size, type) 1.65 + * 1.66 + * Allocates 'size' elements of type 'type' on the stack 1.67 + * 1.68 + * @param stack Stack 1.69 + * @param size Number of elements 1.70 + * @param type Type of element 1.71 + */ 1.72 + 1.73 +/** 1.74 + * @def VARDECL(var) 1.75 + * 1.76 + * Declare variable on stack 1.77 + * 1.78 + * @param var Variable to declare 1.79 + */ 1.80 + 1.81 +/** 1.82 + * @def ALLOC(var, size, type) 1.83 + * 1.84 + * Allocate 'size' elements of 'type' on stack 1.85 + * 1.86 + * @param var Name of variable to allocate 1.87 + * @param size Number of elements 1.88 + * @param type Type of element 1.89 + */ 1.90 + 1.91 +#ifdef ENABLE_VALGRIND 1.92 + 1.93 +#include <valgrind/memcheck.h> 1.94 + 1.95 +#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) 1.96 + 1.97 +#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)))) 1.98 + 1.99 +#else 1.100 + 1.101 +#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) 1.102 + 1.103 +#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) 1.104 + 1.105 +#endif 1.106 + 1.107 +#if defined(VAR_ARRAYS) 1.108 +#define VARDECL(var) 1.109 +#define ALLOC(var, size, type) type var[size] 1.110 +#elif defined(USE_ALLOCA) 1.111 +#define VARDECL(var) var 1.112 +#define ALLOC(var, size, type) var = alloca(sizeof(type)*(size)) 1.113 +#else 1.114 +#define VARDECL(var) var 1.115 +#define ALLOC(var, size, type) var = PUSH(stack, size, type) 1.116 +#endif 1.117 + 1.118 + 1.119 +#endif