Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/stack_alloc.h
Line
Count
Source
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_types.h"
36
#include "opus_defines.h"
37
38
#if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK))
39
#error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode."
40
#endif
41
42
#ifdef USE_ALLOCA
43
# ifdef _WIN32
44
#  include <malloc.h>
45
# else
46
#  ifdef HAVE_ALLOCA_H
47
#   include <alloca.h>
48
#  else
49
#   include <stdlib.h>
50
#  endif
51
# endif
52
#endif
53
54
/**
55
 * @def ALIGN(stack, size)
56
 *
57
 * Aligns the stack to a 'size' boundary
58
 *
59
 * @param stack Stack
60
 * @param size  New size boundary
61
 */
62
63
/**
64
 * @def PUSH(stack, size, type)
65
 *
66
 * Allocates 'size' elements of type 'type' on the stack
67
 *
68
 * @param stack Stack
69
 * @param size  Number of elements
70
 * @param type  Type of element
71
 */
72
73
/**
74
 * @def VARDECL(var)
75
 *
76
 * Declare variable on stack
77
 *
78
 * @param var Variable to declare
79
 */
80
81
/**
82
 * @def ALLOC(var, size, type)
83
 *
84
 * Allocate 'size' elements of 'type' on stack
85
 *
86
 * @param var  Name of variable to allocate
87
 * @param size Number of elements
88
 * @param type Type of element
89
 */
