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.

101 lines
2.4 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. // last ditch effort: if the task allocator returned -1 (may have alloc'd),
  46. // then we assume it is task memory. We do this after the above tests
  47. // since we would prefer to be exact.
  48. if (iDidTaskAlloc == -1)
  49. return MEMCTX_TASK;
  50. return MEMCTX_UNKNOWN;
  51. }
  52. STDAPI_(void FAR*) CoMemAlloc(ULONG size, DWORD memctx, void FAR* lpvNear)
  53. {
  54. if (memctx == MEMCTX_SAME)
  55. {
  56. AssertSz(lpvNear != NULL,0);
  57. memctx = CoMemctxOf(lpvNear);
  58. }
  59. else
  60. AssertSz(lpvNear == NULL,0);
  61. IMalloc FAR* pMalloc;
  62. void FAR* lpv = NULL; // stays null if bad context or out of memory
  63. if (CoGetMalloc(memctx, &pMalloc) == 0) {
  64. lpv = pMalloc->Alloc(size);
  65. pMalloc->Release();
  66. }
  67. return lpv;
  68. }
  69. STDAPI_(void) CoMemFree(void FAR* lpv, DWORD memctx)
  70. {
  71. if (lpv == NULL)
  72. return;
  73. if (memctx == MEMCTX_UNKNOWN)
  74. memctx = CoMemctxOf(lpv);
  75. IMalloc FAR* pMalloc;
  76. if (CoGetMalloc(memctx, &pMalloc) == 0) {
  77. pMalloc->Free(lpv);
  78. pMalloc->Release();
  79. }
  80. }