memmgr.hpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 2k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * memmgr.hpp - Memory manager module description.
  3.  */
  4. /* Inline Functions
  5.  *******************/
  6. //
  7. // N.b., you must use parentheses around the argument to new under the debug
  8. // memory manager, i.e., call "new(int)" not "new int".  This allows the debug
  9. // memory manager to identify orphaned heap elements by file and line number
  10. // where the heap element was allocated.  If you use new without parentheses
  11. // around the argument, the debug memory manager will record incorrect source
  12. // allocation information for the allocated heap element, and you may get
  13. // runtime RIPs in memmgr.c::DebugAllocateMemory() complaining that pcszSize or
  14. // pcszFile is an invalid string pointer.
  15. //
  16. // There is not currently a way to differentiate between "new int" and
  17. // "new(int)" at compile time to warn the developer to use parentheses around
  18. // the argument to new.
  19. //
  20. INLINE PVOID __cdecl operator new(size_t cbSize)
  21. {
  22.    PVOID pv;
  23.    /* Ignore return value. */
  24. #ifdef DEBUG
  25.    DebugAllocateMemory(cbSize, &pv, g_pcszElemHdrSize, g_pcszElemHdrFile, g_ulElemHdrLine);
  26.    // Invalidate debug heap element allocation parameters to try to catch calls
  27.    // to operator new not invoked via the new() macro.
  28.    g_pcszElemHdrSize = NULL;
  29.    g_pcszElemHdrFile = NULL;
  30.    g_ulElemHdrLine = 0;
  31. #else
  32.    IAllocateMemory(cbSize, &pv);
  33. #endif
  34.    return(pv);
  35. }
  36. INLINE void __cdecl operator delete(PVOID pv)
  37. {
  38.    FreeMemory(pv);
  39. }
  40. INLINE int __cdecl _purecall(void)
  41. {
  42.    return(0);
  43. }
  44. /* Macros
  45.  *********/
  46. #ifdef DEBUG
  47. #define new(type)                         (g_pcszElemHdrSize = #type, 
  48.                                            g_pcszElemHdrFile = __FILE__, 
  49.                                            g_ulElemHdrLine = __LINE__, 
  50.                                            new type)
  51. #else
  52. #define new(type)                         (new type)
  53. #endif   /* DEBUG */