153a5a1b3Sopenharmony_ci/* Copyright (C) 2002 Jean-Marc Valin */ 253a5a1b3Sopenharmony_ci/** 353a5a1b3Sopenharmony_ci @file stack_alloc.h 453a5a1b3Sopenharmony_ci @brief Temporary memory allocation on stack 553a5a1b3Sopenharmony_ci*/ 653a5a1b3Sopenharmony_ci/* 753a5a1b3Sopenharmony_ci Redistribution and use in source and binary forms, with or without 853a5a1b3Sopenharmony_ci modification, are permitted provided that the following conditions 953a5a1b3Sopenharmony_ci are met: 1053a5a1b3Sopenharmony_ci 1153a5a1b3Sopenharmony_ci - Redistributions of source code must retain the above copyright 1253a5a1b3Sopenharmony_ci notice, this list of conditions and the following disclaimer. 1353a5a1b3Sopenharmony_ci 1453a5a1b3Sopenharmony_ci - Redistributions in binary form must reproduce the above copyright 1553a5a1b3Sopenharmony_ci notice, this list of conditions and the following disclaimer in the 1653a5a1b3Sopenharmony_ci documentation and/or other materials provided with the distribution. 1753a5a1b3Sopenharmony_ci 1853a5a1b3Sopenharmony_ci - Neither the name of the Xiph.org Foundation nor the names of its 1953a5a1b3Sopenharmony_ci contributors may be used to endorse or promote products derived from 2053a5a1b3Sopenharmony_ci this software without specific prior written permission. 2153a5a1b3Sopenharmony_ci 2253a5a1b3Sopenharmony_ci THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2353a5a1b3Sopenharmony_ci ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2453a5a1b3Sopenharmony_ci LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2553a5a1b3Sopenharmony_ci A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 2653a5a1b3Sopenharmony_ci CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2753a5a1b3Sopenharmony_ci EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2853a5a1b3Sopenharmony_ci PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2953a5a1b3Sopenharmony_ci PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3053a5a1b3Sopenharmony_ci LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3153a5a1b3Sopenharmony_ci NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3253a5a1b3Sopenharmony_ci SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3353a5a1b3Sopenharmony_ci*/ 3453a5a1b3Sopenharmony_ci 3553a5a1b3Sopenharmony_ci#ifndef STACK_ALLOC_H 3653a5a1b3Sopenharmony_ci#define STACK_ALLOC_H 3753a5a1b3Sopenharmony_ci 3853a5a1b3Sopenharmony_ci#ifdef USE_ALLOCA 3953a5a1b3Sopenharmony_ci# ifdef WIN32 4053a5a1b3Sopenharmony_ci# include <malloc.h> 4153a5a1b3Sopenharmony_ci# else 4253a5a1b3Sopenharmony_ci# ifdef HAVE_ALLOCA_H 4353a5a1b3Sopenharmony_ci# include <alloca.h> 4453a5a1b3Sopenharmony_ci# else 4553a5a1b3Sopenharmony_ci# include <stdlib.h> 4653a5a1b3Sopenharmony_ci# endif 4753a5a1b3Sopenharmony_ci# endif 4853a5a1b3Sopenharmony_ci#endif 4953a5a1b3Sopenharmony_ci 5053a5a1b3Sopenharmony_ci/** 5153a5a1b3Sopenharmony_ci * @def ALIGN(stack, size) 5253a5a1b3Sopenharmony_ci * 5353a5a1b3Sopenharmony_ci * Aligns the stack to a 'size' boundary 5453a5a1b3Sopenharmony_ci * 5553a5a1b3Sopenharmony_ci * @param stack Stack 5653a5a1b3Sopenharmony_ci * @param size New size boundary 5753a5a1b3Sopenharmony_ci */ 5853a5a1b3Sopenharmony_ci 5953a5a1b3Sopenharmony_ci/** 6053a5a1b3Sopenharmony_ci * @def PUSH(stack, size, type) 6153a5a1b3Sopenharmony_ci * 6253a5a1b3Sopenharmony_ci * Allocates 'size' elements of type 'type' on the stack 6353a5a1b3Sopenharmony_ci * 6453a5a1b3Sopenharmony_ci * @param stack Stack 6553a5a1b3Sopenharmony_ci * @param size Number of elements 6653a5a1b3Sopenharmony_ci * @param type Type of element 6753a5a1b3Sopenharmony_ci */ 6853a5a1b3Sopenharmony_ci 6953a5a1b3Sopenharmony_ci/** 7053a5a1b3Sopenharmony_ci * @def VARDECL(var) 7153a5a1b3Sopenharmony_ci * 7253a5a1b3Sopenharmony_ci * Declare variable on stack 7353a5a1b3Sopenharmony_ci * 7453a5a1b3Sopenharmony_ci * @param var Variable to declare 7553a5a1b3Sopenharmony_ci */ 7653a5a1b3Sopenharmony_ci 7753a5a1b3Sopenharmony_ci/** 7853a5a1b3Sopenharmony_ci * @def ALLOC(var, size, type) 7953a5a1b3Sopenharmony_ci * 8053a5a1b3Sopenharmony_ci * Allocate 'size' elements of 'type' on stack 8153a5a1b3Sopenharmony_ci * 8253a5a1b3Sopenharmony_ci * @param var Name of variable to allocate 8353a5a1b3Sopenharmony_ci * @param size Number of elements 8453a5a1b3Sopenharmony_ci * @param type Type of element 8553a5a1b3Sopenharmony_ci */ 8653a5a1b3Sopenharmony_ci 8753a5a1b3Sopenharmony_ci#ifdef ENABLE_VALGRIND 8853a5a1b3Sopenharmony_ci 8953a5a1b3Sopenharmony_ci#include <valgrind/memcheck.h> 9053a5a1b3Sopenharmony_ci 9153a5a1b3Sopenharmony_ci#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) 9253a5a1b3Sopenharmony_ci 9353a5a1b3Sopenharmony_ci#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)))) 9453a5a1b3Sopenharmony_ci 9553a5a1b3Sopenharmony_ci#else 9653a5a1b3Sopenharmony_ci 9753a5a1b3Sopenharmony_ci#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) 9853a5a1b3Sopenharmony_ci 9953a5a1b3Sopenharmony_ci#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) 10053a5a1b3Sopenharmony_ci 10153a5a1b3Sopenharmony_ci#endif 10253a5a1b3Sopenharmony_ci 10353a5a1b3Sopenharmony_ci#if defined(VAR_ARRAYS) 10453a5a1b3Sopenharmony_ci#define VARDECL(var) 10553a5a1b3Sopenharmony_ci#define ALLOC(var, size, type) type var[size] 10653a5a1b3Sopenharmony_ci#elif defined(USE_ALLOCA) 10753a5a1b3Sopenharmony_ci#define VARDECL(var) var 10853a5a1b3Sopenharmony_ci#define ALLOC(var, size, type) var = alloca(sizeof(type)*(size)) 10953a5a1b3Sopenharmony_ci#else 11053a5a1b3Sopenharmony_ci#define VARDECL(var) var 11153a5a1b3Sopenharmony_ci#define ALLOC(var, size, type) var = PUSH(stack, size, type) 11253a5a1b3Sopenharmony_ci#endif 11353a5a1b3Sopenharmony_ci 11453a5a1b3Sopenharmony_ci 11553a5a1b3Sopenharmony_ci#endif 116