Skip to content

Commit 9ea1c8d

Browse files
author
Adrià Arrufat
committed
use arena allocator and provide custom panic
1 parent c3c6757 commit 9ea1c8d

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

examples/src/face_alignment.zig

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ pub const std_options: std.Options = .{
1313
.log_level = std.log.default_level,
1414
};
1515

16+
pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace, addr: ?usize) noreturn {
17+
_ = st;
18+
_ = addr;
19+
std.log.err("panic: {s}", .{msg});
20+
@trap();
21+
}
22+
1623
/// These landmarks correspond to the closest to dlib's 5 alignment landmarks.
1724
/// For more information check dlib's blog.
1825
/// https://blog.dlib.net/2017/09/fast-multiclass-object-detection-in.html
@@ -94,18 +101,25 @@ pub export fn extract_aligned_face(
94101
blurring: i32,
95102
landmarks_ptr: [*]const Point2d,
96103
landmarks_len: usize,
97-
extra_ptr: [*]u8,
98-
extra_size: usize,
104+
extra_ptr: ?[*]u8,
105+
extra_len: usize,
99106
) void {
100-
const allocator = blk: {
101-
if (builtin.cpu.arch == .wasm32) {
102-
var fba = std.heap.FixedBufferAllocator.init(extra_ptr[0..extra_size]);
103-
break :blk fba.allocator();
107+
var arena = std.heap.ArenaAllocator.init(blk: {
108+
if (builtin.cpu.arch.isWasm() and builtin.os.tag == .freestanding) {
109+
// We need at least one Image(Rgba) for blurring and one Image(f32) for the integral image.
110+
assert(extra_len >= 8 * rows * cols);
111+
if (extra_ptr) |ptr| {
112+
var fba = std.heap.FixedBufferAllocator.init(ptr[0..extra_len]);
113+
break :blk fba.allocator();
114+
} else {
115+
@panic("ERROR: extra_ptr can't be null when running in WebAssembly.");
116+
}
104117
} else {
105-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
106-
break :blk gpa.allocator();
118+
break :blk std.heap.page_allocator;
107119
}
108-
};
120+
});
121+
defer arena.deinit();
122+
const allocator = arena.allocator();
109123

110124
const image: Image(Rgba) = .{
111125
.rows = rows,

0 commit comments

Comments
 (0)