From 9ca479839b3a5fc7dbe75e94e15a0dd6da21bf16 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 21 Apr 2016 14:07:11 +0300 Subject: Add memorycounter interface. Impliment it in Image. --- src/CMakeLists.txt | 4 ++++ src/Makefile.am | 4 ++++ src/resources/image.cpp | 14 +++++++++++ src/resources/image.h | 2 ++ src/resources/memorycounter.cpp | 47 +++++++++++++++++++++++++++++++++++++ src/resources/memorycounter.h | 43 ++++++++++++++++++++++++++++++++++ src/resources/memorymanager.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ src/resources/memorymanager.h | 47 +++++++++++++++++++++++++++++++++++++ src/resources/resource.cpp | 9 +++++++ src/resources/resource.h | 7 +++++- 10 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 src/resources/memorycounter.cpp create mode 100644 src/resources/memorycounter.h create mode 100644 src/resources/memorymanager.cpp create mode 100644 src/resources/memorymanager.h 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); + 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 . + */ + +#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 . + */ + +#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 . + */ + +#include "memorymanager.h" + +#include + +#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 . + */ + +#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 #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; } -- cgit v1.2.3-60-g2f50