alThunk.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include "alMain.h"
  23. #include "alThunk.h"
  24. static ALenum *ThunkArray;
  25. static ALuint ThunkArraySize;
  26. static RWLock ThunkLock;
  27. void ThunkInit(void)
  28. {
  29. RWLockInit(&ThunkLock);
  30. ThunkArraySize = 1;
  31. ThunkArray = calloc(1, ThunkArraySize * sizeof(*ThunkArray));
  32. }
  33. void ThunkExit(void)
  34. {
  35. free(ThunkArray);
  36. ThunkArray = NULL;
  37. ThunkArraySize = 0;
  38. }
  39. ALenum NewThunkEntry(ALuint *index)
  40. {
  41. ALenum *NewList;
  42. ALuint i;
  43. ReadLock(&ThunkLock);
  44. for(i = 0;i < ThunkArraySize;i++)
  45. {
  46. if(ExchangeInt(&ThunkArray[i], AL_TRUE) == AL_FALSE)
  47. {
  48. ReadUnlock(&ThunkLock);
  49. *index = i+1;
  50. return AL_NO_ERROR;
  51. }
  52. }
  53. ReadUnlock(&ThunkLock);
  54. WriteLock(&ThunkLock);
  55. NewList = realloc(ThunkArray, ThunkArraySize*2 * sizeof(*ThunkArray));
  56. if(!NewList)
  57. {
  58. WriteUnlock(&ThunkLock);
  59. ERR("Realloc failed to increase to %u entries!\n", ThunkArraySize*2);
  60. return AL_OUT_OF_MEMORY;
  61. }
  62. memset(&NewList[ThunkArraySize], 0, ThunkArraySize*sizeof(*ThunkArray));
  63. ThunkArraySize *= 2;
  64. ThunkArray = NewList;
  65. ThunkArray[i] = AL_TRUE;
  66. WriteUnlock(&ThunkLock);
  67. *index = i+1;
  68. return AL_NO_ERROR;
  69. }
  70. void FreeThunkEntry(ALuint index)
  71. {
  72. ReadLock(&ThunkLock);
  73. if(index > 0 && index <= ThunkArraySize)
  74. ExchangeInt(&ThunkArray[index-1], AL_FALSE);
  75. ReadUnlock(&ThunkLock);
  76. }