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.

109 lines
2.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: memctx.cxx
  7. //
  8. // Contents: all memory management for compobj dll (exported as well)
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 10-Mar-94 bobday Ported from ole2.01 (16-bit)
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <headers.cxx>
  18. #pragma hdrstop
  19. #include <ole2sp.h>
  20. #include <olecoll.h>
  21. #include <map_kv.h>
  22. #include <memctx.hxx>
  23. #include "comlocal.hxx"
  24. #include "map_htsk.h"
  25. #include "etask.hxx"
  26. ASSERTDATA
  27. STDAPI_(DWORD) CoMemctxOf(void const FAR* lpv)
  28. {
  29. Etask etask;
  30. HTASK htask;
  31. int iDidTaskAlloc;
  32. if (lpv == NULL)
  33. return MEMCTX_UNKNOWN;
  34. // try shared memory first because of a bootstrapping problem when setting
  35. // the Etask the first time.
  36. if (v_pMallocShared != NULL && v_pMallocShared->DidAlloc((LPVOID)lpv) == 1)
  37. return MEMCTX_SHARED;
  38. // now try the other contexts by getting the pointers to their mallocs
  39. if (!LookupEtask(htask, etask))
  40. return MEMCTX_UNKNOWN;
  41. Assert(etask.m_pMalloc != NULL); // should always have one
  42. if ((iDidTaskAlloc = etask.m_pMalloc->DidAlloc((LPVOID)lpv)) == 1)
  43. return MEMCTX_TASK;
  44. Assert(etask.m_pMallocShared == NULL || etask.m_pMallocShared == v_pMallocShared);
  45. #ifdef NOTYET
  46. if (etask.m_pMallocSBlock != NULL && etask.m_pMallocSBlock->DidAlloc((LPVOID)lpv) == 1)
  47. return MEMCTX_SHAREDBLOCK;
  48. if (etask.m_pMallocPrivate != NULL && etask.m_pMallocPrivate->DidAlloc((LPVOID)lpv) == 1)
  49. return MEMCTX_COPRIVATE;
  50. #endif
  51. // last ditch effort: if the task allocator returned -1 (may have alloc'd),
  52. // then we assume it is task memory. We do this after the above tests
  53. // since we would prefer to be exact.
  54. if (iDidTaskAlloc == -1)
  55. return MEMCTX_TASK;
  56. return MEMCTX_UNKNOWN;
  57. }
  58. STDAPI_(void FAR*) CoMemAlloc(ULONG size, DWORD memctx, void FAR* lpvNear)
  59. {
  60. if (memctx == MEMCTX_SAME)
  61. {
  62. AssertSz(lpvNear != NULL,0);
  63. memctx = CoMemctxOf(lpvNear);
  64. }
  65. else
  66. AssertSz(lpvNear == NULL,0);
  67. IMalloc FAR* pMalloc;
  68. void FAR* lpv = NULL; // stays null if bad context or out of memory
  69. if (CoGetMalloc(memctx, &pMalloc) == 0) {
  70. lpv = pMalloc->Alloc(size);
  71. pMalloc->Release();
  72. }
  73. return lpv;
  74. }
  75. STDAPI_(void) CoMemFree(void FAR* lpv, DWORD memctx)
  76. {
  77. if (lpv == NULL)
  78. return;
  79. if (memctx == MEMCTX_UNKNOWN)
  80. memctx = CoMemctxOf(lpv);
  81. IMalloc FAR* pMalloc;
  82. if (CoGetMalloc(memctx, &pMalloc) == 0) {
  83. pMalloc->Free(lpv);
  84. pMalloc->Release();
  85. }
  86. }