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.

213 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. drdbg
  5. Abstract:
  6. Contains Debug Routines for Remote Desktopping
  7. Author:
  8. Tad Brockway 02/00
  9. Revision History:
  10. --*/
  11. #ifdef TRC_FILE
  12. #undef TRC_FILE
  13. #endif
  14. #define TRC_FILE "_slmdbg"
  15. #include <RemoteDesktop.h>
  16. #include <RemoteDesktopDBG.h>
  17. #if DBG
  18. typedef struct tagREMOTEDESKTOP_MEMHDR
  19. {
  20. DWORD magicNo;
  21. DWORD tag;
  22. DWORD size;
  23. DWORD pad;
  24. } REMOTEDESKTOP_MEMHDR, *PREMOTEDESKTOP_MEMHDR;
  25. void *
  26. RemoteDesktopAllocateMem(
  27. IN size_t size,
  28. IN DWORD tag
  29. )
  30. /*++
  31. Routine Description:
  32. Allocate memory. Similiar to malloc.
  33. Arguments:
  34. size - Number of bytes to allocate.
  35. tag - Tag identifying the allocated block for tracking
  36. memory allocations.
  37. Return Value:
  38. Pointer to allocated memory on success. Otherwise, NULL is returned.
  39. --*/
  40. {
  41. PREMOTEDESKTOP_MEMHDR hdr;
  42. PBYTE p;
  43. DC_BEGIN_FN("RemoteDesktopAllocateMem");
  44. hdr = (PREMOTEDESKTOP_MEMHDR)malloc(size + sizeof(REMOTEDESKTOP_MEMHDR));
  45. if (hdr != NULL) {
  46. hdr->magicNo = GOODMEMMAGICNUMBER;
  47. hdr->tag = tag;
  48. hdr->size = size;
  49. p = (PBYTE)(hdr + 1);
  50. memset(p, UNITIALIZEDMEM, size);
  51. DC_END_FN();
  52. return (void *)p;
  53. }
  54. else {
  55. TRC_ERR((TB, TEXT("Can't allocate %ld bytes."), size));
  56. DC_END_FN();
  57. return NULL;
  58. }
  59. }
  60. void
  61. RemoteDesktopFreeMem(
  62. IN void *ptr
  63. )
  64. /*++
  65. Routine Description:
  66. Release memory allocated by a call to RemoteDesktopAllocateMem.
  67. Arguments:
  68. ptr - Block of memory allocated by a call to RemoteDesktopAllocateMem.
  69. Return Value:
  70. NA
  71. --*/
  72. {
  73. PREMOTEDESKTOP_MEMHDR hdr;
  74. DC_BEGIN_FN("RemoteDesktopFreeMem");
  75. //
  76. // NULL is okay with 'free' so it has to be okay with us.
  77. // (STL passes NULL to 'delete')
  78. //
  79. if (ptr == NULL) {
  80. DC_END_FN();
  81. return;
  82. }
  83. //
  84. // Get a pointer to the header to the memory block.
  85. //
  86. hdr = (PREMOTEDESKTOP_MEMHDR)ptr;
  87. hdr--;
  88. //
  89. // Make sure the block is valid.
  90. //
  91. ASSERT(hdr->magicNo == GOODMEMMAGICNUMBER);
  92. //
  93. // Mark it as freed.
  94. //
  95. hdr->magicNo = FREEDMEMMAGICNUMBER;
  96. //
  97. // Scramble and free the memory.
  98. //
  99. memset(ptr, REMOTEDESKTOPBADMEM, (size_t)hdr->size);
  100. free(hdr);
  101. DC_END_FN();
  102. }
  103. void *
  104. RemoteDesktopReallocMem(
  105. IN void *ptr,
  106. IN size_t size
  107. )
  108. /*++
  109. Routine Description:
  110. Realloc a block.
  111. Arguments:
  112. ptr - Block of memory allocated by a call to RemoteDesktopAllocateMem.
  113. sz - Size of new block.
  114. Return Value:
  115. NA
  116. --*/
  117. {
  118. PREMOTEDESKTOP_MEMHDR hdr, hdr_tmp;
  119. DC_BEGIN_FN("RemoteDesktopReallocMem");
  120. ASSERT(ptr != NULL);
  121. //
  122. // Get a pointer to the header to the memory block.
  123. //
  124. hdr = (PREMOTEDESKTOP_MEMHDR)ptr;
  125. hdr--;
  126. //
  127. // Make sure the block is valid.
  128. //
  129. ASSERT(hdr->magicNo == GOODMEMMAGICNUMBER);
  130. //
  131. // Whack the old block magic number in case we move.
  132. //
  133. hdr->magicNo = FREEDMEMMAGICNUMBER;
  134. //
  135. // Resize.
  136. //
  137. hdr_tmp = (PREMOTEDESKTOP_MEMHDR)realloc(hdr, size + sizeof(REMOTEDESKTOP_MEMHDR));
  138. //
  139. // Update the size and update the magic number.
  140. //
  141. if (hdr_tmp != NULL) {
  142. // prefast - leaking memory if realloc fails
  143. hdr = hdr_tmp;
  144. hdr->magicNo = GOODMEMMAGICNUMBER;
  145. hdr->size = size;
  146. ptr = (PBYTE)(hdr + 1);
  147. }
  148. else {
  149. TRC_ERR((TB, TEXT("Can't allocate %ld bytes."), size));
  150. ptr = NULL;
  151. }
  152. DC_END_FN();
  153. return (void *)ptr;
  154. }
  155. #endif