GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/memorymanager.cpp Lines: 2 18 11.1 %
Date: 2017-11-29 Branches: 0 14 0.0 %

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