Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 1996,1997 Microsoft Corporation
  3. Module Name:
  4. HANDLES.C
  5. Abstract:
  6. Handle Manager
  7. Author:
  8. Aaron Ogus (aarono)
  9. Environment:
  10. Win32/COM
  11. Revision History:
  12. Date Author Description
  13. ====== ====== ============================================================
  14. 2/16/98 aarono Original
  15. 6/6/98 aarono debug support, use My_GlobalAlloc
  16. --*/
  17. #include <windows.h>
  18. #include <mmsystem.h>
  19. #include <dplay.h>
  20. #include <dplaysp.h>
  21. #include <dplaypr.h>
  22. #include "mydebug.h"
  23. #include "arpd.h"
  24. #include "arpdint.h"
  25. #include "macros.h"
  26. #include "handles.h"
  27. VOLLPMYHANDLETABLE InitHandleTable(UINT nSize, CRITICAL_SECTION *pcs, UINT nUserGrowSize)
  28. {
  29. LPMYHANDLETABLE lpTable;
  30. UINT nGrowSize;
  31. UINT i;
  32. if(nUserGrowSize){
  33. nGrowSize=nUserGrowSize;
  34. } else {
  35. nGrowSize=MYHANDLE_DEFAULT_GROWSIZE;
  36. }
  37. lpTable=(LPMYHANDLETABLE)My_GlobalAlloc(GPTR, sizeof(MYHANDLETABLE)+(sizeof(MYHANDLE)*nSize));
  38. if(lpTable){
  39. if(pcs){
  40. InitializeCriticalSection(pcs);
  41. }
  42. lpTable->nUnique=UNIQUE_ADD;
  43. lpTable->nTableSize=nSize;
  44. lpTable->nTableGrowSize=nGrowSize;
  45. lpTable->iNext=0;
  46. for(i=0;i < nGrowSize-1; i++){
  47. lpTable->Table[i].iNext=i+1;
  48. }
  49. lpTable->Table[nGrowSize-1].iNext = LIST_END;
  50. }
  51. return lpTable;
  52. }
  53. VOID FiniHandleTable(LPMYHANDLETABLE lpTable, CRITICAL_SECTION *pcs)
  54. {
  55. My_GlobalFree(lpTable);
  56. if(pcs){
  57. DeleteCriticalSection(pcs);
  58. }
  59. }
  60. DWORD AllocHandleTableEntry(LPVOLLPMYHANDLETABLE lplpTable, CRITICAL_SECTION *pcs, LPVOID lpv)
  61. {
  62. #define lpTable (*lplpTable)
  63. UINT i;
  64. LPMYHANDLETABLE lpNewTable;
  65. if(pcs){
  66. EnterCriticalSection(pcs);
  67. }
  68. if(lpTable->iNext==LIST_END){
  69. // grow the table.
  70. lpNewTable=(LPMYHANDLETABLE)My_GlobalAlloc(GPTR, sizeof(MYHANDLETABLE)+
  71. ((lpTable->nTableGrowSize+lpTable->nTableSize)*sizeof(MYHANDLE)));
  72. if(!lpNewTable){
  73. return 0;
  74. }
  75. memcpy(lpNewTable, lpTable, (lpTable->nTableSize*sizeof(MYHANDLE))+sizeof(MYHANDLETABLE));
  76. lpNewTable->iNext=lpNewTable->nTableSize;
  77. lpNewTable->nTableSize=lpNewTable->nTableSize+lpNewTable->nTableGrowSize;
  78. for(i=lpNewTable->iNext ; i < lpNewTable->nTableSize-1 ; i++){
  79. lpNewTable->Table[i].iNext=i+1;
  80. }
  81. lpNewTable->Table[lpNewTable->nTableSize-1].iNext = LIST_END;
  82. *lplpTable=lpNewTable;
  83. }
  84. i=lpTable->iNext;
  85. lpTable->iNext=lpTable->Table[i].iNext;
  86. // increment uniqueness, never zero.
  87. do {
  88. lpTable->nUnique += UNIQUE_ADD;
  89. } while (!lpTable->nUnique);
  90. lpTable->Table[i].nUnique=lpTable->nUnique;
  91. lpTable->Table[i].lpv=lpv;
  92. if(pcs) {
  93. LeaveCriticalSection(pcs);
  94. }
  95. ASSERT((i+lpTable->Table[i].nUnique & CONTEXT_INDEX_MASK) == i);
  96. return i+lpTable->Table[i].nUnique;
  97. #undef lpTable
  98. }
  99. LPVOID ReadHandleTableEntry( LPVOLLPMYHANDLETABLE lplpTable, CRITICAL_SECTION *pcs, UINT handle)
  100. {
  101. #define lpTable (*lplpTable)
  102. UINT i;
  103. UINT nUnique;
  104. LPVOID lpv;
  105. if(handle){
  106. if(pcs){
  107. EnterCriticalSection(pcs);
  108. }
  109. i = handle & CONTEXT_INDEX_MASK;
  110. nUnique = handle - i;
  111. if(i < lpTable->nTableSize && nUnique == lpTable->Table[i].nUnique){
  112. lpv = lpTable->Table[i].lpv;
  113. } else {
  114. lpv = NULL;
  115. }
  116. if(pcs){
  117. LeaveCriticalSection(pcs);
  118. }
  119. } else {
  120. lpv=NULL;
  121. }
  122. return lpv;
  123. #undef lpTable
  124. }
  125. HRESULT FreeHandleTableEntry(LPVOLLPMYHANDLETABLE lplpTable, CRITICAL_SECTION *pcs, UINT handle)
  126. {
  127. #define lpTable (*lplpTable)
  128. HRESULT hr=DP_OK;
  129. UINT i;
  130. UINT nUnique;
  131. if(handle){
  132. if(pcs){
  133. EnterCriticalSection(pcs);
  134. }
  135. i = handle & CONTEXT_INDEX_MASK;
  136. nUnique = handle - i;
  137. if(i < lpTable->nTableSize && nUnique == lpTable->Table[i].nUnique){
  138. lpTable->Table[i].iNext = lpTable->iNext;
  139. lpTable->iNext=i;
  140. } else {
  141. hr = DPERR_GENERIC;
  142. DPF(0,"FreeHandleTableEntry, ERROR, TRIED TO FREE INVALID HANDLE %x",handle);
  143. ASSERT(0);
  144. }
  145. if(pcs){
  146. LeaveCriticalSection(pcs);
  147. }
  148. } else {
  149. hr=DPERR_GENERIC;
  150. }
  151. return hr;
  152. #undef lpTable
  153. }