Skip to content

Commit b1b316d

Browse files
committed
Remove <atomic> from LifetimeSanitizer.h.
1 parent de10ade commit b1b316d

File tree

3 files changed

+108
-109
lines changed

3 files changed

+108
-109
lines changed

Common/Cpp/LifetimeSanitizer.cpp

Lines changed: 95 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <set>
88
//#include <exception>
9+
#include <atomic>
910
#include <iostream>
1011
#include "Concurrency/SpinLock.h"
1112
#include "LifetimeSanitizer.h"
@@ -44,6 +45,100 @@ void LifetimeSanitizer::set_enabled(bool enabled){
4445
LifetimeSanitizer_enabled.store(false, std::memory_order_relaxed);
4546
}
4647

48+
PA_NO_INLINE void LifetimeSanitizer::terminate_with_dump(){
49+
// Intentionally crash the program here and let the crash dump deal with
50+
// the error reporting.
51+
52+
#ifdef _WIN32
53+
Sleep(500);
54+
#endif
55+
56+
std::cerr << (char*)nullptr << std::endl;
57+
}
58+
59+
60+
61+
62+
LifetimeSanitizer::LifetimeSanitizer(const char* name)
63+
: m_token(SANITIZER_TOKEN)
64+
, m_self(this)
65+
, m_name(name)
66+
{
67+
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
68+
return;
69+
}
70+
internal_construct();
71+
}
72+
LifetimeSanitizer::~LifetimeSanitizer(){
73+
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
74+
m_self = nullptr;
75+
return;
76+
}
77+
internal_destruct();
78+
}
79+
80+
81+
LifetimeSanitizer::LifetimeSanitizer(LifetimeSanitizer&& x)
82+
: m_token(SANITIZER_TOKEN)
83+
, m_self(this)
84+
, m_name(x.m_name)
85+
{
86+
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
87+
return;
88+
}
89+
x.check_usage();
90+
internal_construct();
91+
}
92+
void LifetimeSanitizer::operator=(LifetimeSanitizer&& x){
93+
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
94+
return;
95+
}
96+
check_usage();
97+
x.check_usage();
98+
}
99+
LifetimeSanitizer::LifetimeSanitizer(const LifetimeSanitizer& x)
100+
: m_token(SANITIZER_TOKEN)
101+
, m_self(this)
102+
, m_name(x.m_name)
103+
{
104+
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
105+
return;
106+
}
107+
x.check_usage();
108+
internal_construct();
109+
}
110+
void LifetimeSanitizer::operator=(const LifetimeSanitizer& x){
111+
check_usage();
112+
x.check_usage();
113+
}
114+
115+
116+
117+
void LifetimeSanitizer::check_usage() const{
118+
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
119+
return;
120+
}
121+
122+
if (m_token != SANITIZER_TOKEN || m_self != this){
123+
std::cerr << "Use non-existant: " << this << " : " << m_name << std::endl;
124+
terminate_with_dump();
125+
}
126+
127+
if (LifetimeSanitizer_has_been_disabled){
128+
return;
129+
}
130+
131+
ReadSpinLock lg(sanitizer_lock);
132+
if (SANITIZER_FILTER.contains(m_name)){
133+
std::cout << "LifetimeSanitizer - Using: " << this << " : " << m_name << std::endl;
134+
}
135+
auto iter = sanitizer_map.find(this);
136+
if (iter != sanitizer_map.end()){
137+
return;
138+
}
139+
std::cerr << "Use non-existant: " << this << " : " << m_name << std::endl;
140+
terminate_with_dump();
141+
}
47142

48143

