YSTest: YNew.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YNew.cpp
浏览该文件的文档.
1 /*
2 © 2009-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YSLib/Adaptor/YModules.h"
29 #include YFM_YSLib_Adaptor_YAdaptor
30 #include <cstdlib> // for std::atexit;
31 #include <cassert> // for assert;
32 #include <algorithm> // for std::for_each;
33 #include <ystdex/functional.hpp> // for ystdex::bind1, ystdex::pvoid;
34
35 #ifdef YSL_USE_MEMORY_DEBUG
36
37 namespace YSLib
38 {
39
40 } // namespace YSLib;
41
42 #if 0
43 using YSLib::GetDebugMemoryList;
44
45 void*
46 operator new(std::size_t s, const char* f, int l) ythrow(std::bad_alloc)
47 {
48 void* p(::operator new(s));
49
50 GetDebugMemoryList().Register(p, s, f, l);
51 return p;
52 }
53 void*
54 operator new[](std::size_t s, const char* f, int l) ythrow(std::bad_alloc)
55 {
56 void* p(::operator new[](s));
57
58 GetDebugMemoryList().Register(p, s, f, l);
59 return p;
60 }
61 void*
62 operator new(std::size_t s, const std::nothrow_t&, const char* f, int l)
63 ynothrow
64 {
65 void* p(::operator new(s, std::nothrow));
66
67 GetDebugMemoryList().Register(p, s, f, l);
68 return p;
69 }
70 void*
71 operator new[](std::size_t s, const std::nothrow_t&, const char* f, int l)
72 ynothrow
73 {
74 void* p(::operator new[](s, std::nothrow));
75
76 GetDebugMemoryList().Register(p, s, f, l);
77 return p;
78 }
79
80 void
81 operator delete(void* p, const char* f, int l) ynothrow
82 {
83 GetDebugMemoryList().Unregister(p, f, l);
84 ::operator delete(p);
85 }
86 void
87 operator delete[](void* p, const char* f, int l) ynothrow
88 {
89 GetDebugMemoryList().Unregister(p, f, l);
90 ::operator delete[](p);
91 }
92 void
93 operator delete(void* p, const std::nothrow_t&, const char* f, int l) ynothrow
94 {
95 GetDebugMemoryList().Unregister(p, f, l);
96 ::operator delete(p);
97 }
98 void
99 operator delete[](void* p, const std::nothrow_t&, const char* f, int l) ynothrow
100 {
101 GetDebugMemoryList().Unregister(p, f, l);
102 ::operator delete[](p);
103 }
104 #endif
105
106 namespace YSLib
107 {
108
109 namespace
110 {
111
117 MemoryList DebugMemoryList({});
118
119 } // unnamed namespace;
120
121 MemoryList&
122 GetDebugMemoryList()
123 {
124 static recursive_mutex mtx;
125 lock_guard<recursive_mutex> lck(mtx);
126
127 return DebugMemoryList;
128 }
129
130
131 MemoryList::MemoryList(void(*p)())
132 : Blocks(), DuplicateDeletedBlocks()
133 {
134 if(p)
135 std::atexit(p);
136 }
137
138 void
139 MemoryList::Register(const void* p, std::size_t s, const char* f, int l)
140 {
141 if(p)
142 Blocks.emplace(p, MemoryList::BlockInfo(s, f, l));
143 }
144
145 void
146 MemoryList::Unregister(const void* p, const char* f, int l)
147 {
148 if(p)
149 {
150 std::size_t n(Blocks.erase(p));
151
152 if(n != 1)
153 DuplicateDeletedBlocks.emplace_back(p,
154 MemoryList::BlockInfo(0, f, l));
155 }
156 }
157
158 void
159 MemoryList::Print(const MapType::value_type& val, std::FILE* stream)
160 {
161 std::fprintf(stream, "@%p, [%zu] @ %s: %d;\n", ystdex::pvoid(val.first),
162 val.second.size, val.second.file.c_str(), val.second.line);
163 }
164
165 void
166 MemoryList::PrintAll(std::FILE* stream)
167 {
168 std::for_each(Blocks.cbegin(), Blocks.cend(),
169 ystdex::bind1(MemoryList::Print, stream));
170 }
171
172 void
173 MemoryList::PrintAllDuplicate(std::FILE* stream)
174 {
175 std::for_each(DuplicateDeletedBlocks.cbegin(),
176 DuplicateDeletedBlocks.cend(),
177 ystdex::bind1(MemoryList::Print, stream));
178 }
179
180 } // namespace YSLib;
181
182 #endif
183
ystdex::bind1
auto bind1(_func &&f, _tParams &&... args) -> decltype(std::bind(std::forward< decltype(f)>(f), std::placeholders::_1, std::forward< decltype(args)>(args)...))
复合调用 std::bind 和 std::placeholders::_1 。
Definition: functional.hpp:53
platform::size_t
YB_ATTR_nodiscard YF_API size_t
Definition: FileIO.h:1345
ythrow
#define ythrow(...)
YSLib 动态异常规范：根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:1256
CHRLib::f
f(d, s)
functional.hpp
函数和可调用对象。
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
ystdex::pvoid
enable_if_t< or_< is_object< _type >, is_void< _type > >::value, void * > pvoid(_type *p)
取 void* 类型的指针。
Definition: addressof.hpp:166
YModules.h
YSLib 适配器模块配置文件。
platform::p
return p p
Definition: YCLib/FileSystem.h:209
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
platform::l
return l
Definition: YCLib/FileSystem.h:402