alThunk.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 ATOMIC(ALenum) *ThunkArray;
  25. static ALuint ThunkArraySize;
  26. static RWLock ThunkLock;
  27. void ThunkInit(void)
  28. {
  29. RWLockInit(&ThunkLock);
  30. ThunkArraySize = 1;
  31. ThunkArray = al_calloc(16, ThunkArraySize * sizeof(*ThunkArray));
  32. }
  33. void ThunkExit(void)
  34. {
  35. al_free(ThunkArray);
  36. ThunkArray = NULL;
  37. ThunkArraySize = 0;
  38. }
  39. ALenum NewThunkEntry(ALuint *index)
  40. {
  41. void *NewList;
  42. ALuint i;
  43. ReadLock(&ThunkLock);
  44. for(i = 0;i < ThunkArraySize;i++)
  45. {
  46. if(ATOMIC_EXCHANGE(ALenum, &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. /* Double-check that there's still no free entries, in case another
  56. * invocation just came through and increased the size of the array.
  57. */
  58. for(;i < ThunkArraySize;i++)
  59. {
  60. if(ATOMIC_EXCHANGE(ALenum, &ThunkArray[i], AL_TRUE) == AL_FALSE)
  61. {
  62. WriteUnlock(&ThunkLock);
  63. *index = i+1;
  64. return AL_NO_ERROR;
  65. }
  66. }
  67. NewList = al_calloc(16, ThunkArraySize*2 * sizeof(*ThunkArray));
  68. if(!NewList)
  69. {
  70. WriteUnlock(&ThunkLock);
  71. ERR("Realloc failed to increase to %u entries!\n", ThunkArraySize*2);
  72. return AL_OUT_OF_MEMORY;
  73. }
  74. memcpy(NewList, ThunkArray, ThunkArraySize*sizeof(*ThunkArray));
  75. al_free(ThunkArray);
  76. ThunkArray = NewList;
  77. ThunkArraySize *= 2;
  78. ATOMIC_STORE(&ThunkArray[i], AL_TRUE);
  79. WriteUnlock(&ThunkLock);
  80. *index = i+1;
  81. return AL_NO_ERROR;
  82. }
  83. void FreeThunkEntry(ALuint index)
  84. {
  85. ReadLock(&ThunkLock);
  86. if(index > 0 && index <= ThunkArraySize)
  87. ATOMIC_STORE(&ThunkArray[index-1], AL_FALSE);
  88. ReadUnlock(&ThunkLock);
  89. }