Avoid unnecessary memory allocation and redundant checks

This commit is contained in:
yunusey
2025-06-01 20:01:28 -04:00
parent 40b591ce34
commit 5f849b2060

View File

@ -1236,15 +1236,13 @@ pub fn prepBackgroundImage(self: *OpenGL, path: []const u8) !void {
return error.InvalidData; return error.InvalidData;
} }
}; };
defer self.alloc.free(decoded_image.data); errdefer self.alloc.free(decoded_image.data);
// Copy the data into the pending state // Copy the data into the pending state
const data = try self.alloc.dupe(u8, decoded_image.data);
errdefer self.alloc.free(data);
const pending: Image.Pending = .{ const pending: Image.Pending = .{
.width = decoded_image.width, .width = decoded_image.width,
.height = decoded_image.height, .height = decoded_image.height,
.data = data.ptr, .data = @constCast(decoded_image.data).ptr,
}; };
// Store the image // Store the image
@ -1253,7 +1251,6 @@ pub fn prepBackgroundImage(self: *OpenGL, path: []const u8) !void {
/// Reads the content of the given image path and returns it /// Reads the content of the given image path and returns it
pub fn readImageContent(self: *OpenGL, path: []const u8) ![]u8 { pub fn readImageContent(self: *OpenGL, path: []const u8) ![]u8 {
assert(std.fs.path.isAbsolute(path));
// Open the file // Open the file
var file = std.fs.openFileAbsolute(path, .{}) catch |err| { var file = std.fs.openFileAbsolute(path, .{}) catch |err| {
log.warn("failed to open file {s}: {}", .{ path, err }); log.warn("failed to open file {s}: {}", .{ path, err });