Don't attempt to free null effects or filters
[openal-soft.git] / OpenAL32 / alEffect.c
blob1faca089ca1368277a1cacb817cc1e3e6fdde668
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include <stdlib.h>
23 #include "config.h"
25 #include "AL/al.h"
26 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alEffect.h"
31 static ALeffect *g_EffectList;
32 static ALuint g_EffectCount;
34 static void InitEffectParams(ALeffect *effect, ALenum type);
37 AL_API ALvoid AL_APIENTRY alGenEffects(ALsizei n, ALuint *effects)
39 ALCcontext *Context;
40 ALsizei i;
42 Context = alcGetCurrentContext();
43 SuspendContext(Context);
45 if (n > 0)
47 // Check that enough memory has been allocted in the 'sources' array for n Sources
48 if (!IsBadWritePtr((void*)effects, n * sizeof(ALuint)))
50 ALeffect **list = &g_EffectList;
51 while(*list)
52 list = &(*list)->next;
54 i = 0;
55 while(i < n)
57 *list = calloc(1, sizeof(ALeffect));
58 if(!(*list))
60 // We must have run out or memory
61 alDeleteEffects(i, effects);
62 alSetError(AL_OUT_OF_MEMORY);
63 break;
66 effects[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
67 (*list)->effect = effects[i];
69 InitEffectParams(*list, AL_EFFECT_NULL);
70 g_EffectCount++;
71 i++;
76 ProcessContext(Context);
79 AL_API ALvoid AL_APIENTRY alDeleteEffects(ALsizei n, ALuint *effects)
81 ALCcontext *Context;
82 ALeffect *ALEffect;
83 ALsizei i;
85 Context = alcGetCurrentContext();
86 SuspendContext(Context);
88 if (n >= 0)
90 // Check that all effects are valid
91 for (i = 0; i < n; i++)
93 if (!alIsEffect(effects[i]))
95 alSetError(AL_INVALID_NAME);
96 break;
100 if (i == n)
102 // All effects are valid
103 for (i = 0; i < n; i++)
105 // Recheck that the effect is valid, because there could be duplicated names
106 if (effects[i] && alIsEffect(effects[i]))
108 ALeffect **list;
110 ALEffect = ((ALeffect*)ALTHUNK_LOOKUPENTRY(effects[i]));
112 // Remove Source from list of Sources
113 list = &g_EffectList;
114 while(*list && *list != ALEffect)
115 list = &(*list)->next;
117 if(*list)
118 *list = (*list)->next;
119 ALTHUNK_REMOVEENTRY(ALEffect->effect);
121 memset(ALEffect, 0, sizeof(ALeffect));
122 free(ALEffect);
124 g_EffectCount--;
129 else
130 alSetError(AL_INVALID_VALUE);
132 ProcessContext(Context);
135 AL_API ALboolean AL_APIENTRY alIsEffect(ALuint effect)
137 ALCcontext *Context;
138 ALeffect **list;
140 Context = alcGetCurrentContext();
141 SuspendContext(Context);
143 list = &g_EffectList;
144 while(*list && (*list)->effect != effect)
145 list = &(*list)->next;
147 ProcessContext(Context);
149 return ((*list || !effect) ? AL_TRUE : AL_FALSE);
152 AL_API ALvoid AL_APIENTRY alEffecti(ALuint effect, ALenum param, ALint iValue)
154 ALCcontext *Context;
156 (void)iValue;
158 Context = alcGetCurrentContext();
159 SuspendContext(Context);
161 if (alIsEffect(effect))
163 switch(param)
165 default:
166 alSetError(AL_INVALID_ENUM);
167 break;
170 else
171 alSetError(AL_INVALID_NAME);
173 ProcessContext(Context);
176 AL_API ALvoid AL_APIENTRY alEffectiv(ALuint effect, ALenum param, ALint *piValues)
178 ALCcontext *Context;
180 (void)piValues;
182 Context = alcGetCurrentContext();
183 SuspendContext(Context);
185 if (alIsEffect(effect))
187 switch(param)
189 default:
190 alSetError(AL_INVALID_ENUM);
191 break;
194 else
195 alSetError(AL_INVALID_NAME);
197 ProcessContext(Context);
200 AL_API ALvoid AL_APIENTRY alEffectf(ALuint effect, ALenum param, ALfloat flValue)
202 ALCcontext *Context;
204 (void)flValue;
206 Context = alcGetCurrentContext();
207 SuspendContext(Context);
209 if (alIsEffect(effect))
211 switch(param)
213 default:
214 alSetError(AL_INVALID_ENUM);
215 break;
218 else
219 alSetError(AL_INVALID_NAME);
221 ProcessContext(Context);
224 AL_API ALvoid AL_APIENTRY alEffectfv(ALuint effect, ALenum param, ALfloat *pflValues)
226 ALCcontext *Context;
228 (void)pflValues;
230 Context = alcGetCurrentContext();
231 SuspendContext(Context);
233 if (alIsEffect(effect))
235 switch(param)
237 default:
238 alSetError(AL_INVALID_ENUM);
239 break;
242 else
243 alSetError(AL_INVALID_NAME);
245 ProcessContext(Context);
248 AL_API ALvoid AL_APIENTRY alGetEffecti(ALuint effect, ALenum param, ALint *piValue)
250 ALCcontext *Context;
252 (void)piValue;
254 Context = alcGetCurrentContext();
255 SuspendContext(Context);
257 if (alIsEffect(effect))
259 switch(param)
261 default:
262 alSetError(AL_INVALID_ENUM);
263 break;
266 else
267 alSetError(AL_INVALID_NAME);
269 ProcessContext(Context);
272 AL_API ALvoid AL_APIENTRY alGetEffectiv(ALuint effect, ALenum param, ALint *piValues)
274 ALCcontext *Context;
276 (void)piValues;
278 Context = alcGetCurrentContext();
279 SuspendContext(Context);
281 if (alIsEffect(effect))
283 switch(param)
285 default:
286 alSetError(AL_INVALID_ENUM);
287 break;
290 else
291 alSetError(AL_INVALID_NAME);
293 ProcessContext(Context);
296 AL_API ALvoid AL_APIENTRY alGetEffectf(ALuint effect, ALenum param, ALfloat *pflValue)
298 ALCcontext *Context;
300 (void)pflValue;
302 Context = alcGetCurrentContext();
303 SuspendContext(Context);
305 if (alIsEffect(effect))
307 switch(param)
309 default:
310 alSetError(AL_INVALID_ENUM);
311 break;
314 else
315 alSetError(AL_INVALID_NAME);
317 ProcessContext(Context);
320 AL_API ALvoid AL_APIENTRY alGetEffectfv(ALuint effect, ALenum param, ALfloat *pflValues)
322 ALCcontext *Context;
324 (void)pflValues;
326 Context = alcGetCurrentContext();
327 SuspendContext(Context);
329 if (alIsEffect(effect))
331 switch(param)
333 default:
334 alSetError(AL_INVALID_ENUM);
335 break;
338 else
339 alSetError(AL_INVALID_NAME);
341 ProcessContext(Context);
345 ALvoid ReleaseALEffects(ALvoid)
347 #ifdef _DEBUG
348 if(g_EffectCount > 0)
349 AL_PRINT("exit() %d Effect(s) NOT deleted\n", g_EffectCount);
350 #endif
352 while(g_EffectList)
354 ALeffect *temp = g_EffectList;
355 g_EffectList = g_EffectList->next;
357 // Release effect structure
358 memset(temp, 0, sizeof(ALeffect));
359 free(temp);
361 g_EffectCount = 0;
365 static void InitEffectParams(ALeffect *effect, ALenum type)
367 effect->type = type;