YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ynew.cpp
浏览该文件的文档.
1 /*
2  © 2009-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 #include "YSLib/Adaptor/YModules.h"
29 #include YFM_YSLib_Adaptor_Image
30 #include <cstdlib> // for std::atexit, std::malloc & std::free;
31 #include <cassert> // for assert;
32 #include <algorithm> // for std::for_each;
33 #include <functional> // for std::bind;
34 #if YF_Multithread == 1
35 # include <mutex>
36 #endif
37 
38 #ifdef YSL_USE_MEMORY_DEBUG
39 
40 namespace YSLib
41 {
42 
43 } // namespace YSLib;
44 
45 #if 0
46 using YSLib::GetDebugMemoryList;
47 
48 void*
49 operator new(std::size_t s, const char* f, int l) ythrow(std::bad_alloc)
50 {
51  void* p(::operator new(s));
52 
53  GetDebugMemoryList().Register(p, s, f, l);
54  return p;
55 }
56 void*
57 operator new[](std::size_t s, const char* f, int l) ythrow(std::bad_alloc)
58 {
59  void* p(::operator new[](s));
60 
61  GetDebugMemoryList().Register(p, s, f, l);
62  return p;
63 }
64 void*
65 operator new(std::size_t s, const std::nothrow_t&, const char* f, int l)
66  ynothrow
67 {
68  void* p(::operator new(s, std::nothrow));
69 
70  GetDebugMemoryList().Register(p, s, f, l);
71  return p;
72 }
73 void*
74 operator new[](std::size_t s, const std::nothrow_t&, const char* f, int l)
75  ynothrow
76 {
77  void* p(::operator new[](s, std::nothrow));
78 
79  GetDebugMemoryList().Register(p, s, f, l);
80  return p;
81 }
82 
83 void
84 operator delete(void* p, const char* f, int l) ynothrow
85 {
86  GetDebugMemoryList().Unregister(p, f, l);
87  ::operator delete(p);
88 }
89 void
90 operator delete[](void* p, const char* f, int l) ynothrow
91 {
92  GetDebugMemoryList().Unregister(p, f, l);
93  ::operator delete[](p);
94 }
95 void
96 operator delete(void* p, const std::nothrow_t&, const char* f, int l) ynothrow
97 {
98  GetDebugMemoryList().Unregister(p, f, l);
99  ::operator delete(p);
100 }
101 void
102 operator delete[](void* p, const std::nothrow_t&, const char* f, int l) ynothrow
103 {
104  GetDebugMemoryList().Unregister(p, f, l);
105  ::operator delete[](p);
106 }
107 #endif
108 
109 namespace YSLib
110 {
111 
112 namespace
113 {
114 
120 static MemoryList DebugMemoryList(nullptr);
121 
122 } // unnamed namespace;
123 
124 MemoryList&
125 GetDebugMemoryList()
126 {
127 #if YF_Multithread
128  static std::mutex mtx;
129  std::lock_guard<std::mutex> lck(mtx);
130 #endif
131 
132  return DebugMemoryList;
133 }
134 
135 
136 MemoryList::MemoryList(void(*p)())
137  : Blocks(), DuplicateDeletedBlocks()
138 {
139  if(p)
140  std::atexit(p);
141 }
142 
143 void
144 MemoryList::Register(const void* p, std::size_t s, const char* f, int l)
145 {
146  if(p)
147  Blocks.insert(std::make_pair(p, MemoryList::BlockInfo(s, f, l)));
148 }
149 
150 void
151 MemoryList::Unregister(const void* p, const char* f, int l)
152 {
153  if(p)
154  {
155  std::size_t n(Blocks.erase(p));
156 
157  if(n != 1)
158  DuplicateDeletedBlocks.push_back(std::make_pair(p,
159  MemoryList::BlockInfo(0, f, l)));
160  }
161 }
162 
163 void
164 MemoryList::Print(const MapType::value_type& val, std::FILE* stream)
165 {
166  std::fprintf(stream, "@%p, [%u] @ %s: %d;\n", val.first,
167  val.second.size, val.second.file.c_str(), val.second.line);
168 }
169 
170 void
171 MemoryList::PrintAll(std::FILE* stream)
172 {
173  std::for_each(Blocks.begin(), Blocks.end(),
174  std::bind(MemoryList::Print, std::placeholders::_1, stream));
175 }
176 
177 void
178 MemoryList::PrintAllDuplicate(std::FILE* stream)
179 {
180  std::for_each(DuplicateDeletedBlocks.begin(), DuplicateDeletedBlocks.end(),
181  std::bind(MemoryList::Print, std::placeholders::_1, stream));
182 }
183 
184 } // namespace YSLib;
185 
186 #endif
187 
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
#define ythrow(...)
YSLib 动态异常规范:根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
_tWidget _fCallable && f
Definition: ywgtevt.h:597