YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ynew.cpp
浏览该文件的文档.
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 #include "YSLib/Adaptor/ynew.h"
29 #include <cstdlib> // for std::atexit, std::malloc & std::free;
30 #include <cassert> // for assert;
31 #include <algorithm> // for std::for_each;
32 #include <functional> // for std::bind;
33 
34 #ifdef YSL_USE_MEMORY_DEBUG
35 
37 
38 YSL_END
39 
40 /*
41 using YSLib::GetDebugMemoryList;
42 
43 void*
44 operator new(std::size_t s, const char* f, int l) ythrow(std::bad_alloc)
45 {
46  void* p(::operator new(s));
47 
48  GetDebugMemoryList().Register(p, s, f, l);
49  return p;
50 }
51 void*
52 operator new[](std::size_t s, const char* f, int l) ythrow(std::bad_alloc)
53 {
54  void* p(::operator new[](s));
55 
56  GetDebugMemoryList().Register(p, s, f, l);
57  return p;
58 }
59 void*
60 operator new(std::size_t s, const std::nothrow_t&, const char* f, int l)
61  ynothrow
62 {
63  void* p(::operator new(s, std::nothrow));
64 
65  GetDebugMemoryList().Register(p, s, f, l);
66  return p;
67 }
68 void*
69 operator new[](std::size_t s, const std::nothrow_t&, const char* f, int l)
70  ynothrow
71 {
72  void* p(::operator new[](s, std::nothrow));
73 
74  GetDebugMemoryList().Register(p, s, f, l);
75  return p;
76 }
77 
78 void
79 operator delete(void* p, const char* f, int l) ynothrow
80 {
81  GetDebugMemoryList().Unregister(p, f, l);
82  ::operator delete(p);
83 }
84 void
85 operator delete[](void* p, const char* f, int l) ynothrow
86 {
87  GetDebugMemoryList().Unregister(p, f, l);
88  ::operator delete[](p);
89 }
90 void
91 operator delete(void* p, const std::nothrow_t&, const char* f, int l) ynothrow
92 {
93  GetDebugMemoryList().Unregister(p, f, l);
94  ::operator delete(p);
95 }
96 void
97 operator delete[](void* p, const std::nothrow_t&, const char* f, int l) ynothrow
98 {
99  GetDebugMemoryList().Unregister(p, f, l);
100  ::operator delete[](p);
101 }
102 */
103 
104 YSL_BEGIN
105 
106 namespace
107 {
108 
115 static MemoryList DebugMemoryList(nullptr);
116 
117 } // unnamed namespace;
118 
119 MemoryList&
121 {
122  return DebugMemoryList;
123 }
124 
125 
126 MemoryList::MemoryList(void(*p)())
127  : Blocks(), DuplicateDeletedBlocks()
128 {
129  if(p)
130  std::atexit(p);
131 }
132 
133 void
134 MemoryList::Register(const void* p, std::size_t s, const char* f, int l)
135 {
136  if(p)
137  Blocks.insert(std::make_pair(p, MemoryList::BlockInfo(s, f, l)));
138 }
139 
140 void
141 MemoryList::Unregister(const void* p, const char* f, int l)
142 {
143  if(p)
144  {
145  std::size_t n(Blocks.erase(p));
146 
147  if(n != 1)
148  DuplicateDeletedBlocks.push_back(std::make_pair(p,
149  MemoryList::BlockInfo(0, f, l)));
150  }
151 }
152 
153 void
154 MemoryList::Print(const MapType::value_type& val, std::FILE* stream)
155 {
156  std::fprintf(stream, "@%p, [%u] @ %s: %d;\n", val.first,
157  val.second.size, val.second.file.c_str(), val.second.line);
158 }
159 
160 void
161 MemoryList::PrintAll(std::FILE* stream)
162 {
163  std::for_each(Blocks.begin(), Blocks.end(),
164  std::bind(MemoryList::Print, std::placeholders::_1, stream));
165 }
166 
167 void
168 MemoryList::PrintAllDuplicate(std::FILE* stream)
169 {
170  std::for_each(DuplicateDeletedBlocks.begin(), DuplicateDeletedBlocks.end(),
171  std::bind(MemoryList::Print, std::placeholders::_1, stream));
172 }
173 
174 YSL_END
175 
176 #endif
177