Skip to content

Commit

Permalink
core: con't copy App and apprt.App
Browse files Browse the repository at this point in the history
Besides avoiding copying, this allows consumers to choose to allocate
these structs on the stack or to allocate on the heap. It also gives the
apprt.App a stable pointer sooner in the process.
  • Loading branch information
jcollie committed Feb 1, 2025
1 parent c5508e7 commit 9edf0eb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
17 changes: 5 additions & 12 deletions src/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,23 @@ pub const CreateError = Allocator.Error || font.SharedGridSet.InitError;
///
/// After calling this function, well behaved apprts should then call
/// `focusEvent` to set the initial focus state of the app.
pub fn create(
pub fn init(
self: *App,
alloc: Allocator,
) CreateError!*App {
var app = try alloc.create(App);
errdefer alloc.destroy(app);

) CreateError!void {
var font_grid_set = try font.SharedGridSet.init(alloc);
errdefer font_grid_set.deinit();

app.* = .{
self.* = .{
.alloc = alloc,
.surfaces = .{},
.mailbox = .{},
.font_grid_set = font_grid_set,
.config_conditional_state = .{},
};
errdefer app.surfaces.deinit(alloc);

return app;
}

pub fn destroy(self: *App) void {
pub fn deinit(self: *App) void {
// Clean up all our surfaces
for (self.surfaces.items) |surface| surface.deinit();
self.surfaces.deinit(self.alloc);
Expand All @@ -114,8 +109,6 @@ pub fn destroy(self: *App) void {
// should gracefully close all surfaces.
assert(self.font_grid_set.count() == 0);
self.font_grid_set.deinit();

self.alloc.destroy(self);
}

/// Tick ticks the app loop. This will drain our mailbox and process those
Expand Down
17 changes: 11 additions & 6 deletions src/apprt/embedded.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ pub const App = struct {
keymap_state: input.Keymap.State,

pub fn init(
self: *App,
core_app: *CoreApp,
config: *const Config,
opts: Options,
) !App {
) !void {
// We have to clone the config.
const alloc = core_app.alloc;
var config_clone = try config.clone(alloc);
Expand All @@ -108,7 +109,7 @@ pub const App = struct {
var keymap = try input.Keymap.init();
errdefer keymap.deinit();

return .{
self.* = .{
.core_app = core_app,
.config = config_clone,
.opts = opts,
Expand Down Expand Up @@ -1338,13 +1339,16 @@ pub const CAPI = struct {
opts: *const apprt.runtime.App.Options,
config: *const Config,
) !*App {
var core_app = try CoreApp.create(global.alloc);
errdefer core_app.destroy();
var core_app = try global.alloc.create(CoreApp);
errdefer {
core_app.deinit();
global.alloc.destroy(core_app);
}

// Create our runtime app
var app = try global.alloc.create(App);
errdefer global.alloc.destroy(app);
app.* = try App.init(core_app, config, opts.*);
try app.init(core_app, config, opts.*);
errdefer app.terminate();

return app;
Expand All @@ -1367,7 +1371,8 @@ pub const CAPI = struct {
const core_app = v.core_app;
v.terminate();
global.alloc.destroy(v);
core_app.destroy();
core_app.deinit();
global.alloc.destroy(core_app);
}

/// Update the focused state of the app.
Expand Down
4 changes: 2 additions & 2 deletions src/apprt/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub const App = struct {

pub const Options = struct {};

pub fn init(core_app: *CoreApp, _: Options) !App {
pub fn init(self: *App, core_app: *CoreApp, _: Options) !void {
if (comptime builtin.target.isDarwin()) {
log.warn("WARNING WARNING WARNING: GLFW ON MAC HAS BUGS.", .{});
log.warn("You should use the AppKit-based app instead. The official download", .{});
Expand Down Expand Up @@ -101,7 +101,7 @@ pub const App = struct {
// We want the event loop to wake up instantly so we can process our tick.
glfw.postEmptyEvent();

return .{
self.* = .{
.app = core_app,
.config = config,
.darwin = darwin,
Expand Down
4 changes: 2 additions & 2 deletions src/apprt/gtk/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ quit_timer: union(enum) {
expired: void,
} = .{ .off = {} },

pub fn init(core_app: *CoreApp, opts: Options) !App {
pub fn init(self: *App, core_app: *CoreApp, opts: Options) !void {
_ = opts;

// Log our GTK version
Expand Down Expand Up @@ -460,7 +460,7 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
c.GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 3,
);

return .{
self.* = .{
.core_app = core_app,
.app = app,
.config = config,
Expand Down
8 changes: 5 additions & 3 deletions src/main_ghostty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ pub fn main() !MainReturn {
}

// Create our app state
var app = try App.create(alloc);
defer app.destroy();
var app: App = undefined;
defer app.deinit();
try app.init(alloc);

// Create our runtime app
var app_runtime = try apprt.App.init(app, .{});
var app_runtime: apprt.App = undefined;
try app_runtime.init(&app, .{});
defer app_runtime.terminate();

// Since - by definition - there are no surfaces when first started, the
Expand Down

0 comments on commit 9edf0eb

Please sign in to comment.