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.

69 lines
1.3 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // File: memutils.cxx
  4. //
  5. // Contents: IMalloc-related helpers
  6. //
  7. //----------------------------------------------------------------------
  8. #include "headers.hxx"
  9. #pragma hdrstop
  10. HRESULT
  11. OleAllocMem(MEMCTX ctx, ULONG cb, LPVOID FAR* ppv)
  12. {
  13. HRESULT r;
  14. LPMALLOC pMalloc;
  15. if (OK(r = CoGetMalloc(ctx, &pMalloc)))
  16. {
  17. *ppv = pMalloc->Alloc(cb);
  18. if (*ppv == NULL)
  19. {
  20. DOUT(TEXT("o2base/memutils/OleAllocMem failed\r\n"));
  21. r = E_OUTOFMEMORY;
  22. }
  23. pMalloc->Release();
  24. }
  25. return r;
  26. }
  27. void
  28. OleFreeMem(MEMCTX ctx, LPVOID pv)
  29. {
  30. LPMALLOC pMalloc;
  31. if (OK(CoGetMalloc(ctx, &pMalloc)))
  32. {
  33. pMalloc->Free(pv);
  34. pMalloc->Release();
  35. }
  36. }
  37. HRESULT
  38. OleAllocString(MEMCTX ctx, LPCOLESTR lpstrSrc, LPOLESTR FAR* ppstr)
  39. {
  40. HRESULT r;
  41. if (lpstrSrc == NULL)
  42. {
  43. *ppstr = NULL;
  44. r = NOERROR;
  45. }
  46. else
  47. {
  48. r = OleAllocMem(ctx,
  49. (ostrlen(lpstrSrc)+1) * sizeof(OLECHAR),
  50. (LPVOID FAR*)ppstr);
  51. if (*ppstr != NULL)
  52. {
  53. ostrcpy((LPOLESTR)*ppstr, lpstrSrc);
  54. }
  55. }
  56. return r;
  57. }
  58. void
  59. OleFreeString(MEMCTX ctx, LPOLESTR lpstr)
  60. {
  61. OleFreeMem(ctx, lpstr);
  62. }