Removed continuous memory allocations in the FLAC audio decoding
authorSam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 19:26:59 -0500
changeset 5240f6aa6179b1d
parent 523 3214c1b0f0f4
child 525 88fafa3f3af2
Removed continuous memory allocations in the FLAC audio decoding
music_flac.c
     1.1 --- a/music_flac.c	Sat Dec 31 19:12:45 2011 -0500
     1.2 +++ b/music_flac.c	Sat Dec 31 19:26:59 2011 -0500
     1.3 @@ -414,15 +414,11 @@
     1.4  	/* GET AUDIO WAVE DATA */
     1.5  	// set the max number of characters to read
     1.6  	music->flac_data.max_to_read = 8192;
     1.7 -
     1.8 -	// clear out the data buffer if it exists
     1.9 -	if (music->flac_data.data) {
    1.10 -		free (music->flac_data.data);
    1.11 -	}
    1.12 -
    1.13  	music->flac_data.data_len = music->flac_data.max_to_read;
    1.14  	music->flac_data.data_read = 0;
    1.15 -	music->flac_data.data = (char *)malloc (music->flac_data.data_len);
    1.16 +	if (!music->flac_data.data) {
    1.17 +		music->flac_data.data = (char *)malloc (music->flac_data.data_len);
    1.18 +	}
    1.19  
    1.20  	// we have data to read
    1.21  	while(music->flac_data.max_to_read > 0) {
    1.22 @@ -434,15 +430,12 @@
    1.23  				size_t overflow_extra_len = overflow_len -
    1.24  												music->flac_data.max_to_read;
    1.25  
    1.26 -				char* new_overflow = (char *)malloc (overflow_extra_len);
    1.27  				memcpy (music->flac_data.data+music->flac_data.data_read,
    1.28  					music->flac_data.overflow, music->flac_data.max_to_read);
    1.29  				music->flac_data.data_read += music->flac_data.max_to_read;
    1.30 -				memcpy (new_overflow,
    1.31 +				memcpy (music->flac_data.overflow,
    1.32  					music->flac_data.overflow + music->flac_data.max_to_read,
    1.33  					overflow_extra_len);
    1.34 -				free (music->flac_data.overflow);
    1.35 -				music->flac_data.overflow = new_overflow;
    1.36  				music->flac_data.overflow_len = overflow_extra_len;
    1.37  				music->flac_data.overflow_read = overflow_extra_len;
    1.38  				music->flac_data.max_to_read = 0;