GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/memorymanager.cpp Lines: 2 21 9.5 %
Date: 2021-03-17 Branches: 0 14 0.0 %

Line Branch Exec Source
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
22
#include "resources/memorymanager.h"
23
24
#include "gui/widgets/tabs/chat/chattab.h"
25
26
#include "resources/resourcemanager/resourcemanager.h"
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
38
1
MemoryManager memoryManager;
39
40
MemoryManager::MemoryManager()
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
89
    ResourceManager::calcMemory(0);
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),
97
            ChatMsgType::BY_SERVER,
98
            IgnoreRecord_false,
99
            TryRemoveColors_true);
100
    }
101
#endif  // DYECMD
102
2
}