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 78E2F623D5 for ; Thu, 20 Jan 2022 11:16:39 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6A19B23107 for ; Thu, 20 Jan 2022 11:16:09 +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 DF679230FD for ; Thu, 20 Jan 2022 11:16:08 +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 A843D44968 for ; Thu, 20 Jan 2022 11:16:08 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 20 Jan 2022 11:16:08 +0100 Message-Id: <20220120101608.1062152-1-d.csapak@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.161 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 Subject: [pbs-devel] [PATCH proxmox-backup] pbs-tools: LruCache: implement Drop 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, 20 Jan 2022 10:16:39 -0000 this fixes the leaked memory for the cache, as we had only pointers in the map/list which were freed, not the underlying chunks moves the 'clear' implementation out of the trait bounds so that Drop can reuse it this is used e.g. for file download from a pxar Signed-off-by: Dominik Csapak --- pbs-tools/src/lru_cache.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pbs-tools/src/lru_cache.rs b/pbs-tools/src/lru_cache.rs index b1c0c6ac..fbd7673a 100644 --- a/pbs-tools/src/lru_cache.rs +++ b/pbs-tools/src/lru_cache.rs @@ -100,9 +100,25 @@ pub struct LruCache { _marker: PhantomData>>, } +impl Drop for LruCache { + fn drop (&mut self) { + self.clear(); + } +} + // trivial: if our contents are Send, the whole cache is Send unsafe impl Send for LruCache {} +impl LruCache { + /// Clear all the entries from the cache. + pub fn clear(&mut self) { + // This frees only the HashMap with the node pointers. + self.map.clear(); + // This frees the actual nodes and resets the list head and tail. + self.list.clear(); + } +} + impl LruCache { /// Create LRU cache instance which holds up to `capacity` nodes at once. pub fn new(capacity: usize) -> Self { @@ -115,14 +131,6 @@ impl LruCache { } } - /// Clear all the entries from the cache. - pub fn clear(&mut self) { - // This frees only the HashMap with the node pointers. - self.map.clear(); - // This frees the actual nodes and resets the list head and tail. - self.list.clear(); - } - /// Insert or update an entry identified by `key` with the given `value`. /// This entry is placed as the most recently used node at the head. pub fn insert(&mut self, key: K, value: V) { -- 2.30.2