Debian Codegraph @

external/opusfile/opus/celt/stack_alloc.h

raw
1 /* Copyright (C) 2002-2003 Jean-Marc Valin
2 Copyright (C) 2007-2009 Xiph.Org Foundation */
3 /**
4 @file stack_alloc.h
5 @brief Temporary memory allocation on stack
6 */
7 /*
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14
15 - Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef STACK_ALLOC_H
33 #define STACK_ALLOC_H
34
35 #include "opus__config.h"
36 #include "opus_types.h"
37 #include "opus_defines.h"
38
39 #if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK))
40 #error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode."
41 #endif
42
43 #ifdef USE_ALLOCA
44 # ifdef WIN32
45 # include <malloc.h>
46 # else
47 # ifdef HAVE_ALLOCA_H
48 # include <alloca.h>
49 # else
50 # include <stdlib.h>
51 # endif
52 # endif
53 #endif
54
55 /**
56 * @def ALIGN(stack, size)
57 *
58 * Aligns the stack to a 'size' boundary
59 *
60 * @param stack Stack
61 * @param size New size boundary
62 */
63
64 /**
65 * @def PUSH(stack, size, type)
66 *
67 * Allocates 'size' elements of type 'type' on the stack
68 *
69 * @param stack Stack
70 * @param size Number of elements
71 * @param type Type of element
72 */
73
74 /**
75 * @def VARDECL(var)
76 *
77 * Declare variable on stack
78 *
79 * @param var Variable to declare
80 */
81
82 /**
83 * @def ALLOC(var, size, type)
84 *
85 * Allocate 'size' elements of 'type' on stack
86 *
87 * @param var Name of variable to allocate
88 * @param size Number of elements
89 * @param type Type of element
90 */
91
92 #if defined(VAR_ARRAYS)
93
94 #define VARDECL(type, var)
95 #define ALLOC(var, size, type) type var[size]
96 #define SAVE_STACK
97 #define RESTORE_STACK
98 #define ALLOC_STACK
99 /* C99 does not allow VLAs of size zero */
100 #define ALLOC_NONE 1
101
102 #elif defined(USE_ALLOCA)
103
104 #define VARDECL(type, var) type *var
105
106 # ifdef WIN32
107 # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
108 # else
109 # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
110 # endif
111
112 #define SAVE_STACK
113 #define RESTORE_STACK
114 #define ALLOC_STACK
115 #define ALLOC_NONE 0
116
117 #else
118
119 #ifdef CELT_C
120 char *scratch_ptr=0;
121 char *global_stack=0;
122 #else
123 extern char *global_stack;
124 extern char *scratch_ptr;
125 #endif /* CELT_C */
126
127 #ifdef ENABLE_VALGRIND
128
129 #include <valgrind/memcheck.h>
130
131 #ifdef CELT_C
132 char *global_stack_top=0;
133 #else
134 extern char *global_stack_top;
135 #endif /* CELT_C */
136
137 #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
138 #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))))
139 #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
140 #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;
141
142 #else
143
144 #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
145 #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
146 #if 0 /* Set this to 1 to instrument pseudostack usage */
147 #define RESTORE_STACK (printf("%ld %s:%d\n", global_stack-scratch_ptr, __FILE__, __LINE__),global_stack = _saved_stack)
148 #else
149 #define RESTORE_STACK (global_stack = _saved_stack)
150 #endif
151 #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? (scratch_ptr=opus_alloc_scratch(GLOBAL_STACK_SIZE)) : global_stack); _saved_stack = global_stack;
152
153 #endif /* ENABLE_VALGRIND */
154
155 #include "os_support.h"
156 #define VARDECL(type, var) type *var
157 #define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
158 #define SAVE_STACK char *_saved_stack = global_stack;
159 #define ALLOC_NONE 0
160
161 #endif /* VAR_ARRAYS */
162
163
164 #ifdef ENABLE_VALGRIND
165
166 #include <valgrind/memcheck.h>
167 #define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
168 #define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
169 #define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
170 #define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
171 #define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0)
172 #define OPUS_FPRINTF fprintf
173
174 #else
175
176 static OPUS_INLINE int _opus_false(void) {return 0;}
177 #define OPUS_CHECK_ARRAY(ptr, len) _opus_false()
178 #define OPUS_CHECK_VALUE(value) _opus_false()
179 #define OPUS_PRINT_INT(value) do{}while(0)
180 #define OPUS_FPRINTF (void)
181
182 #endif
183
184
185 #endif /* STACK_ALLOC_H */