From f0b3d5166ef1255fac880b146875ef46d2599a13 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 4 Apr 2020 13:22:18 +0200 Subject: Reorganize --- safe_libc/src/util.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 safe_libc/src/util.rs (limited to 'safe_libc/src/util.rs') diff --git a/safe_libc/src/util.rs b/safe_libc/src/util.rs new file mode 100644 index 0000000..b8a4ed5 --- /dev/null +++ b/safe_libc/src/util.rs @@ -0,0 +1,18 @@ +use core::mem; + +#[inline] +pub fn zst(len: usize) -> bool { + mem::size_of::() == 0 || len == 0 +} + +#[inline] +pub fn check_ptr(p: *const T, len: usize) { + debug_assert!((p as usize) % mem::align_of::() == 0, "unaligned ptr"); + assert!(zst::(len) || !p.is_null(), "NULL ptr"); +} + +#[inline] +pub fn must_succeed(p: *mut T) -> *mut T { + assert!(!p.is_null(), "allocation failure"); + p +} -- cgit v1.2.3