forked from mozilla/mp4parse_fallible
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
175 lines (150 loc) · 5.45 KB
/
Copy pathlib.rs
File metadata and controls
175 lines (150 loc) · 5.45 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::io::Read;
use std::convert::TryInto as _;
use std::mem;
use std::vec::Vec;
extern "C" {
fn realloc(ptr: *mut u8, bytes: usize) -> *mut u8;
fn malloc(bytes: usize) -> *mut u8;
}
pub trait FallibleVec<T> {
/// Append |val| to the end of |vec|. Returns Ok(()) on success,
/// Err(()) if it fails, which can only be due to lack of memory.
fn try_push(&mut self, value: T) -> Result<(), ()>;
/// Reserves capacity for at least `additional` more elements to
/// be inserted in the vector. Does nothing if capacity is already
/// sufficient. Return Ok(()) on success, Err(()) if it fails either
/// due to lack of memory, or overflowing the `usize` used to store
/// the capacity.
fn try_reserve(&mut self, additional: usize) -> Result<(), ()>;
/// Clones and appends all elements in a slice to the Vec.
/// Returns Ok(()) on success, Err(()) if it fails, which can
/// only be due to lack of memory.
fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), ()> where T: Clone;
}
/// Reserves the upper limit of what `src` can generate before reading all
/// bytes until EOF in this source, placing them into buf. If the allocation
/// is unsuccessful, or reading from the source generates an error before
/// reaching EOF, this will return an error. Otherwise, it will return the
/// number of bytes read.
///
/// Since `src.limit()` may return a value greater than the number of bytes
/// which can be read from the source, it's possible this function may fail
/// in the allocation phase even though allocating the number of bytes available
/// to read would have succeeded. In general, it is assumed that the callers
/// have accurate knowledge of the number of bytes of interest and have created
/// `src` accordingly.
pub fn try_read_to_end<T>(src: &mut std::io::Take<T>, buf: &mut Vec<u8>) -> Result<usize, ()>
where T: Read
{
let limit: usize = src.limit().try_into().map_err(|_| ())?;
FallibleVec::try_reserve(buf, limit)?;
let bytes_read = src.read_to_end(buf).map_err(|_| ())?;
Ok(bytes_read)
}
/////////////////////////////////////////////////////////////////
// Vec
impl<T> FallibleVec<T> for Vec<T> {
#[inline]
fn try_push(&mut self, val: T) -> Result<(), ()> {
if self.capacity() == self.len() {
let old_cap: usize = self.capacity();
let new_cap: usize
= if old_cap == 0 { 4 } else { old_cap.checked_mul(2).ok_or(()) ? };
try_extend_vec(self, new_cap)?;
debug_assert!(self.capacity() > self.len());
}
self.push(val);
Ok(())
}
#[inline]
fn try_reserve(&mut self, additional: usize) -> Result<(), ()> {
let available = self.capacity().checked_sub(self.len()).expect("capacity >= len");
if additional > available {
let increase = additional.checked_sub(available).expect("additional > available");
let new_cap = self.capacity().checked_add(increase).ok_or(())?;
try_extend_vec(self, new_cap)?;
debug_assert!(self.capacity() == new_cap);
}
Ok(())
}
#[inline]
fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), ()> where T: Clone {
FallibleVec::try_reserve(self, other.len())?;
self.extend_from_slice(other);
Ok(())
}
}
#[inline(never)]
#[cold]
fn try_extend_vec<T>(vec: &mut Vec<T>, new_cap: usize) -> Result<(), ()> {
let old_ptr = vec.as_mut_ptr();
let old_len = vec.len();
let old_cap: usize = vec.capacity();
if old_cap >= new_cap {
return Ok(());
}
let new_size_bytes
= new_cap.checked_mul(mem::size_of::<T>()).ok_or(()) ? ;
let new_ptr = unsafe {
if old_cap == 0 {
malloc(new_size_bytes)
} else {
realloc(old_ptr as *mut u8, new_size_bytes)
}
};
if new_ptr.is_null() {
return Err(());
}
let new_vec = unsafe {
Vec::from_raw_parts(new_ptr as *mut T, old_len, new_cap)
};
mem::forget(mem::replace(vec, new_vec));
Ok(())
}
#[test]
fn oom() {
let mut vec: Vec<char> = Vec::new();
match FallibleVec::try_reserve(&mut vec, std::usize::MAX) {
Ok(_) => panic!("it should be OOM"),
_ => (),
}
}
#[test]
fn try_reserve() {
let mut vec = vec![1];
let old_cap = vec.capacity();
let new_cap = old_cap + 1;
FallibleVec::try_reserve(&mut vec, new_cap).unwrap();
assert!(vec.capacity() >= new_cap);
}
#[test]
fn capacity_overflow() {
let mut vec = vec![1];
match FallibleVec::try_reserve(&mut vec, std::usize::MAX) {
Ok(_) => panic!("capacity calculation should overflow"),
_ => (),
}
}
#[test]
fn extend_from_slice() {
let mut vec = b"foo".to_vec();
FallibleVec::try_extend_from_slice(&mut vec, b"bar").unwrap();
assert_eq!(&vec, b"foobar");
}
#[test]
fn try_read_to_end_() {
let mut src = b"1234567890".take(5);
let mut buf = vec![];
let bytes_read = try_read_to_end(&mut src, &mut buf).unwrap();
assert_eq!(bytes_read, 5);
assert_eq!(buf, b"12345");
}
#[test]
fn try_read_to_end_oom() {
let mut src = b"1234567890".take(std::usize::MAX.try_into().expect("usize < u64"));
let mut buf = vec![];
assert!(try_read_to_end(&mut src, &mut buf).is_err());
}