Source code of Windows XP (NT5)
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.

117 lines
2.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C M E M . H
  7. //
  8. // Contents: Common memory management routines.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 24 Mar 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #ifndef _NCMEM_H_
  17. #define _NCMEM_H_
  18. #include <malloc.h>
  19. VOID*
  20. MemAlloc (
  21. size_t cb);
  22. VOID*
  23. MemAllocRaiseException (
  24. size_t cb);
  25. VOID
  26. MemFree (
  27. VOID* pv);
  28. // A simple wrapper around malloc that returns E_OUTOFMEMORY if the
  29. // allocation fails. Avoids having to explicitly do this at each
  30. // call site of malloc.
  31. //
  32. HRESULT
  33. HrMalloc (
  34. size_t cb,
  35. PVOID* ppv);
  36. // This CANNOT be an inline function. If it is ever not inlined,
  37. // the memory allocated will be destroyed. (We're dealing with the stack
  38. // here.)
  39. //
  40. #define PvAllocOnStack(_st) _alloca(_st)
  41. // Define a structure so that we can overload operator new with a
  42. // signature specific to our purpose.
  43. //
  44. struct raiseexception_t {};
  45. extern const raiseexception_t raiseexception;
  46. VOID*
  47. __cdecl
  48. operator new (
  49. size_t cb,
  50. const raiseexception_t&
  51. );
  52. // Define a structure so that we can overload operator new with a
  53. // signature specific to our purpose.
  54. //
  55. struct extrabytes_t {};
  56. extern const extrabytes_t extrabytes;
  57. VOID*
  58. __cdecl
  59. operator new (
  60. size_t cb,
  61. const extrabytes_t&,
  62. size_t cbExtra);
  63. #ifdef USE_HEAP_ALLOC
  64. inline
  65. void * Nccalloc(size_t n, size_t s)
  66. {
  67. return HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, (n * s));
  68. }
  69. inline
  70. void Ncfree(void * p)
  71. {
  72. HeapFree (GetProcessHeap(), 0, p);
  73. }
  74. inline
  75. void * Ncmalloc(size_t n)
  76. {
  77. return HeapAlloc (GetProcessHeap(), 0, n);
  78. }
  79. inline
  80. void * Ncrealloc(void * p, size_t n)
  81. {
  82. return (NULL == p)
  83. ? HeapAlloc (GetProcessHeap(), 0, n)
  84. : HeapReAlloc (GetProcessHeap(), 0, p, n);
  85. }
  86. #define calloc Nccalloc
  87. #define free Ncfree
  88. #define malloc Ncmalloc
  89. #define realloc Ncrealloc
  90. #endif // USE_HEAP_ALLOC
  91. #endif // _NCMEM_H_