public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH backup] config: don't manually track padding size
@ 2022-03-03 14:00 Wolfgang Bumiller
  2022-03-10 10:15 ` [pbs-devel] applied: " Thomas Lamprecht
  0 siblings, 1 reply; 2+ messages in thread
From: Wolfgang Bumiller @ 2022-03-03 14:00 UTC (permalink / raw)
  To: pbs-devel

make ConfigVersionCacheData a #[repr(C)] union to fix its
size and let it transparently `Deref{,Mut}` to its actual
contents

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
 pbs-config/src/config_version_cache.rs | 34 ++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/pbs-config/src/config_version_cache.rs b/pbs-config/src/config_version_cache.rs
index 39a433ed..958ae457 100644
--- a/pbs-config/src/config_version_cache.rs
+++ b/pbs-config/src/config_version_cache.rs
@@ -1,7 +1,7 @@
 use std::path::Path;
 use std::sync::Arc;
 use std::sync::atomic::{AtomicUsize, Ordering};
-use std::mem::MaybeUninit;
+use std::mem::{MaybeUninit, ManuallyDrop};
 
 use anyhow::{bail, Error};
 use once_cell::sync::OnceCell;
@@ -18,7 +18,7 @@ use proxmox_shared_memory::*;
 
 #[derive(Debug)]
 #[repr(C)]
-struct ConfigVersionCacheData {
+struct ConfigVersionCacheDataInner {
     magic: [u8; 8],
     // User (user.cfg) cache generation/version.
     user_cache_generation: AtomicUsize,
@@ -27,11 +27,35 @@ struct ConfigVersionCacheData {
     // datastore (datastore.cfg) generation/version
     datastore_generation: AtomicUsize,
 
-    // Add further atomics here (and reduce padding size)
-
-    padding: [u8; 4096 - 4*8],
+    // Add further atomics here
 }
 
+#[repr(C)]
+union ConfigVersionCacheData {
+    data: ManuallyDrop<ConfigVersionCacheDataInner>,
+    _padding: [u8; 4096],
+}
+
+#[test]
+fn assert_cache_size() {
+    assert_eq!(std::mem::size_of::<ConfigVersionCacheData>(), 4096);
+}
+
+impl std::ops::Deref for ConfigVersionCacheData {
+    type Target = ConfigVersionCacheDataInner;
+
+    #[inline]
+    fn deref(&self) -> &ConfigVersionCacheDataInner {
+        unsafe { &self.data }
+    }
+}
+
+impl std::ops::DerefMut for ConfigVersionCacheData {
+    #[inline]
+    fn deref_mut(&mut self) -> &mut ConfigVersionCacheDataInner {
+        unsafe { &mut self.data }
+    }
+}
 
 impl Init for ConfigVersionCacheData {
     fn initialize(this: &mut MaybeUninit<Self>) {
-- 
2.30.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pbs-devel] applied: [PATCH backup] config: don't manually track padding size
  2022-03-03 14:00 [pbs-devel] [PATCH backup] config: don't manually track padding size Wolfgang Bumiller
@ 2022-03-10 10:15 ` Thomas Lamprecht
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Lamprecht @ 2022-03-10 10:15 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Wolfgang Bumiller

On 03.03.22 15:00, Wolfgang Bumiller wrote:
> make ConfigVersionCacheData a #[repr(C)] union to fix its
> size and let it transparently `Deref{,Mut}` to its actual
> contents
> 
> Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
> ---
>  pbs-config/src/config_version_cache.rs | 34 ++++++++++++++++++++++----
>  1 file changed, 29 insertions(+), 5 deletions(-)
> 
>

applied, thanks!




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-10 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03 14:00 [pbs-devel] [PATCH backup] config: don't manually track padding size Wolfgang Bumiller
2022-03-10 10:15 ` [pbs-devel] applied: " Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal