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.

312 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. buffer.cxx
  5. Abstract:
  6. Contains functions for managing BUFFER_INFO 'objects'
  7. Contents:
  8. CreateBuffer
  9. DestroyBuffer
  10. AcquireBufferLock
  11. ReleaseBufferLock
  12. ReferenceBuffer
  13. DereferenceBuffer
  14. Author:
  15. Richard L Firth (rfirth) 02-Nov-1994
  16. Revision History:
  17. 02-Nov-1994 rfirth
  18. Created
  19. --*/
  20. #include <wininetp.h>
  21. #include "gfrapih.h"
  22. //
  23. // data
  24. //
  25. DEBUG_DATA(LONG, NumberOfBuffers, 0);
  26. //
  27. // functions
  28. //
  29. LPBUFFER_INFO
  30. CreateBuffer(
  31. OUT LPDWORD Error
  32. )
  33. /*++
  34. Routine Description:
  35. Creates and initializes a BUFFER_INFO
  36. Arguments:
  37. Error - pointer to returned error
  38. Return Value:
  39. LPBUFFER_INFO
  40. Success - pointer to new BUFFER_INFO
  41. Failure - NULL
  42. --*/
  43. {
  44. LPBUFFER_INFO bufferInfo;
  45. DWORD error;
  46. HANDLE hEvent;
  47. bufferInfo = NEW(BUFFER_INFO);
  48. if (bufferInfo != NULL) {
  49. //
  50. // create an event that is initially unsignalled. The thread that
  51. // creates this buffer info will signal the event when the response
  52. // has been received from the server. At that point, any other
  53. // concurrent requesters can access the data
  54. //
  55. // bufferInfo->RequestEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  56. // if (bufferInfo->RequestEvent != NULL) {
  57. //bufferInfo->ConnectedSocket.Socket = INVALID_SOCKET;
  58. // InitializeCriticalSection(&bufferInfo->CriticalSection);
  59. bufferInfo->Socket = new ICSocket();
  60. if ( bufferInfo->Socket != NULL )
  61. {
  62. error = ERROR_SUCCESS;
  63. BUFFER_CREATED();
  64. }
  65. else
  66. error = ERROR_NOT_ENOUGH_MEMORY;
  67. // } else {
  68. // error = GetLastError();
  69. // }
  70. } else {
  71. error = ERROR_NOT_ENOUGH_MEMORY;
  72. }
  73. if (error != ERROR_SUCCESS) {
  74. if (bufferInfo != NULL) {
  75. DEL(bufferInfo);
  76. }
  77. *Error = error;
  78. }
  79. return bufferInfo;
  80. }
  81. VOID
  82. DestroyBuffer(
  83. IN LPBUFFER_INFO BufferInfo
  84. )
  85. /*++
  86. Routine Description:
  87. Frees resources belonging to BUFFER_INFO and frees memory occupied by
  88. BUFFER_INFO. BufferInfo must have been removed from any lists first
  89. Arguments:
  90. BufferInfo - pointer to BUFFER_INFO to destroy
  91. Return Value:
  92. None.
  93. --*/
  94. {
  95. DEBUG_ENTER((DBG_GOPHER,
  96. None,
  97. "DestroyBuffer",
  98. "%x",
  99. BufferInfo
  100. ));
  101. INET_ASSERT(BufferInfo != NULL);
  102. INET_ASSERT(BufferInfo->ReferenceCount == 0);
  103. // INET_ASSERT(! BufferInfo->Socket->IsValid());
  104. //if (BufferInfo->ConnectedSocket.Socket != INVALID_SOCKET) {
  105. INET_ASSERT(BufferInfo->Socket);
  106. if (BufferInfo->Socket->IsValid()) {
  107. //
  108. // BUGBUG - do we need to set linger or don't linger?
  109. //
  110. //
  111. // close the socket
  112. //
  113. BufferInfo->Socket->Close();
  114. }
  115. BufferInfo->Socket->Dereference();
  116. // if (BufferInfo->RequestEvent != NULL) {
  117. // CloseHandle(BufferInfo->RequestEvent);
  118. // }
  119. //
  120. // if we allocated a buffer for the response then free it
  121. //
  122. if ((BufferInfo->Buffer != NULL)
  123. && (BufferInfo->Flags & BI_BUFFER_RESPONSE)) {
  124. FREE_FIXED_MEMORY(BufferInfo->Buffer);
  125. }
  126. // DeleteCriticalSection(&BufferInfo->CriticalSection);
  127. DEL(BufferInfo);
  128. BUFFER_DESTROYED();
  129. DEBUG_LEAVE(0);
  130. }
  131. VOID
  132. AcquireBufferLock(
  133. IN LPBUFFER_INFO BufferInfo
  134. )
  135. /*++
  136. Routine Description:
  137. Locks the BUFFER_INFO against simultaneous updates of the Buffer
  138. Arguments:
  139. BufferInfo - pointer to BUFFER_INFO to lock
  140. Return Value:
  141. None.
  142. --*/
  143. {
  144. INET_ASSERT(BufferInfo != NULL);
  145. // EnterCriticalSection(&BufferInfo->CriticalSection);
  146. }
  147. VOID
  148. ReleaseBufferLock(
  149. IN LPBUFFER_INFO BufferInfo
  150. )
  151. /*++
  152. Routine Description:
  153. Opposite of AcquireBufferLock
  154. Arguments:
  155. BufferInfo - pointer to BUFFER_INFO to unlock
  156. Return Value:
  157. None.
  158. --*/
  159. {
  160. INET_ASSERT(BufferInfo != NULL);
  161. // LeaveCriticalSection(&BufferInfo->CriticalSection);
  162. }
  163. VOID
  164. ReferenceBuffer(
  165. IN LPBUFFER_INFO BufferInfo
  166. )
  167. /*++
  168. Routine Description:
  169. Increments the BUFFER_INFO reference count
  170. Arguments:
  171. BufferInfo - pointer to BUFFER_INFO to reference (by 1)
  172. Return Value:
  173. None.
  174. --*/
  175. {
  176. INET_ASSERT(BufferInfo != NULL);
  177. InterlockedIncrement(&BufferInfo->ReferenceCount);
  178. }
  179. LPBUFFER_INFO
  180. DereferenceBuffer(
  181. IN LPBUFFER_INFO BufferInfo
  182. )
  183. /*++
  184. Routine Description:
  185. Reduces the BUFFER_INFO reference count by 1. If the reference count goes
  186. to zero, the BUFFER_INFO is destroyed
  187. Arguments:
  188. BufferInfo - pointer to BUFFER_INFO to dereference/destroy
  189. Return Value:
  190. DWORD
  191. previous value of reference count
  192. --*/
  193. {
  194. INET_ASSERT(BufferInfo != NULL);
  195. INET_ASSERT(BufferInfo->ReferenceCount >= 1);
  196. if (InterlockedDecrement(&BufferInfo->ReferenceCount) == 0) {
  197. DestroyBuffer(BufferInfo);
  198. BufferInfo = NULL;
  199. }
  200. return BufferInfo;
  201. }