summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-04-21 14:07:11 +0300
committerAndrei Karas <akaras@inbox.ru>2016-04-22 00:00:06 +0300
commit9ca479839b3a5fc7dbe75e94e15a0dd6da21bf16 (patch)
tree4aa2d89a08f00cad31d417ea790a4aa5a3455cde /src
parent20727e946194e9bf984fc9b4dd7687a59fc82fc1 (diff)
downloadplus-9ca479839b3a5fc7dbe75e94e15a0dd6da21bf16.tar.gz
plus-9ca479839b3a5fc7dbe75e94e15a0dd6da21bf16.tar.bz2
plus-9ca479839b3a5fc7dbe75e94e15a0dd6da21bf16.tar.xz
plus-9ca479839b3a5fc7dbe75e94e15a0dd6da21bf16.zip
Add memorycounter interface. Impliment it in Image.
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt4
-rw-r--r--src/Makefile.am4
-rw-r--r--src/resources/image.cpp14
-rw-r--r--src/resources/image.h2
-rw-r--r--src/resources/memorycounter.cpp47
-rw-r--r--src/resources/memorycounter.h43
-rw-r--r--src/resources/memorymanager.cpp52
-rw-r--r--src/resources/memorymanager.h47
-rw-r--r--src/resources/resource.cpp9
-rw-r--r--src/resources/resource.h7
10 files changed, 228 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 45ef888a1..f2d91fd51 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -676,6 +676,10 @@ SET(SRCS
enums/resources/map/mapitemtype.h
resources/mapreader.cpp
resources/mapreader.h
+ resources/memorycounter.cpp
+ resources/memorycounter.h
+ resources/memorymanager.cpp
+ resources/memorymanager.h
resources/mobileopenglscreenshothelper.cpp
resources/mobileopenglscreenshothelper.h
resources/modinfo.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 0da76f1a0..a28ed7985 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -701,6 +701,10 @@ SRC += events/actionevent.h \
enums/input/inputaction.h \
enums/input/inputgroup.h \
input/inputactionmap.h \
+ resources/memorycounter.cpp \
+ resources/memorycounter.h \
+ resources/memorymanager.cpp \
+ resources/memorymanager.h \
winver.h
if ENABLE_PUGIXML
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index d820adcfb..ad5d0d91c 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -29,6 +29,7 @@
#ifdef USE_OPENGL
#include "resources/openglimagehelper.h"
#endif // USE_OPENGL
+#include "resources/memorymanager.h"
#include "resources/sdlimagehelper.h"
#include "resources/subimage.h"
@@ -473,6 +474,19 @@ void Image::SDLTerminateAlphaCache()
mUseAlphaCache = false;
}
+int Image::calcMemoryLocal()
+{
+ // +++ this calculation can be wrong for SDL2
+ int sz = sizeof(Image) +
+ sizeof(std::map<float, SDL_Surface*>);
+ if (mSDLSurface)
+ {
+ sz += CAST_S32(mAlphaCache.size()) *
+ memoryManager.getSurfaceSize(mSDLSurface);
+ }
+ return sz;
+}
+
#ifdef USE_OPENGL
void Image::decRef()
{
diff --git a/src/resources/image.h b/src/resources/image.h
index 55f28a055..7fbbcac21 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -189,6 +189,8 @@ class Image notfinal : public Resource
SDL_Surface* getSDLSurface()
{ return mSDLSurface; }
+ int calcMemoryLocal() override;
+
SDL_Rect mBounds;
float mAlpha;
diff --git a/src/resources/memorycounter.cpp b/src/resources/memorycounter.cpp
new file mode 100644
index 000000000..e2961556e
--- /dev/null
+++ b/src/resources/memorycounter.cpp
@@ -0,0 +1,47 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "resources/memorycounter.h"
+
+#include "resources/memorymanager.h"
+
+#include "debug.h"
+
+MemoryCounter::MemoryCounter()
+{
+}
+
+int MemoryCounter::calcMemoryLocal()
+{
+ return 0;
+}
+
+int MemoryCounter::calcMemory(const int level)
+{
+ const int sumLocal = calcMemoryLocal();
+ const int sumChilds = calcMemoryChilds(level);
+ memoryManager.printMemory(level, sumLocal, sumChilds);
+ return sumLocal + sumChilds;
+}
+
+int MemoryCounter::calcMemoryChilds(const int level A_UNUSED)
+{
+ return 0;
+}
diff --git a/src/resources/memorycounter.h b/src/resources/memorycounter.h
new file mode 100644
index 000000000..5f1d6cc23
--- /dev/null
+++ b/src/resources/memorycounter.h
@@ -0,0 +1,43 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef RESOURCES_MEMORYCOUNTER_H
+#define RESOURCES_MEMORYCOUNTER_H
+
+#include "localconsts.h"
+
+class MemoryCounter notfinal
+{
+ public:
+ MemoryCounter();
+
+ A_DELETE_COPY(MemoryCounter)
+
+ virtual ~MemoryCounter()
+ { }
+
+ int calcMemory(const int level);
+
+ virtual int calcMemoryLocal();
+
+ virtual int calcMemoryChilds(const int level);
+};
+
+#endif // RESOURCES_MEMORYCOUNTER_H
diff --git a/src/resources/memorymanager.cpp b/src/resources/memorymanager.cpp
new file mode 100644
index 000000000..546c19c45
--- /dev/null
+++ b/src/resources/memorymanager.cpp
@@ -0,0 +1,52 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "memorymanager.h"
+
+#include <SDL_video.h>
+
+#include "debug.h"
+
+MemoryManager memoryManager;
+
+MemoryManager::MemoryManager()
+{
+}
+
+
+int MemoryManager::getSurfaceSize(SDL_Surface *const surface)
+{
+ if (!surface)
+ return 0;
+ return sizeof(SDL_Surface) +
+ sizeof(SDL_PixelFormat) +
+ // aproximation for sizeof(SDL_BlitMap)
+ 28 +
+ // pixels
+ surface->w * surface->h * 4 +
+ // private_hdata aproximation
+ 10;
+}
+
+void MemoryManager::printMemory(const int level A_UNUSED,
+ const int localSum A_UNUSED,
+ const int childsSum A_UNUSED)
+{
+}
diff --git a/src/resources/memorymanager.h b/src/resources/memorymanager.h
new file mode 100644
index 000000000..8773d849a
--- /dev/null
+++ b/src/resources/memorymanager.h
@@ -0,0 +1,47 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MEMORYMANAGER_H
+#define MEMORYMANAGER_H
+
+#include "localconsts.h"
+
+struct SDL_Surface;
+
+class MemoryManager final
+{
+ public:
+ MemoryManager();
+
+ A_DELETE_COPY(MemoryManager)
+
+ ~MemoryManager()
+ { }
+
+ int getSurfaceSize(SDL_Surface *const surface);
+
+ void printMemory(const int level,
+ const int localSum,
+ const int childsSum);
+};
+
+extern MemoryManager memoryManager;
+
+#endif // MEMORYMANAGER_H
diff --git a/src/resources/resource.cpp b/src/resources/resource.cpp
index 96524724b..40d44a7ec 100644
--- a/src/resources/resource.cpp
+++ b/src/resources/resource.cpp
@@ -24,6 +24,8 @@
#include "logger.h"
+#include "resources/memorycounter.h"
+#include "resources/memorymanager.h"
#include "resources/resourcemanager.h"
#include "debug.h"
@@ -68,3 +70,10 @@ void Resource::decRef()
resourceManager->release(this);
}
}
+
+int Resource::calcMemoryLocal()
+{
+ return sizeof(Resource) +
+ CAST_S32(mIdPath.size()) +
+ CAST_S32(mSource.size());
+}
diff --git a/src/resources/resource.h b/src/resources/resource.h
index e237af526..c2d3ad865 100644
--- a/src/resources/resource.h
+++ b/src/resources/resource.h
@@ -23,6 +23,8 @@
#ifndef RESOURCES_RESOURCE_H
#define RESOURCES_RESOURCE_H
+#include "resources/memorycounter.h"
+
#include <string>
#include "localconsts.h"
@@ -30,7 +32,7 @@
/**
* A generic reference counted resource object.
*/
-class Resource notfinal
+class Resource notfinal : public MemoryCounter
{
friend class ResourceManager;
@@ -39,6 +41,7 @@ class Resource notfinal
* Constructor
*/
Resource() :
+ MemoryCounter(),
mIdPath(),
mSource(),
mTimeStamp(0),
@@ -96,6 +99,8 @@ class Resource notfinal
void setNotCount(const bool b)
{ mNotCount = b; }
+ int calcMemoryLocal() override;
+
#ifdef DEBUG_DUMP_LEAKS
bool getDumped() const A_WARN_UNUSED
{ return mDumped; }