1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* The ManaPlus Client
* Copyright (C) 2013-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 "utils/sdlcheckutils.h"
#ifdef DEBUG_SDL_SURFACES
#include "logger.h"
#include "utils/sdlmemoryobject.h"
#include "utils/stringutils.h"
#include <map>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "debug.h"
#define DEBUG_SURFACE_ALLOCATION 1
std::map<void*, SDLMemoryObject*> mSurfaces;
static SDL_Surface *addSurface(const char *restrict const name,
SDL_Surface *restrict const surface,
const char *restrict const file,
const unsigned line)
{
#ifdef DEBUG_SURFACE_ALLOCATION
logger->log("add surface: %s %s:%u %p", name,
file, line, static_cast<void*>(surface));
#endif // DEBUG_SURFACE_ALLOCATION
std::map<void*, SDLMemoryObject*>::iterator
it = mSurfaces.find(surface);
if (it != mSurfaces.end())
{
SDLMemoryObject *const obj = (*it).second;
if (obj)
{ // found some time ago created surface
#ifdef DEBUG_SURFACE_ALLOCATION
logger->log("adding existing surface: %p, count:%d\n"
"was add %s\nwas deleted %s", surface, obj->mCnt,
obj->mAddFile.c_str(), obj->mRemoveFile.c_str());
#endif // DEBUG_SURFACE_ALLOCATION
obj->mCnt ++;
}
}
else
{ // creating surface object
mSurfaces[surface] = new SDLMemoryObject(name, file, line);
}
return surface;
}
static void deleteSurface(const char *restrict const name A_UNUSED,
SDL_Surface *restrict const surface,
const char *restrict const file,
const unsigned line)
{
#ifdef DEBUG_SURFACE_ALLOCATION
logger->log("delete surface: %s %s:%u %p", name, file, line, surface);
#endif // DEBUG_SURFACE_ALLOCATION
std::map<void*, SDLMemoryObject*>::iterator
it = mSurfaces.find(surface);
if (it == mSurfaces.end())
{
logger->log("bad surface delete: %p at %s:%d",
static_cast<void*>(surface), file, line);
}
else
{
SDLMemoryObject *const obj = (*it).second;
if (obj)
{
const int cnt = obj->mCnt;
#ifdef DEBUG_SURFACE_ALLOCATION
logger->log("debug deleting surface: %p, count:%d\n"
"was add %s\nwas deleted %s", surface, cnt,
obj->mAddFile.c_str(), obj->mRemoveFile.c_str());
#endif // DEBUG_SURFACE_ALLOCATION
if (cnt < 1)
{ // surface was here but was deleted
logger->log("deleting already deleted surface: %p at %s:%d\n"
"was add %s\nwas deleted %s", static_cast<void*>(surface),
file, line, obj->mAddFile.c_str(),
obj->mRemoveFile.c_str());
}
else if (cnt == 1)
{
mSurfaces.erase(surface);
delete obj;
}
else
{
obj->mCnt --;
obj->mRemoveFile = strprintf("%s:%u", file, line);
}
}
}
}
SDL_Surface *FakeIMG_LoadPNG_RW(SDL_RWops *const src, const char *const file,
const unsigned line)
{
return addSurface("IMG_LoadPNG_RW", IMG_LoadPNG_RW(src), file, line);
}
SDL_Surface *FakeIMG_LoadJPG_RW(SDL_RWops *const src, const char *const file,
const unsigned line)
{
return addSurface("IMG_LoadJPG_RW", IMG_LoadJPG_RW(src), file, line);
}
SDL_Surface *FakeIMG_Load(const char *name, const char *const file,
const unsigned line)
{
return addSurface("IMG_Load", IMG_Load(name), file, line);
}
void FakeSDL_FreeSurface(SDL_Surface *const surface, const char *const file,
const unsigned line)
{
deleteSurface("SDL_FreeSurface", surface, file, line);
SDL_FreeSurface(surface);
}
SDL_Surface *FakeSDL_CreateRGBSurface(const uint32_t flags,
const int width, const int height,
const int depth,
const uint32_t rMask,
const uint32_t gMask,
const uint32_t bMask,
const uint32_t aMask,
const char *const file,
const unsigned line)
{
return addSurface("SDL_CreateRGBSurface", SDL_CreateRGBSurface(flags,
width, height, depth, rMask, gMask, bMask, aMask), file, line);
}
SDL_Surface *FakeSDL_ConvertSurface(SDL_Surface *const src,
SDL_PixelFormat *const fmt,
const uint32_t flags,
const char *const file,
const unsigned line)
{
return addSurface("SDL_ConvertSurface", SDL_ConvertSurface(
src, fmt, flags), file, line);
}
SDL_Surface *FakeTTF_RenderUTF8_Blended(_TTF_Font *const font,
const char *restrict const text,
const SDL_Color &fg,
const char *restrict const file,
const unsigned line)
{
return addSurface("TTF_RenderUTF8_Blended", TTF_RenderUTF8_Blended(
font, text, fg), file, line);
}
SDL_Surface *FakeSDL_DisplayFormat(SDL_Surface *const surface,
const char *const file,
const unsigned line)
{
return addSurface("SDL_DisplayFormat",
SDL_DisplayFormat(surface), file, line);
}
SDL_Surface *FakeSDL_DisplayFormatAlpha(SDL_Surface *const surface,
const char *const file,
const unsigned line)
{
return addSurface("SDL_DisplayFormatAlpha",
SDL_DisplayFormatAlpha(surface), file, line);
}
#endif // DEBUG_SDL_SURFACES
|