134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/* Copyright (C) 2002 Jean-Marc Valin */
234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/**
334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   @file stack_alloc.h
434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   @brief Temporary memory allocation on stack
534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)*/
634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/*
734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   Redistribution and use in source and binary forms, with or without
834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   modification, are permitted provided that the following conditions
934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   are met:
1034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   - Redistributions of source code must retain the above copyright
1234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   notice, this list of conditions and the following disclaimer.
1334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   - Redistributions in binary form must reproduce the above copyright
1534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   notice, this list of conditions and the following disclaimer in the
1634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   documentation and/or other materials provided with the distribution.
1734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   - Neither the name of the Xiph.org Foundation nor the names of its
1934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   contributors may be used to endorse or promote products derived from
2034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   this software without specific prior written permission.
2134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
2234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
2634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)*/
3434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
3534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#ifndef STACK_ALLOC_H
3634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define STACK_ALLOC_H
3734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
3834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#ifdef USE_ALLOCA
3934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)# ifdef WIN32
4034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#  include <malloc.h>
4134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)# else
4234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#  ifdef HAVE_ALLOCA_H
4334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#   include <alloca.h>
4434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#  else
4534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#   include <stdlib.h>
4634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#  endif
4734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)# endif
4834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#endif
4934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
5034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/**
5134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @def ALIGN(stack, size)
5234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
5334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * Aligns the stack to a 'size' boundary
5434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
5534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param stack Stack
5634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param size  New size boundary
5734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) */
5834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
5934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/**
6034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @def PUSH(stack, size, type)
6134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
6234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * Allocates 'size' elements of type 'type' on the stack
6334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
6434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param stack Stack
6534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param size  Number of elements
6634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param type  Type of element
6734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) */
6834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
6934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/**
7034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @def VARDECL(var)
7134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
7234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * Declare variable on stack
7334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
7434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param var Variable to declare
7534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) */
7634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
7734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/**
7834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @def ALLOC(var, size, type)
7934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
8034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * Allocate 'size' elements of 'type' on stack
8134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) *
8234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param var  Name of variable to allocate
8334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param size Number of elements
8434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) * @param type Type of element
8534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles) */
8634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
8734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#ifdef ENABLE_VALGRIND
8834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
8934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#include <valgrind/memcheck.h>
9034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
9134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
9234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
9334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#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))))
9434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
9534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#else
9634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
9734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
9834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
9934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type))))
10034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
10134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#endif
10234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
10334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#if defined(VAR_ARRAYS)
10434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define VARDECL(var)
10534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define ALLOC(var, size, type) type var[size]
10634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#elif defined(USE_ALLOCA)
10734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define VARDECL(var) var
10834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define ALLOC(var, size, type) var = alloca(sizeof(type)*(size))
10934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#else
11034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define VARDECL(var) var
11134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#define ALLOC(var, size, type) var = PUSH(stack, size, type)
11234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#endif
11334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
11434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
11534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#endif
116