michael@0: /* Copyright (C) 2002 Jean-Marc Valin */ michael@0: /** michael@0: @file stack_alloc.h michael@0: @brief Temporary memory allocation on stack michael@0: */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: - Neither the name of the Xiph.org Foundation nor the names of its michael@0: contributors may be used to endorse or promote products derived from michael@0: this software without specific prior written permission. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR michael@0: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef STACK_ALLOC_H michael@0: #define STACK_ALLOC_H michael@0: michael@0: #ifdef WIN32 michael@0: # include michael@0: # ifndef alloca michael@0: # define alloca(_x) _alloca(_x); michael@0: # endif michael@0: #else michael@0: #ifdef HAVE_ALLOCA_H michael@0: # include michael@0: # else michael@0: # include michael@0: # endif michael@0: #endif michael@0: michael@0: /** michael@0: * @def ALIGN(stack, size) michael@0: * michael@0: * Aligns the stack to a 'size' boundary michael@0: * michael@0: * @param stack Stack michael@0: * @param size New size boundary michael@0: */ michael@0: michael@0: /** michael@0: * @def PUSH(stack, size, type) michael@0: * michael@0: * Allocates 'size' elements of type 'type' on the stack michael@0: * michael@0: * @param stack Stack michael@0: * @param size Number of elements michael@0: * @param type Type of element michael@0: */ michael@0: michael@0: /** michael@0: * @def VARDECL(var) michael@0: * michael@0: * Declare variable on stack michael@0: * michael@0: * @param var Variable to declare michael@0: */ michael@0: michael@0: /** michael@0: * @def ALLOC(var, size, type) michael@0: * michael@0: * Allocate 'size' elements of 'type' on stack michael@0: * michael@0: * @param var Name of variable to allocate michael@0: * @param size Number of elements michael@0: * @param type Type of element michael@0: */ michael@0: michael@0: #ifdef ENABLE_VALGRIND michael@0: michael@0: #include michael@0: michael@0: #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) michael@0: michael@0: #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: michael@0: #else michael@0: michael@0: #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) michael@0: michael@0: #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) michael@0: michael@0: #endif michael@0: michael@0: #if defined(VAR_ARRAYS) michael@0: #define VARDECL(var) michael@0: #define ALLOC(var, size, type) type var[size] michael@0: #elif defined(USE_ALLOCA) michael@0: #define VARDECL(var) var michael@0: #define ALLOC(var, size, type) var = alloca(sizeof(type)*(size)) michael@0: #else michael@0: #define VARDECL(var) var michael@0: #define ALLOC(var, size, type) var = PUSH(stack, size, type) michael@0: #endif michael@0: michael@0: michael@0: #endif