summaryrefslogtreecommitdiffstats
path: root/third_party/rust/malloc_buf
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/malloc_buf
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/malloc_buf')
-rw-r--r--third_party/rust/malloc_buf/.cargo-checksum.json1
-rw-r--r--third_party/rust/malloc_buf/Cargo.toml14
-rw-r--r--third_party/rust/malloc_buf/src/lib.rs95
3 files changed, 110 insertions, 0 deletions
diff --git a/third_party/rust/malloc_buf/.cargo-checksum.json b/third_party/rust/malloc_buf/.cargo-checksum.json
new file mode 100644
index 0000000000..70cd125dfe
--- /dev/null
+++ b/third_party/rust/malloc_buf/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"d87624d9d117489bd49dbaeb70077a0721e00ac2a76eb371faec818a92da5661","src/lib.rs":"1130dacbe045a20c1ea1e65e00976f18eb7513da492b07d538e7b1b5c64b8842"},"package":"62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"} \ No newline at end of file
diff --git a/third_party/rust/malloc_buf/Cargo.toml b/third_party/rust/malloc_buf/Cargo.toml
new file mode 100644
index 0000000000..cfb0c082d8
--- /dev/null
+++ b/third_party/rust/malloc_buf/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "malloc_buf"
+version = "0.0.6"
+authors = ["Steven Sheldon"]
+
+description = "Structs for handling malloc'd memory passed to Rust."
+repository = "https://github.com/SSheldon/malloc_buf"
+documentation = "http://ssheldon.github.io/malloc_buf/malloc_buf/"
+license = "MIT"
+
+exclude = [".gitignore"]
+
+[dependencies]
+libc = ">= 0.1, < 0.3"
diff --git a/third_party/rust/malloc_buf/src/lib.rs b/third_party/rust/malloc_buf/src/lib.rs
new file mode 100644
index 0000000000..24066b69d4
--- /dev/null
+++ b/third_party/rust/malloc_buf/src/lib.rs
@@ -0,0 +1,95 @@
+extern crate libc;
+
+use std::marker::PhantomData;
+use std::ops::Deref;
+use std::slice;
+use libc::c_void;
+
+struct MallocPtr(*mut c_void);
+
+impl Drop for MallocPtr {
+ fn drop(&mut self) {
+ unsafe {
+ libc::free(self.0);
+ }
+ }
+}
+
+/// A type that represents a `malloc`'d chunk of memory.
+pub struct MallocBuffer<T> {
+ ptr: MallocPtr,
+ len: usize,
+ items: PhantomData<[T]>,
+}
+
+impl<T: Copy> MallocBuffer<T> {
+ /// Constructs a new `MallocBuffer` for a `malloc`'d buffer
+ /// with the given length at the given pointer.
+ /// Returns `None` if the given pointer is null and the length is not 0.
+ ///
+ /// When this `MallocBuffer` drops, the buffer will be `free`'d.
+ ///
+ /// Unsafe because there must be `len` contiguous, valid instances of `T`
+ /// at `ptr`.
+ pub unsafe fn new(ptr: *mut T, len: usize) -> Option<MallocBuffer<T>> {
+ if len > 0 && ptr.is_null() {
+ None
+ } else {
+ Some(MallocBuffer {
+ ptr: MallocPtr(ptr as *mut c_void),
+ len: len,
+ items: PhantomData,
+ })
+ }
+ }
+}
+
+impl<T> Deref for MallocBuffer<T> {
+ type Target = [T];
+
+ fn deref(&self) -> &[T] {
+ let ptr = if self.len == 0 && self.ptr.0.is_null() {
+ // Even a 0-size slice cannot be null, so just use another pointer
+ 0x1 as *const T
+ } else {
+ self.ptr.0 as *const T
+ };
+ unsafe {
+ slice::from_raw_parts(ptr, self.len)
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::ptr;
+ use libc;
+
+ use super::MallocBuffer;
+
+ #[test]
+ fn test_null_buf() {
+ let buf = unsafe {
+ MallocBuffer::<u32>::new(ptr::null_mut(), 0).unwrap()
+ };
+ assert!(&*buf == []);
+ assert!(Some(&*buf) == Some(&[]));
+
+ let buf = unsafe {
+ MallocBuffer::<u32>::new(ptr::null_mut(), 7)
+ };
+ assert!(buf.is_none());
+ }
+
+ #[test]
+ fn test_buf() {
+ let buf = unsafe {
+ let ptr = libc::malloc(12) as *mut u32;
+ *ptr = 1;
+ *ptr.offset(1) = 2;
+ *ptr.offset(2) = 3;
+ MallocBuffer::new(ptr, 3).unwrap()
+ };
+ assert!(&*buf == [1, 2, 3]);
+ }
+}