Skip to content

Commit cb1eab5

Browse files
authored
Merge pull request drowe67#256 from drowe67/ss-alloca
MSYS2/MinGW Compiler problem using alloca.h
2 parents a801aed + b995f1c commit cb1eab5

File tree

7 files changed

+48
-39
lines changed

7 files changed

+48
-39
lines changed

src/fmfsk.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void fmfsk_demod(struct FMFSK *fmfsk, uint8_t rx_bits[],float fmfsk_in[]){
196196
memcpy (&oldsamps[nold], &fmfsk_in[0] , sizeof(float)*nin );
197197

198198
/* Allocate memory for filtering */
199-
float *rx_filt = alloca(sizeof(float)*(nsym+1)*Ts);
199+
float *rx_filt = malloc(sizeof(float)*(nsym+1)*Ts);
200200

201201
/* Integrate over Ts input symbols at every offset */
202202
for(i=0; i<(nsym+1)*Ts; i++){
@@ -364,4 +364,6 @@ void fmfsk_demod(struct FMFSK *fmfsk, uint8_t rx_bits[],float fmfsk_in[]){
364364

365365
modem_probe_samp_f("t_norm_rx_timing",&norm_rx_timing,1);
366366
modem_probe_samp_f("t_rx_filt",rx_filt,(nsym+1)*Ts);
367+
368+
free(rx_filt);
367369
}

src/fmfsk_demod.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929

3030
#include <stdio.h>
31+
#include <stdlib.h>
3132
#include <string.h>
3233
#include "fmfsk.h"
3334
#include "modem_stats.h"
@@ -85,13 +86,13 @@ int main(int argc,char *argv[]){
8586

8687
if(fin==NULL || fout==NULL || fmfsk==NULL){
8788
fprintf(stderr,"Couldn't open test vector files\n");
88-
goto cleanup;
89+
exit(1);
8990
}
9091

9192
/* allocate buffers for processing */
92-
bitbuf = (uint8_t*)alloca(sizeof(uint8_t)*fmfsk->nbit);
93-
rawbuf = (int16_t*)alloca(sizeof(int16_t)*(fmfsk->N+fmfsk->Ts*2));
94-
modbuf = (float*)alloca(sizeof(float)*(fmfsk->N+fmfsk->Ts*2));
93+
bitbuf = (uint8_t*)malloc(sizeof(uint8_t)*fmfsk->nbit);
94+
rawbuf = (int16_t*)malloc(sizeof(int16_t)*(fmfsk->N+fmfsk->Ts*2));
95+
modbuf = (float*)malloc(sizeof(float)*(fmfsk->N+fmfsk->Ts*2));
9596

9697
/* Demodulate! */
9798
while( fread(rawbuf,sizeof(int16_t),fmfsk_nin(fmfsk),fin) == fmfsk_nin(fmfsk) ){
@@ -135,10 +136,15 @@ int main(int argc,char *argv[]){
135136
}
136137

137138
modem_probe_close();
138-
cleanup:
139+
140+
free(modbuf);
141+
free(rawbuf);
142+
free(bitbuf);
143+
139144
fclose(fin);
140145
fclose(fout);
141146
fmfsk_destroy(fmfsk);
147+
142148
exit(0);
143149
}
144150

src/fmfsk_mod.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ int main(int argc,char *argv[]){
7070

7171
if(fin==NULL || fout==NULL || fmfsk==NULL){
7272
fprintf(stderr,"Couldn't open test vector files\n");
73-
goto cleanup;
73+
exit(1);
7474
}
7575

7676
/* allocate buffers for processing */
77-
bitbuf = (uint8_t*)alloca(sizeof(uint8_t)*fmfsk->nbit);
78-
rawbuf = (int16_t*)alloca(sizeof(int16_t)*fmfsk->N);
79-
modbuf = (float*)alloca(sizeof(float)*fmfsk->N);
77+
bitbuf = (uint8_t*)malloc(sizeof(uint8_t)*fmfsk->nbit);
78+
rawbuf = (int16_t*)malloc(sizeof(int16_t)*fmfsk->N);
79+
modbuf = (float*)malloc(sizeof(float)*fmfsk->N);
8080

8181
/* Modulate! */
8282
while( fread(bitbuf,sizeof(uint8_t),fmfsk->nbit,fin) == fmfsk->nbit ){
@@ -91,9 +91,14 @@ int main(int argc,char *argv[]){
9191
}
9292
}
9393

94-
cleanup:
94+
free(modbuf);
95+
free(rawbuf);
96+
free(bitbuf);
97+
98+
fmfsk_destroy(fmfsk);
99+
95100
fclose(fin);
96101
fclose(fout);
97-
fmfsk_destroy(fmfsk);
102+
98103
exit(0);
99104
}

src/freedv_fsk.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ void freedv_fsk_ldpc_open(struct freedv *f, struct freedv_advanced *adv) {
145145

146146
f->bits_per_modem_frame = f->ldpc->data_bits_per_frame;
147147
int bits_per_frame = f->ldpc->coded_bits_per_frame + sizeof(fsk_ldpc_uw);
148-
f->tx_payload_bits = malloc(f->bits_per_modem_frame); assert(f->tx_payload_bits != NULL);
149-
f->rx_payload_bits = malloc(f->bits_per_modem_frame); assert(f->rx_payload_bits != NULL);
148+
f->tx_payload_bits = MALLOC(f->bits_per_modem_frame); assert(f->tx_payload_bits != NULL);
149+
f->rx_payload_bits = MALLOC(f->bits_per_modem_frame); assert(f->rx_payload_bits != NULL);
150150

151151
/* sample buffer size for tx modem samples, we modulate a full frame */
152152
f->n_nom_modem_samples = f->fsk->Ts*(bits_per_frame/(f->fsk->mode>>1));
@@ -163,12 +163,12 @@ void freedv_fsk_ldpc_open(struct freedv *f, struct freedv_advanced *adv) {
163163

164164
/* deframer set up */
165165
f->frame_llr_size = 2*bits_per_frame;
166-
f->frame_llr = (float*)malloc(f->frame_llr_size*sizeof(float)); assert(f->frame_llr != NULL);
166+
f->frame_llr = (float*)MALLOC(f->frame_llr_size*sizeof(float)); assert(f->frame_llr != NULL);
167167
f->frame_llr_nbits = 0;
168168

169-
f->twoframes_hard = malloc(2*bits_per_frame); assert(f->twoframes_hard != NULL);
169+
f->twoframes_hard = MALLOC(2*bits_per_frame); assert(f->twoframes_hard != NULL);
170170
memset(f->twoframes_hard, 0, 2*bits_per_frame);
171-
f->twoframes_llr = (float*)malloc(2*bits_per_frame*sizeof(float)); assert(f->twoframes_llr != NULL);
171+
f->twoframes_llr = (float*)MALLOC(2*bits_per_frame*sizeof(float)); assert(f->twoframes_llr != NULL);
172172
for(int i=0; i<2*bits_per_frame; i++) f->twoframes_llr[i] = 0.0;
173173

174174
/* currently configured a simple frame-frame approach */
@@ -227,7 +227,7 @@ void freedv_tx_fsk_voice(struct freedv *f, short mod_out[]) {
227227
}
228228

229229
/* Allocate floating point buffer for FSK mod */
230-
tx_float = alloca(sizeof(float)*f->n_nom_modem_samples);
230+
tx_float = MALLOC(sizeof(float)*f->n_nom_modem_samples);
231231

232232
/* do 4fsk mod */
233233
if(FDV_MODE_ACTIVE( FREEDV_MODE_2400A, f->mode) || FDV_MODE_ACTIVE( FREEDV_MODE_800XA, f->mode)){
@@ -252,6 +252,8 @@ void freedv_tx_fsk_voice(struct freedv *f, short mod_out[]) {
252252
mod_out[i] = (short)(tx_float[i]*FMFSK_SCALE);
253253
}
254254
}
255+
256+
FREE(tx_float);
255257
}
256258

257259
/* TX routines for 2400 FSK modes, after codec2 encoding */
@@ -297,7 +299,7 @@ void freedv_comptx_fsk_voice(struct freedv *f, COMP mod_out[]) {
297299
}
298300

299301
/* Allocate floating point buffer for FSK mod */
300-
tx_float = alloca(sizeof(float)*f->n_nom_modem_samples);
302+
tx_float = MALLOC(sizeof(float)*f->n_nom_modem_samples);
301303

302304
/* do 4fsk mod */
303305
if(FDV_MODE_ACTIVE( FREEDV_MODE_2400A, f->mode) || FDV_MODE_ACTIVE( FREEDV_MODE_800XA, f->mode)){
@@ -314,6 +316,8 @@ void freedv_comptx_fsk_voice(struct freedv *f, COMP mod_out[]) {
314316
mod_out[i].real = (tx_float[i]);
315317
}
316318
}
319+
320+
FREE(tx_float);
317321
}
318322

319323
/* TX routines for 2400 FSK modes, data channel */
@@ -327,7 +331,7 @@ void freedv_tx_fsk_data(struct freedv *f, short mod_out[]) {
327331
fvhff_frame_data_bits(f->deframer, FREEDV_VHF_FRAME_A,(uint8_t*)(f->tx_bits));
328332

329333
/* Allocate floating point buffer for FSK mod */
330-
tx_float = alloca(sizeof(float)*f->n_nom_modem_samples);
334+
tx_float = MALLOC(sizeof(float)*f->n_nom_modem_samples);
331335

332336
/* do 4fsk mod */
333337
if (FDV_MODE_ACTIVE( FREEDV_MODE_2400A, f->mode) || FDV_MODE_ACTIVE( FREEDV_MODE_800XA, f->mode)){
@@ -344,6 +348,8 @@ void freedv_tx_fsk_data(struct freedv *f, short mod_out[]) {
344348
mod_out[i] = (short)(tx_float[i]*FMFSK_SCALE);
345349
}
346350
}
351+
352+
FREE(tx_float);
347353
}
348354

349355
int freedv_tx_fsk_ldpc_bits_per_frame(struct freedv *f) {

src/fsk.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
/* This needs square roots, may take more cpu time than it's worth */
3636
#define EST_EBNO
3737

38-
/* This is a flag to make the mod/demod allocate their memory on the stack instead of the heap */
39-
/* At large sample rates, there's not enough stack space to run the demod */
40-
#define DEMOD_ALLOC_STACK
41-
4238
/* This is a flag for the freq. estimator to use a precomputed/rt computed hann window table
4339
On platforms with slow cosf, this will produce a substantial speedup at the cost of a small
4440
amount of memory
@@ -472,14 +468,8 @@ void fsk_demod_freq_est(struct FSK *fsk, COMP fsk_in[], float *freqs, int M) {
472468
int freqi[M];
473469
int st,en,f_zero;
474470

475-
/* Array to do complex FFT from using kiss_fft */
476-
#ifdef DEMOD_ALLOC_STACK
477-
kiss_fft_cpx *fftin = (kiss_fft_cpx*)alloca(sizeof(kiss_fft_cpx)*Ndft);
478-
kiss_fft_cpx *fftout = (kiss_fft_cpx*)alloca(sizeof(kiss_fft_cpx)*Ndft);
479-
#else
480471
kiss_fft_cpx *fftin = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*Ndft);
481472
kiss_fft_cpx *fftout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*Ndft);
482-
#endif
483473

484474
st = (fsk->est_min*Ndft)/Fs + Ndft/2; if (st < 0) st = 0;
485475
en = (fsk->est_max*Ndft)/Fs + Ndft/2; if (en > Ndft) en = Ndft;
@@ -605,14 +595,13 @@ void fsk_demod_freq_est(struct FSK *fsk, COMP fsk_in[], float *freqs, int M) {
605595
//fprintf(stderr, "fsk->tone_spacing: %d\n",fsk->tone_spacing);
606596
for (int m=0; m<M; m++)
607597
fsk->f2_est[m] = foff + m*fsk->tone_spacing;
598+
608599
#ifdef MODEMPROBE_ENABLE
609600
modem_probe_samp_f("t_f2_est",fsk->f2_est,M);
610601
#endif
611602

612-
#ifndef DEMOD_ALLOC_STACK
613603
free(fftin);
614604
free(fftout);
615-
#endif
616605
}
617606

618607
/* core demodulator function */

src/fsk_get_test_bits.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
#include <stdio.h>
3131
#include <stdlib.h>
32-
#include <alloca.h>
3332
#include <string.h>
3433
#include "fsk.h"
3534

@@ -70,11 +69,11 @@ int main(int argc,char *argv[]){
7069

7170
if(fout==NULL){
7271
fprintf(stderr,"Couldn't open output file: %s\n", argv[1]);
73-
goto cleanup;
72+
exit(1);
7473
}
7574

7675
/* allocate buffers for processing */
77-
bitbuf = (uint8_t*)alloca(sizeof(uint8_t)*framesize);
76+
bitbuf = (uint8_t*)malloc(sizeof(uint8_t)*framesize);
7877

7978
/* Generate buffer of test frame bits from known seed */
8079
srand(158324);
@@ -91,7 +90,7 @@ int main(int argc,char *argv[]){
9190
}
9291
}
9392

94-
cleanup:
93+
free(bitbuf);
9594
fclose(fout);
9695

9796
return 0;

src/fsk_put_test_bits.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
#include <stdio.h>
3131
#include <stdlib.h>
32-
#include <alloca.h>
3332
#include <string.h>
3433
#include <getopt.h>
3534
#include "fsk.h"
@@ -106,8 +105,8 @@ int main(int argc,char *argv[]){
106105
}
107106

108107
/* allocate buffers for processing */
109-
bitbuf_tx = (uint8_t*)alloca(sizeof(uint8_t)*framesize);
110-
bitbuf_rx = (uint8_t*)alloca(sizeof(uint8_t)*framesize);
108+
bitbuf_tx = (uint8_t*)malloc(sizeof(uint8_t)*framesize);
109+
bitbuf_rx = (uint8_t*)malloc(sizeof(uint8_t)*framesize);
111110

112111
/* Generate known tx frame from known seed */
113112
srand(158324);
@@ -154,6 +153,9 @@ int main(int argc,char *argv[]){
154153
}
155154
}
156155

156+
free(bitbuf_rx);
157+
free(bitbuf_tx);
158+
157159
fclose(fin);
158160

159161
fprintf(stderr,"[%04d] BER %5.3f, bits tested %6d, bit errors %6d\n", packetcnt, ber, bitcnt, biterr);

0 commit comments

Comments
 (0)