michael@0: /* Copyright (C) 2002-2003 Jean-Marc Valin michael@0: Copyright (C) 2007-2009 Xiph.Org Foundation */ 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: 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 COPYRIGHT OWNER michael@0: OR 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: #include "opus_types.h" michael@0: #include "opus_defines.h" michael@0: michael@0: #if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK)) michael@0: #error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode." michael@0: #endif michael@0: michael@0: #ifdef USE_ALLOCA michael@0: # ifdef WIN32 michael@0: # include 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: #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: #if defined(VAR_ARRAYS) michael@0: michael@0: #define VARDECL(type, var) michael@0: #define ALLOC(var, size, type) type var[size] michael@0: #define SAVE_STACK michael@0: #define RESTORE_STACK michael@0: #define ALLOC_STACK michael@0: /* C99 does not allow VLAs of size zero */ michael@0: #define ALLOC_NONE 1 michael@0: michael@0: #elif defined(USE_ALLOCA) michael@0: michael@0: #define VARDECL(type, var) type *var michael@0: michael@0: # ifdef WIN32 michael@0: # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size))) michael@0: # else michael@0: # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size))) michael@0: # endif michael@0: michael@0: #define SAVE_STACK michael@0: #define RESTORE_STACK michael@0: #define ALLOC_STACK michael@0: #define ALLOC_NONE 0 michael@0: michael@0: #else michael@0: michael@0: #ifdef CELT_C michael@0: char *global_stack=0; michael@0: #else michael@0: extern char *global_stack; michael@0: #endif /* CELT_C */ michael@0: michael@0: #ifdef ENABLE_VALGRIND michael@0: michael@0: #include michael@0: michael@0: #ifdef CELT_C michael@0: char *global_stack_top=0; michael@0: #else michael@0: extern char *global_stack_top; michael@0: #endif /* CELT_C */ michael@0: michael@0: #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) michael@0: #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: #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)) michael@0: #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: michael@0: #else michael@0: michael@0: #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) michael@0: #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: #define RESTORE_STACK (global_stack = _saved_stack) michael@0: #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: michael@0: #endif /* ENABLE_VALGRIND */ michael@0: michael@0: #include "os_support.h" michael@0: #define VARDECL(type, var) type *var michael@0: #define ALLOC(var, size, type) var = PUSH(global_stack, size, type) michael@0: #define SAVE_STACK char *_saved_stack = global_stack; michael@0: #define ALLOC_NONE 0 michael@0: michael@0: #endif /* VAR_ARRAYS */ michael@0: michael@0: michael@0: #ifdef ENABLE_VALGRIND michael@0: michael@0: #include michael@0: #define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr)) michael@0: #define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value) michael@0: #define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr)) michael@0: #define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value) michael@0: #define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0) michael@0: #define OPUS_FPRINTF fprintf michael@0: michael@0: #else michael@0: michael@0: static OPUS_INLINE int _opus_false(void) {return 0;} michael@0: #define OPUS_CHECK_ARRAY(ptr, len) _opus_false() michael@0: #define OPUS_CHECK_VALUE(value) _opus_false() michael@0: #define OPUS_PRINT_INT(value) do{}while(0) michael@0: #define OPUS_FPRINTF (void) michael@0: michael@0: #endif michael@0: michael@0: michael@0: #endif /* STACK_ALLOC_H */