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.

131 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. if (hloc) {
  54. *ppv = hloc;
  55. hres = NOERROR;
  56. } else {
  57. hres = E_OUTOFMEMORY;
  58. }
  59. } else { /* Freeing */
  60. if (hloc) {
  61. LocalFree(hloc);
  62. *ppv = 0; /* All gone */
  63. } else {
  64. /* Nothing to free */
  65. }
  66. hres = NOERROR;
  67. }
  68. return hres;
  69. }
  70. /*****************************************************************************
  71. *
  72. * AllocCbPpv
  73. *
  74. * Simple wrapper that forces *ppvObj = 0 before calling Realloc.
  75. *
  76. *****************************************************************************/
  77. STDMETHODIMP EXTERNAL
  78. AllocCbPpv(UINT cb, PPV ppv)
  79. {
  80. *ppv = 0;
  81. return ReallocCbPpv(cb, ppv);
  82. }
  83. #else
  84. /*****************************************************************************
  85. *
  86. * AllocCbPpv
  87. *
  88. * Allocate memory into the ppv.
  89. *
  90. *****************************************************************************/
  91. STDMETHODIMP EXTERNAL
  92. AllocCbPpv(UINT cb, PPV ppv)
  93. {
  94. HRESULT hres;
  95. *ppv = LocalAlloc(LPTR, cb);
  96. hres = *ppv ? NOERROR : E_OUTOFMEMORY;
  97. return hres;
  98. }
  99. /*****************************************************************************
  100. *
  101. * FreePpv
  102. *
  103. * Free memory from the ppv.
  104. *
  105. *****************************************************************************/
  106. void EXTERNAL
  107. FreePpv(PV ppv)
  108. {
  109. #ifdef _M_IA64
  110. PV pv = (PV)InterlockedExchange64(ppv, 0);
  111. #else
  112. PV pv = (PV)InterlockedExchange(ppv, 0);
  113. #endif
  114. if (pv) {
  115. FreePv(pv);
  116. }
  117. }
  118. #endif