-
Notifications
You must be signed in to change notification settings - Fork 10
/
ObjSupplier.hpp
312 lines (276 loc) · 10 KB
/
ObjSupplier.hpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/// \file fon9/ObjSupplier.hpp
/// \author fonwinz@gmail.com
#ifndef __fon9_ObjSupplier_hpp__
#define __fon9_ObjSupplier_hpp__
#include "fon9/Timer.hpp"
#include <deque>
namespace fon9 {
/// \ingroup Misc
/// 不會直接使用此 class, 您應該使用 ObjCarrierTape<>.
/// - Clear(); Alloc(); **不是 thread safe**
/// - 一塊包含 ObjectCount_ 個 Objects 物件的記憶體.
/// - 這裡定義的是頭端的管理資料.
/// - 當全部的 Objects 都死亡時才會釋放 this;
class fon9_API ObjArrayMem : protected intrusive_ref_counter<ObjArrayMem> {
fon9_NON_COPY_NON_MOVE(ObjArrayMem);
template< typename DerivedT, typename CounterPolicyT >
friend unsigned int sp_adl_block::intrusive_ptr_release(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT;
protected:
uint32_t RemainCount_;
public:
const uint32_t ObjectCount_;
const uint16_t ArrayOffset_;
const uint16_t ObjectSize_;
fon9_MSC_WARN_DISABLE(4355); // 'this': used in base member initializer list
template <class ObjT, size_t kCount>
ObjArrayMem(ObjT (&array)[kCount])
: RemainCount_{kCount}
, ObjectCount_{kCount}
, ArrayOffset_{static_cast<uint16_t>(reinterpret_cast<byte*>(&array[0]) - reinterpret_cast<byte*>(this))}
, ObjectSize_{static_cast<uint16_t>(reinterpret_cast<byte*>(&array[1]) - reinterpret_cast<byte*>(&array[0]))} {
assert(reinterpret_cast<byte*>(this) + this->ArrayOffset_ == reinterpret_cast<byte*>(&array[0]));
assert(this->ObjectSize_ == (reinterpret_cast<byte*>(&array[1]) - reinterpret_cast<byte*>(&array[0])));
}
fon9_MSC_WARN_POP;
virtual ~ObjArrayMem();
virtual void Clear() = 0;
void* Alloc() {
if (fon9_LIKELY(this->RemainCount_ > 0))
return reinterpret_cast<byte*>(this) + this->ArrayOffset_ + this->ObjectSize_ * (--this->RemainCount_);
return nullptr;
}
size_t RemainCount() const {
return this->RemainCount_;
}
/// ObjArrayMem 的擁有者指標, 類似 std::unique_ptr 的功能.
/// - 在擁有者死亡時, 會呼叫 ObjArrayMem::Clear 清除剩餘未分配的物件.
/// - 但要等到已分配的物件也死光時, 才會釋放 ObjArrayMem.
class ThisSP {
fon9_NON_COPYABLE(ThisSP);
ObjArrayMem* Ptr_;
public:
ThisSP(ObjArrayMem* p = nullptr) : Ptr_{p} {
if (p)
intrusive_ptr_add_ref(p);
}
~ThisSP() {
if (this->Ptr_) {
this->Ptr_->Clear();
intrusive_ptr_release(this->Ptr_);
}
}
ThisSP(ThisSP&& rhs) : Ptr_{rhs.Ptr_} {
rhs.Ptr_ = nullptr;
}
ThisSP& operator=(ThisSP&& rhs) {
ThisSP old{std::move(*this)};
this->Ptr_ = rhs.Ptr_;
rhs.Ptr_ = nullptr;
return *this;
}
void swap(ThisSP& rhs) {
ObjArrayMem* rhsPtr = rhs.Ptr_;
rhs.Ptr_ = this->Ptr_;
this->Ptr_ = rhsPtr;
}
ObjArrayMem* operator->() const {
assert(this->Ptr_ != nullptr);
return this->Ptr_;
}
ObjArrayMem* get() const {
return this->Ptr_;
}
ObjArrayMem* release() {
ObjArrayMem* retval = this->Ptr_;
this->Ptr_ = nullptr;
return retval;
}
};
class ObjInjection {
friend class ObjArrayMem;
ObjArrayMem* MemOwner_{nullptr};
public:
~ObjInjection() {
assert(this->MemOwner_ == nullptr);
}
template<class Derived>
static void FreeObject(Derived* pDerived) {
if (pDerived->MemOwner_)
pDerived->MemOwner_->FreeObject(pDerived);
else
delete pDerived;
}
};
protected:
void SetObjectOwner(ObjInjection* pobj) {
assert(pobj->MemOwner_ == nullptr);
pobj->MemOwner_ = this;
intrusive_ptr_add_ref(this);
}
template<class Derived>
void FreeObject(Derived* pobj) {
assert(static_cast<ObjInjection*>(pobj)->MemOwner_ == this);
assert(reinterpret_cast<byte*>(this) + this->ArrayOffset_ <= reinterpret_cast<byte*>(pobj)
&& reinterpret_cast<byte*>(pobj) < reinterpret_cast<byte*>(this) + this->ArrayOffset_ + this->ObjectSize_ * this->ObjectCount_);
static_cast<ObjInjection*>(pobj)->MemOwner_ = nullptr;
fon9::destroy_at(pobj);
intrusive_ptr_release(this);
}
};
using ObjArrayMemSP = ObjArrayMem::ThisSP;
/// \ingroup Misc
/// Object Carrier Tape.
/// - Clear(); Alloc(); **不是 thread safe**
/// - 用於想要快速分配「已初始化的物件」
/// - 物件全部釋(Alloc + FreeThis)放後, 才會釋放 this;
/// - 適用於: 物件分配後幾乎不會釋放(或每日釋放全部)的情境.
/// - 例如: OMS 的下單要求、委託書, 這類的應用.
/// 因為這些物件一旦分配, 就會一直保留到隔日清檔.
/// - ObjectT 必須提供保護: 從這裡分配出去的物件禁止直接 delete, 必須透過 FreeThis() 釋放;
/// \code
/// class Object {
/// protected:
/// // 禁止直接 delete this; 需透過 FreeThis() 釋放;
/// virtual ~Object();
/// public:
/// // 必須透過 FreeThis() 來刪除, 預設 delete this;
/// // 若有使用 ObjCarrierTape 則將 this 還給 ObjCarrierTape;
/// virtual void FreeThis() { delete this; }
/// };
/// \endcode
template <class ObjectT, uint32_t kCount>
class ObjCarrierTape : public ObjArrayMem {
fon9_NON_COPY_NON_MOVE(ObjCarrierTape);
using base = ObjArrayMem;
fon9_MSC_WARN_DISABLE(4582// 'ObjectArray_' : constructor is not implicitly called
4583// 'ObjectArray_' : destructor is not implicitly called
4355// 'this' : used in base member initializer list
);
// 僅允許在 intrusive_ptr_release() 時刪除.
~ObjCarrierTape() {
}
public:
/// args = ObjectT 的建構參數.
template <class... ArgsT>
ObjCarrierTape(ArgsT&&... args) : base{this->ObjectArray_} {
memset(&this->ObjectArray_, 0, sizeof(this->ObjectArray_));
auto* iobj = &this->ObjectArray_[0];
for (uint32_t L = 0; L < kCount; ++L, ++iobj) {
auto* pobj = InplaceNew<TapedObject>(&*iobj, std::forward<ArgsT>(args)...);
this->SetObjectOwner(pobj);
}
}
fon9_MSC_WARN_POP;
fon9_MSC_WARN_DISABLE(4623); // default constructor was implicitly defined as deleted
struct TapedObject final : public ObjectT, public ObjInjection {
fon9_NON_COPY_NON_MOVE(TapedObject);
using ObjectT::ObjectT;
TapedObject() = default;
void FreeThis() { // override
ObjInjection::FreeObject(this);
}
};
fon9_MSC_WARN_POP;
class ThisSP : public ObjArrayMemSP {
fon9_NON_COPYABLE(ThisSP);
using base = ObjArrayMemSP;
public:
ThisSP(ObjCarrierTape* p = nullptr) : base{p} {
}
ThisSP(ThisSP&& rhs) = default;
ThisSP& operator=(ThisSP&& rhs) = default;
ObjCarrierTape* operator->() const {
assert(this->get() != nullptr);
return this->get();
}
ObjCarrierTape* get() const {
return static_cast<ObjCarrierTape*>(this->base::get());
}
};
TapedObject* Alloc() {
if (fon9_LIKELY(this->RemainCount_ > 0))
return &this->ObjectArray_[--this->RemainCount_];
return nullptr;
}
void Clear() override {
while (this->RemainCount_ > 0)
this->FreeObject(&this->ObjectArray_[--this->RemainCount_]);
}
private:
union { // ObjectArray_ 放在 union 裡面: 建構時不要初始化!
TapedObject ObjectArray_[kCount];
};
};
/// \ingroup Misc
/// 不會直接使用此 class, 您應該使用 ObjSupplier<>.
class fon9_API ObjSupplierBase : public intrusive_ref_counter<ObjSupplierBase> {
fon9_NON_COPY_NON_MOVE(ObjSupplierBase);
const uint32_t ExtPoolCount_;
struct Timer : public DataMemberTimer {
fon9_NON_COPY_NON_MOVE(Timer);
Timer() = default;
void EmitOnTimer(TimeStamp now) override;
};
Timer FillTimer_;
using PoolImpl = std::deque<ObjArrayMemSP>;
using Pool = fon9::MustLock<PoolImpl>;
Pool Pool_;
virtual ObjArrayMemSP MakeCarrierTape() = 0;
void* AllocFormNewPool(ObjArrayMemSP& tlsTape);
protected:
void* Alloc(ObjArrayMemSP& tlsTape) {
if (auto* tape = tlsTape.get()) {
if (void* pobj = tape->Alloc())
return pobj;
}
return this->AllocFormNewPool(tlsTape);
}
/// 停止 FillTimer_; 應由衍生者解構時呼叫.
/// 因為在 FillPool() 時, 會呼叫 this->MakeCarrierTape();
/// 若衍生者已死, 則會 "pure virtual method called".
void OnBeforeDestroy();
public:
ObjSupplierBase(uint32_t extPoolCount)
: ExtPoolCount_{extPoolCount} {
}
virtual ~ObjSupplierBase();
void FillPool();
};
/// \ingroup Misc
/// - 每個 thread 一個 thread_local ObjCarrierTape;
/// - 當 thread_local ObjCarrierTape; 用完時, 從 Pool 取得一個 ObjCarrierTape;
/// 如果 Pool 裡面的 ObjCarrierTape 也用完(尚未補充), 則直接建立「一個」新的 ObjectT;
/// - 定時(例如:0.1秒), 補充 Pool 裡面的 ObjCarrierTape;
/// - ObjectT 必須提供預設建構: new ObjectT{}.
template <class ObjectT, uint32_t kObjectCount, uint32_t kExtPoolCount = 1>
class ObjSupplier : public ObjSupplierBase {
fon9_NON_COPY_NON_MOVE(ObjSupplier);
using base = ObjSupplierBase;
/// 僅能使用 Make() 建立一個 singleton;
ObjSupplier() : base{kExtPoolCount} {
this->FillPool();
}
~ObjSupplier() {
this->OnBeforeDestroy();
}
using CarrierTape = ObjCarrierTape<ObjectT, kObjectCount>;
ObjArrayMemSP MakeCarrierTape() override {
return new CarrierTape{};
}
public:
using ThisSP = intrusive_ptr<ObjSupplier>;
using TapedObject = typename CarrierTape::TapedObject;
/// 即使呼叫多次 Make() 傳回的都是同一個.
static ThisSP Make() {
static ThisSP singleton{new ObjSupplier};
return singleton;
}
ObjectT* Alloc() {
thread_local ObjArrayMemSP TlsTape_;
if (void* pobj = base::Alloc(TlsTape_))
return reinterpret_cast<TapedObject*>(pobj);
return new ObjectT{};
}
};
} // namespaces
#endif//__fon9_ObjSupplier_hpp__