ManaPlus
memorymanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2016-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
25 
27 
28 #include "utils/gettext.h"
29 #include "utils/stringutils.h"
30 
31 PRAGMA48(GCC diagnostic push)
32 PRAGMA48(GCC diagnostic ignored "-Wshadow")
33 #include <SDL_video.h>
34 PRAGMA48(GCC diagnostic pop)
35 
36 #include "debug.h"
37 
39 
41 {
42 }
43 
44 
45 int MemoryManager::getSurfaceSize(const SDL_Surface *const surface)
46 {
47  if (surface == nullptr)
48  return 0;
49  return CAST_S32(sizeof(SDL_Surface)) +
50  CAST_S32(sizeof(SDL_PixelFormat)) +
51  // aproximation for sizeof(SDL_BlitMap)
52  28 +
53  // pixels
54  surface->w * surface->h * 4 +
55  // private_hdata aproximation
56  10;
57 }
58 
59 void MemoryManager::printMemory(const std::string &name,
60  const int level,
61  const int localSum,
62  const int childsSum)
63 {
64  std::string str(level, ' ');
65  if (childsSum > 0)
66  {
67  logger->log("%s%s: %d = %d + %d",
68  str.c_str(),
69  name.c_str(),
70  localSum + childsSum,
71  localSum,
72  childsSum);
73  }
74  else
75  {
76  logger->log("%s%s: %d",
77  str.c_str(),
78  name.c_str(),
79  localSum);
80  }
81 }
82 
83 void MemoryManager::printAllMemory(ChatTab *const tab A_DYECMD_UNUSED)
84 {
85  if (logger == nullptr)
86  return;
87 
88 #ifdef DYECMD
90 #else // DYECMD
91 
92  if (tab != nullptr)
93  {
94  int sz = ResourceManager::calcMemory(0);
95  // TRANSLATORS: memory usage chat message
96  tab->chatLog(strprintf(_("Calculated memory usage: %d"), sz),
100  }
101 #endif // DYECMD
102 }
#define CAST_S32
Definition: cast.h:30
void log(const char *const log_text,...)
Definition: logger.cpp:269
static int getSurfaceSize(const SDL_Surface *const surface)
static void printAllMemory(ChatTab *const tab)
static void printMemory(const std::string &name, const int level, const int localSum, const int childsSum)
#define _(s)
Definition: gettext.h:35
const bool IgnoreRecord_false
Definition: ignorerecord.h:30
#define PRAGMA48(str)
Definition: localconsts.h:199
Logger * logger
Definition: logger.cpp:89
MemoryManager memoryManager
int calcMemory(const int level)
std::string strprintf(const char *const format,...)
const bool TryRemoveColors_true