Skip to content

Commit

Permalink
added: overload of new for allocating by value
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed May 8, 2024
1 parent a64e3ea commit eed2efc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/builtin.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,17 @@ cfree :: (ptr: rawptr) => raw_free(context.allocator, ptr);
return res;
}

new_temp :: macro (T: type_expr) => {
#overload
new :: macro (v: $T, allocator := context.allocator) -> &T {
use core

out := cast(&T) raw_alloc(allocator, sizeof T);
core.memory.set(out, 0, sizeof T);
*out = v;
return out;
}

new_temp :: macro (T) => {
return new(T, allocator=context.temp_allocator);
}

Expand Down

0 comments on commit eed2efc

Please sign in to comment.