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.

129 lines
3.2 KiB

  1. /*****************************************************************************
  2. *
  3. * DIMem.c
  4. *
  5. * Copyright (c) 1996 - 1999 Microsoft Corporation. All Rights Reserved.
  6. *
  7. * Abstract:
  8. *
  9. * Memory management
  10. *
  11. * Contents:
  12. *
  13. * ReallocCbPpv
  14. * AllocCbPpv
  15. * FreePpv
  16. *
  17. *****************************************************************************/
  18. #include "dinputpr.h"
  19. #ifdef NEED_REALLOC
  20. /*****************************************************************************
  21. *
  22. * ReallocCbPpv
  23. *
  24. * Change the size of some zero-initialized memory.
  25. *
  26. * This is the single place where all memory is allocated, resized,
  27. * and freed.
  28. *
  29. * If you realloc from a null pointer, memory is allocated.
  30. * If you realloc to zero-size, memory is freed.
  31. *
  32. * These semantics avoid boundary cases. For example, it is no
  33. * longer a problem trying to realloc something down to zero.
  34. * You don't have to worry about special-casing an alloc of 0 bytes.
  35. *
  36. * If an error is returned, the original pointer is UNCHANGED.
  37. * This saves you from having to the double-switch around a realloc.
  38. *
  39. *****************************************************************************/
  40. STDMETHODIMP EXTERNAL
  41. ReallocCbPpv(UINT cb, PV ppvArg)
  42. {
  43. HRESULT hres;
  44. PPV ppv = ppvArg;
  45. HLOCAL hloc = *ppv;
  46. if (cb) { /* Alloc or realloc */
  47. if (hloc) { /* Realloc */
  48. hloc = LocalReAlloc(hloc, cb,
  49. LMEM_MOVEABLE+LMEM_ZEROINIT);
  50. } else { /* Alloc */
  51. hloc = LocalAlloc(LPTR, cb);
  52. }
  53. hres = hloc ? NOERROR : E_OUTOFMEMORY;
  54. } else { /* Freeing */
  55. if (hloc) {
  56. LocalFree(hloc);
  57. hloc = 0;
  58. hres = NOERROR; /* All gone */
  59. } else {
  60. hres = NOERROR; /* Nothing to free */
  61. }
  62. }
  63. if (SUCCEEDED(hres)) {
  64. *ppv = hloc;
  65. }
  66. return hres;
  67. }
  68. /*****************************************************************************
  69. *
  70. * AllocCbPpv
  71. *
  72. * Simple wrapper that forces *ppvObj = 0 before calling Realloc.
  73. *
  74. *****************************************************************************/
  75. STDMETHODIMP EXTERNAL
  76. AllocCbPpv(UINT cb, PPV ppv)
  77. {
  78. *ppv = 0;
  79. return ReallocCbPpv(cb, ppv);
  80. }
  81. #else
  82. /*****************************************************************************
  83. *
  84. * AllocCbPpv
  85. *
  86. * Allocate memory into the ppv.
  87. *
  88. *****************************************************************************/
  89. STDMETHODIMP EXTERNAL
  90. AllocCbPpv(UINT cb, PPV ppv)
  91. {
  92. HRESULT hres;
  93. *ppv = LocalAlloc(LPTR, cb);
  94. hres = *ppv ? NOERROR : E_OUTOFMEMORY;
  95. return hres;
  96. }
  97. /*****************************************************************************
  98. *
  99. * FreePpv
  100. *
  101. * Free memory from the ppv.
  102. *
  103. *****************************************************************************/
  104. void EXTERNAL
  105. FreePpv(PV ppv)
  106. {
  107. #ifdef _M_IA64
  108. PV pv = (PV)InterlockedExchange64(ppv, 0);
  109. #else
  110. PV pv = (PV)InterlockedExchange(ppv, 0);
  111. #endif
  112. if (pv) {
  113. FreePv(pv);
  114. }
  115. }
  116. #endif