Exemplo n.º 1
0
void SoundDecoder_FreeSample(SoundDecoder_Sample* sound_sample)
{
	SoundDecoder_SampleInternal* sample_internal;
	LinkedListNode* the_node;

	/* Quit unloads all samples, so it is not possible to free a sample
	 * when not initialized.
	 */
	if(0 == s_isInitialized)
	{
		return;
	}

	if(sound_sample == NULL)
	{
		return;
	}

	/* SDL_sound keeps a linked list of all the loaded samples.
	 * We want to remove the current sample from that list.
	 */
	the_node = LinkedList_Find(s_listOfLoadedSamples, sound_sample, NULL);
	if(NULL == the_node)
	{
		SoundDecoder_SetError("SoundDecoder_FreeSample: Internal Error, sample does not exist in linked list.");
		return;
	}
	LinkedList_Remove(s_listOfLoadedSamples, the_node);

	sample_internal = (SoundDecoder_SampleInternal*)sound_sample->opaque;

	/* Ugh...SDL_sound has a lot of pointers. 
	 * I hope I didn't miss any dynamic memory.
	 */

	/* Call close on the decoder */
	sample_internal->funcs->close(sound_sample);

	if(NULL != sample_internal->rw)
	{
		sample_internal->rw->close(sample_internal->rw);
	}

	/* Ooops. The public buffer might be shared with the internal buffer.
	 * Make sure to not accidentally double delete.
	 */
	if((NULL != sample_internal->buffer)
		&& (sample_internal->buffer != sound_sample->buffer)
	)
	{
		free(sample_internal->buffer);
	}
	free(sample_internal);

	if(NULL != sound_sample->buffer)
	{
		free(sound_sample->buffer);
	}
	free(sound_sample);
}
Exemplo n.º 2
0
int TestLinkedList(int argc, char* argv[])
{
	int count;
	int number;
	wLinkedList* list;

	list = LinkedList_New();

	LinkedList_AddFirst(list, (void*) (size_t) 1);
	LinkedList_AddLast(list, (void*) (size_t) 2);
	LinkedList_AddLast(list, (void*) (size_t) 3);

	count = LinkedList_Count(list);

	if (count != 3)
	{
		printf("LinkedList_Count: expected %d, actual: %d\n", 3, count);
		return -1;
	}

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		number = (int) (size_t) LinkedList_Enumerator_Current(list);
		printf("\t%d\n", number);
	}
	printf("\n");

	printf("LinkedList First: %d Last: %d\n",
			(int) (size_t) LinkedList_First(list), (int) (size_t) LinkedList_Last(list));

	LinkedList_RemoveFirst(list);
	LinkedList_RemoveLast(list);

	count = LinkedList_Count(list);

	if (count != 1)
	{
		printf("LinkedList_Count: expected %d, actual: %d\n", 1, count);
		return -1;
	}

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		number = (int) (size_t) LinkedList_Enumerator_Current(list);
		printf("\t%d\n", number);
	}
	printf("\n");

	printf("LinkedList First: %d Last: %d\n",
			(int) (size_t) LinkedList_First(list), (int) (size_t) LinkedList_Last(list));

	LinkedList_RemoveFirst(list);
	LinkedList_RemoveLast(list);

	count = LinkedList_Count(list);

	if (count != 0)
	{
		printf("LinkedList_Count: expected %d, actual: %d\n", 0, count);
		return -1;
	}

	LinkedList_AddFirst(list, (void*) (size_t) 4);
	LinkedList_AddLast(list, (void*) (size_t) 5);
	LinkedList_AddLast(list, (void*) (size_t) 6);

	count = LinkedList_Count(list);

	if (count != 3)
	{
		printf("LinkedList_Count: expected %d, actual: %d\n", 3, count);
		return -1;
	}

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		number = (int) (size_t) LinkedList_Enumerator_Current(list);
		printf("\t%d\n", number);
	}
	printf("\n");

	printf("LinkedList First: %d Last: %d\n",
			(int) (size_t) LinkedList_First(list), (int) (size_t) LinkedList_Last(list));

	LinkedList_Remove(list, (void*) (size_t) 5);

	LinkedList_Enumerator_Reset(list);

	while (LinkedList_Enumerator_MoveNext(list))
	{
		number = (int) (size_t) LinkedList_Enumerator_Current(list);
		printf("\t%d\n", number);
	}
	printf("\n");

	printf("LinkedList First: %d Last: %d\n",
			(int) (size_t) LinkedList_First(list), (int) (size_t) LinkedList_Last(list));

	LinkedList_Free(list);

	/* Test enumerator robustness */

	/* enumerator on an empty list */
	list = LinkedList_New();
	LinkedList_Enumerator_Reset(list);
	while (LinkedList_Enumerator_MoveNext(list))
	{
		number = (int) (size_t) LinkedList_Enumerator_Current(list);
		printf("\t%d\n", number);
	}
	printf("\n");
	LinkedList_Free(list);

	/* Use an enumerator without reset */
	list = LinkedList_New();
	LinkedList_AddFirst(list, (void*) (size_t) 4);
	LinkedList_AddLast(list, (void*) (size_t) 5);
	LinkedList_AddLast(list, (void*) (size_t) 6);
	while (LinkedList_Enumerator_MoveNext(list))
	{
		number = (int) (size_t) LinkedList_Enumerator_Current(list);
		printf("\t%d\n", number);
	}
	printf("\n");
	LinkedList_Free(list);


	return 0;
}