Skip to content

Commit 1a681e1

Browse files
author
srsampson
committed
Use malloc versus alloca in fsk utilities
1 parent 29732ed commit 1a681e1

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
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) {

unittest/test_700c_eq.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
results=$(mktemp)
66

7-
c2enc 700C ../raw/kristoff.raw /dev/null --var 2> $results
7+
../build_linux/src/c2enc 700C ../raw/kristoff.raw /dev/null --var 2> $results
88
var=$(cat $results | sed -n "s/.*var: \([0-9..]*\) .*/\1/p")
9-
c2enc 700C ../raw/kristoff.raw /dev/null --var --eq 2> $results
9+
../build_linux/src/c2enc 700C ../raw/kristoff.raw /dev/null --var --eq 2> $results
1010
var_eq=$(cat $results | sed -n "s/.*var: \([0-9..]*\) .*/\1/p")
1111
printf "var: %5.2f var_eq: %5.2f\n" $var $var_eq
1212
python -c "import sys; sys.exit(0) if $var_eq<=$var else sys.exit(1)"

0 commit comments

Comments
 (0)