History

Reduce usage of flexible array fields

evpobr committed 6 years ago

Commit 36dfd681 parent 8a83ad33 files · +94 −32Browse files

Showing the changes to src/src_sinc.c (1 of 3 files). Show all

Unified · Split

src/src_sinc.c+25−9
@@ -53,8 +53,7 @@ typedef struct · 52 unmodified lines
5353 /* Sure hope noone does more than 128 channels at once. */
5454 double left_calc [128], right_calc [128] ;
5555
56− /* C99 struct flexible array. */
57− float buffer [] ;
56+ float *buffer ;
5857 } SINC_FILTER ;
5958
6059 static enum SRC_ERR sinc_multichan_vari_process (SRC_STATE *state, SRC_DATA *data) ;
@@ -220,10 +219,16 @@ sinc_set_converter (SRC_STATE *state, int src_enum) · 159 unmodified lines
220219 temp_filter.b_len *= state->channels ;
221220 temp_filter.b_len += 1 ; // There is a <= check against samples_in_hand requiring a buffer bigger than the calculation above
222221
223− if ((filter = ZERO_ALLOC (SINC_FILTER, sizeof (SINC_FILTER) + sizeof (filter->buffer [0]) * (temp_filter.b_len + state->channels))) == NULL)
222+ if ((filter = ZERO_ALLOC (SINC_FILTER, sizeof (SINC_FILTER))) == NULL)
224223 return SRC_ERR_MALLOC_FAILED ;
225224
226225 *filter = temp_filter ;
226+ filter->buffer = calloc (temp_filter.b_len + state->channels, sizeof (float)) ;
227+ if (!filter->buffer)
228+ { free (filter) ;
229+ return SRC_ERR_MALLOC_FAILED ;
230+ } ;
231+
227232 memset (&temp_filter, 0xEE, sizeof (temp_filter)) ;
228233
229234 state->private_data = filter ;
@@ -267,12 +272,18 @@ sinc_copy (SRC_STATE *from, SRC_STATE *to) · 37 unmodified lines
267272
268273 SINC_FILTER *to_filter = NULL ;
269274 SINC_FILTER* from_filter = (SINC_FILTER*) from->private_data ;
270− size_t private_length = sizeof (SINC_FILTER) + sizeof (from_filter->buffer [0]) * (from_filter->b_len + from->channels) ;
271275
272− if ((to_filter = ZERO_ALLOC (SINC_FILTER, private_length)) == NULL)
276+ if ((to_filter = ZERO_ALLOC (SINC_FILTER, sizeof (SINC_FILTER))) == NULL)
273277 return SRC_ERR_MALLOC_FAILED ;
278+ memcpy (to_filter, from_filter, sizeof (SINC_FILTER)) ;
279+ to_filter->buffer = malloc (sizeof (float) * (from_filter->b_len + from->channels)) ;
280+ if (!to_filter->buffer)
281+ {
282+ free (to_filter) ;
283+ return SRC_ERR_MALLOC_FAILED ;
284+ }
285+ memcpy (to_filter->buffer, from_filter->buffer, sizeof (float) * (from_filter->b_len + from->channels)) ;
274286
275− memcpy (to_filter, from_filter, private_length) ;
276287 to->private_data = to_filter ;
277288
278289 return SRC_ERR_NO_ERROR ;
@@ -1150,10 +1161,15 @@ sinc_close (SRC_STATE *state) · 871 unmodified lines
11501161 {
11511162 if (state)
11521163 {
1153− if (state->private_data)
1164+ SINC_FILTER *sinc = (SINC_FILTER *) state->private_data ;
1165+ if (sinc)
11541166 {
1155− free (state->private_data) ;
1156− state->private_data = NULL ;
1167+ if (sinc->buffer)
1168+ { free (sinc->buffer) ;
1169+ sinc->buffer = NULL ;
1170+ } ;
1171+ free (sinc) ;
1172+ sinc = NULL ;
11571173 }
11581174 }
11591175 } /* sinc_close */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t