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.

113 lines
3.0 KiB

  1. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. mem.cpp
  5. Abstract:
  6. Replacements for operators new & delete.
  7. Routines implemented in this module:
  8. _InitMem()
  9. operator new
  10. operator delete
  11. These routines delegate to HeapAlloc/HeapFree, and party on the process
  12. heap. This helps overcome the problem of using general-purpose allocation
  13. functions as well as using new/delete to deal with C++ objects.
  14. Rudimentary allocation tracking is enabled in debug builds that
  15. allows us to see (via the last line in the log file) how much memory
  16. went unallocated at process termination. This doesn't take into account
  17. kernel, gdi, or user objects.
  18. Memory deallocation routines are "safe" in the sense that you can pass
  19. NULL pointers (invalid pointers aren't detected).
  20. Author:
  21. Paul M Midgen (pmidge) 01-June-2000
  22. Revision History:
  23. 01-June-2000 pmidge
  24. Created
  25. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  26. #include "common.h"
  27. //#include "mem.h"
  28. HANDLE g_hProcessHeap = NULL;
  29. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  30. _InitMem()
  31. WHAT : Sets the global process heap handle call this before any
  32. allocations occur or you'll fault. Pretty simple.
  33. ARGS : none.
  34. RETURNS : nothing
  35. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  36. void _InitMem(void)
  37. {
  38. g_hProcessHeap = GetProcessHeap();
  39. }
  40. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  41. operator new
  42. WHAT : Replaces the global operator new. Same usage semantics. Allocates
  43. objects, makes implicit calls to their constructors.
  44. ARGS : size - size in bytes of the object to be allocated. the compiler
  45. pushes this argument on the stack automagically.
  46. RETURNS : void pointer to allocated memory.
  47. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  48. void* __cdecl operator new(size_t size)
  49. {
  50. void* pv = NULL;
  51. if( !g_hProcessHeap )
  52. _InitMem();
  53. pv = HeapAlloc(g_hProcessHeap, HEAP_ZERO_MEMORY, size);
  54. DEBUG_ALLOC(pv);
  55. return pv;
  56. }
  57. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  58. operator delete
  59. WHAT : Replaces the global operator delete. Same usage semantics. Deletes
  60. objects, makes implicit call to their destructors.
  61. ARGS : pv - pointer to object to be freed. same compiler magic as with
  62. operator new.
  63. RETURNS : nothing.
  64. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  65. void __cdecl operator delete(void* pv)
  66. {
  67. if( !g_hProcessHeap )
  68. _InitMem();
  69. if( pv )
  70. {
  71. DEBUG_FREE(pv);
  72. HeapFree(g_hProcessHeap, 0, pv);
  73. }
  74. }