Skip to content

Commit 04411ba

Browse files
marler8997Hartmnt
authored andcommitted
FIX: Handle non-VLA compilers
Leverages the C std version and the standard __STDC_NO_VLA__ macro to detect whether VLA is supported, and if not falls back to using alloca. Original merge request: xiph/rnnoise#219
1 parent bcbd437 commit 04411ba

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/arch.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,15 @@ static RENAMENOISE_INLINE int renamenoise_isnan(float x)
192192
#define RENAMENOISE_GLOBAL_STACK_SIZE 120000
193193
#endif
194194

195+
#if __STDC_VERSION__ < 199901L || (__STDC_VERSION__ > 201000L && __STDC_NO_VLA__ == 1)
196+
#define RENAMENOISE_NO_VLA
197+
#endif
198+
199+
#ifdef RENAMENOISE_NO_VLA
200+
#include <malloc.h>
201+
#define renamenoise_stackalloc(type, id, len) type *id = alloca((len) * sizeof(type))
202+
#else
203+
#define renamenoise_stackalloc(type, id, len) type id[len]
204+
#endif
205+
195206
#endif /* ARCH_H */

src/pitch.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ void renamenoise_pitch_search(const renamenoise_val16 *x_lp, renamenoise_val16 *
235235
renamenoise_assert(max_pitch>0);
236236
lag = len+max_pitch;
237237

238-
renamenoise_val16 x_lp4[len>>2];
239-
renamenoise_val16 y_lp4[lag>>2];
240-
renamenoise_val32 xcorr[max_pitch>>1];
238+
renamenoise_stackalloc(renamenoise_val16, x_lp4, len>>2);
239+
renamenoise_stackalloc(renamenoise_val16, y_lp4, lag>>2);
240+
renamenoise_stackalloc(renamenoise_val32, xcorr, max_pitch>>1);
241241

242242
/* Downsample by 2 again */
243243
for (j=0;j<len>>2;j++)
@@ -313,7 +313,7 @@ renamenoise_val16 renamenoise_remove_doubling(renamenoise_val16 *x, int maxperio
313313
*T0_=maxperiod-1;
314314

315315
T = T0 = *T0_;
316-
renamenoise_val32 yy_lookup[maxperiod+1];
316+
renamenoise_stackalloc(renamenoise_val32, yy_lookup, maxperiod+1);
317317
renamenoise_dual_inner_prod(x, x, x-T0, N, &xx, &xy);
318318
yy_lookup[0] = xx;
319319
yy=xx;

src/renamenoise_lpc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void renamenoise_fir(
8383
int ord)
8484
{
8585
int i,j;
86-
renamenoise_val16 rnum[ord];
86+
renamenoise_stackalloc(renamenoise_val16, rnum, ord);
8787
for(i=0;i<ord;i++)
8888
rnum[i] = num[ord-i-1];
8989
for (i=0;i<N-3;i+=4)
@@ -134,8 +134,8 @@ void renamenoise_iir(const renamenoise_val32 *_x,
134134
#else
135135
int i,j;
136136
renamenoise_assert((ord&3)==0);
137-
renamenoise_val16 rden[ord];
138-
renamenoise_val16 y[N+ord];
137+
renamenoise_stackalloc(renamenoise_val16, rden, ord);
138+
renamenoise_stackalloc(renamenoise_val16, y, N+ord);
139139
for(i=0;i<ord;i++)
140140
rden[i] = den[ord-i-1];
141141
for(i=0;i<ord;i++)
@@ -195,7 +195,7 @@ int _renamenoise_autocorr(
195195
int fastN=n-lag;
196196
int shift;
197197
const renamenoise_val16 *xptr;
198-
renamenoise_val16 xx[n];
198+
renamenoise_stackalloc(renamenoise_val16, xx, n);
199199
renamenoise_assert(n>0);
200200
renamenoise_assert(overlap>=0);
201201
if (overlap == 0)

0 commit comments

Comments
 (0)