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.

172 lines
3.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: MEMLB.CXX
  7. //
  8. // Contents:
  9. //
  10. // Classes: Implements the memory-based ILockBytes class.
  11. //
  12. // Functions:
  13. //
  14. // History: 12-13-95 JoeS (Joe Souza) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <urlint.h>
  18. #include <urlmon.hxx>
  19. #include "clockbyt.hxx"
  20. #include "memlb.hxx"
  21. MemLockBytes::MemLockBytes() : _CRefs()
  22. {
  23. memhandle = NULL;
  24. }
  25. STDMETHODIMP_(ULONG) MemLockBytes::Release(void)
  26. {
  27. VDATETHIS(this);
  28. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN MemLockBytes::Release\n", this));
  29. UrlMkAssert((_CRefs > 0));
  30. LONG lRet = --_CRefs;
  31. if (_CRefs == 0)
  32. {
  33. if (memhandle)
  34. GlobalFree(memhandle);
  35. delete this;
  36. }
  37. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT MemLockBytes::Release\n", this));
  38. return lRet;
  39. }
  40. HRESULT MemLockBytes::ReadAt(THIS_ ULARGE_INTEGER ulOffset, VOID HUGEP *pv,
  41. ULONG cb, ULONG FAR *pcbRead)
  42. {
  43. HRESULT hresult = E_FAIL;
  44. LPVOID memptr;
  45. DWORD memsize;
  46. ULONG count;
  47. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN MemLockBytes::ReadAt\n", this));
  48. *pcbRead = 0;
  49. if (!cb)
  50. goto ReadAtExit;
  51. if (!memhandle)
  52. goto ReadAtExit;
  53. memsize = GlobalSize(memhandle);
  54. count = cb;
  55. if (ulOffset.QuadPart + cb > memsize)
  56. {
  57. if (ulOffset.QuadPart > memsize)
  58. {
  59. goto ReadAtExit;
  60. }
  61. else
  62. {
  63. count = memsize - (DWORD)ulOffset.QuadPart;
  64. }
  65. }
  66. memptr = GlobalLock(memhandle);
  67. if (!memptr)
  68. goto ReadAtExit;
  69. *(DWORD *)&memptr += (DWORD)ulOffset.QuadPart;
  70. memcpy(pv, memptr, count);
  71. *pcbRead = count;
  72. GlobalUnlock(memhandle);
  73. hresult = NOERROR;
  74. ReadAtExit:
  75. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT MemLockBytes::ReadAt\n", this));
  76. return(hresult);
  77. }
  78. HRESULT MemLockBytes::WriteAt(THIS_ ULARGE_INTEGER ulOffset, VOID const HUGEP *pv,
  79. ULONG cb, ULONG FAR *pcbWritten)
  80. {
  81. HRESULT hresult = E_FAIL;
  82. LPVOID memptr;
  83. DWORD memsize;
  84. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN MemLockBytes::WriteAt\n", this));
  85. *pcbWritten = 0;
  86. if (!cb)
  87. goto WriteAtExit;
  88. if (!memhandle)
  89. {
  90. memhandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_DISCARDABLE,
  91. cb + (DWORD)ulOffset.QuadPart);
  92. }
  93. if (!memhandle)
  94. goto WriteAtExit;
  95. while (1)
  96. {
  97. memsize = GlobalSize(memhandle);
  98. if (ulOffset.QuadPart + cb > memsize)
  99. {
  100. memhandle = GlobalReAlloc(memhandle, cb + (DWORD)ulOffset.QuadPart,
  101. GMEM_MOVEABLE);
  102. }
  103. else
  104. break;
  105. }
  106. memptr = GlobalLock(memhandle);
  107. if (!memptr)
  108. goto WriteAtExit;
  109. *(DWORD *)&memptr += (DWORD)ulOffset.QuadPart;
  110. memcpy(memptr, pv, cb);
  111. GlobalUnlock(memhandle);
  112. *pcbWritten = cb;
  113. hresult = NOERROR;
  114. WriteAtExit:
  115. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT MemLockBytes::WriteAt\n", this));
  116. return(hresult);
  117. }
  118. HRESULT MemLockBytes::SetSize(THIS_ ULARGE_INTEGER cb)
  119. {
  120. HRESULT hresult = NOERROR;
  121. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN MemLockBytes::SetSize\n", this));
  122. if (!memhandle)
  123. {
  124. memhandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_DISCARDABLE,
  125. (DWORD)cb.QuadPart);
  126. }
  127. else
  128. {
  129. memhandle = GlobalReAlloc(memhandle, (DWORD)cb.QuadPart, GMEM_MOVEABLE);
  130. }
  131. if (!memhandle)
  132. hresult = E_FAIL;
  133. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT MemLockBytes::SetSize\n", this));
  134. return(hresult);
  135. }