summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-06-08 00:55:42 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-06-08 00:55:42 -0400
commit2dad06cae3991d7de819f39cf7d63f8e2b519aca (patch)
tree9354fa7fc9b570114e7f82d759bf49070ff723b5 /src/lib.rs
downloadleak_memory-2dad06cae3991d7de819f39cf7d63f8e2b519aca.tar.gz
leak_memory-2dad06cae3991d7de819f39cf7d63f8e2b519aca.tar.bz2
leak_memory-2dad06cae3991d7de819f39cf7d63f8e2b519aca.zip
initial
Diffstat (limited to '')
-rw-r--r--src/lib.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..ebf3274
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,58 @@
+use std::cell::RefCell;
+use libc::malloc;
+
+pub mod error;
+pub mod parse;
+
+#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
+pub struct PissAmount {
+ to_piss: u128
+}
+
+#[derive(Copy, Clone, Debug)]
+pub enum Toliet {
+ Rust,
+ Lazy,
+ Unsafe
+}
+
+fn leak_rust(amount: usize) {
+ Vec::<u8>::with_capacity(amount).leak();
+}
+
+thread_local!(static TOLIET: RefCell<Vec<Vec<u8>>> = RefCell::new(Vec::new()));
+
+fn leak_lazy(amount: usize) {
+ TOLIET.with(|t| {
+ t.borrow_mut().push(Vec::<u8>::with_capacity(amount));
+ })
+}
+
+fn leak_unsafe(amount: usize) {
+ unsafe {
+ malloc(amount);
+ }
+}
+
+fn leak_match(amount: usize, method: Toliet) {
+ match method {
+ Toliet::Rust => leak_rust(amount),
+ Toliet::Lazy => leak_lazy(amount),
+ Toliet::Unsafe => leak_unsafe(amount),
+ }
+}
+
+pub fn leak<T: Into<PissAmount>>(amount: T, method: Toliet) {
+ let num: u128 = amount.into().to_piss;
+ let count = num / usize::MAX as u128;
+
+ for _ in 0..count {
+ leak_match(usize::MAX, method);
+ }
+
+ leak_match((num % usize::MAX as u128) as usize, method);
+}
+
+pub async fn leak_async<T: Into<PissAmount>>(amount: T, method: Toliet) {
+ leak(amount, method);
+}