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.

214 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. mem.c
  5. Abstract:
  6. Contains memory allocation routines.
  7. SnmpUtilMemAlloc
  8. SnmpUtilMemReAlloc
  9. SnmpUtilMemFree
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // //
  16. // Include files //
  17. // //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <snmp.h>
  20. #include <snmputil.h>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // //
  23. // Global Variables //
  24. // //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. UINT g_nBytesTotal = 0;
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // //
  29. // Public Procedures //
  30. // //
  31. ///////////////////////////////////////////////////////////////////////////////
  32. VOID
  33. SNMP_FUNC_TYPE
  34. SnmpUtilMemFree(
  35. LPVOID pMem
  36. )
  37. /*++
  38. Routine Description:
  39. Releases memory used by SNMP entities.
  40. Arguments:
  41. pMem - pointer to memory to release.
  42. Return Values:
  43. None.
  44. --*/
  45. {
  46. // validate
  47. if (pMem != NULL) {
  48. #if defined(DBG) && defined(_SNMPDLL_)
  49. // substract memory from global count
  50. g_nBytesTotal -= (UINT)GlobalSize((HGLOBAL)pMem);
  51. SNMPDBG((
  52. SNMP_LOG_VERBOSE,
  53. "SNMP: MEM: releasing 0x%08lx (%d bytes, %d total).\n",
  54. pMem, GlobalSize((HGLOBAL)pMem), g_nBytesTotal
  55. ));
  56. #endif
  57. // release memory
  58. GlobalFree((HGLOBAL)pMem);
  59. }
  60. }
  61. LPVOID
  62. SNMP_FUNC_TYPE
  63. SnmpUtilMemAlloc(
  64. UINT nBytes
  65. )
  66. /*++
  67. Routine Description:
  68. Allocates memory used by SNMP entities.
  69. Arguments:
  70. nBytes - number of bytes to allocate.
  71. Return Values:
  72. Returns pointer to memory.
  73. --*/
  74. {
  75. LPVOID pMem;
  76. // attempt to allocate memory from process heap
  77. pMem = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, (DWORD)nBytes);
  78. #if defined(DBG) && defined(_SNMPDLL_)
  79. // add allocated memory to global count if successful
  80. g_nBytesTotal += (UINT)((pMem != NULL) ? GlobalSize((HGLOBAL)pMem) : 0);
  81. SNMPDBG((
  82. SNMP_LOG_VERBOSE,
  83. "SNMP: MEM: allocated 0x%08lx (%d bytes, %d total).\n",
  84. pMem, (pMem != NULL) ? GlobalSize((HGLOBAL)pMem) : 0, g_nBytesTotal
  85. ));
  86. #endif
  87. return pMem;
  88. }
  89. LPVOID
  90. SNMP_FUNC_TYPE
  91. SnmpUtilMemReAlloc(
  92. LPVOID pMem,
  93. UINT nBytes
  94. )
  95. /*++
  96. Routine Description:
  97. Reallocates memory used by SNMP entities.
  98. Arguments:
  99. pMem - pointer to memory to reallocate.
  100. nBytes - number of bytes to allocate.
  101. Return Values:
  102. Returns pointer to memory.
  103. --*/
  104. {
  105. LPVOID pNew;
  106. // validate
  107. if (pMem == NULL) {
  108. // forward to alloc routine
  109. pNew = SnmpUtilMemAlloc(nBytes);
  110. } else {
  111. #if defined(DBG) && defined(_SNMPDLL_)
  112. SNMPDBG((
  113. SNMP_LOG_VERBOSE,
  114. "SNMP: MEM: expanding 0x%08lx (%d bytes) to %d bytes.\n",
  115. pMem, GlobalSize((HGLOBAL)pMem), nBytes
  116. ));
  117. // substract current memory from total
  118. g_nBytesTotal -= (UINT)GlobalSize((HGLOBAL)pMem);
  119. #endif
  120. // reallocate memory
  121. pNew = GlobalReAlloc(
  122. (HGLOBAL)pMem,
  123. (DWORD)nBytes,
  124. GMEM_MOVEABLE |
  125. GMEM_ZEROINIT
  126. );
  127. #if defined(DBG) && defined(_SNMPDLL_)
  128. // add new memory to total count
  129. g_nBytesTotal += (UINT)((pNew != NULL) ? GlobalSize((HGLOBAL)pNew) : 0);
  130. SNMPDBG((
  131. SNMP_LOG_VERBOSE,
  132. "SNMP: MEM: allocated 0x%08lx (%d bytes, %d total).\n",
  133. pNew, (pNew != NULL) ? GlobalSize((HGLOBAL)pNew) : 0, g_nBytesTotal
  134. ));
  135. #endif
  136. }
  137. return pNew;
  138. }