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