-
Notifications
You must be signed in to change notification settings - Fork 8
/
shared.cc
230 lines (193 loc) · 5.3 KB
/
shared.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <atomic>
#include <windows.h>
// Simple shared object,
// wrapped around Crafting Interpreter hash table (http://www.craftinginterpreters.com/hash-tables.html)
// and IDispatch interface (https://docs.microsoft.com/en-us/windows/win32/api/oaidl/nn-oaidl-idispatch).
// Refs
// https://github.com/AutoItMicro/AutoItObject
// https://github.com/nomi-san/mp.au3/blob/8e74a590943887f1ff035dadf246674561e617f3/mp.cc#L285
class Entry
{
public:
const UINT32 Key = 0;
const VARIANT Value = { VT_EMPTY };
~Entry()
{
VariantClear(const_cast<VARIANT *>(&Value));
}
void Set(UINT32 key, const VARIANT *value)
{
const_cast<UINT &>(Key) = key;
VariantCopy(const_cast<VARIANT *>(&Value), value);
}
bool IsEmpty() const
{
return Value.vt == VT_EMPTY;
}
};
class Shared : public IDispatch
{
Entry *m_entries;
int m_count, m_capacity;
std::atomic<int> m_ref;
static constexpr VARIANT Empty = { VT_EMPTY };
// FNV-1a 32
static UINT32 FnvHash(LPCWSTR key, UINT length)
{
UINT32 hash = 2166136261U;
for (UINT i = 0; i < length; i++)
{
hash ^= key[i];
hash *= 16777619;
}
return hash;
}
public:
Shared()
{
m_ref = 0;
m_count = 0;
m_capacity = 0;
m_entries = nullptr;
}
~Shared()
{
if (m_entries != nullptr)
{
delete[] m_entries;
m_entries = nullptr;
}
m_count = 0;
m_capacity = 0;
}
ULONG STDMETHODCALLTYPE AddRef() override
{
// Increase ref count.
return ++m_ref;
}
ULONG STDMETHODCALLTYPE Release() override
{
// Decrease ref count.
if (--m_ref == 0)
{
// Self release.
delete this;
return 0;
}
return m_ref;
}
HRESULT STDMETHODCALLTYPE QueryInterface(const IID &riid, void **ppvObject) override
{
if (riid == IID_IUnknown || riid == IID_IDispatch)
{
this->AddRef();
*ppvObject = this;
return S_OK;
}
return E_NOINTERFACE;
}
HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT *pctinfo) override
{
if (pctinfo) *pctinfo = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) override
{
if (ppTInfo) *ppTInfo = NULL;
return S_OK;
}
HRESULT STDMETHODCALLTYPE GetIDsOfNames(const IID &riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) override
{
*rgDispId = FnvHash(*rgszNames, lstrlenW(*rgszNames));
return S_OK;
}
HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, const IID &riid, LCID lcid, WORD wFlags,
DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) override
{
if ((wFlags & DISPATCH_METHOD) || (wFlags & DISPATCH_PROPERTYGET))
{
Get((UINT32)dispIdMember, pVarResult);
return S_OK;
}
else if ((wFlags & DISPATCH_PROPERTYPUT) || (wFlags & DISPATCH_PROPERTYPUTREF))
{
Set((UINT32)dispIdMember, &pDispParams->rgvarg[0]);
return S_OK;
}
return DISP_E_MEMBERNOTFOUND;
}
private:
void Get(UINT32 key, VARIANT *value)
{
if (m_count != 0)
{
Entry *entry = FindEntry(m_entries, m_capacity, key);
if (entry->Key != 0)
{
VariantCopy(value, &entry->Value);
return;
}
}
VariantCopy(value, &Empty);
}
void Set(UINT32 key, const VARIANT *value)
{
if (m_count >= m_capacity * 0.75)
{
Expand((m_capacity < 8) ? 8 : (m_capacity * 2));
}
Entry *entry = FindEntry(m_entries, m_capacity, key);
bool isNewKey = entry->Key == 0;
if (isNewKey && entry->IsEmpty())
{
m_count++;
}
entry->Set(key, value);
}
static Entry *FindEntry(Entry *entries, int capacity, UINT32 key)
{
int capMask = capacity - 1;
UINT32 index = key & capMask;
Entry *tombstone = nullptr;
for (;;)
{
Entry *entry = &entries[index];
if (entry->Key == 0)
{
if (entry->IsEmpty())
{
return tombstone != nullptr ? tombstone : entry;
}
else
{
if (tombstone == nullptr) tombstone = entry;
}
}
else if (entry->Key == key)
{
return entry;
}
index = (index + 1) & capMask;
}
}
void Expand(int capacity)
{
Entry *entries = new Entry[capacity];
m_count = 0;
for (int i = 0; i < m_capacity; i++)
{
Entry *entry = &m_entries[i];
if (entry->Key == 0) continue;
Entry *dest = FindEntry(entries, capacity, entry->Key);
dest->Set(entry->Key, &entry->Value);
m_count++;
}
this->~Shared();
m_capacity = capacity;
m_entries = entries;
}
};
LPDISPATCH N_CreateShared()
{
return new Shared();
}