Update for allocgate

master
Austin Rude 3 years ago
parent 157a554e6d
commit a95c85f5c7

@ -24,7 +24,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type {
set: *SparseSet(Entity), set: *SparseSet(Entity),
instances: std.ArrayList(ComponentOrDummy), instances: std.ArrayList(ComponentOrDummy),
allocator: ?*std.mem.Allocator, allocator: ?std.mem.Allocator,
/// doesnt really belong here...used to denote group ownership /// doesnt really belong here...used to denote group ownership
super: usize = 0, super: usize = 0,
safeDeinit: fn (*Self) void, safeDeinit: fn (*Self) void,
@ -34,7 +34,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type {
update: Signal(Entity), update: Signal(Entity),
destruction: Signal(Entity), destruction: Signal(Entity),
pub fn init(allocator: *std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
var store = Self{ var store = Self{
.set = SparseSet(Entity).initPtr(allocator), .set = SparseSet(Entity).initPtr(allocator),
.instances = undefined, .instances = undefined,
@ -73,7 +73,7 @@ pub fn ComponentStorage(comptime Component: type, comptime Entity: type) type {
return store; return store;
} }
pub fn initPtr(allocator: *std.mem.Allocator) *Self { pub fn initPtr(allocator: std.mem.Allocator) *Self {
var store = allocator.create(Self) catch unreachable; var store = allocator.create(Self) catch unreachable;
store.set = SparseSet(Entity).initPtr(allocator); store.set = SparseSet(Entity).initPtr(allocator);
if (!is_empty_struct) { if (!is_empty_struct) {

@ -18,7 +18,7 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver
handles: []HandleType, handles: []HandleType,
append_cursor: IndexType = 0, append_cursor: IndexType = 0,
last_destroyed: ?IndexType = null, last_destroyed: ?IndexType = null,
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
const invalid_id = std.math.maxInt(IndexType); const invalid_id = std.math.maxInt(IndexType);
@ -43,11 +43,11 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver
} }
}; };
pub fn init(allocator: *std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
return initWithCapacity(allocator, 32); return initWithCapacity(allocator, 32);
} }
pub fn initWithCapacity(allocator: *std.mem.Allocator, capacity: usize) Self { pub fn initWithCapacity(allocator: std.mem.Allocator, capacity: usize) Self {
return Self{ return Self{
.handles = allocator.alloc(HandleType, capacity) catch unreachable, .handles = allocator.alloc(HandleType, capacity) catch unreachable,
.allocator = allocator, .allocator = allocator,

@ -35,7 +35,7 @@ pub const Registry = struct {
contexts: std.AutoHashMap(u32, usize), contexts: std.AutoHashMap(u32, usize),
groups: std.ArrayList(*GroupData), groups: std.ArrayList(*GroupData),
singletons: TypeStore, singletons: TypeStore,
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
/// internal, persistant data structure to manage the entities in a group /// internal, persistant data structure to manage the entities in a group
pub const GroupData = struct { pub const GroupData = struct {
@ -49,7 +49,7 @@ pub const Registry = struct {
registry: *Registry, registry: *Registry,
current: usize, current: usize,
pub fn initPtr(allocator: *std.mem.Allocator, registry: *Registry, hash: u64, owned: []u32, include: []u32, exclude: []u32) *GroupData { pub fn initPtr(allocator: std.mem.Allocator, registry: *Registry, hash: u64, owned: []u32, include: []u32, exclude: []u32) *GroupData {
// std.debug.assert(std.mem.indexOfAny(u32, owned, include) == null); // std.debug.assert(std.mem.indexOfAny(u32, owned, include) == null);
// std.debug.assert(std.mem.indexOfAny(u32, owned, exclude) == null); // std.debug.assert(std.mem.indexOfAny(u32, owned, exclude) == null);
// std.debug.assert(std.mem.indexOfAny(u32, include, exclude) == null); // std.debug.assert(std.mem.indexOfAny(u32, include, exclude) == null);
@ -68,7 +68,7 @@ pub const Registry = struct {
return group_data; return group_data;
} }
pub fn deinit(self: *GroupData, allocator: *std.mem.Allocator) void { pub fn deinit(self: *GroupData, allocator: std.mem.Allocator) void {
// only deinit th SparseSet for non-owning groups // only deinit th SparseSet for non-owning groups
if (self.owned.len == 0) { if (self.owned.len == 0) {
self.entity_set.deinit(); self.entity_set.deinit();
@ -184,7 +184,7 @@ pub const Registry = struct {
} }
}; };
pub fn init(allocator: *std.mem.Allocator) Registry { pub fn init(allocator: std.mem.Allocator) Registry {
return Registry{ return Registry{
.handles = EntityHandles.init(allocator), .handles = EntityHandles.init(allocator),
.components = std.AutoHashMap(u32, usize).init(allocator), .components = std.AutoHashMap(u32, usize).init(allocator),

@ -13,9 +13,9 @@ pub fn SparseSet(comptime SparseT: type) type {
sparse: std.ArrayList(?[]SparseT), sparse: std.ArrayList(?[]SparseT),
dense: std.ArrayList(SparseT), dense: std.ArrayList(SparseT),
entity_mask: SparseT, entity_mask: SparseT,
allocator: ?*std.mem.Allocator, allocator: ?std.mem.Allocator,
pub fn initPtr(allocator: *std.mem.Allocator) *Self { pub fn initPtr(allocator: std.mem.Allocator) *Self {
var set = allocator.create(Self) catch unreachable; var set = allocator.create(Self) catch unreachable;
set.sparse = std.ArrayList(?[]SparseT).initCapacity(allocator, 16) catch unreachable; set.sparse = std.ArrayList(?[]SparseT).initCapacity(allocator, 16) catch unreachable;
set.dense = std.ArrayList(SparseT).initCapacity(allocator, 16) catch unreachable; set.dense = std.ArrayList(SparseT).initCapacity(allocator, 16) catch unreachable;
@ -24,7 +24,7 @@ pub fn SparseSet(comptime SparseT: type) type {
return set; return set;
} }
pub fn init(allocator: *std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
return Self{ return Self{
.sparse = std.ArrayList(?[]SparseT).init(allocator), .sparse = std.ArrayList(?[]SparseT).init(allocator),
.dense = std.ArrayList(SparseT).init(allocator), .dense = std.ArrayList(SparseT).init(allocator),
@ -104,8 +104,8 @@ pub fn SparseSet(comptime SparseT: type) type {
pub fn contains(self: Self, sparse: SparseT) bool { pub fn contains(self: Self, sparse: SparseT) bool {
const curr = self.page(sparse); const curr = self.page(sparse);
return curr < self.sparse.items.len and return curr < self.sparse.items.len and
self.sparse.items[curr] != null and self.sparse.items[curr] != null and
self.sparse.items[curr].?[self.offset(sparse)] != std.math.maxInt(SparseT); self.sparse.items[curr].?[self.offset(sparse)] != std.math.maxInt(SparseT);
} }
/// Returns the position of an entity in a sparse set /// Returns the position of an entity in a sparse set

@ -4,9 +4,9 @@ const utils = @import("utils.zig");
/// stores a single object of type T for each T added /// stores a single object of type T for each T added
pub const TypeStore = struct { pub const TypeStore = struct {
map: std.AutoHashMap(u32, []u8), map: std.AutoHashMap(u32, []u8),
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) TypeStore { pub fn init(allocator: std.mem.Allocator) TypeStore {
return TypeStore{ return TypeStore{
.map = std.AutoHashMap(u32, []u8).init(allocator), .map = std.AutoHashMap(u32, []u8).init(allocator),
.allocator = allocator, .allocator = allocator,

@ -3,16 +3,14 @@ const std = @import("std");
/// Processes are run by the Scheduler. They use a similar pattern to Allocators in that they are created and /// Processes are run by the Scheduler. They use a similar pattern to Allocators in that they are created and
/// added as fields in a parent struct, your actual process that will be run. /// added as fields in a parent struct, your actual process that will be run.
pub const Process = struct { pub const Process = struct {
const State = enum(u8) { const State = enum(u8) { uninitialized, running, paused, succeeded, failed, aborted, finished };
uninitialized, running, paused, succeeded, failed, aborted, finished
};
updateFn: fn (self: *Process) void, updateFn: fn (self: *Process) void,
startFn: ?fn (self: *Process) void = null, startFn: ?fn (self: *Process) void = null,
abortedFn: ?fn (self: *Process) void = null, abortedFn: ?fn (self: *Process) void = null,
failedFn: ?fn (self: *Process) void = null, failedFn: ?fn (self: *Process) void = null,
succeededFn: ?fn (self: *Process) void = null, succeededFn: ?fn (self: *Process) void = null,
deinit: fn (self: *Process, allocator: *std.mem.Allocator) void = undefined, deinit: fn (self: *Process, allocator: std.mem.Allocator) void = undefined,
state: State = .uninitialized, state: State = .uninitialized,
stopped: bool = false, stopped: bool = false,

@ -12,16 +12,16 @@ const Process = @import("process.zig").Process;
/// - in any callback you can get your oiginal struct back via `process.getParent(@This())` /// - in any callback you can get your oiginal struct back via `process.getParent(@This())`
pub const Scheduler = struct { pub const Scheduler = struct {
processes: std.ArrayList(*Process), processes: std.ArrayList(*Process),
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
/// helper to create and prepare a process /// helper to create and prepare a process
fn createProcessHandler(comptime T: type, data: anytype, allocator: *std.mem.Allocator) *Process { fn createProcessHandler(comptime T: type, data: anytype, allocator: std.mem.Allocator) *Process {
var proc = allocator.create(T) catch unreachable; var proc = allocator.create(T) catch unreachable;
proc.initialize(data); proc.initialize(data);
// get a closure so that we can safely deinit this later // get a closure so that we can safely deinit this later
proc.process.deinit = struct { proc.process.deinit = struct {
fn deinit(process: *Process, alloc: *std.mem.Allocator) void { fn deinit(process: *Process, alloc: std.mem.Allocator) void {
if (process.next) |next_process| { if (process.next) |next_process| {
next_process.deinit(next_process, alloc); next_process.deinit(next_process, alloc);
} }
@ -35,9 +35,9 @@ pub const Scheduler = struct {
/// returned when appending a process so that sub-processes can be added to the process /// returned when appending a process so that sub-processes can be added to the process
const Continuation = struct { const Continuation = struct {
process: *Process, process: *Process,
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
pub fn init(process: *Process, allocator: *std.mem.Allocator) Continuation { pub fn init(process: *Process, allocator: std.mem.Allocator) Continuation {
return .{ .process = process, .allocator = allocator }; return .{ .process = process, .allocator = allocator };
} }
@ -48,7 +48,7 @@ pub const Scheduler = struct {
} }
}; };
pub fn init(allocator: *std.mem.Allocator) Scheduler { pub fn init(allocator: std.mem.Allocator) Scheduler {
return .{ return .{
.processes = std.ArrayList(*Process).init(allocator), .processes = std.ArrayList(*Process).init(allocator),
.allocator = allocator, .allocator = allocator,
@ -72,7 +72,7 @@ pub const Scheduler = struct {
return Continuation.init(process, self.allocator); return Continuation.init(process, self.allocator);
} }
fn updateProcess(process: **Process, allocator: *std.mem.Allocator) bool { fn updateProcess(process: **Process, allocator: std.mem.Allocator) bool {
const current_process = process.*; const current_process = process.*;
current_process.tick(); current_process.tick();

@ -4,9 +4,9 @@ const Cache = @import("cache.zig").Cache;
pub const Assets = struct { pub const Assets = struct {
caches: std.AutoHashMap(u32, usize), caches: std.AutoHashMap(u32, usize),
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) Assets { pub fn init(allocator: std.mem.Allocator) Assets {
return Assets{ return Assets{
.caches = std.AutoHashMap(u32, usize).init(allocator), .caches = std.AutoHashMap(u32, usize).init(allocator),
.allocator = allocator, .allocator = allocator,

@ -10,9 +10,9 @@ pub fn Cache(comptime T: type) type {
safe_deinit: fn (*@This()) void, safe_deinit: fn (*@This()) void,
resources: std.AutoHashMap(u32, *T), resources: std.AutoHashMap(u32, *T),
allocator: ?*std.mem.Allocator = null, allocator: ?std.mem.Allocator = null,
pub fn initPtr(allocator: *std.mem.Allocator) *@This() { pub fn initPtr(allocator: std.mem.Allocator) *@This() {
var cache = allocator.create(@This()) catch unreachable; var cache = allocator.create(@This()) catch unreachable;
cache.safe_deinit = struct { cache.safe_deinit = struct {
fn deinit(self: *Self) void { fn deinit(self: *Self) void {
@ -26,7 +26,7 @@ pub fn Cache(comptime T: type) type {
return cache; return cache;
} }
pub fn init(allocator: *std.mem.Allocator) @This() { pub fn init(allocator: std.mem.Allocator) @This() {
return .{ return .{
.safe_deinit = struct { .safe_deinit = struct {
fn deinit(self: *Self) void { fn deinit(self: *Self) void {

@ -5,9 +5,9 @@ const utils = @import("../ecs/utils.zig");
pub const Dispatcher = struct { pub const Dispatcher = struct {
signals: std.AutoHashMap(u32, usize), signals: std.AutoHashMap(u32, usize),
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) Dispatcher { pub fn init(allocator: std.mem.Allocator) Dispatcher {
return Dispatcher{ return Dispatcher{
.signals = std.AutoHashMap(u32, usize).init(allocator), .signals = std.AutoHashMap(u32, usize).init(allocator),
.allocator = allocator, .allocator = allocator,

@ -7,9 +7,9 @@ pub fn Signal(comptime Event: type) type {
const Self = @This(); const Self = @This();
calls: std.ArrayList(Delegate(Event)), calls: std.ArrayList(Delegate(Event)),
allocator: ?*std.mem.Allocator = null, allocator: ?std.mem.Allocator = null,
pub fn init(allocator: *std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
// we purposely do not store the allocator locally in this case so we know not to destroy ourself in deint! // we purposely do not store the allocator locally in this case so we know not to destroy ourself in deint!
return Self{ return Self{
.calls = std.ArrayList(Delegate(Event)).init(allocator), .calls = std.ArrayList(Delegate(Event)).init(allocator),
@ -17,7 +17,7 @@ pub fn Signal(comptime Event: type) type {
} }
/// heap allocates a Signal /// heap allocates a Signal
pub fn create(allocator: *std.mem.Allocator) *Self { pub fn create(allocator: std.mem.Allocator) *Self {
var signal = allocator.create(Self) catch unreachable; var signal = allocator.create(Self) catch unreachable;
signal.calls = std.ArrayList(Delegate(Event)).init(allocator); signal.calls = std.ArrayList(Delegate(Event)).init(allocator);
signal.allocator = allocator; signal.allocator = allocator;

Loading…
Cancel
Save