Skip to content

Commit e12e830

Browse files
committed
Disable SDL debugging options and make them toggleable in debug.zig
This should get rid of lots of memory being used on keeping track of SDL's internal allocations.
1 parent dcd7dd9 commit e12e830

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ VERSION ?= "0.0.0"
44
PREFIX ?= "./zig-out"
55

66
debug_local_platform:
7-
zig build --prefix $(PREFIX)
7+
zig build -Dcpu=baseline --prefix $(PREFIX)
88

99
release:
1010
@echo "Building OpenTitus $(VERSION)"
11-
zig build --release=small -Dtarget=x86_64-linux-gnu.2.27 -Dversion=$(VERSION) --prefix $(PREFIX)
12-
zig build --release=small -Dtarget=x86_64-windows -Dversion=$(VERSION) --prefix $(PREFIX)
11+
zig build --release=small -Dtarget=x86_64-linux-gnu.2.27 -Dcpu=baseline -Dversion=$(VERSION) --prefix $(PREFIX)
12+
zig build --release=small -Dtarget=x86_64-windows -Dcpu=baseline -Dversion=$(VERSION) --prefix $(PREFIX)
1313

1414
test:
1515
@echo "Running tests..."

src/SDL.zig

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ const TrackingHashMap = std.AutoHashMap(*Surface, void);
8282

8383
var tracking_map: TrackingHashMap = undefined;
8484

85-
const sdl_gpa_type = std.heap.GeneralPurposeAllocator(.{ .retain_metadata = true, .never_unmap = true, .stack_trace_frames = 10 });
85+
const debug = @import("_debug.zig");
86+
87+
const sdl_gpa_type = std.heap.GeneralPurposeAllocator(.{ .retain_metadata = true, .never_unmap = false, .stack_trace_frames = 10 });
8688
var sdl_gpa = sdl_gpa_type{};
8789
var sdl_allocator: std.mem.Allocator = undefined;
8890
var sdl_allocations: ?std.AutoHashMap(usize, usize) = null;
@@ -158,20 +160,29 @@ fn sdl_free(maybe_ptr: ?*anyopaque) callconv(.C) void {
158160
}
159161

160162
pub fn init(allocator: Allocator) bool {
161-
sdl_allocator = sdl_gpa.allocator();
162-
tracking_map = TrackingHashMap.init(allocator);
163-
sdl_allocations = std.AutoHashMap(usize, usize).init(allocator);
164-
_ = @This().SDL_SetMemoryFunctions(sdl_malloc, sdl_calloc, sdl_realloc, sdl_free);
163+
if(debug.track_sdl_surfaces) {
164+
tracking_map = TrackingHashMap.init(allocator);
165+
}
166+
if(debug.track_sdl_allocations) {
167+
sdl_allocator = sdl_gpa.allocator();
168+
sdl_allocations = std.AutoHashMap(usize, usize).init(allocator);
169+
_ = @This().SDL_SetMemoryFunctions(sdl_malloc, sdl_calloc, sdl_realloc, sdl_free);
170+
}
171+
165172
return @This().SDL_Init(@This().SDL_INIT_VIDEO | @This().SDL_INIT_EVENTS | @This().SDL_INIT_JOYSTICK | @This().SDL_INIT_GAMEPAD);
166173
}
167174

