From d8ca34276be334642b8a930d97cfe4654c69e88d Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:45:39 +0900 Subject: [PATCH 01/32] save --- src/aligned_memory.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index a9dcfe56..c44071ae 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -15,31 +15,43 @@ impl Pod for i16 {} impl Pod for i32 {} impl Pod for i64 {} +#[derive(Debug, PartialEq, Eq)] +struct TlsVecU8(Vec); + +impl std::ops::Deref for TlsVecU8 { + type Target = Vec; + fn deref(&self) -> &::Target { todo!() } +} + +impl std::ops::DerefMut for TlsVecU8 { + fn deref_mut(&mut self) -> &mut ::Target { todo!() } +} + /// Provides u8 slices at a specified alignment #[derive(Debug, PartialEq, Eq)] pub struct AlignedMemory { max_len: usize, align_offset: usize, - mem: Vec, + mem: TlsVecU8, zero_up_to_max_len: bool, } impl AlignedMemory { - fn get_mem(max_len: usize) -> (Vec, usize) { + fn get_mem(max_len: usize) -> (TlsVecU8, usize) { let mut mem: Vec = Vec::with_capacity(max_len.saturating_add(ALIGN)); mem.push(0); let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(align_offset, 0); - (mem, align_offset) + (TlsVecU8(mem), align_offset) } - fn get_mem_zeroed(max_len: usize) -> (Vec, usize) { + fn get_mem_zeroed(max_len: usize) -> (TlsVecU8, usize) { // use calloc() to get zeroed memory from the OS instead of using // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = vec![0; max_len]; let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); - (mem, align_offset) + (TlsVecU8(mem), align_offset) } /// Returns a filled AlignedMemory by copying the given slice pub fn from_slice(data: &[u8]) -> Self { From af4963b830a7b335dc77d65c97f0779e5231dbbc Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:45:57 +0900 Subject: [PATCH 02/32] save --- src/aligned_memory.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index c44071ae..a6aa7cb3 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -20,11 +20,11 @@ struct TlsVecU8(Vec); impl std::ops::Deref for TlsVecU8 { type Target = Vec; - fn deref(&self) -> &::Target { todo!() } + fn deref(&self) -> &::Target { &self.0 } } impl std::ops::DerefMut for TlsVecU8 { - fn deref_mut(&mut self) -> &mut ::Target { todo!() } + fn deref_mut(&mut self) -> &mut ::Target { &mut self.0 } } /// Provides u8 slices at a specified alignment From 6b88f973802c9b99ee6911ccc075e8d5e608b50c Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:46:06 +0900 Subject: [PATCH 03/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index a6aa7cb3..43530a8f 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -16,7 +16,7 @@ impl Pod for i32 {} impl Pod for i64 {} #[derive(Debug, PartialEq, Eq)] -struct TlsVecU8(Vec); +struct TlsVecU8(Vec, bool); impl std::ops::Deref for TlsVecU8 { type Target = Vec; From bdebeee48d1a9ec3785c490c4fb145afe3e3de2f Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:46:19 +0900 Subject: [PATCH 04/32] save --- src/aligned_memory.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 43530a8f..1f8e75d2 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -42,7 +42,7 @@ impl AlignedMemory { mem.push(0); let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(align_offset, 0); - (TlsVecU8(mem), align_offset) + (TlsVecU8(mem, false), align_offset) } fn get_mem_zeroed(max_len: usize) -> (TlsVecU8, usize) { // use calloc() to get zeroed memory from the OS instead of using @@ -51,7 +51,7 @@ impl AlignedMemory { let mut mem = vec![0; max_len]; let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); - (TlsVecU8(mem), align_offset) + (TlsVecU8(mem, true), align_offset) } /// Returns a filled AlignedMemory by copying the given slice pub fn from_slice(data: &[u8]) -> Self { From 4537fa9d751012e4cb78d15d25d2494124e2eda5 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:46:31 +0900 Subject: [PATCH 05/32] save --- src/aligned_memory.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 1f8e75d2..2c5330fd 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -18,6 +18,9 @@ impl Pod for i64 {} #[derive(Debug, PartialEq, Eq)] struct TlsVecU8(Vec, bool); +impl Drop for TlsVecU8 { +} + impl std::ops::Deref for TlsVecU8 { type Target = Vec; fn deref(&self) -> &::Target { &self.0 } From eb434ad014786b688fd0c58a26a6ec4cb0964930 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:50:15 +0900 Subject: [PATCH 06/32] save --- src/aligned_memory.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 2c5330fd..de59aae4 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -18,7 +18,17 @@ impl Pod for i64 {} #[derive(Debug, PartialEq, Eq)] struct TlsVecU8(Vec, bool); +thread_local! { + static vecs: RefCell; +} + impl Drop for TlsVecU8 { + fn drop(&mut self) { + if !self.1 { + return; + } + + } } impl std::ops::Deref for TlsVecU8 { From 0dc12c3a512b3c540d8c37068cedb07d4bcb7668 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:51:03 +0900 Subject: [PATCH 07/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index de59aae4..57608374 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, bool); thread_local! { - static vecs: RefCell; + static vecs: RefCell> = const { RefCell::new(BTreeMap::new()) }; } impl Drop for TlsVecU8 { From c58c1b56ee10db0de9f558adaf72d1c699d3a8bc Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:51:22 +0900 Subject: [PATCH 08/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 57608374..af953adf 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, bool); thread_local! { - static vecs: RefCell> = const { RefCell::new(BTreeMap::new()) }; + static VECS: RefCell> = const { RefCell::new(BTreeMap::new()) }; } impl Drop for TlsVecU8 { From bd9d638bd34b3c0bc625e85303536d926885bfe6 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:52:06 +0900 Subject: [PATCH 09/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index af953adf..b6f28c3e 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, bool); thread_local! { - static VECS: RefCell> = const { RefCell::new(BTreeMap::new()) }; + static VECS: std::cell::RefCell> = const { RefCell::new(BTreeMap::new()) }; } impl Drop for TlsVecU8 { From 51ea7c6d21763f1cc12756963a3519044bdbd19c Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:52:21 +0900 Subject: [PATCH 10/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index b6f28c3e..ad1343c6 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, bool); thread_local! { - static VECS: std::cell::RefCell> = const { RefCell::new(BTreeMap::new()) }; + static VECS: std::cell::RefCell>> = const { RefCell::new(BTreeMap::new()) }; } impl Drop for TlsVecU8 { From 11998c71cb2e0175b826b9433e52eeb79ec94752 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:52:37 +0900 Subject: [PATCH 11/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index ad1343c6..a37fd878 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, bool); thread_local! { - static VECS: std::cell::RefCell>> = const { RefCell::new(BTreeMap::new()) }; + static VECS: std::cell::RefCell>> = const { std::cell::RefCell::new(std::collections::BTreeMap::new()) }; } impl Drop for TlsVecU8 { From d691a9b777cbee260665c8fef46a410d3f0ad823 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:52:45 +0900 Subject: [PATCH 12/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index a37fd878..ebd305d0 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, bool); thread_local! { - static VECS: std::cell::RefCell>> = const { std::cell::RefCell::new(std::collections::BTreeMap::new()) }; + static VECS: std::cell::RefCell>> = const { std::cell::RefCell::new(std::collections::BTreeMap::new()) }; } impl Drop for TlsVecU8 { From 0b6d4885030792ef1a3b9d1c9241d7ea202b8f2e Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:53:33 +0900 Subject: [PATCH 13/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index ebd305d0..89674efd 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -27,7 +27,7 @@ impl Drop for TlsVecU8 { if !self.1 { return; } - + let vec = std::mem::take(&mut self.0); } } From c6501fb07d09fa514fb9977d120cf3e724fec711 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:54:34 +0900 Subject: [PATCH 14/32] save --- src/aligned_memory.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 89674efd..19589ab5 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -16,7 +16,7 @@ impl Pod for i32 {} impl Pod for i64 {} #[derive(Debug, PartialEq, Eq)] -struct TlsVecU8(Vec, bool); +struct TlsVecU8(Vec, usize, bool); thread_local! { static VECS: std::cell::RefCell>> = const { std::cell::RefCell::new(std::collections::BTreeMap::new()) }; @@ -24,10 +24,12 @@ thread_local! { impl Drop for TlsVecU8 { fn drop(&mut self) { - if !self.1 { + if !self.2 { return; } let vec = std::mem::take(&mut self.0); + VECS.with_borrow_mut(|vecs| { + }); } } @@ -55,7 +57,7 @@ impl AlignedMemory { mem.push(0); let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(align_offset, 0); - (TlsVecU8(mem, false), align_offset) + (TlsVecU8(mem, max_len, false), align_offset) } fn get_mem_zeroed(max_len: usize) -> (TlsVecU8, usize) { // use calloc() to get zeroed memory from the OS instead of using @@ -64,7 +66,7 @@ impl AlignedMemory { let mut mem = vec![0; max_len]; let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); - (TlsVecU8(mem, true), align_offset) + (TlsVecU8(mem, max_len, true), align_offset) } /// Returns a filled AlignedMemory by copying the given slice pub fn from_slice(data: &[u8]) -> Self { From ae6a564f0f3317a1b3fed2cbd121e2a536253039 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:55:21 +0900 Subject: [PATCH 15/32] save --- src/aligned_memory.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 19589ab5..be594e2d 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,7 +19,7 @@ impl Pod for i64 {} struct TlsVecU8(Vec, usize, bool); thread_local! { - static VECS: std::cell::RefCell>> = const { std::cell::RefCell::new(std::collections::BTreeMap::new()) }; + static VECS: std::cell::RefCell>>> = const { std::cell::RefCell::new(std::collections::BTreeMap::new()) }; } impl Drop for TlsVecU8 { @@ -29,6 +29,7 @@ impl Drop for TlsVecU8 { } let vec = std::mem::take(&mut self.0); VECS.with_borrow_mut(|vecs| { + vecs.entry(self.1).or_default().push(vec); }); } } From ff1016e0074a2d5482f2ab831654e79f8cd48649 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:56:29 +0900 Subject: [PATCH 16/32] save --- src/aligned_memory.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index be594e2d..ce6c6eb2 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -64,7 +64,9 @@ impl AlignedMemory { // use calloc() to get zeroed memory from the OS instead of using // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 - let mut mem = vec![0; max_len]; + let mut mem = VECS.with_borrow_mut(|vecs| { + vec![0; max_len] + }); let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); (TlsVecU8(mem, max_len, true), align_offset) From a137148d84d34559961867354a7338fcb565f65d Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:57:30 +0900 Subject: [PATCH 17/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index ce6c6eb2..d80f67df 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,7 +65,7 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { - vec![0; max_len] + vecs.entry(max_len).or_default().pop().or_else(|| vec![0; max_len]) }); let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); From 7062cd1dc1f3e2445e55ad4aa69a7ad6f91cd276 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:57:48 +0900 Subject: [PATCH 18/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index d80f67df..c192da02 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,7 +65,7 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { - vecs.entry(max_len).or_default().pop().or_else(|| vec![0; max_len]) + vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) }); let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); From fe1eec30f1b2aaf8450b093dbe317e7697d75d0c Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:59:42 +0900 Subject: [PATCH 19/32] save --- src/aligned_memory.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index c192da02..e1a6e590 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,6 +65,9 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { + for (l, v) in vecs { + eprintln!("size: {l}, buffer count: {}", v.len()); + } vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) }); let align_offset = mem.as_ptr().align_offset(ALIGN); From d8397995a0ac63c872259e306504a92bd0491b75 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 13:59:49 +0900 Subject: [PATCH 20/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index e1a6e590..ec933111 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,7 +65,7 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { - for (l, v) in vecs { + for (l, v) in &vecs { eprintln!("size: {l}, buffer count: {}", v.len()); } vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) From 90cd3ba4af1b652dc605602a6d19a8821eae03d1 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:00:01 +0900 Subject: [PATCH 21/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index ec933111..86222deb 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,7 +65,7 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { - for (l, v) in &vecs { + for (l, v) in vecs.entries() { eprintln!("size: {l}, buffer count: {}", v.len()); } vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) From 4fea7b85026133d95e39cc2d5a6adacc9424ba31 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:00:14 +0900 Subject: [PATCH 22/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 86222deb..1b70da38 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,7 +65,7 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { - for (l, v) in vecs.entries() { + for (l, v) in vecs.key_values() { eprintln!("size: {l}, buffer count: {}", v.len()); } vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) From 7347d279be0b3d6f7030eb6b434882e6ff53affe Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:00:22 +0900 Subject: [PATCH 23/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 1b70da38..a6b478b0 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -65,7 +65,7 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let mut mem = VECS.with_borrow_mut(|vecs| { - for (l, v) in vecs.key_values() { + for (l, v) in vecs.iter() { eprintln!("size: {l}, buffer count: {}", v.len()); } vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) From c7f77d9e8b2c18e9791cb4bcbf17e2216449b0a1 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:16:14 +0900 Subject: [PATCH 24/32] save --- src/aligned_memory.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index a6b478b0..8f1724d9 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -68,10 +68,13 @@ impl AlignedMemory { for (l, v) in vecs.iter() { eprintln!("size: {l}, buffer count: {}", v.len()); } - vecs.entry(max_len).or_default().pop().unwrap_or_else(|| vec![0; max_len]) + vecs.entry(max_len).or_default().pop().unwrap_or_else(|| { + let mut mem = vec![0; max_len]; + let align_offset = mem.as_ptr().align_offset(ALIGN); + mem.resize(max_len.saturating_add(align_offset), 0); + mem + }) }); - let align_offset = mem.as_ptr().align_offset(ALIGN); - mem.resize(max_len.saturating_add(align_offset), 0); (TlsVecU8(mem, max_len, true), align_offset) } /// Returns a filled AlignedMemory by copying the given slice From d8c0a143dcbdecfb3bbddc399be63066b14c2b42 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:17:06 +0900 Subject: [PATCH 25/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 8f1724d9..1db2f962 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -72,7 +72,7 @@ impl AlignedMemory { let mut mem = vec![0; max_len]; let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); - mem + (mem, align_offset) }) }); (TlsVecU8(mem, max_len, true), align_offset) From 5894f37e3d633765bcd483fe9080f074af553092 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:17:15 +0900 Subject: [PATCH 26/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 1db2f962..449bf947 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -64,7 +64,7 @@ impl AlignedMemory { // use calloc() to get zeroed memory from the OS instead of using // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 - let mut mem = VECS.with_borrow_mut(|vecs| { + let mut (mem, align_offset) = VECS.with_borrow_mut(|vecs| { for (l, v) in vecs.iter() { eprintln!("size: {l}, buffer count: {}", v.len()); } From 29151e9f8268805b73363a6bed8149a516b472ac Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:17:26 +0900 Subject: [PATCH 27/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 449bf947..fc35ffa5 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -64,7 +64,7 @@ impl AlignedMemory { // use calloc() to get zeroed memory from the OS instead of using // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 - let mut (mem, align_offset) = VECS.with_borrow_mut(|vecs| { + let (mut mem, align_offset) = VECS.with_borrow_mut(|vecs| { for (l, v) in vecs.iter() { eprintln!("size: {l}, buffer count: {}", v.len()); } From b683423367468fef3f6c7f0cfcadb4e1cb6bce02 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:18:01 +0900 Subject: [PATCH 28/32] save --- src/aligned_memory.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index fc35ffa5..e673d67f 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -68,7 +68,10 @@ impl AlignedMemory { for (l, v) in vecs.iter() { eprintln!("size: {l}, buffer count: {}", v.len()); } - vecs.entry(max_len).or_default().pop().unwrap_or_else(|| { + vecs.entry(max_len).or_default().pop().map(|mem| { + let align_offset = mem.as_ptr().align_offset(ALIGN); + (mem, align_offset) + }).unwrap_or_else(|| { let mut mem = vec![0; max_len]; let align_offset = mem.as_ptr().align_offset(ALIGN); mem.resize(max_len.saturating_add(align_offset), 0); From 91b7d46e550f41b32f3cfa2a843783e34cd3364b Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:18:07 +0900 Subject: [PATCH 29/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index e673d67f..e6f86e66 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -64,7 +64,7 @@ impl AlignedMemory { // use calloc() to get zeroed memory from the OS instead of using // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 - let (mut mem, align_offset) = VECS.with_borrow_mut(|vecs| { + let (mem, align_offset) = VECS.with_borrow_mut(|vecs| { for (l, v) in vecs.iter() { eprintln!("size: {l}, buffer count: {}", v.len()); } From 85b1d9d1f8e0117d505b5b3231a192c38b477884 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:22:41 +0900 Subject: [PATCH 30/32] save --- src/aligned_memory.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index e6f86e66..fa2da1da 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -29,6 +29,7 @@ impl Drop for TlsVecU8 { } let vec = std::mem::take(&mut self.0); VECS.with_borrow_mut(|vecs| { + vec.fill(0); vecs.entry(self.1).or_default().push(vec); }); } From 28b63fe631aca9ecd5893290ea29a48f86837cdc Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:22:47 +0900 Subject: [PATCH 31/32] save --- src/aligned_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index fa2da1da..26c93750 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -27,7 +27,7 @@ impl Drop for TlsVecU8 { if !self.2 { return; } - let vec = std::mem::take(&mut self.0); + let mut vec = std::mem::take(&mut self.0); VECS.with_borrow_mut(|vecs| { vec.fill(0); vecs.entry(self.1).or_default().push(vec); From 3107f6b77c435fd76837538e309f0e5870119a88 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Wed, 15 May 2024 14:59:59 +0900 Subject: [PATCH 32/32] Remove debug --- src/aligned_memory.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index 26c93750..6c75aaec 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -66,9 +66,6 @@ impl AlignedMemory { // malloc() + memset(), see // https://github.com/rust-lang/rust/issues/54628 let (mem, align_offset) = VECS.with_borrow_mut(|vecs| { - for (l, v) in vecs.iter() { - eprintln!("size: {l}, buffer count: {}", v.len()); - } vecs.entry(max_len).or_default().pop().map(|mem| { let align_offset = mem.as_ptr().align_offset(ALIGN); (mem, align_offset)