alThunk.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. typedef struct {
  25. ALvoid *ptr;
  26. ALboolean InUse;
  27. } ThunkEntry;
  28. static ThunkEntry *g_ThunkArray;
  29. static ALuint g_ThunkArraySize;
  30. static CRITICAL_SECTION g_ThunkLock;
  31. void alThunkInit(void)
  32. {
  33. InitializeCriticalSection(&g_ThunkLock);
  34. g_ThunkArraySize = 1;
  35. g_ThunkArray = calloc(1, g_ThunkArraySize * sizeof(ThunkEntry));
  36. }
  37. void alThunkExit(void)
  38. {
  39. free(g_ThunkArray);
  40. g_ThunkArray = NULL;
  41. g_ThunkArraySize = 0;
  42. DeleteCriticalSection(&g_ThunkLock);
  43. }
  44. ALuint alThunkAddEntry(ALvoid *ptr)
  45. {
  46. ALuint index;
  47. EnterCriticalSection(&g_ThunkLock);
  48. for(index = 0;index < g_ThunkArraySize;index++)
  49. {
  50. if(g_ThunkArray[index].InUse == AL_FALSE)
  51. break;
  52. }
  53. if(index == g_ThunkArraySize)
  54. {
  55. ThunkEntry *NewList;
  56. NewList = realloc(g_ThunkArray, g_ThunkArraySize*2 * sizeof(ThunkEntry));
  57. if(!NewList)
  58. {
  59. LeaveCriticalSection(&g_ThunkLock);
  60. AL_PRINT("Realloc failed to increase to %u enties!\n", g_ThunkArraySize*2);
  61. return 0;
  62. }
  63. memset(&NewList[g_ThunkArraySize], 0, g_ThunkArraySize*sizeof(ThunkEntry));
  64. g_ThunkArraySize *= 2;
  65. g_ThunkArray = NewList;
  66. }
  67. g_ThunkArray[index].ptr = ptr;
  68. g_ThunkArray[index].InUse = AL_TRUE;
  69. LeaveCriticalSection(&g_ThunkLock);
  70. return index+1;
  71. }
  72. void alThunkRemoveEntry(ALuint index)
  73. {
  74. EnterCriticalSection(&g_ThunkLock);
  75. if(index > 0 && index <= g_ThunkArraySize)
  76. g_ThunkArray[index-1].InUse = AL_FALSE;
  77. LeaveCriticalSection(&g_ThunkLock);
  78. }
  79. ALvoid *alThunkLookupEntry(ALuint index)
  80. {
  81. ALvoid *ptr = NULL;
  82. EnterCriticalSection(&g_ThunkLock);
  83. if(index > 0 && index <= g_ThunkArraySize)
  84. ptr = g_ThunkArray[index-1].ptr;
  85. LeaveCriticalSection(&g_ThunkLock);
  86. return ptr;
  87. }