public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup] config: don't manually track padding size
Date: Thu,  3 Mar 2022 15:00:05 +0100	[thread overview]
Message-ID: <20220303140005.167674-1-w.bumiller@proxmox.com> (raw)

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





             reply	other threads:[~2022-03-03 14:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03 14:00 Wolfgang Bumiller [this message]
2022-03-10 10:15 ` [pbs-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220303140005.167674-1-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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