YSTest: YNew.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

YNew.h
浏览该文件的文档.
1 /*
2 © 2010-2015, 2017 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 void*
61 operator new(std::size_t, const char*, int) ythrow(std::bad_alloc);
62 YF_API YB_ALLOCATOR 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
124 /*
125 \brief new 表达式分配记录器。
126 */
127 class NewRecorder final : private ystdex::noncopyable
128 {
129 private:
130 MemoryList& blocks;
131 const char* file;
132 const int line;
133
134 public:
135 explicit yconstfn
136 NewRecorder(const char* f, int l, MemoryList& b = GetDebugMemoryList())
137 : blocks(b), file(f), line(l)
138 {}
139
140 public:
147 template<typename _type>
148 _type*
149 operator->*(_type* p)
150 {
151 blocks.Register(p, sizeof(_type), file, line);
152 return p;
153 }
154 };
155
156 using MapType = std::map<const void*, BlockInfo, std::less<const void*>,
157 MemoryListAllocator<std::pair<const void* const, BlockInfo>>>;
158 using ListType = std::list<std::pair<const void*, BlockInfo>,
159 MemoryListAllocator<std::pair<const void*, BlockInfo>>>;
160
161 MapType Blocks;
162 ListType DuplicateDeletedBlocks;
163
164 explicit
165 MemoryList(void(*)());
166
167 DefGetter(const ynothrow, MapType::size_type, Size, Blocks.size())
168
169 void
170 Register(const void*, std::size_t, const char*, int);
171
172 void
173 Unregister(const void*, const char*, int);
174
176 static void
177 Print(const MapType::value_type&, std::FILE*);
178
179 void
180 PrintAll(std::FILE*);
181
182 void
183 PrintAllDuplicate(std::FILE*);
184 };
185
186 } // namespace YSLib;
187
188 /*
189 \def ynew
190 \def ynew_nothrow
191 \def ydelete
192 \def ydelete_array
193 */
194 # define ynew YSLib::MemoryList::NewRecorder(__FILE__, __LINE__)->*new
195 # define ynew_nothrow new(std::nothrow, __FILE__, __LINE__)
196 # define ydelete(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
197 __LINE__), delete p)
198 # define ydelete_array(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
199 __LINE__), \
200 delete[] p)
201
202 #else
203
204 # define ynew new
205 # define ynew_nothrow new(std::nothrow)
206 # define ydelete delete
207 # define ydelete_array(p) (delete[] p)
208
209 #endif
210
211 #endif
212
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
ydef.h
系统环境和公用类型和宏的基础定义。
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
CHRLib::f
f(d, s)
platform::containers::string
basic_string< char > string
Definition: Container.h:101
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
YModules.h
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
base.h
基类实用设施。
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
ythrow
#define ythrow(...)
YSLib 动态异常规范：根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:817
YB_ALLOCATOR
#define YB_ALLOCATOR
指示修饰的是分配器，或返回分配器调用的函数或函数模板。
Definition: ydef.h:432
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:41
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
YSLib::IO::int
YF_API int
Definition: File.h:51
YSLib::UI::EventMapping::MapType
map< VisualEvent, MappedType > MapType
映射表类型。
Definition: YWidgetEvent.h:485
CHRLib::p
const auto p(d)