90
91
#if defined(VAR_ARRAYS)
92
93
#define VARDECL(type, var)
94
9.86M
#define ALLOC(var, size, type) type var[size]
95
#define SAVE_STACK
96
#define RESTORE_STACK
97
#define ALLOC_STACK
98
/* C99 does not allow VLAs of size zero */
99
2.07M
#define ALLOC_NONE 1
100
101
#elif defined(USE_ALLOCA)
102
103
#define VARDECL(type, var) type *var
104
105
# ifdef _WIN32
106
#  define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
107
# else
108
#  define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
109
# endif
110
111
#define SAVE_STACK
112
#define RESTORE_STACK
113
#define ALLOC_STACK
114
#define ALLOC_NONE 0
115
116
#else
117
118
#ifdef CELT_C
119
char *scratch_ptr=0;
120
char *global_stack=0;
121
#else
122
extern char *global_stack;
123
extern char *scratch_ptr;
124
#endif /* CELT_C */
125
126
#if __STDC_VERSION__ >= 201112L
127
#  include <stdalign.h>
128
#  define ALIGNOF(T) alignof(T)
129
#elif defined(__GNUC__) || defined(__clang__)
130
#  define ALIGNOF(T) __alignof__(T)
131
#else
132
# include <stddef.h>
133
# ifdef __cplusplus
134
template <typename T>
135
struct alignment_helper {
136
    char c;
137
    T member;
138
};
139
# define ALIGNOF(T) (offsetof(alignment_helper<T>, member))
140
# else
141
# define ALIGNOF(T) (offsetof(struct { char c; T member; }, member))
142
# endif
143
#endif
144
145
#ifdef ENABLE_VALGRIND
146
147
#include <valgrind/memcheck.h>
148
149
#ifdef CELT_C
150
char *global_stack_top=0;
151
#else
152
extern char *global_stack_top;
153
#endif /* CELT_C */
154
155
#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
156
#define PUSH(stack, size, type) (VALGRIND_MAKE_MEM_NOACCESS(stack, global_stack_top-stack),ALIGN((stack),ALIGNOF(type)),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))))
157
#define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
158
#define ALLOC_STACK char *_saved_stack; ((global_stack = (global_stack==0) ? ((global_stack_top=(char*)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;
159
160
#else
161
162
#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
163
#ifdef ENABLE_HARDENING
164
#include "arch.h"
165
#define PUSH(stack, size, type) (ALIGN((stack),ALIGNOF(type)),(void)(((int)((size)*(sizeof(type)/(sizeof(char)))) <= (scratch_ptr)+GLOBAL_STACK_SIZE-(stack))?0:CELT_FATAL("pseudostack overflow")),(stack)+=(size)*(sizeof(type)/(sizeof(char))),(type*)(void*)((stack)-(size)*(sizeof(type)/(sizeof(char)))))
166
#else
167
#define PUSH(stack, size, type) (ALIGN((stack),ALIGNOF(type)),(stack)+=(size)*(sizeof(type)/(sizeof(char))),(type*)(void*)((stack)-(size)*(sizeof(type)/(sizeof(char)))))
168
#endif
169
170
#if 0 /* Set this to 1 to instrument pseudostack usage */
171
#define RESTORE_STACK (printf("%ld %s:%d\n", global_stack-scratch_ptr, __FILE__, __LINE__),global_stack = _saved_stack)
172
#else
173
#define RESTORE_STACK (global_stack = _saved_stack)
174
#endif
175
#define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? (scratch_ptr=(char*)opus_alloc_scratch(GLOBAL_STACK_SIZE)) : global_stack); _saved_stack = global_stack;
176
177
#endif /* ENABLE_VALGRIND */
178
179
#include "os_support.h"
180
#define VARDECL(type, var) type *var
181
#define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
182
#define SAVE_STACK char *_saved_stack = global_stack;
183
#define ALLOC_NONE 0
184
185
#endif /* VAR_ARRAYS */
186
187
188
#ifdef ENABLE_VALGRIND
189
190
#include <valgrind/memcheck.h>
191
#define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
192
#define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
193
#define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
194
#define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
195
#define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0)
196
#define OPUS_FPRINTF fprintf
197
198
#else
199
200
665k
static OPUS_INLINE int _opus_false(void) {return 0;}
Unexecuted instantiation: celt.c:_opus_false
Unexecuted instantiation: pitch_sse.c:_opus_false
Unexecuted instantiation: opus_multistream_decoder.c:_opus_false
opus_decoder.c:_opus_false
Line
Count
Source
200
665k
static OPUS_INLINE int _opus_false(void) {return 0;}
Unexecuted instantiation: opus_multistream.c:_opus_false
Unexecuted instantiation: celt_decoder.c:_opus_false
Unexecuted instantiation: mdct.c:_opus_false
Unexecuted instantiation: modes.c:_opus_false
Unexecuted instantiation: pitch.c:_opus_false
Unexecuted instantiation: celt_lpc.c:_opus_false
Unexecuted instantiation: quant_bands.c:_opus_false
Unexecuted instantiation: rate.c:_opus_false
Unexecuted instantiation: vq.c:_opus_false
Unexecuted instantiation: vq_sse2.c:_opus_false
Unexecuted instantiation: dec_API.c:_opus_false
Unexecuted instantiation: resampler_private_down_FIR.c:_opus_false
Unexecuted instantiation: resampler_private_IIR_FIR.c:_opus_false
Unexecuted instantiation: bands.c:_opus_false
Unexecuted instantiation: cwrs.c:_opus_false
Unexecuted instantiation: kiss_fft.c:_opus_false
Unexecuted instantiation: decode_frame.c:_opus_false
Unexecuted instantiation: PLC.c:_opus_false
Unexecuted instantiation: CNG.c:_opus_false
Unexecuted instantiation: decode_core.c:_opus_false
Unexecuted instantiation: opus_multistream_encoder.c:_opus_false
Unexecuted instantiation: repacketizer.c:_opus_false
Unexecuted instantiation: celt_encoder.c:_opus_false
Unexecuted instantiation: opus_encoder.c:_opus_false
Unexecuted instantiation: analysis.c:_opus_false
Unexecuted instantiation: enc_API.c:_opus_false
Unexecuted instantiation: encode_pulses.c:_opus_false
Unexecuted instantiation: stereo_LR_to_MS.c:_opus_false
Unexecuted instantiation: control_codec.c:_opus_false
Unexecuted instantiation: encode_frame_FLP.c:_opus_false
Unexecuted instantiation: NSQ_del_dec_avx2.c:_opus_false
Unexecuted instantiation: NSQ.c:_opus_false
Unexecuted instantiation: NSQ_del_dec.c:_opus_false
Unexecuted instantiation: VAD.c:_opus_false
Unexecuted instantiation: resampler_down2_3.c:_opus_false
Unexecuted instantiation: NSQ_sse4_1.c:_opus_false
Unexecuted instantiation: NSQ_del_dec_sse4_1.c:_opus_false
Unexecuted instantiation: VAD_sse4_1.c:_opus_false
Unexecuted instantiation: NLSF_encode.c:_opus_false
201
665k
#define OPUS_CHECK_ARRAY(ptr, len) _opus_false()
202
#define OPUS_CHECK_VALUE(value) _opus_false()
203
0
#define OPUS_PRINT_INT(value) do{}while(0)
204
#define OPUS_FPRINTF (void)
205
206
#endif
207
208
209
#endif /* STACK_ALLOC_H */