49144
void LifetimeSanitizer::internal_construct(){
@@ -93,42 +188,5 @@ void LifetimeSanitizer::internal_destruct(){
93188

94189

95190

96-
void LifetimeSanitizer::internal_check_usage() const{
97-
if (m_token != SANITIZER_TOKEN || m_self != this){
98-
std::cerr << "Use non-existant: " << this << " : " << m_name << std::endl;
99-
terminate_with_dump();
100-
}
101-
102-
if (LifetimeSanitizer_has_been_disabled){
103-
return;
104-
}
105-
106-
ReadSpinLock lg(sanitizer_lock);
107-
if (SANITIZER_FILTER.contains(m_name)){
108-
std::cout << "LifetimeSanitizer - Using: " << this << " : " << m_name << std::endl;
109-
}
110-
auto iter = sanitizer_map.find(this);
111-
if (iter != sanitizer_map.end()){
112-
return;
113-
}
114-
std::cerr << "Use non-existant: " << this << " : " << m_name << std::endl;
115-
terminate_with_dump();
116-
}
117-
118-
119-
PA_NO_INLINE void LifetimeSanitizer::terminate_with_dump(){
120-
// Intentionally crash the program here and let the crash dump deal with
121-
// the error reporting.
122-
123-
#ifdef _WIN32
124-
Sleep(500);
125-
#endif
126-
127-
std::cerr << (char*)nullptr << std::endl;
128-
}
129-
130-
131-
132-
133191
#endif
134192
}

Common/Cpp/LifetimeSanitizer.h

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,33 @@
88
#define PokemonAutomation_LifetimeSanitizer_H
99

1010
#include <stdint.h>
11-
//#include <string>
12-
#include <atomic>
13-
#include "Common/Compiler.h"
1411

1512
#define PA_SANITIZER_ENABLE
1613

1714
namespace PokemonAutomation{
1815

1916

2017
#ifdef PA_SANITIZER_ENABLE
21-
extern std::atomic<bool> LifetimeSanitizer_enabled;
2218

2319

2420
class LifetimeSanitizer{
2521
static constexpr uint64_t SANITIZER_TOKEN = 0x7db76f7a6a834ef0;
2622

2723

2824
public:
29-
// Default + Destruct
30-
31-
LifetimeSanitizer(const char* name = "(unnamed class)")
32-
: m_token(SANITIZER_TOKEN)
33-
, m_self(this)
34-
, m_name(name)
35-
{
36-
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
37-
return;
38-
}
39-
internal_construct();
40-
}
41-
~LifetimeSanitizer(){
42-
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
43-
m_self = nullptr;
44-
return;
45-
}
46-
internal_destruct();
47-
}
25+
// Rule of 5
4826

49-
50-
public:
51-
// Move
52-
53-
LifetimeSanitizer(LifetimeSanitizer&& x)
54-
: m_token(SANITIZER_TOKEN)
55-
, m_self(this)
56-
, m_name(x.m_name)
57-
{
58-
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
59-
return;
60-
}
61-
x.check_usage();
62-
internal_construct();
63-
}
64-
void operator=(LifetimeSanitizer&& x){
65-
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
66-
return;
67-
}
68-
check_usage();
69-
x.check_usage();
70-
}
27+
~LifetimeSanitizer();
28+
LifetimeSanitizer(LifetimeSanitizer&& x);
29+
void operator=(LifetimeSanitizer&& x);
30+
LifetimeSanitizer(const LifetimeSanitizer& x);
31+
void operator=(const LifetimeSanitizer& x);
7132

7233

7334
public:
74-
// Copy
75-
76-
LifetimeSanitizer(const LifetimeSanitizer& x)
77-
: m_token(SANITIZER_TOKEN)
78-
, m_self(this)
79-
, m_name(x.m_name)
80-
{
81-
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
82-
return;
83-
}
84-
x.check_usage();
85-
internal_construct();
86-
}
87-
void operator=(const LifetimeSanitizer& x){
88-
check_usage();
89-
x.check_usage();
90-
}
35+
// Constructor
36+
37+
LifetimeSanitizer(const char* name = "(unnamed class)");
9138

9239

9340
public:
@@ -108,12 +55,7 @@ class LifetimeSanitizer{
10855
const LifetimeSanitizer& m_parent;
10956
};
11057

111-
void check_usage() const{
112-
if (!LifetimeSanitizer_enabled.load(std::memory_order_relaxed)){
113-
return;
114-
}
115-
internal_check_usage();
116-
}
58+
void check_usage() const;
11759
CheckScope check_scope() const{
11860
return CheckScope(*this);
11961
}
@@ -124,9 +66,8 @@ class LifetimeSanitizer{
12466

12567

12668
private:
127-
PA_NO_INLINE void internal_construct();
128-
PA_NO_INLINE void internal_destruct();
129-
PA_NO_INLINE void internal_check_usage() const;
69+
void internal_construct();
70+
void internal_destruct();
13071

13172
private:
13273
uint64_t m_token;

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace PokemonAutomation{
2525
const bool IS_BETA_VERSION = true;
2626
const int PROGRAM_VERSION_MAJOR = 0;
2727
const int PROGRAM_VERSION_MINOR = 50;
28-
const int PROGRAM_VERSION_PATCH = 18;
28+
const int PROGRAM_VERSION_PATCH = 19;
2929

3030
const std::string PROGRAM_VERSION_BASE =
3131
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +

0 commit comments

Comments
 (0)