forked from RPCS3/rpcs3
/
IdManager.h
621 lines (504 loc) · 14.2 KB
/
IdManager.h
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#pragma once
#include "Utilities/types.h"
#include "Utilities/mutex.h"
#include <memory>
#include <vector>
// Helper namespace
namespace id_manager
{
// Common global mutex
extern shared_mutex g_mutex;
// ID traits
template <typename T, typename = void>
struct id_traits
{
static_assert(sizeof(T) == 0, "ID object must specify: id_base, id_step, id_count");
static const u32 base = 1; // First ID (N = 0)
static const u32 step = 1; // Any ID: N * id_step + id_base
static const u32 count = 65535; // Limit: N < id_count
static const u32 invalid = 0;
};
template <typename T>
struct id_traits<T, std::void_t<decltype(&T::id_base), decltype(&T::id_step), decltype(&T::id_count)>>
{
static const u32 base = T::id_base;
static const u32 step = T::id_step;
static const u32 count = T::id_count;
static const u32 invalid = -+!base;
// Note: full 32 bits range cannot be used at current implementation
static_assert(count && step && u64{step} * (count - 1) + base < u64{UINT32_MAX} + (base != 0 ? 1 : 0), "ID traits: invalid object range");
};
// Correct usage testing
template <typename T, typename T2, typename = void>
struct id_verify : std::integral_constant<bool, std::is_base_of<T, T2>::value>
{
// If common case, T2 shall be derived from or equal to T
};
template <typename T, typename T2>
struct id_verify<T, T2, std::void_t<typename T2::id_type>> : std::integral_constant<bool, std::is_same<T, typename T2::id_type>::value>
{
// If T2 contains id_type type, T must be equal to it
};
class typeinfo
{
// Global variable for each registered type
template <typename T>
struct registered
{
static const u32 index;
};
// Increment type counter
static u32 add_type(u32 i)
{
static atomic_t<u32> g_next{0};
return g_next.fetch_add(i);
}
public:
// Get type index
template <typename T>
static inline u32 get_index()
{
return registered<T>::index;
}
// Get type count
static inline u32 get_count()
{
return add_type(0);
}
};
template <typename T>
const u32 typeinfo::registered<T>::index = typeinfo::add_type(1);
// ID value with additional type stored
class id_key
{
u32 m_value; // ID value
u32 m_type; // True object type
public:
id_key() = default;
id_key(u32 value, u32 type)
: m_value(value)
, m_type(type)
{
}
u32 value() const
{
return m_value;
}
u32 type() const
{
return m_type;
}
operator u32() const
{
return m_value;
}
};
using id_map = std::vector<std::pair<id_key, std::shared_ptr<void>>>;
}
// Object manager for emulated process. Multiple objects of specified arbitrary type are given unique IDs.
class idm
{
// Last allocated ID for constructors
static thread_local u32 g_id;
// Type Index -> ID -> Object. Use global since only one process is supported atm.
static std::vector<id_manager::id_map> g_map;
template <typename T>
static inline u32 get_type()
{
return id_manager::typeinfo::get_index<T>();
}
template <typename T>
static constexpr u32 get_index(u32 id)
{
using traits = id_manager::id_traits<T>;
// Note: if id is lower than base, diff / step will be higher than count
u32 diff = id - traits::base;
if (diff % traits::step)
{
// id is invalid, return invalid index
return traits::count;
}
// Get actual index
return diff / traits::step;
}
// Helper
template <typename F>
struct function_traits;
template <typename F, typename R, typename A1, typename A2>
struct function_traits<R (F::*)(A1, A2&) const>
{
using object_type = A2;
using result_type = R;
};
template <typename F, typename R, typename A1, typename A2>
struct function_traits<R (F::*)(A1, A2&)>
{
using object_type = A2;
using result_type = R;
};
template <typename F, typename A1, typename A2>
struct function_traits<void (F::*)(A1, A2&) const>
{
using object_type = A2;
using void_type = void;
};
template <typename F, typename A1, typename A2>
struct function_traits<void (F::*)(A1, A2&)>
{
using object_type = A2;
using void_type = void;
};
// Helper type: pointer + return value propagated
template <typename T, typename RT>
struct return_pair
{
std::shared_ptr<T> ptr;
RT ret;
explicit operator bool() const
{
return ptr.operator bool();
}
T& operator*() const
{
return *ptr;
}
T* operator->() const
{
return ptr.get();
}
};
// Unsafe specialization (not refcounted)
template <typename T, typename RT>
struct return_pair<T*, RT>
{
T* ptr;
RT ret;
explicit operator bool() const
{
return ptr != nullptr;
}
T& operator*() const
{
return *ptr;
}
T* operator->() const
{
return ptr;
}
};
// Prepare new ID (returns nullptr if out of resources)
static id_manager::id_map::pointer allocate_id(const id_manager::id_key& info, u32 base, u32 step, u32 count);
// Find ID (additionally check type if types are not equal)
template <typename T, typename Type>
static id_manager::id_map::pointer find_id(u32 id)
{
static_assert(id_manager::id_verify<T, Type>::value, "Invalid ID type combination");
const u32 index = get_index<Type>(id);
if (index >= id_manager::id_traits<Type>::count)
{
return nullptr;
}
auto& vec = g_map[get_type<T>()];
if (index >= vec.size())
{
return nullptr;
}
auto& data = vec[index];
if (data.second)
{
if (std::is_same<T, Type>::value || data.first.type() == get_type<Type>())
{
return &data;
}
}
return nullptr;
}
// Allocate new ID and assign the object from the provider()
template <typename T, typename Type, typename F>
static id_manager::id_map::pointer create_id(F&& provider)
{
static_assert(id_manager::id_verify<T, Type>::value, "Invalid ID type combination");
// ID info
const id_manager::id_key info{get_type<T>(), get_type<Type>()};
// ID traits
using traits = id_manager::id_traits<Type>;
// Allocate new id
std::lock_guard lock(id_manager::g_mutex);
if (auto* place = allocate_id(info, traits::base, traits::step, traits::count))
{
// Get object, store it
place->second = provider();
if (place->second)
{
return place;
}
}
return nullptr;
}
public:
// Initialize object manager
static void init();
// Remove all objects
static void clear();
// Get last ID (updated in create_id/allocate_id)
static inline u32 last_id()
{
return g_id;
}
// Add a new ID of specified type with specified constructor arguments (returns object or nullptr)
template <typename T, typename Make = T, typename... Args>
static inline std::enable_if_t<std::is_constructible<Make, Args...>::value, std::shared_ptr<Make>> make_ptr(Args&&... args)
{
if (auto pair = create_id<T, Make>([&] { return std::make_shared<Make>(std::forward<Args>(args)...); }))
{
return {pair->second, static_cast<Make*>(pair->second.get())};
}
return nullptr;
}
// Add a new ID of specified type with specified constructor arguments (returns id)
template <typename T, typename Make = T, typename... Args>
static inline std::enable_if_t<std::is_constructible<Make, Args...>::value, u32> make(Args&&... args)
{
if (auto pair = create_id<T, Make>([&] { return std::make_shared<Make>(std::forward<Args>(args)...); }))
{
return pair->first;
}
return id_manager::id_traits<Make>::invalid;
}
// Add a new ID for an existing object provided (returns new id)
template <typename T, typename Made = T>
static inline u32 import_existing(const std::shared_ptr<T>& ptr)
{
if (auto pair = create_id<T, Made>([&] { return ptr; }))
{
return pair->first;
}
return id_manager::id_traits<Made>::invalid;
}
// Add a new ID for an object returned by provider()
template <typename T, typename Made = T, typename F, typename = std::invoke_result_t<F>>
static inline u32 import(F&& provider)
{
if (auto pair = create_id<T, Made>(std::forward<F>(provider)))
{
return pair->first;
}
return id_manager::id_traits<Made>::invalid;
}
// Access the ID record without locking (unsafe)
template <typename T, typename Get = T>
static inline id_manager::id_map::pointer find_unlocked(u32 id)
{
return find_id<T, Get>(id);
}
// Check the ID without locking (can be called from other method)
template <typename T, typename Get = T>
static inline Get* check_unlocked(u32 id)
{
if (const auto found = find_id<T, Get>(id))
{
return static_cast<Get*>(found->second.get());
}
return nullptr;
}
// Check the ID
template <typename T, typename Get = T>
static inline Get* check(u32 id)
{
reader_lock lock(id_manager::g_mutex);
return check_unlocked<T, Get>(id);
}
// Check the ID, access object under shared lock
template <typename T, typename Get = T, typename F, typename FRT = std::invoke_result_t<F, Get&>>
static inline auto check(u32 id, F&& func)
{
reader_lock lock(id_manager::g_mutex);
if (const auto ptr = check_unlocked<T, Get>(id))
{
if constexpr (!std::is_void_v<FRT>)
{
return return_pair<Get*, FRT>{ptr, func(*ptr)};
}
else
{
func(*ptr);
return ptr;
}
}
if constexpr (!std::is_void_v<FRT>)
{
return return_pair<Get*, FRT>{nullptr};
}
else
{
return static_cast<Get*>(nullptr);
}
}
// Get the object without locking (can be called from other method)
template <typename T, typename Get = T>
static inline std::shared_ptr<Get> get_unlocked(u32 id)
{
const auto found = find_id<T, Get>(id);
if (found == nullptr) [[unlikely]]
{
return nullptr;
}
return std::static_pointer_cast<Get>(found->second);
}
// Get the object
template <typename T, typename Get = T>
static inline std::shared_ptr<Get> get(u32 id)
{
reader_lock lock(id_manager::g_mutex);
const auto found = find_id<T, Get>(id);
if (found == nullptr) [[unlikely]]
{
return nullptr;
}
return std::static_pointer_cast<Get>(found->second);
}
// Get the object, access object under reader lock
template <typename T, typename Get = T, typename F, typename FRT = std::invoke_result_t<F, Get&>>
static inline std::conditional_t<std::is_void_v<FRT>, std::shared_ptr<Get>, return_pair<Get, FRT>> get(u32 id, F&& func)
{
reader_lock lock(id_manager::g_mutex);
const auto found = find_id<T, Get>(id);
if (found == nullptr) [[unlikely]]
{
return {nullptr};
}
const auto ptr = static_cast<Get*>(found->second.get());
if constexpr (std::is_void_v<FRT>)
{
func(*ptr);
return {found->second, ptr};
}
else
{
return {{found->second, ptr}, func(*ptr)};
}
}
// Access all objects of specified type. Returns the number of objects processed.
template <typename T, typename Get = T, typename F, typename FT = decltype(&std::decay_t<F>::operator()), typename FRT = typename function_traits<FT>::void_type>
static inline u32 select(F&& func, int = 0)
{
static_assert(id_manager::id_verify<T, Get>::value, "Invalid ID type combination");
reader_lock lock(id_manager::g_mutex);
u32 result = 0;
for (auto& id : g_map[get_type<T>()])
{
if (id.second)
{
if (std::is_same<T, Get>::value || id.first.type() == get_type<Get>())
{
func(id.first, *static_cast<typename function_traits<FT>::object_type*>(id.second.get()));
result++;
}
}
}
return result;
}
// Access all objects of specified type. If function result evaluates to true, stop and return the object and the value.
template <typename T, typename Get = T, typename F, typename FT = decltype(&std::decay_t<F>::operator()), typename FRT = typename function_traits<FT>::result_type>
static inline auto select(F&& func)
{
static_assert(id_manager::id_verify<T, Get>::value, "Invalid ID type combination");
using object_type = typename function_traits<FT>::object_type;
using result_type = return_pair<object_type, FRT>;
reader_lock lock(id_manager::g_mutex);
for (auto& id : g_map[get_type<T>()])
{
if (auto ptr = static_cast<object_type*>(id.second.get()))
{
if (std::is_same<T, Get>::value || id.first.type() == get_type<Get>())
{
if (FRT result = func(id.first, *ptr))
{
return result_type{{id.second, ptr}, std::move(result)};
}
}
}
}
return result_type{nullptr};
}
// Remove the ID
template <typename T, typename Get = T>
static inline bool remove(u32 id)
{
std::shared_ptr<void> ptr;
{
std::lock_guard lock(id_manager::g_mutex);
if (const auto found = find_id<T, Get>(id))
{
ptr = std::move(found->second);
}
else
{
return false;
}
}
return true;
}
// Remove the ID if matches the weak/shared ptr
template <typename T, typename Get = T, typename Ptr>
static inline bool remove_verify(u32 id, Ptr sptr)
{
std::shared_ptr<void> ptr;
{
std::lock_guard lock(id_manager::g_mutex);
if (const auto found = find_id<T, Get>(id); found &&
(!found->second.owner_before(sptr) && !sptr.owner_before(found->second)))
{
ptr = std::move(found->second);
}
else
{
return false;
}
}
return true;
}
// Remove the ID and return the object
template <typename T, typename Get = T>
static inline std::shared_ptr<Get> withdraw(u32 id)
{
std::shared_ptr<Get> ptr;
{
std::lock_guard lock(id_manager::g_mutex);
if (const auto found = find_id<T, Get>(id))
{
ptr = std::static_pointer_cast<Get>(std::move(found->second));
}
}
return ptr;
}
// Remove the ID after accessing the object under writer lock, return the object and propagate return value
template <typename T, typename Get = T, typename F, typename FRT = std::invoke_result_t<F, Get&>>
static inline std::conditional_t<std::is_void_v<FRT>, std::shared_ptr<Get>, return_pair<Get, FRT>> withdraw(u32 id, F&& func)
{
std::unique_lock lock(id_manager::g_mutex);
if (const auto found = find_id<T, Get>(id))
{
const auto _ptr = static_cast<Get*>(found->second.get());
if constexpr (std::is_void_v<FRT>)
{
func(*_ptr);
return std::static_pointer_cast<Get>(std::move(found->second));
}
else
{
FRT ret = func(*_ptr);
if (ret)
{
// If return value evaluates to true, don't delete the object (error code)
return {{found->second, _ptr}, std::move(ret)};
}
return {std::static_pointer_cast<Get>(std::move(found->second)), std::move(ret)};
}
}
return {nullptr};
}
};
#include "util/fixed_typemap.hpp"
extern stx::manual_fixed_typemap<void> g_fixed_typemap;
constexpr stx::manual_fixed_typemap<void>* g_fxo = &g_fixed_typemap;