summaryrefslogtreecommitdiffstats
path: root/safe_libc/src/boxed.rs
blob: 4a38b70e91267d27ff302052cb1c70e8ea893e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::util;

use core::{fmt, mem, ptr};
use core::ops::{Deref, DerefMut};

fn alloc<T>(len: usize) -> *mut T {
	if util::zst::<T>(len) {
		return ptr::null_mut();
	}
	let size = len.checked_mul(mem::size_of::<T>()).expect("allocation overflow");
	let align = mem::align_of::<T>();
	util::must_succeed(
		unsafe {
			libc::memalign(align, size)
		}
	).cast()
}

#[inline]
const fn dangling<T>() -> *mut T {
	mem::align_of::<T>() as *mut T
}

#[inline]
fn slice_len<T>(p: *const [T]) -> usize {
	unsafe { mem::transmute::<_, [usize; 2]>(p)[1] }
}

pub trait SafePtr {
	fn safe_ptr(p: *mut Self) -> *mut Self;
}

impl<T> SafePtr for T {
	#[inline]
	fn safe_ptr(p: *mut T) -> *mut T {
		if util::zst::<T>(1) {
			return dangling();
		}

		debug_assert!(!p.is_null(), "NULL ptr");
		p
	}
}

impl<T> SafePtr for [T] {
	#[inline]
	fn safe_ptr(p: *mut [T]) -> *mut [T] {
		let len = slice_len(p);
		if util::zst::<T>(len) {
			return ptr::slice_from_raw_parts_mut(dangling(), len);
		}

		debug_assert!(!p.is_null(), "NULL ptr");
		p
	}
}

pub struct CBox<T: SafePtr + ?Sized>(*mut T);

impl<T: SafePtr + ?Sized> CBox<T> {
	#[inline]
	pub unsafe fn from_raw_unchecked(p: *mut T) -> CBox<T> {
		CBox(p)
	}

	#[inline]
	pub fn into_raw(self) -> *mut T {
		let p = self.0;
		mem::forget(self);
		p
	}

	#[inline]
	pub fn as_ptr(&self) -> *const T {
		self.0
	}

	#[inline]
	pub fn as_mut_ptr(&mut self) -> *mut T {
		self.0
	}
}

impl<T> CBox<T> {
	#[inline]
	pub fn new(value: T) -> CBox<T> {
		let p = alloc(1);
		unsafe {
			ptr::write(p, value);
			CBox::from_raw_unchecked(p)
		}
	}

	#[inline]
	pub unsafe fn from_raw(p: *mut T) -> CBox<T> {
		util::check_ptr(p, 1);
		CBox(p)
	}

	#[inline]
	pub unsafe fn slice_from_raw_parts_unchecked(p: *mut T, len: usize) -> CBox<[T]> {
		CBox(ptr::slice_from_raw_parts_mut(p, len))
	}

	#[inline]
	pub unsafe fn slice_from_raw_parts(p: *mut T, len: usize) -> CBox<[T]> {
		util::check_ptr(p, len);
		CBox::slice_from_raw_parts_unchecked(p, len)
	}
}

impl<T: SafePtr + ?Sized> Drop for CBox<T> {
	#[inline]
	fn drop(&mut self) {
		unsafe {
			ptr::drop_in_place(self.0);
			libc::free(self.0.cast());
		}
	}
}

impl<T: SafePtr + ?Sized> Deref for CBox<T> {
	type Target = T;

	#[inline]
	fn deref(&self) -> &T {
		unsafe { &*T::safe_ptr(self.0) }
	}
}

impl<T: SafePtr + ?Sized> DerefMut for CBox<T> {
	#[inline]
	fn deref_mut(&mut self) -> &mut T {
		unsafe { &mut *T::safe_ptr(self.0) }
	}
}

impl<T: fmt::Debug + SafePtr + ?Sized> fmt::Debug for CBox<T> {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Debug::fmt(&**self, f)
	}
}

impl<T: fmt::Display + SafePtr + ?Sized> fmt::Display for CBox<T> {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Display::fmt(&**self, f)
	}
}