Skip to content

Commit

Permalink
Removed continuous memory allocations in the FLAC audio decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 1, 2012
1 parent edaa1ab commit 38788d0
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions music_flac.c
Expand Up @@ -414,15 +414,11 @@ static void FLAC_getsome(FLAC_music *music)
/* GET AUDIO WAVE DATA */
// set the max number of characters to read
music->flac_data.max_to_read = 8192;

// clear out the data buffer if it exists
if (music->flac_data.data) {
free (music->flac_data.data);
}

music->flac_data.data_len = music->flac_data.max_to_read;
music->flac_data.data_read = 0;
music->flac_data.data = (char *)malloc (music->flac_data.data_len);
if (!music->flac_data.data) {
music->flac_data.data = (char *)malloc (music->flac_data.data_len);
}

// we have data to read
while(music->flac_data.max_to_read > 0) {
Expand All @@ -434,15 +430,12 @@ static void FLAC_getsome(FLAC_music *music)
size_t overflow_extra_len = overflow_len -
music->flac_data.max_to_read;

char* new_overflow = (char *)malloc (overflow_extra_len);
memcpy (music->flac_data.data+music->flac_data.data_read,
music->flac_data.overflow, music->flac_data.max_to_read);
music->flac_data.data_read += music->flac_data.max_to_read;
memcpy (new_overflow,
memcpy (music->flac_data.overflow,
music->flac_data.overflow + music->flac_data.max_to_read,
overflow_extra_len);
free (music->flac_data.overflow);
music->flac_data.overflow = new_overflow;
music->flac_data.overflow_len = overflow_extra_len;
music->flac_data.overflow_read = overflow_extra_len;
music->flac_data.max_to_read = 0;
Expand Down

0 comments on commit 38788d0

Please sign in to comment.