2
0

DetachedStateBase.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. class DetachedStateBase
  9. {
  10. protected:
  11. TypeId typeId;
  12. bool hasBeenClaimed;
  13. public:
  14. DetachedStateBase(TypeId typeId)
  15. : typeId(typeId),
  16. hasBeenClaimed(false)
  17. {
  18. }
  19. virtual ~DetachedStateBase()
  20. {
  21. }
  22. TypeId GetTypeId() { return typeId; }
  23. bool HasBeenClaimed() { return hasBeenClaimed; }
  24. void MarkAsClaimed() { hasBeenClaimed = true; }
  25. void CleanUp()
  26. {
  27. if (!hasBeenClaimed)
  28. {
  29. DiscardState();
  30. }
  31. ClearSelfOnly();
  32. }
  33. virtual void ClearSelfOnly() = 0;
  34. virtual void DiscardState() = 0;
  35. virtual void Discard() = 0;
  36. virtual void AddRefBufferContent() = 0;
  37. virtual long ReleaseRefBufferContent() = 0;
  38. };
  39. typedef enum ArrayBufferAllocationType
  40. {
  41. Heap = 0x0,
  42. CoTask = 0x1,
  43. MemAlloc = 0x02,
  44. External = 0x03,
  45. } ArrayBufferAllocationType;
  46. class RefCountedBuffer;
  47. class ArrayBufferDetachedStateBase : public DetachedStateBase
  48. {
  49. public:
  50. RefCountedBuffer* buffer;
  51. uint32 bufferLength;
  52. ArrayBufferAllocationType allocationType;
  53. ArrayBufferDetachedStateBase(TypeId typeId, RefCountedBuffer* buffer, uint32 bufferLength, ArrayBufferAllocationType allocationType)
  54. : DetachedStateBase(typeId),
  55. buffer(buffer),
  56. bufferLength(bufferLength),
  57. allocationType(allocationType)
  58. {}
  59. virtual void AddRefBufferContent() override;
  60. virtual long ReleaseRefBufferContent() override;
  61. protected:
  62. // Clean up all local state. Different subclasses use different cleanup mechanisms for the buffer allocation.
  63. template <typename TFreeFn> void DiscardStateBase(TFreeFn freeFunction)
  64. {
  65. // this function will be called in the case where transferable object is going away and current arraybuffer is not claimed.
  66. if (this->buffer != nullptr)
  67. {
  68. RefCountedBuffer *local = this->buffer;
  69. this->buffer = nullptr;
  70. Assert(local->GetBuffer());
  71. if (local->GetBuffer() != nullptr)
  72. {
  73. long ref = local->Release();
  74. // The ref may not be 0, as we may have put the object in the DelayFree buffer list.
  75. if (ref == 0)
  76. {
  77. freeFunction(local->GetBuffer());
  78. HeapDelete(local);
  79. }
  80. }
  81. }
  82. this->bufferLength = 0;
  83. }
  84. };
  85. }