168175
pub fn deinit() void {
169-
tracking_map.deinit();
170176
@This().SDL_Quit();
171-
sdl_allocations.?.deinit();
172-
sdl_allocations = null;
173-
if (sdl_gpa.deinit() == .leak) {
174-
std.log.err("SDL memory leaked!", .{});
177+
if(debug.track_sdl_surfaces) {
178+
tracking_map.deinit();
179+
}
180+
if(debug.track_sdl_allocations) {
181+
sdl_allocations.?.deinit();
182+
sdl_allocations = null;
183+
if (sdl_gpa.deinit() == .leak) {
184+
std.log.err("SDL memory leaked!", .{});
185+
}
175186
}
176187
}
177188

@@ -188,22 +199,31 @@ pub fn getTicks() u64 {
188199
// Surfaces
189200

190201
pub fn destroySurface(surface: [*c]Surface) void {
191-
if (tracking_map.remove(surface)) {
202+
if(debug.track_sdl_surfaces) {
203+
if (tracking_map.remove(surface)) {
204+
@This().SDL_DestroySurface(surface);
205+
} else {
206+
std.log.err("destroySurface: DOUBLE FREE OF {*}", .{surface});
207+
}
208+
}
209+
else {
192210
@This().SDL_DestroySurface(surface);
193-
} else {
194-
std.log.err("destroySurface: DOUBLE FREE OF {*}", .{surface});
195211
}
196212
}
197213

198214
pub fn duplicateSurface(surface: [*c]Surface) !*Surface {
215+
199216
const result = @This().SDL_DuplicateSurface(surface);
200217
if (result == null) {
201218
return error.Failed;
202219
}
203-
if (tracking_map.remove(result)) {
204-
std.log.err("duplicateSurface: surface was already tracked! {*}", .{result});
220+
if(debug.track_sdl_surfaces) {
221+
if (tracking_map.remove(result)) {
222+
std.log.err("duplicateSurface: surface was already tracked! {*}", .{result});
223+
}
224+
tracking_map.put(result, {}) catch {};
205225
}
206-
tracking_map.put(result, {}) catch {};
226+
207227
return result;
208228
}
209229

@@ -212,19 +232,24 @@ pub fn convertSurface(src: *Surface, pixel_format: PixelFormat) !*Surface {
212232
if (result == null) {
213233
return error.Failed;
214234
}
215-
if (tracking_map.remove(result)) {
216-
std.log.err("convertSurface: surface was already tracked! {*}", .{result});
235+
if(debug.track_sdl_surfaces) {
236+
if (tracking_map.remove(result)) {
237+
std.log.err("convertSurface: surface was already tracked! {*}", .{result});
238+
}
239+
tracking_map.put(result, {}) catch {};
217240
}
218-
tracking_map.put(result, {}) catch {};
241+
219242
return result;
220243
}
221244

222245
pub fn createSurface(width: c_int, height: c_int, format: u32) [*c]Surface {
223246
const result = @This().SDL_CreateSurface(width, height, format);
224-
if (tracking_map.remove(result)) {
225-
std.log.err("createSurface: surface was already tracked! {*}", .{result});
247+
if(debug.track_sdl_surfaces) {
248+
if (tracking_map.remove(result)) {
249+
std.log.err("createSurface: surface was already tracked! {*}", .{result});
250+
}
251+
tracking_map.put(result, {}) catch {};
226252
}
227-
tracking_map.put(result, {}) catch {};
228253
return result;
229254
}
230255

@@ -234,10 +259,12 @@ pub const createSurfacePalette = @This().SDL_CreateSurfacePalette;
234259

235260
pub fn loadBMP_IO(src: ?*@This().SDL_IOStream, closeio: bool) [*c]@This().Surface {
236261
const result = @This().SDL_LoadBMP_IO(src, closeio);
237-
if (tracking_map.remove(result)) {
238-
std.log.err("loadBMP_IO: surface was already tracked! {*}", .{result});
262+
if(debug.track_sdl_surfaces) {
263+
if (tracking_map.remove(result)) {
264+
std.log.err("loadBMP_IO: surface was already tracked! {*}", .{result});
265+
}
266+
tracking_map.put(result, {}) catch {};
239267
}
240-
tracking_map.put(result, {}) catch {};
241268
return result;
242269
}
243270
pub const saveBMP = @This().SDL_SaveBMP;

src/_debug.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ pub const ladder_flag: bool = false;
66
pub const controller_osd: bool = false;
77
pub const dump_sprites: bool = false;
88
pub const enable_cheats: bool = false;
9+
10+
pub const track_sdl_surfaces: bool = false;
11+
pub const track_sdl_allocations: bool = false;

0 commit comments

Comments
 (0)