comparison SDL_sound.c @ 167:82acaa7107c2

Fixed Sound_DecodeAll(), and made Sound_FreeSample() a little more robust.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 26 Nov 2001 04:33:31 +0000
parents 9b26ed9eaf04
children 705dcbf94639
comparison
equal deleted inserted replaced
166:d8904267d23c 167:82acaa7107c2
566 } /* if */ 566 } /* if */
567 567
568 /* nuke it... */ 568 /* nuke it... */
569 if (internal->rw != NULL) /* this condition is a "just in case" thing. */ 569 if (internal->rw != NULL) /* this condition is a "just in case" thing. */
570 SDL_RWclose(internal->rw); 570 SDL_RWclose(internal->rw);
571 assert(internal->buffer != NULL); 571
572 if (internal->buffer != sample->buffer) 572 if ((internal->buffer != NULL) && (internal->buffer != sample->buffer))
573 free(internal->buffer); 573 free(internal->buffer);
574
574 free(internal); 575 free(internal);
576
575 if (sample->buffer != NULL) 577 if (sample->buffer != NULL)
576 free(sample->buffer); 578 free(sample->buffer);
579
577 free(sample); 580 free(sample);
578 } /* Sound_FreeSample */ 581 } /* Sound_FreeSample */
579 582
580 583
581 int Sound_SetBufferSize(Sound_Sample *sample, Uint32 newSize) 584 int Sound_SetBufferSize(Sound_Sample *sample, Uint32 newSize)
633 636
634 Uint32 Sound_DecodeAll(Sound_Sample *sample) 637 Uint32 Sound_DecodeAll(Sound_Sample *sample)
635 { 638 {
636 Sound_SampleInternal *internal = NULL; 639 Sound_SampleInternal *internal = NULL;
637 void *buf = NULL; 640 void *buf = NULL;
641 void *ptr;
638 Uint32 newBufSize = 0; 642 Uint32 newBufSize = 0;
639 643
640 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0); 644 BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
641 645
642 internal = (Sound_SampleInternal *) sample->opaque; 646 internal = (Sound_SampleInternal *) sample->opaque;
643 647
644 while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) && 648 while ( ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) &&
645 ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) ) 649 ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) )
646 { 650 {
647 void *ptr = realloc(buf, newBufSize + sample->buffer_size); 651 ptr = realloc(buf, newBufSize + sample->buffer_size);
648 if (ptr == NULL) 652 if (ptr == NULL)
649 { 653 {
650 sample->flags |= SOUND_SAMPLEFLAG_ERROR; 654 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
651 Sound_SetError(ERR_OUT_OF_MEMORY); 655 Sound_SetError(ERR_OUT_OF_MEMORY);
652 } /* if */ 656 } /* if */
653 else 657 else
654 { 658 {
655 Uint32 br = Sound_Decode(sample); 659 Uint32 br;
660 buf = ptr;
661 br = Sound_Decode(sample);
656 memcpy( ((char *) buf) + newBufSize, sample->buffer, br ); 662 memcpy( ((char *) buf) + newBufSize, sample->buffer, br );
657 newBufSize += br; 663 newBufSize += br;
658 } /* else */ 664 } /* else */
659 } /* while */ 665 } /* while */
660 666
667 if (internal->buffer != sample->buffer)
668 free(internal->buffer);
669
661 free(sample->buffer); 670 free(sample->buffer);
662 sample->buffer = internal->buffer = internal->sdlcvt.buf = buf; 671
672 internal->sdlcvt.buf = internal->buffer = sample->buffer = buf;
663 sample->buffer_size = newBufSize; 673 sample->buffer_size = newBufSize;
664 internal->buffer_size = newBufSize / internal->sdlcvt.len_mult; 674 internal->buffer_size = newBufSize / internal->sdlcvt.len_mult;
665 internal->sdlcvt.len_mult = internal->buffer_size; 675 internal->sdlcvt.len = internal->buffer_size;
666 676
667 return(newBufSize); 677 return(newBufSize);
668 } /* Sound_DecodeAll */ 678 } /* Sound_DecodeAll */
669 679
670
671 /* end of SDL_sound.c ... */ 680 /* end of SDL_sound.c ... */
672 681