aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2017-04-30 16:16:41 -0400
committerClyne Sullivan <tullivan99@gmail.com>2017-04-30 16:16:41 -0400
commit51629a66ff14a9025d9bd9ea69831e3d824a0760 (patch)
tree6e749c251d7b41d9bb3bf5362ccfe6aa18527f0e /main.cpp
parent40d164ea3d8437cd5a06a06d5b89bd938d3dd906 (diff)
mem track+, drop func. added
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp36
1 files changed, 30 insertions, 6 deletions
diff --git a/main.cpp b/main.cpp
index b6e5e24..df209c6 100644
--- a/main.cpp
+++ b/main.cpp
@@ -35,6 +35,15 @@ std::string xmlFolder = "./xml/";
*/
vec2 offset;
+std::size_t getUsedMem(void);
+void deleteRemaining(void);
+extern int balance;
+
+void print(void) {
+ std::cout << "Used memory: " << getUsedMem() << " bytes\n";
+ std::cout << "New/delete balance: " << balance << '\n';
+}
+
/**
* The main program.
* Init, load, run. Die.
@@ -44,6 +53,10 @@ int main(int argc, char *argv[])
static bool worldReset = false, worldDontReallyRun = false;
std::string worldActuallyUseThisXMLFile;
+ print();
+ atexit(print);
+ //atexit(deleteRemaining);
+
// handle command line args
if (argc > 1) {
for (int i = 1; i < argc; i++) {
@@ -191,7 +204,7 @@ int main(int argc, char *argv[])
WorldSystem::die();
WindowSystem::die();
- return 0; // Calls everything passed to atexit
+ return 0; // Calls everything passed to atexit
}
constexpr int memEntries = 2048;
@@ -210,11 +223,24 @@ std::size_t getUsedMem(void)
return total;
}
+void deleteRemaining(void)
+{
+ for (int i = 0; i < memEntries; i++) {
+ if (mems[i] != nullptr) {
+ balance--;
+ std::free(mems[i]);
+ mems[i] = nullptr;
+ sizs[i] = 0;
+ }
+ }
+
+}
+
void *operator new(std::size_t n) throw (std::bad_alloc)
{
- auto buf = std::malloc(n);
balance++;
+ auto buf = std::malloc(n);
if (buf == nullptr)
throw std::bad_alloc();
@@ -232,11 +258,10 @@ void *operator new(std::size_t n) throw (std::bad_alloc)
void operator delete(void* p) throw ()
{
if (p != nullptr) {
- std::free(p);
balance--;
-
for (int i = 0; i < memEntries; i++) {
if (mems[i] == p) {
+ std::free(p);
mems[i] = nullptr;
sizs[i] = 0;
break;
@@ -250,11 +275,10 @@ void operator delete(void* p, std::size_t n) throw ()
(void)n;
if (p != nullptr) {
- std::free(p);
balance--;
-
for (int i = 0; i < memEntries; i++) {
if (mems[i] == p) {
+ std::free(p);
mems[i] = nullptr;
sizs[i] = 0;
break;