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.

124 lines
2.5 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1994-1997 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: mydebug.c
  6. * Content: debugging printf - stolen from direct draw.
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * aarono splurp.
  12. * 6/6/98 aarono Debug support for link statistics
  13. *@@END_MSINTERNAL
  14. *
  15. ***************************************************************************/
  16. #undef WIN32_LEAN_AND_MEAN
  17. #define WIN32_LEAN_AND_MEAN
  18. #include <windows.h>
  19. #include "newdpf.h"
  20. #include "mydebug.h"
  21. #include "bilink.h"
  22. #include <stdarg.h>
  23. #ifdef DEBUG
  24. typedef struct _MEM_BLOCK {
  25. union {
  26. BILINK Bilink;
  27. struct _FB {
  28. struct _MEM_BLOCK *pNext;
  29. struct _MEM_BLOCK *pPrev;
  30. } FB;
  31. };
  32. UINT len;
  33. UINT tmAlloc;
  34. CHAR data[4];
  35. } MEM_BLOCK, *PMEM_BLOCK;
  36. LONG TotalMem = 0;
  37. struct _MEMLIST {
  38. union{
  39. BILINK Bilink;
  40. struct _FB FB;
  41. };
  42. } MemList={(BILINK *)&MemList,(BILINK *)&MemList};
  43. UINT nInit=0xFFFFFFFF;
  44. CRITICAL_SECTION MEM_CS;
  45. VOID My_GlobalAllocInit()
  46. {
  47. if(!InterlockedIncrement(&nInit)){
  48. InitializeCriticalSection(&MEM_CS);
  49. }
  50. }
  51. VOID My_GlobalAllocDeInit()
  52. {
  53. if(InterlockedDecrement(&nInit)&0x80000000){
  54. DeleteCriticalSection(&MEM_CS);
  55. }
  56. }
  57. HGLOBAL
  58. My_GlobalAlloc(
  59. UINT uFlags,
  60. DWORD dwBytes
  61. )
  62. {
  63. PMEM_BLOCK pMem;
  64. UINT lTotalMem;
  65. pMem=(PMEM_BLOCK)GlobalAlloc(uFlags,dwBytes+sizeof(MEM_BLOCK)-4);
  66. pMem->len=dwBytes;
  67. pMem->tmAlloc=GetTickCount();
  68. EnterCriticalSection(&MEM_CS);
  69. InsertAfter(&pMem->Bilink, &MemList.Bilink);
  70. TotalMem+=dwBytes;
  71. lTotalMem=TotalMem;
  72. LeaveCriticalSection(&MEM_CS);
  73. DPF(9,"GlobalAlloc: Allocated %d TotalMem %d\n",dwBytes, lTotalMem);
  74. {
  75. IN_WRITESTATS InWS;
  76. memset((PVOID)&InWS,0xFF,sizeof(IN_WRITESTATS));
  77. InWS.stat_USER2=lTotalMem;
  78. DbgWriteStats(&InWS);
  79. }
  80. return((HGLOBAL)&pMem->data[0]);
  81. }
  82. HGLOBAL
  83. My_GlobalFree(
  84. HGLOBAL hMem
  85. )
  86. {
  87. PUCHAR pData=(PUCHAR)(hMem);
  88. PMEM_BLOCK pMem;
  89. UINT lTotalMem;
  90. UINT dwBytes;
  91. pMem=CONTAINING_RECORD(pData, MEM_BLOCK, data);
  92. EnterCriticalSection(&MEM_CS);
  93. Delete(&pMem->Bilink);
  94. TotalMem-=pMem->len;
  95. dwBytes=pMem->len;
  96. lTotalMem=TotalMem;
  97. LeaveCriticalSection(&MEM_CS);
  98. DPF(9,"GlobalFree: Freed %d TotalMem %d\n",dwBytes,lTotalMem);
  99. {
  100. IN_WRITESTATS InWS;
  101. memset((PVOID)&InWS,0xFF,sizeof(IN_WRITESTATS));
  102. InWS.stat_USER2=lTotalMem;
  103. DbgWriteStats(&InWS);
  104. }
  105. return GlobalFree(pMem);
  106. }
  107. #endif /* DEBUG */