From 8478b887a594ebbad9789888db006eb149d3b0c3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 29 Jun 2020 16:17:40 +0100 Subject: [PATCH] Add `mem.check_zero` and `mem.check_zero_ptr` --- core/mem/mem.odin | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/core/mem/mem.odin b/core/mem/mem.odin index fc15d2c47..0c27cfdee 100644 --- a/core/mem/mem.odin +++ b/core/mem/mem.odin @@ -78,6 +78,44 @@ compare_byte_ptrs :: proc(a, b: ^byte, n: int) -> int #no_bounds_check { return 0; } +check_zero :: proc(data: []byte) -> bool { + return check_zero_ptr(raw_data(data), len(data)); +} + +check_zero_ptr :: proc(ptr: rawptr, len: int) -> bool { + switch { + case len <= 0: + return true; + case ptr == nil: + return true; + } + + start := uintptr(ptr); + start_aligned := align_forward_uintptr(start, align_of(uintptr)); + end := start + uintptr(len); + end_aligned := align_backward_uintptr(end, align_of(uintptr)); + + for b in start.. bool where intrinsics.type_is_simple_compare(T) { a, b := a, b; return compare_byte_ptrs((^byte)(&a), (^byte)(&b), size_of(T)) == 0;