34
35
#ifndef STACK_ALLOC_H
35
36
#define STACK_ALLOC_H
43
* @def ALIGN(stack, size)
45
* Aligns the stack to a 'size' boundary
48
* @param size New size boundary
52
* @def PUSH(stack, size, type)
54
* Allocates 'size' elements of type 'type' on the stack
57
* @param size Number of elements
58
* @param type Type of element
62
* @def PUSHS(stack, type)
64
* Allocates a struct stack
67
* @param type Struct type
73
* Declare variable on stack
75
* @param var Variable to declare
79
* @def ALLOC(var, size, type)
81
* Allocate 'size' elements of 'type' on stack
83
* @param var Name of variable to allocate
84
* @param size Number of elements
85
* @param type Type of element
37
88
#ifdef ENABLE_VALGRIND
39
90
#include <valgrind/memcheck.h>
40
/*Aligns the stack to a 'size' boundary */
41
92
#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
43
/* Allocates 'size' elements of type 'type' on the stack */
44
94
#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))))
46
/* Allocates a struct stack */
47
96
#define PUSHS(stack, type) (VALGRIND_MAKE_NOACCESS(stack, 1000),ALIGN((stack),sizeof(long)),VALGRIND_MAKE_WRITABLE(stack, (sizeof(type))),(stack)+=(sizeof(type)),(type*)((stack)-(sizeof(type))))
52
/*Aligns the stack to a 'size' boundary */
53
100
#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
55
/* Allocates 'size' elements of type 'type' on the stack */
56
102
#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type))))
58
/* Allocates a struct stack */
59
104
#define PUSHS(stack, type) (ALIGN((stack),sizeof(long)),(stack)+=(sizeof(type)),(type*)((stack)-(sizeof(type))))
108
#if defined(VAR_ARRAYS)
110
#define ALLOC(var, size, type) type var[size]
111
#elif defined(USE_ALLOCA)
112
#define VARDECL(var) var
113
#define ALLOC(var, size, type) var = alloca(sizeof(type)*size)
115
#define VARDECL(var) var
116
#define ALLOC(var, size, type) var = PUSH(stack, size, type)