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.

159 lines
4.0 KiB

  1. /*****************************************************************************
  2. *
  3. * Dimem.c
  4. *
  5. * Copyright (c) 1999 Microsoft Corporation. All Rights Reserved.
  6. *
  7. * dimem.c - Memory management
  8. *
  9. * WARNING! These do not go through OLE allocation. Use these
  10. * only for private allocation.
  11. *
  12. *****************************************************************************/
  13. #include "PIDpr.h"
  14. #ifdef NEED_REALLOC
  15. /*****************************************************************************
  16. *
  17. * ReallocCbPpv
  18. *
  19. * Change the size of some zero-initialized memory.
  20. *
  21. * This is the single place where all memory is allocated, resized,
  22. * and freed.
  23. *
  24. * If you realloc from a null pointer, memory is allocated.
  25. * If you realloc to zero-size, memory is freed.
  26. *
  27. * These semantics avoid boundary cases. For example, it is no
  28. * longer a problem trying to realloc something down to zero.
  29. * You don't have to worry about special-casing an alloc of 0 bytes.
  30. *
  31. * If an error is returned, the original pointer is UNCHANGED.
  32. * This saves you from having to the double-switch around a realloc.
  33. *
  34. *****************************************************************************/
  35. STDMETHODIMP EXTERNAL
  36. ReallocCbPpv(UINT cb, PV ppvArg)
  37. {
  38. HRESULT hres;
  39. PPV ppv = ppvArg;
  40. HLOCAL hloc = *ppv;
  41. if (cb) { /* Alloc or realloc */
  42. if (hloc) { /* Realloc */
  43. hloc = LocalReAlloc(*ppv, cb,
  44. LMEM_MOVEABLE+LMEM_ZEROINIT);
  45. } else { /* Alloc */
  46. hloc = LocalAlloc(LPTR, cb);
  47. }
  48. hres = hloc ? NOERROR : E_OUTOFMEMORY;
  49. } else { /* Freeing */
  50. if (hloc) {
  51. LocalFree(hloc);
  52. hloc = 0;
  53. hres = NOERROR; /* All gone */
  54. } else {
  55. hres = NOERROR; /* Nothing to free */
  56. }
  57. }
  58. if (SUCCEEDED(hres)) {
  59. *ppv = hloc;
  60. }
  61. return hres;
  62. }
  63. /*****************************************************************************
  64. *
  65. * AllocCbPpv
  66. *
  67. * Simple wrapper that forces *ppvObj = 0 before calling Realloc.
  68. *
  69. *****************************************************************************/
  70. STDMETHODIMP EXTERNAL
  71. AllocCbPpv(UINT cb, PPV ppv)
  72. {
  73. *ppv = 0;
  74. return ReallocCbPpv(cb, ppv);
  75. }
  76. #else
  77. /*****************************************************************************
  78. *
  79. * AllocCbPpv
  80. *
  81. * Allocate memory into the ppv.
  82. *
  83. *****************************************************************************/
  84. STDMETHODIMP EXTERNAL
  85. AllocCbPpv(UINT cb, PPV ppv)
  86. {
  87. HRESULT hres;
  88. *ppv = LocalAlloc(LPTR, cb);
  89. hres = *ppv ? NOERROR : E_OUTOFMEMORY;
  90. return hres;
  91. }
  92. /*****************************************************************************
  93. *
  94. * FreePpv
  95. *
  96. * Free memory from the ppv.
  97. *
  98. *****************************************************************************/
  99. void EXTERNAL
  100. FreePpv(PV ppv)
  101. {
  102. PV pv = (PV)InterlockedExchange(ppv, 0);
  103. if (pv) {
  104. FreePv(pv);
  105. }
  106. }
  107. #endif
  108. /*****************************************************************************
  109. *
  110. * @doc INTERNAL
  111. *
  112. * @func HRESULT | hresDupPtszPptsz |
  113. *
  114. * OLEish version of strdup.
  115. *
  116. * @parm LPCTSTR | ptszSrc |
  117. *
  118. * Source string being duplicated.
  119. *
  120. * @parm LPTSTR * | pptszDst |
  121. *
  122. * Receives the duplicated string.
  123. *
  124. * @returns
  125. *
  126. * <c S_OK> or an error code.
  127. *
  128. *****************************************************************************/
  129. HRESULT EXTERNAL
  130. hresDupPtszPptsz(LPCTSTR ptszSrc, LPTSTR *pptszDst)
  131. {
  132. HRESULT hres;
  133. hres = AllocCbPpv(cbCtch(lstrlen(ptszSrc) + 1), pptszDst);
  134. if(SUCCEEDED(hres))
  135. {
  136. lstrcpy(*pptszDst, ptszSrc);
  137. hres = S_OK;
  138. }
  139. return hres;
  140. }