YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ynew.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2009 - 2013.
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 //包含编译配置。
32 #include "config.h"
33 
34 //包含 YBase 基础定义。
35 #include <ydef.h>
36 
37 //引入 YSLib 命名空间宏。
38 #include "../Core/ybasemac.h"
39 
40 #ifdef YSL_USE_MEMORY_DEBUG
41 
42 #include <new> // for std::nothrow_t;
43 #include <string>
44 #include <list>
45 #include <map>
46 #include <cstdio>
47 #include <ext/malloc_allocator.h> // for libstdc++ malloc allocator;
48 #include <ystdex/utility.hpp> // for ystdex::noncopyable;
49 
50 
51 #if 0
52 
53 /* \defgroup YSLMemoryDebugFunctions YSLib Memory Debug Functions
54 \brief 调试用重载 ::operator new 和 ::operator delete 。
55 \since build 173
56 */
57 YF_API YB_ALLOCATOR void*
58 operator new(std::size_t, const char*, int) ythrow(std::bad_alloc);
59 YF_API YB_ALLOCATOR void*
60 operator new[](std::size_t, const char*, int) ythrow(std::bad_alloc);
61 YF_API YB_ALLOCATOR void*
62 operator new(std::size_t, const std::nothrow_t&, const char*, int) ynothrow;
63 YF_API YB_ALLOCATOR void*
64 operator new[](std::size_t, const std::nothrow_t&, const char*, int) ynothrow;
65 YF_API void
66 operator delete(void*, const char*, int) ynothrow;
67 YF_API void
68 operator delete[](void*, const char*, int) ynothrow;
69 YF_API void
70 operator delete(void*, const std::nothrow_t&, const char*, int) ynothrow;
71 YF_API void
72 operator delete[](void*, const std::nothrow_t&, const char*, int) ynothrow;
74 #endif
75 
76 
78 
79 class MemoryList;
80 
85 YF_API MemoryList&
87 
88 
95 {
96 public:
97  struct BlockInfo final
98  {
99  public:
100  std::size_t size;
102  int line;
103 
104  explicit
105  BlockInfo(std::size_t s, const char* f, int l)
106  : size(s), file(f), line(l)
107  {}
108  };
109 
110  /*
111  \brief new 表达式分配记录器。
112  */
113  class NewRecorder final : private ystdex::noncopyable
114  {
115  private:
117  const char* file;
118  const int line;
119 
120  public:
121  explicit yconstfn
122  NewRecorder(const char* f, int l, MemoryList& b = GetDebugMemoryList())
123  : blocks(b), file(f), line(l)
124  {}
125 
126  public:
133  template<typename _type>
134  _type*
135  operator->*(_type* p)
136  {
137  blocks.Register(p, sizeof(_type), file, line);
138  return p;
139  }
140  };
141 
142  typedef std::map<const void*, BlockInfo, std::less<const void*>,
143  __gnu_cxx::malloc_allocator<std::pair<const void* const, BlockInfo>> >
145  typedef std::list<std::pair<const void*, BlockInfo>,
146  __gnu_cxx::malloc_allocator<std::pair<const void*, BlockInfo>> >
148 
151 
152  explicit
153  MemoryList(void(*)());
154 
155  DefGetter(const ynothrow, MapType::size_type, Size, Blocks.size())
156 
157  void
158  Register(const void*, std::size_t, const char*, int);
159 
160  void
161  Unregister(const void*, const char*, int);
162 
164  static void
165  Print(const MapType::value_type&, std::FILE*);
166 
167  void
168  PrintAll(std::FILE*);
169 
170  void
171  PrintAllDuplicate(std::FILE*);
172 };
173 
174 YSL_END
175 
176 /*
177 \def ynew
178 \def ynew_nothrow
179 \def ydelete
180 \def ydelete_array
181 \bug 调试内存列表非线程安全。
182 */
183 # define ynew YSLib::MemoryList::NewRecorder(__FILE__, __LINE__)->*new
184 # define ynew_nothrow new(std::nothrow, __FILE__, __LINE__)
185 # define ydelete(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
186  __LINE__), delete p)
187 # define ydelete_array(p) (GetDebugMemoryList().Unregister(p, __FILE__, \
188  __LINE__), \
189  delete[] p)
190 
191 #else
192 
193 # define ynew new
194 # define ynew_nothrow new(std::nothrow)
195 # define ydelete delete
196 # define ydelete_array(p) (delete[] p)
197 
198 #endif
199 
200 #endif
201