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.

142 lines
3.1 KiB

  1. /***
  2. *memory.cpp - RTC support
  3. *
  4. * Copyright (c) 1998-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *
  7. *Revision History:
  8. * 07-28-98 JWM Module incorporated into CRTs (from KFrei)
  9. * 12-01-98 KBF Added some debugging info for _RTC_DEBUG
  10. * 12-02-98 KBF Fixed MC 11240
  11. * 05-11-99 KBF Error if RTC support define not enabled
  12. * 05-26-99 KBF Wrapped in _RTC_ADVMEM, simplified due to loss of -RTClv
  13. *
  14. ****/
  15. #ifndef _RTC
  16. #error RunTime Check support not enabled!
  17. #endif
  18. #include "rtcpriv.h"
  19. #ifdef _RTC_ADVMEM
  20. #ifdef _RTC_DEBUG
  21. #include <windows.h>
  22. #pragma intrinsic(strcpy)
  23. #pragma intrinsic(strcat)
  24. char *IntToString(int i)
  25. {
  26. static char buf[15];
  27. bool neg = i < 0;
  28. int pos = 14;
  29. buf[14] = 0;
  30. do {
  31. int val = i % 16;
  32. if (val < 10)
  33. val += '0';
  34. else
  35. val = val - 10 + 'A';
  36. buf[--pos] = val;
  37. i /= 16;
  38. } while (i);
  39. if (neg)
  40. buf[--pos] = '-';
  41. return &buf[pos];
  42. }
  43. #endif
  44. void __cdecl
  45. _RTC_Allocate(void *addr, size_t size, short level)
  46. {
  47. if (!addr)
  48. return;
  49. #ifdef _RTC_DEBUG
  50. char buf[88];
  51. strcpy(buf, IntToString((int)retaddr));
  52. strcat(buf, " Allocate Memory located @ ");
  53. strcat(buf, IntToString((int)addr));
  54. strcat(buf, " of size ");
  55. strcat(buf, IntToString((int)size));
  56. strcat(buf, "\n");
  57. OutputDebugString(buf);
  58. #endif
  59. _RTC_HeapBlock key(addr, level);
  60. _RTC_HeapBlock *hb = _RTC_heapblocks->find(&key);
  61. if (!hb)
  62. {
  63. hb = new _RTC_HeapBlock(addr, level, size);
  64. _RTC_heapblocks->add(hb);
  65. } else
  66. {
  67. hb->size(size);
  68. }
  69. if (level)
  70. {
  71. _RTC_Container *parent = _RTC_memhier->AddChild(hb);
  72. if (parent && parent->info())
  73. {
  74. hb->tag(_RTC_MSRenumberShadow((memptr)addr, size, parent->info()->tag()));
  75. return;
  76. }
  77. }
  78. hb->tag(_RTC_MSAllocShadow((memptr)addr, size, IDX_STATE_FULLY_KNOWN));
  79. }
  80. void __cdecl
  81. _RTC_Free(void *mem, short level)
  82. {
  83. if (!mem)
  84. return;
  85. #ifdef _RTC_DEBUG
  86. char buf[88];
  87. strcpy(buf, IntToString((int)retaddr));
  88. strcat(buf, " Freeing Memory located at ");
  89. strcat(buf, IntToString((int)mem));
  90. strcat(buf, "\n");
  91. OutputDebugString(buf);
  92. #endif
  93. bool fail = false;
  94. _RTC_HeapBlock key(mem, level);
  95. _RTC_HeapBlock *hb = _RTC_heapblocks->find(&key);
  96. if (hb)
  97. {
  98. if (level)
  99. {
  100. _RTC_Container *parent = _RTC_memhier->DelChild(hb);
  101. if (parent)
  102. {
  103. if (parent->info())
  104. _RTC_MSRestoreShadow((memptr)(hb->addr()), hb->size(), parent->info()->tag());
  105. else
  106. _RTC_MSFreeShadow((memptr)(hb->addr()), hb->size());
  107. }
  108. } else
  109. {
  110. _RTC_heapblocks->del(hb);
  111. _RTC_MSFreeShadow((memptr)(hb->addr()), hb->size());
  112. delete hb;
  113. }
  114. }
  115. }
  116. #endif // _RTC_ADVMEM