File: pgsAlloc.cpp

package info (click to toggle)
pgadmin3 1.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 69,368 kB
  • sloc: cpp: 163,662; sh: 4,339; ansic: 1,636; pascal: 1,120; yacc: 927; makefile: 733; lex: 421; perl: 40
file content (123 lines) | stat: -rw-r--r-- 2,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
//
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////


#include "pgAdmin3.h"

#if defined(PGSDEBUG)

#include "pgscript/utilities/pgsAlloc.h"
#include <wx/string.h>
#include <wx/log.h>

#undef new
#undef delete

pgsAlloc::pgsAlloc()
{

}

void pgsAlloc::add_malloc(const pgsMallocInfo &malloc_info)
{
	m_malloc_info[malloc_info.ptr] = malloc_info;
}

void pgsAlloc::rm_malloc(const void *ptr)
{
	if (m_malloc_info.size() != 0)
	{
		pgsMallocInfoMap::iterator it = m_malloc_info.find(ptr);
		if (it != m_malloc_info.end())
		{
			m_malloc_info.erase(it);
		}
	}
}

void pgsAlloc::dump()
{
	pgsMallocInfoMap::const_iterator it = m_malloc_info.begin();
	for (it = m_malloc_info.begin(); it != m_malloc_info.end(); it++)
	{
		const pgsMallocInfo &info = it->second;

		wxLogError(wxString() << info.filename << wxT(":")
		           << info.line_nb << wxT(" - ") << (int) info.ptr
		           << wxT(" of size ") << info.size);
	}
}

void *pgsAlloc::pmalloc(size_t size, const char *filename, size_t line_nb)
{
	// Nothing to do if there is nothing to allocate
	if (size == 0) return 0;

	void *ptr = 0;
	ptr = malloc(size);

	// Add allocation in the allocation map
	pgsMallocInfo malloc_info;
	malloc_info.ptr = ptr;
	malloc_info.size = size;
	malloc_info.filename = wxString(filename, wxConvFile);
	malloc_info.line_nb = line_nb;
	this->add_malloc(malloc_info);

	return ptr;
}

void pgsAlloc::pfree(void *ptr)
{
	// Remove information from the map if this piece of data exists
	this->rm_malloc(ptr);

	// Delete the data
	free(ptr);
}

pgsAlloc &pgsAlloc::instance()
{
	static pgsAlloc x;
	return x;
}

void *operator new(size_t size) throw (std::bad_alloc)
{
	return malloc(size);
}

void *operator new[](size_t size) throw (std::bad_alloc)
{
	return malloc(size);
}

void *operator new(size_t size, const char *filename, size_t line_nb)
throw (std::bad_alloc)
{
	return pgsAlloc::instance().pmalloc(size, filename, line_nb);
}

void *operator new[](size_t size, const char *filename, size_t line_nb)
throw (std::bad_alloc)
{
	return pgsAlloc::instance().pmalloc(size, filename, line_nb);
}

void operator delete(void *ptr) throw()
{
	pgsAlloc::instance().pfree(ptr);
}

void operator delete[](void *ptr) throw()
{
	pgsAlloc::instance().pfree(ptr);
}

#endif