20 struct track_alloc : std::allocator<T> {
21 typedef typename std::allocator<T>::pointer pointer;
22 typedef typename std::allocator<T>::size_type size_type;
26 typedef track_alloc<U> other;
32 track_alloc(track_alloc<U>
const& u)
33 :
std::allocator<T>(u) {}
35 pointer allocate(size_type size,
36 std::allocator<void>::const_pointer = 0) {
37 void * p = std::malloc(size *
sizeof(T));
39 throw std::bad_alloc();
41 return static_cast<pointer
>(p);
44 void deallocate(pointer p, size_type) {
55 h=CreateMutex(0,FALSE,0);
58 ~MemDbgMutex () { CloseHandle(h); }
60 WaitForSingleObject(h, INFINITE);
71 typedef std::map< void*, Allocation, std::less<void*>,
72 track_alloc< std::pair<void* const, std::size_t> > > MapType;
77 void Store(
void *mem,
const Allocation& alloc) {
82 void Remove(
void *mem) {
89 dbgprintf(
"Allocations: %d\n", map.size());
91 for (
auto i = map.begin(); i != map.end(); ++i)
93 Allocation& a = i->second;
94 dbgprintf(
"Allocation: %d bytes: @ line %d in '%s'\n" , a.size, a.line, a.srcfile);
101 struct track_printer {
103 track_printer(AllocMap * track):track(track) {}
110 AllocMap * get_map() {
112 static AllocMap * track =
new (std::malloc(
sizeof *track)) AllocMap;
113 static track_printer printer(track);
117 void *
operator new(
size_t s,
const char* file,
int line) {
119 void * mem = std::malloc(s == 0 ? 1 : s);
121 throw std::bad_alloc();
123 Allocation alloc = { file, line ,s };
124 get_map()->Store (mem, alloc);
127 void *
operator new[](
size_t s,
const char* file,
int line) {
129 void * mem = std::malloc(s == 0 ? 1 : s);
131 throw std::bad_alloc();
133 Allocation alloc = { file, line ,s };
134 get_map()->Store(mem,alloc);
138 void operator delete(
void * mem) {
139 get_map()->Remove(mem);
142 void operator delete[](
void * mem) {
143 get_map()->Remove(mem);
void dbgprintf(const char *fmt,...)