alThunk.c 2.8 KB

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