Bug Summary

File:c:\siege\siege/src/siege/audio/source.c
Location:line 75, column 22
Description:Call to 'malloc' has an allocation size of 0 bytes

Annotated Source Code

1/*
2 Copyright (c) 2007 SIEGE Development Team
3 All rights reserved.
4
5 This file is part of libSIEGE.
6
7 This software is copyrighted work licensed under the terms of the
8 2-clause BSD license. Please consult the file "license.txt" for
9 details.
10
11 If you did not recieve the file with this program, please email
12 Tim Chas <darkuranium@gmail.com>.
13*/
14
15#define SG_BUILD_LIBRARY
16#include <siege/audio/source.h>
17#include <siege/util/list.h> // plist
18
19#include <stdlib.h>
20#include <math.h>
21
22#include <al.h>
23#include <alc.h>
24
25static ALCdevice* audioDev;
26static ALCcontext* audioCtx;
27
28#define ALSRCD(x)(*(ALuint*)(x)->handle) (*(ALuint*)(x)->handle)
29#define ALSRC(x)(*(ALuint*)((x)->dispatch)->handle) ALSRCD((x)->dispatch)(*(ALuint*)((x)->dispatch)->handle)
30
31static ALint queryi(ALuint source, ALenum q)
32{
33 ALint ret;
34 alGetSourcei(source, q, &ret);
35 return ret;
36}
37static float queryf(ALuint source, ALenum q)
38{
39 float ret;
40 alGetSourcef(source, q, &ret);
41 return ret;
42}
43static ALint state(ALuint source)
44{
45 return queryi(source, AL_SOURCE_STATE0x1010);
46}
47
48static SGuint maxSources(void)
49{
50 alGetError();
51 ALuint threshold = 256;
52
53 ALuint max;
54 ALuint* buf = malloc(threshold * sizeof(ALuint));
55 for(max = 1; max <= threshold; max++)
56 {
57 alGenSources(max, buf);
58 if(alGetError() != AL_NO_ERROR0)
59 {
60 max--;
61 break;
62 }
63 alDeleteSources(max, buf);
64 }
65 return max;
66}
67
68SGbool SG_CALL__cdecl _sgAudioSourceInit(void)
69{
70 audioDev = alcOpenDevice(NULL((void*)0));
71 audioCtx = alcCreateContext(audioDev, NULL((void*)0));
72 alcMakeContextCurrent(audioCtx);
73
74 _sg_srcDisLength = maxSources();
75 _sg_srcDisList = malloc(_sg_srcDisLength * sizeof(SGAudioSourceDispatch));
Call to 'malloc' has an allocation size of 0 bytes
76
77 SGuint i;
78 for(i = 0; i < _sg_srcDisLength; i++)
79 {
80 _sg_srcDisList[i].source = NULL((void*)0);
81 _sg_srcDisList[i].handle = malloc(sizeof(ALuint));
82 alGenSources(1, _sg_srcDisList[i].handle);
83 }
84
85 _sg_srcDestroy = sgListCreate();
86
87 return SG_TRUE1;
88}
89SGbool SG_CALL__cdecl _sgAudioSourceDeinit(void)
90{
91 while(_sg_srcDestroy->head)
92 sgAudioSourceDestroy(sgListPopFirst(_sg_srcDestroy));
93 sgListDestroy(_sg_srcDestroy);
94
95 SGuint i;
96 for(i = 0; i <_sg_srcDisLength; i++)
97 {
98 alDeleteSources(1, _sg_srcDisList[i].handle);
99 free(_sg_srcDisList[i].handle);
100 }
101 free(_sg_srcDisList);
102
103 alcMakeContextCurrent(NULL((void*)0));
104 alcDestroyContext(audioCtx);
105 alcCloseDevice(audioDev);
106
107 return SG_TRUE1;
108}
109
110SGAudioSourceDispatch* SG_CALL__cdecl _sgAudioSourceGetFreeDispatch(SGAudioSource* source)
111{
112 SGlong mini = -1;
113 float minf = HUGE_VAL__builtin_huge_val();
114
115 SGlong blanki = -1;
116 SGuint processed;
117 SGuint queued;
118
119 SGlong i;
120 for(i = 0; i <_sg_srcDisLength; i++)
121 {
122 if(_sg_srcDisList[i].source == NULL((void*)0))
123 {
124 _sg_srcDisList[i].source = source;
125 return &_sg_srcDisList[i];
126 }
127 processed = queryi(ALSRCD(&_sg_srcDisList[i])(*(ALuint*)(&_sg_srcDisList[i])->handle), AL_BUFFERS_PROCESSED0x1016);
128 queued = queryi(ALSRCD(&_sg_srcDisList[i])(*(ALuint*)(&_sg_srcDisList[i])->handle), AL_BUFFERS_QUEUED0x1015);
129 if(processed == queued)
130 blanki = i;
131 if(_sg_srcDisList[i].source->priority < minf)
132 {
133 mini = i;
134 minf = _sg_srcDisList[i].source->priority;
135 }
136 }
137
138 // failed to find, so now we revert to used ones
139 if((blanki >= 0) || (mini >= 0))
140 {
141 if(blanki >= 0)
142 i = blanki; // try to use a source that's finished playing
143 else if(mini >= 0)
144 i = mini; // all sources playing, so try to find one with a smaller priority
145
146 _sg_srcDisList[i].source->dispatch = NULL((void*)0);
147 _sg_srcDisList[i].source = source;
148 return &_sg_srcDisList[i];
149 }
150
151 // nothing found, so give up
152 return NULL((void*)0);
153}
154
155SGAudioSource* SG_CALL__cdecl sgAudioSourceCreate(float priority, float volume, float pitch, SGbool looping)
156{
157 SGListNode* node;
158 SGListNode* next;
159 for(node = _sg_srcDestroy->head; node; node = next)
160 {
161 next = node->next;
162 if(!sgAudioSourceIsPlaying(node->item))
163 {
164 sgAudioSourceDestroy(node->item);
165 sgListRemoveNode(_sg_srcDestroy, node);
166 }
167 }
168
169 SGAudioSource* source = malloc(sizeof(SGAudioSource));
170 source->priority = priority;
171
172 source->dispatch = _sgAudioSourceGetFreeDispatch(source);
173 // todo: enqueue?
174 if(source->dispatch == NULL((void*)0))
175 {
176 free(source);
177 return NULL((void*)0);
178 }
179
180 sgAudioSourceSetVolume(source, volume);
181 sgAudioSourceSetPitch(source, pitch);
182 sgAudioSourceSetLooping(source, looping);
183
184 return source;
185}
186void SG_CALL__cdecl sgAudioSourceDestroy(SGAudioSource* source)
187{
188 if(source == NULL((void*)0))
189 return;
190
191 sgAudioSourceUnqueueBuffers(source, sgAudioSourceGetNumQueuedBuffers(source));
192
193 // tell dispatch that the source is available
194 if(source->dispatch != NULL((void*)0))
195 source->dispatch->source = NULL((void*)0);
196 free(source);
197}
198void SG_CALL__cdecl sgAudioSourceDestroyLazy(SGAudioSource* source)
199{
200 sgAudioSourceSetLooping(source, SG_FALSE0);
201 sgListAppend(_sg_srcDestroy, source);
202}
203
204void SG_CALL__cdecl sgAudioSourcePlay(SGAudioSource* source)
205{
206 if(source == NULL((void*)0))
207 return;
208 if(source->dispatch == NULL((void*)0))
209 return;
210
211 alSourcePlay(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle));
212}
213SGbool SG_CALL__cdecl sgAudioSourceIsPlaying(SGAudioSource* source)
214{
215 if(source == NULL((void*)0))
216 return SG_FALSE0;
217 if(source->dispatch == NULL((void*)0))
218 return SG_FALSE0;
219
220 return state(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle)) == AL_PLAYING0x1012;
221}
222void SG_CALL__cdecl sgAudioSourcePause(SGAudioSource* source)
223{
224 if(source == NULL((void*)0))
225 return;
226 if(source->dispatch == NULL((void*)0))
227 return;
228
229 alSourcePause(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle));
230}
231SGbool SG_CALL__cdecl sgAudioSourceIsPaused(SGAudioSource* source)
232{
233 if(source == NULL((void*)0))
234 return SG_FALSE0;
235 if(source->dispatch == NULL((void*)0))
236 return SG_FALSE0;
237
238 return state(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle)) == AL_PAUSED0x1013;
239}
240void SG_CALL__cdecl sgAudioSourceRewind(SGAudioSource* source)
241{
242 if(source == NULL((void*)0))
243 return;
244 if(source->dispatch == NULL((void*)0))
245 return;
246
247 alSourceRewind(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle));
248}
249/*SGbool SG_CALL sgAudioSourceIsRewinded(SGAudioSource* source)
250{
251 if(source == NULL)
252 return SG_FALSE;
253 if(source->dispatch == NULL)
254 return SG_FALSE;
255
256 SGbool rewinded = SG_FALSE;
257 if(psgmAudioSourceIsRewinded != NULL)
258 psgmAudioSourceIsRewinded(source->dispatch->handle, &rewinded);
259 return rewinded;
260}*/
261void SG_CALL__cdecl sgAudioSourceStop(SGAudioSource* source)
262{
263 if(source == NULL((void*)0))
264 return;
265 if(source->dispatch == NULL((void*)0))
266 return;
267
268 alSourceStop(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle));
269}
270SGbool SG_CALL__cdecl sgAudioSourceIsStopped(SGAudioSource* source)
271{
272 if(source == NULL((void*)0))
273 return SG_FALSE0;
274 if(source->dispatch == NULL((void*)0))
275 return SG_FALSE0;
276
277 return state(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle)) == AL_STOPPED0x1014;
278}
279SGbool SG_CALL__cdecl sgAudioSourceIsActive(SGAudioSource* source)
280{
281 if(source == NULL((void*)0))
282 return SG_FALSE0;
283 if(source->dispatch == NULL((void*)0))
284 return SG_FALSE0;
285
286 SGuint processed = sgAudioSourceGetNumProcessedBuffers(source);
287 SGuint queued = sgAudioSourceGetNumQueuedBuffers(source);
288
289 return processed != queued;
290}
291
292size_t SG_CALL__cdecl sgAudioSourceGetNumProcessedBuffers(SGAudioSource* source)
293{
294 if(source == NULL((void*)0))
295 return 0;
296 if(source->dispatch == NULL((void*)0))
297 return 0;
298
299 return queryi(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_BUFFERS_PROCESSED0x1016);
300}
301size_t SG_CALL__cdecl sgAudioSourceGetNumQueuedBuffers(SGAudioSource* source)
302{
303 if(source == NULL((void*)0))
304 return 0;
305 if(source->dispatch == NULL((void*)0))
306 return 0;
307
308 return queryi(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_BUFFERS_QUEUED0x1015);
309}
310
311void SG_CALL__cdecl sgAudioSourceQueueBuffers(SGAudioSource* source, SGAudioBuffer** buffers, size_t numbuffers)
312{
313 if(source == NULL((void*)0))
314 return;
315 if(source->dispatch == NULL((void*)0))
316 return;
317
318 size_t i;
319 for(i = 0; i < numbuffers; i++)
320 sgAudioSourceQueueBuffer(source, buffers[i]);
321}
322void SG_CALL__cdecl sgAudioSourceQueueBuffer(SGAudioSource* source, SGAudioBuffer* buffer)
323{
324 if(source == NULL((void*)0))
325 return;
326 if(source->dispatch == NULL((void*)0))
327 return;
328
329 alSourceQueueBuffers(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), 1, buffer->handle);
330}
331void SG_CALL__cdecl sgAudioSourceUnqueueBuffer(SGAudioSource* source)
332{
333 if(source == NULL((void*)0))
334 return;
335 if(source->dispatch == NULL((void*)0))
336 return;
337
338 ALuint dummy;
339 alSourceUnqueueBuffers(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), 1, &dummy);
340}
341void SG_CALL__cdecl sgAudioSourceUnqueueBuffers(SGAudioSource* source, size_t numbuffers)
342{
343 if(source == NULL((void*)0))
344 return;
345 if(source->dispatch == NULL((void*)0))
346 return;
347
348 ALuint* dummies = malloc(numbuffers * sizeof(ALuint));
349 alSourceUnqueueBuffers(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), numbuffers, dummies);
350 free(dummies);
351}
352
353void SG_CALL__cdecl sgAudioSourceSetPosition3f(SGAudioSource* source, float x, float y, float z)
354{
355 if(source == NULL((void*)0))
356 return;
357 if(source->dispatch == NULL((void*)0))
358 return;
359
360 alSource3f(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_POSITION0x1004, x, y, z);
361}
362void SG_CALL__cdecl sgAudioSourceSetPosition2f(SGAudioSource* source, float x, float y)
363{
364 sgAudioSourceSetPosition3f(source, x, y, 0.0f);
365}
366void SG_CALL__cdecl sgAudioSourceGetPosition3f(SGAudioSource* source, float* x, float* y, float* z)
367{
368 if(source == NULL((void*)0))
369 return;
370 if(source->dispatch == NULL((void*)0))
371 return;
372
373 float tmp;
374 if(!x) x = &tmp;
375 if(!y) y = &tmp;
376 if(!z) z = &tmp;
377
378 alGetSource3f(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_POSITION0x1004, x, y, z);
379}
380void SG_CALL__cdecl sgAudioSourceGetPosition2f(SGAudioSource* source, float* x, float* y)
381{
382 sgAudioSourceGetPosition3f(source, x, y, NULL((void*)0));
383}
384
385void SG_CALL__cdecl sgAudioSourceSetVelocity3f(SGAudioSource* source, float x, float y, float z)
386{
387 if(source == NULL((void*)0))
388 return;
389 if(source->dispatch == NULL((void*)0))
390 return;
391
392 alSource3f(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_VELOCITY0x1006, x, y, z);
393}
394void SG_CALL__cdecl sgAudioSourceSetVelocity2f(SGAudioSource* source, float x, float y)
395{
396 sgAudioSourceSetVelocity3f(source, x, y, 0.0f);
397}
398void SG_CALL__cdecl sgAudioSourceGetVelocity3f(SGAudioSource* source, float* x, float* y, float* z)
399{
400 if(source == NULL((void*)0))
401 return;
402 if(source->dispatch == NULL((void*)0))
403 return;
404
405 float tmp;
406 if(!x) x = &tmp;
407 if(!y) y = &tmp;
408 if(!z) z = &tmp;
409
410 alGetSource3f(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_VELOCITY0x1006, x, y, z);
411}
412void SG_CALL__cdecl sgAudioSourceGetVelocity2f(SGAudioSource* source, float* x, float* y)
413{
414 sgAudioSourceGetVelocity3f(source, x, y, NULL((void*)0));
415}
416
417//void SG_CALL sgAudioSourceSetFalloff(SGAudioSource* source, float falloff);
418//float SG_CALL sgAudioSourceGetFalloff(SGAudioSource* source);
419
420void SG_CALL__cdecl sgAudioSourceSetPitch(SGAudioSource* source, float pitch)
421{
422 if(source == NULL((void*)0))
423 return;
424 if(source->dispatch == NULL((void*)0))
425 return;
426
427 alSourcef(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_PITCH0x1003, pitch);
428}
429float SG_CALL__cdecl sgAudioSourceGetPitch(SGAudioSource* source)
430{
431 if(source == NULL((void*)0))
432 return SG_NAN__builtin_nanf("0");
433 if(source->dispatch == NULL((void*)0))
434 return SG_NAN__builtin_nanf("0");
435
436 return queryf(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_PITCH0x1003);
437}
438
439void SG_CALL__cdecl sgAudioSourceSetVolume(SGAudioSource* source, float volume)
440{
441 if(source == NULL((void*)0))
442 return;
443 if(source->dispatch == NULL((void*)0))
444 return;
445
446 alSourcef(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_GAIN0x100A, volume);
447}
448float SG_CALL__cdecl sgAudioSourceGetVolume(SGAudioSource* source)
449{
450 if(source == NULL((void*)0))
451 return SG_NAN__builtin_nanf("0");
452 if(source->dispatch == NULL((void*)0))
453 return SG_NAN__builtin_nanf("0");
454
455 return queryf(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_GAIN0x100A);
456}
457
458void SG_CALL__cdecl sgAudioSourceSetLooping(SGAudioSource* source, SGbool looping)
459{
460 if(source == NULL((void*)0))
461 return;
462 if(source->dispatch == NULL((void*)0))
463 return;
464
465 alSourcei(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_LOOPING0x1007, looping);
466}
467SGbool SG_CALL__cdecl sgAudioSourceGetLooping(SGAudioSource* source)
468{
469 if(source == NULL((void*)0))
470 return SG_FALSE0;
471 if(source->dispatch == NULL((void*)0))
472 return SG_FALSE0;
473
474 return queryi(ALSRC(source)(*(ALuint*)((source)->dispatch)->handle), AL_LOOPING0x1007) != 0;
475}