YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ynew.h
浏览该文件的文档.
1 /*
2  © 2010-2014 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 YB_IMPL_GNUCPP
50 # include <ext/malloc_allocator.h> // for libstdc++ malloc allocator;
51 #endif
52 #include <ystdex/utility.hpp> // for ystdex::noncopyable;
53 
54 
55 #if 0
56 
57 /* \defgroup YSLMemoryDebugFunctions YSLib Memory Debug Functions
58 \brief 调试用重载 ::operator new 和 ::operator delete 。
59 \since build 173
60 */
61 YF_API YB_ALLOCATOR void*
62 operator new(std::size_t, const char*, int) ythrow(std::bad_alloc);
63 YF_API YB_ALLOCATOR void*
64 operator new[](std::size_t, const char*, int) ythrow(std::bad_alloc);
65 YF_API YB_ALLOCATOR void*
66 operator new(std::size_t, const std::nothrow_t&, const char*, int) ynothrow;
67 YF_API YB_ALLOCATOR void*
68 operator new[](std::size_t, const std::nothrow_t&, const char*, int) ynothrow;
69 YF_API void
70 operator delete(void*, const char*, int) ynothrow;
71 YF_API void
72 operator delete[](void*, const char*, int) ynothrow;
73 YF_API void
74 operator delete(void*, const std::nothrow_t&, const char*, int) ynothrow;
75 YF_API void
76 operator delete[](void*, const std::nothrow_t&, const char*, int) ynothrow;
78 #endif
79 
80 
81 namespace YSLib
82 {
83 
84 class MemoryList;
85 
90 YF_API MemoryList&
91 GetDebugMemoryList();
92 
93 
95 template<typename _type>
96 using MemoryListAllocator
97 #if YB_IMPL_GNUCPP
98  = __gnu_cxx::malloc_allocator<_type>;
99 #else
100  = std::allocator<_type>;
101 #endif
102 
103 
109 class YF_API MemoryList : private ystdex::noncopyable
110 {
111 public:
112  struct BlockInfo final
113  {
114  public:
115  std::size_t size;
116  std::string file;
117  int line;
118 
119  explicit
120  BlockInfo(std::size_t s, const char* f, int l)
121  : size(s), file(f), line(l)
122  {}
123  };
124 
125  /*
126  \brief new 表达式分配记录器。
127  */
128  class NewRecorder final : private ystdex::noncopyable
129  {
130  private:
131  MemoryList& blocks;
132  const char* file;
133  const int line;
134 
135  public:
136  explicit yconstfn
137  NewRecorder(const char* f, int l, MemoryList& b = GetDebugMemoryList())
138  : blocks(b), file(f), line(l)
139  {}
140 
141  public:
148  template<typename _type>
149  _type*
150  operator->*(_type* p)
151  {
152  blocks.Register(p, sizeof(_type), file, line);
153  return p;
154  }
155  };
156 
157  using MapType = std::map<const void*, BlockInfo, std::less<const void*>,
158  MemoryListAllocator<std::pair<const void* const, BlockInfo>>>;
159  using ListType = std::list<std::pair<const void*, BlockInfo>,
160  MemoryListAllocator<std::pair<const void*, BlockInfo>>>;
161 
162  MapType Blocks;
163  ListType DuplicateDeletedBlocks;
164 
165  explicit
166  MemoryList(void(*)());
167 
168  DefGetter(const ynothrow, MapType::size_type, Size, Blocks.size())
169 
170  void
171  Register(const void*, std::size_t, const char*, int);
172 
173  void
174  Unregister(const void*, const char*, int);
175 
177  static void
178  Print(const MapType::value_type&, std::FILE*);
179 
180  void
181  PrintAll(std::FILE*);
182 
183  void
184  PrintAllDuplicate(std::FILE*);
185 };
186 
187 } // namespace YSLib;
188 
189 /*
190 \def ynew
191 \def ynew_nothrow
192 \def ydelete
193 \def ydelete_array
194 */
195 # define ynew YSLib::MemoryList::NewRecorder(__FILE__, __LINE__)->*new
196 # define ynew_nothrow new(std::nothrow, __FILE__, __LINE__)
197 # define ydelete(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
198  __LINE__), delete p)
199 # define ydelete_array(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
200  __LINE__), \
201  delete[] p)
202 
203 #else
204 
205 # define ynew new
206 # define ynew_nothrow new(std::nothrow)
207 # define ydelete delete
208 # define ydelete_array(p) (delete[] p)
209 
210 #endif
211 
212 #endif
213 
实用设施。
#define YF_API
Definition: Platform.h:64
系统环境和公用类型和宏的基础定义。
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
map< VisualEvent, MappedType > MapType
映射表类型。
Definition: ywgtevt.h:426
不可复制对象:禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: utility.hpp:75
#define YB_ALLOCATOR
指示修饰的是分配器,或返回分配器调用的函数或函数模板。
Definition: ydef.h:281
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
#define ythrow(...)
YSLib 动态异常规范:根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
_tWidget _fCallable && f
Definition: ywgtevt.h:597