YSTest: YNew.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YNew.h
浏览该文件的文档.
1 /*
2 © 2010-2015, 2017-2020 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 #ifndef YSL_INC_Adaptor_YNew_h_
29 #define YSL_INC_Adaptor_YNew_h_ 1
30
31 #include "YModules.h"
32
33 //包含编译配置。
34 #include YFM_YSLib_Adaptor_Configuration
35
36 //包含 YBase 基础定义。
37 #include <ydef.h>
38
39 //引入 YSLib 基本宏。
40 #include YFM_YBaseMacro
41
42 #ifdef YSL_USE_MEMORY_DEBUG
43
44 #include <new> // for std::nothrow_t;
45 #include <string>
46 #include <list>
47 #include <map>
48 #include <cstdio>
49 #if defined(__GLIBCXX__)
50 # include <ext/malloc_allocator.h> // for libstdc++ malloc allocator;
51 #endif
52 #include <ystdex/base.h> // for ystdex::noncopyable;
53
54 #if false
55
56 /* \defgroup YSLMemoryDebugFunctions YSLib Memory Debug Functions
57 \brief 调试用重载 ::operator new 和 ::operator delete 。
58 \since build 173
59 */
60 YF_API YB_ALLOCATOR YB_ATTR_returns_nonnull void*
61 operator new(std::size_t, const char*, int) ythrow(std::bad_alloc);
62 YF_API YB_ALLOCATOR YB_ATTR_returns_nonnull void*
63 operator new[](std::size_t, const char*, int) ythrow(std::bad_alloc);
64 YF_API YB_ALLOCATOR void*
65 operator new(std::size_t, const std::nothrow_t&, const char*, int) ynothrow;
66 YF_API YB_ALLOCATOR void*
67 operator new[](std::size_t, const std::nothrow_t&, const char*, int) ynothrow;
68 YF_API void
69 operator delete(void*, const char*, int) ynothrow;
70 YF_API void
71 operator delete[](void*, const char*, int) ynothrow;
72 YF_API void
73 operator delete(void*, const std::nothrow_t&, const char*, int) ynothrow;
74 YF_API void
75 operator delete[](void*, const std::nothrow_t&, const char*, int) ynothrow;
77 #endif
78
79
80 namespace YSLib
81 {
82
83 class MemoryList;
84
89 YF_API MemoryList&
90 GetDebugMemoryList();
91
92
94 template<typename _type>
95 using MemoryListAllocator
96 #if defined(__GLIBCXX__)
97 = __gnu_cxx::malloc_allocator<_type>;
98 #else
99 = std::allocator<_type>;
100 #endif
101
102
108 class YF_API MemoryList : private ystdex::noncopyable
109 {
110 public:
111 struct BlockInfo final
112 {
113 public:
114 std::size_t size;
115 std::string file;
116 int line;
117
118 explicit
119 BlockInfo(std::size_t s, const char* f, int l)
120 : size(s), file(f), line(l)
121 {}
122 };
123
125 class NewRecorder final : private ystdex::noncopyable
126 {
127 private:
128 MemoryList& blocks;
129 const char* file;
130 const int line;
131
132 public:
133 explicit yconstfn
134 NewRecorder(const char* f, int l, MemoryList& b = GetDebugMemoryList())
135 : blocks(b), file(f), line(l)
136 {}
137
138 public:
145 template<typename _type>
146 _type*
147 operator->*(_type* p)
148 {
149 blocks.Register(p, sizeof(_type), file, line);
150 return p;
151 }
152 };
153
154 using MapType = std::map<const void*, BlockInfo, std::less<const void*>,
155 MemoryListAllocator<std::pair<const void* const, BlockInfo>>>;
156 using ListType = std::list<std::pair<const void*, BlockInfo>,
157 MemoryListAllocator<std::pair<const void*, BlockInfo>>>;
158
159 MapType Blocks;
160 ListType DuplicateDeletedBlocks;
161
162 explicit
163 MemoryList(void(*)());
164
165 DefGetter(const ynothrow, MapType::size_type, Size, Blocks.size())
166
167 void
168 Register(const void*, std::size_t, const char*, int);
169
170 void
171 Unregister(const void*, const char*, int);
172
174 static void
175 Print(const MapType::value_type&, std::FILE*);
176
177 void
178 PrintAll(std::FILE*);
179
180 void
181 PrintAllDuplicate(std::FILE*);
182 };
183
184 } // namespace YSLib;
185
192 # define ynew YSLib::MemoryList::NewRecorder(__FILE__, __LINE__)->*new
193 # define ynew_nothrow new(std::nothrow, __FILE__, __LINE__)
194 # define ydelete(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
195 __LINE__), delete p)
196 # define ydelete_array(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
197 __LINE__), \
198 delete[] p)
199
200 #else
201
202 # define ynew new
203 # define ynew_nothrow new(std::nothrow)
204 # define ydelete delete
205 # define ydelete_array(p) (delete[] p)
206
207 #endif
208
209 #endif
210
size
size_t size
Definition: memory_resource.cpp:295
base.h
基类实用设施。
platform::size_t
YB_ATTR_nodiscard YF_API size_t
Definition: FileIO.h:1345
YSLib::IO::int
YF_API int
Definition: File.h:51
YF_API
#define YF_API
Definition: Platform.h:56
ythrow
#define ythrow(...)
YSLib 动态异常规范：根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:1256
CHRLib::f
f(d, s)
YB_ATTR_returns_nonnull
#define YB_ATTR_returns_nonnull
指示返回非空属性。
Definition: ydef.h:761
YB_ALLOCATOR
#define YB_ALLOCATOR
指示返回指针的函数或函数模板若为非空值则对应的存储不和其它对象指针共享别名。
Definition: ydef.h:785
ydef.h
语言实现和系统环境相关特性及公用类型和宏的基础定义。
std
Definition: optional.h:982
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YModules.h
YSLib 用户界面模块配置文件。
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
platform::p
return p p
Definition: YCLib/FileSystem.h:209
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
ystdex::cpp2017::string
basic_string< char > string
Definition: string.hpp:772
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::bases::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:50
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
platform::l
return l
Definition: YCLib/FileSystem.h:402