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.

100 lines
2.5 KiB

  1. /*****************************************************************************
  2. *
  3. * mem.c - Memory management
  4. *
  5. * WARNING! These do not go through OLE allocation. Use these
  6. * only for private allocation.
  7. *
  8. *****************************************************************************/
  9. #include "map.h"
  10. #ifdef NEED_REALLOC
  11. /*****************************************************************************
  12. *
  13. * ReallocCbPpv
  14. *
  15. * Change the size of some zero-initialized memory.
  16. *
  17. * This is the single place where all memory is allocated, resized,
  18. * and freed.
  19. *
  20. * If you realloc from a null pointer, memory is allocated.
  21. * If you realloc to zero-size, memory is freed.
  22. *
  23. * These semantics avoid boundary cases. For example, it is no
  24. * longer a problem trying to realloc something down to zero.
  25. * You don't have to worry about special-casing an alloc of 0 bytes.
  26. *
  27. * If an error is returned, the original pointer is UNCHANGED.
  28. * This saves you from having to the double-switch around a realloc.
  29. *
  30. *****************************************************************************/
  31. STDMETHODIMP EXTERNAL
  32. ReallocCbPpv(UINT cb, PV ppvArg)
  33. {
  34. HRESULT hres;
  35. PPV ppv = ppvArg;
  36. HLOCAL hloc = *ppv;
  37. if (cb) { /* Alloc or realloc */
  38. if (hloc) { /* Realloc */
  39. hloc = LocalReAlloc(hloc, cb,
  40. LMEM_MOVEABLE+LMEM_ZEROINIT);
  41. } else { /* Alloc */
  42. hloc = LocalAlloc(LPTR, cb);
  43. }
  44. hres = hloc ? S_OK : E_OUTOFMEMORY;
  45. } else { /* Freeing */
  46. if (hloc) {
  47. LocalFree(hloc);
  48. hloc = 0;
  49. hres = S_OK; /* All gone */
  50. } else {
  51. hres = S_OK; /* Nothing to free */
  52. }
  53. }
  54. if (SUCCEEDED(hres)) {
  55. *ppv = hloc;
  56. }
  57. return hres;
  58. }
  59. /*****************************************************************************
  60. *
  61. * AllocCbPpv
  62. *
  63. * Simple wrapper that forces *ppvObj = 0 before calling Realloc.
  64. *
  65. *****************************************************************************/
  66. STDMETHODIMP EXTERNAL
  67. AllocCbPpv(UINT cb, PPV ppv)
  68. {
  69. *ppv = 0;
  70. return ReallocCbPpv(cb, ppv);
  71. }
  72. #else
  73. /*****************************************************************************
  74. *
  75. * AllocCbPpv
  76. *
  77. * Allocate memory into the ppv.
  78. *
  79. *****************************************************************************/
  80. STDMETHODIMP EXTERNAL
  81. AllocCbPpv(UINT cb, PPV ppv)
  82. {
  83. HRESULT hres;
  84. *ppv = LocalAlloc(LPTR, cb);
  85. hres = *ppv ? S_OK : E_OUTOFMEMORY;
  86. return hres;
  87. }
  88. #endif