From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 73CDA64658 for ; Thu, 3 Mar 2022 15:00:37 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4484032C67 for ; Thu, 3 Mar 2022 15:00:07 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4FBE932C5E for ; Thu, 3 Mar 2022 15:00:06 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 18B2846E98 for ; Thu, 3 Mar 2022 15:00:06 +0100 (CET) From: Wolfgang Bumiller To: pbs-devel@lists.proxmox.com Date: Thu, 3 Mar 2022 15:00:05 +0100 Message-Id: <20220303140005.167674-1-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.372 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [self.data] Subject: [pbs-devel] [PATCH backup] config: don't manually track padding size X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Mar 2022 14:00:37 -0000 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 --- 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, + _padding: [u8; 4096], +} + +#[test] +fn assert_cache_size() { + assert_eq!(std::mem::size_of::(), 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) { -- 2.30.2