about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2024-08-16T11·11+0300
committerclbot <clbot@tvl.fyi>2024-08-19T11·50+0000
commit4d5abbe232b786359659dc1c3a717e1557fa4751 (patch)
tree46c9e0fc05697f3635d854924b83b8c5fe378171
parentef75a6300b5ae659b26587cbae58e2ff9ffb97bf (diff)
feat(tazjin/german-string): add Drop impl for transient strings r/8523
All of these strings are currently transient (the storage class is not yet
represented anywhere), and the ones that are heap allocated need to be
deallocated when the transient string dies.

Change-Id: Iba0ca926df5db7594f304c5d5318db9d69c6f42c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12235
Autosubmit: tazjin <tazjin@tvl.su>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
-rw-r--r--users/tazjin/german-string/src/lib.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/users/tazjin/german-string/src/lib.rs b/users/tazjin/german-string/src/lib.rs
index c35cb3ad4464..813cd5b1e515 100644
--- a/users/tazjin/german-string/src/lib.rs
+++ b/users/tazjin/german-string/src/lib.rs
@@ -65,4 +65,21 @@ impl GermanString {
             GermanString(GSRepr { large })
         }
     }
+
+    fn len(&self) -> usize {
+        // SAFETY: The length field is located in the same location for both
+        // variants, reading it from either is safe.
+        unsafe { self.0.small.len as usize }
+    }
+}
+
+impl Drop for GermanString {
+    fn drop(&mut self) {
+        if self.len() > 12 {
+            let layout = Layout::array::<u8>(self.len()).unwrap();
+            unsafe {
+                std::alloc::dealloc(self.0.large.data, layout);
+            }
+        }
+    }
 }