mach: eliminate heap allocation of Core

Prior to this change `mach.Core.init` would heap allocate the structure, returning `*Core`:

```zig
app.core = try mach.Core.init(allocator, .{});
```

This was obviously not ideal, but wasn't possible to eliminate before due to how Core was
entangled with the platform abstraction. Now that it has been removed, we can reduce Core
initialization to take a `*Core` to initialize. In practice this means initialization looks
something like this:

```zig
try mach.Core.init(&app.core, alloctor, .{});
```

Or more simply:

```zig
try app.core.init(allocator, .{});
```

And we eliminate the `*Core` allocation entirely in most cases.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-01-17 00:05:16 -07:00 committed by Stephen Gutekanst
parent f54d584991
commit 8082228186
4 changed files with 16 additions and 27 deletions

View file

@ -25,15 +25,16 @@ pub fn App(
return struct {
engine: ecs.World(modules),
core: Core,
pub fn init(app: *@This()) !void {
try app.core.init(allocator, .{});
app.* = .{
.core = app.core,
.engine = try ecs.World(modules).init(allocator),
};
var core = try allocator.create(Core);
core.* = try Core.init(allocator, .{});
app.engine.set(.mach, .core, core);
app.engine.set(.mach, .device, core.device());
app.engine.set(.mach, .core, &app.core);
app.engine.set(.mach, .device, app.core.device());
try app_init(&app.engine);
}