Leaked source code of windows server 2003
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.

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