Path: blob/master/dep/cubeb/subprojects/speex/stack_alloc.h
4247 views
/* Copyright (C) 2002 Jean-Marc Valin */1/**2@file stack_alloc.h3@brief Temporary memory allocation on stack4*/5/*6Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions8are met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in the15documentation and/or other materials provided with the distribution.1617- Neither the name of the Xiph.org Foundation nor the names of its18contributors may be used to endorse or promote products derived from19this software without specific prior written permission.2021THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS22``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT23LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR24A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR25CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,26EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,27PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/3334#ifndef STACK_ALLOC_H35#define STACK_ALLOC_H3637#ifdef USE_ALLOCA38# ifdef WIN3239# include <malloc.h>40# else41# ifdef HAVE_ALLOCA_H42# include <alloca.h>43# else44# include <stdlib.h>45# endif46# endif47#endif4849/**50* @def ALIGN(stack, size)51*52* Aligns the stack to a 'size' boundary53*54* @param stack Stack55* @param size New size boundary56*/5758/**59* @def PUSH(stack, size, type)60*61* Allocates 'size' elements of type 'type' on the stack62*63* @param stack Stack64* @param size Number of elements65* @param type Type of element66*/6768/**69* @def VARDECL(var)70*71* Declare variable on stack72*73* @param var Variable to declare74*/7576/**77* @def ALLOC(var, size, type)78*79* Allocate 'size' elements of 'type' on stack80*81* @param var Name of variable to allocate82* @param size Number of elements83* @param type Type of element84*/8586#ifdef ENABLE_VALGRIND8788#include <valgrind/memcheck.h>8990#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))9192#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))))9394#else9596#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))9798#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type))))99100#endif101102#if defined(VAR_ARRAYS)103#define VARDECL(var)104#define ALLOC(var, size, type) type var[size]105#elif defined(USE_ALLOCA)106#define VARDECL(var) var107#define ALLOC(var, size, type) var = alloca(sizeof(type)*(size))108#else109#define VARDECL(var) var110#define ALLOC(var, size, type) var = PUSH(stack, size, type)111#endif112113114#endif115116117