Skip to content

Commit 20af346

Browse files
committed
refactor: simplify
1 parent 35f4bd5 commit 20af346

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datadog-alloc/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ version.workspace = true
88
rust-version.workspace = true
99
license.workspace = true
1010

11-
[features]
12-
1311
[dependencies]
1412
allocator-api2 = { version = "0.2", default-features = false }
1513

datadog-alloc/src/virtual_alloc.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,9 @@ mod tests {
215215

216216
#[test]
217217
fn test_zero_sized() {
218-
let alloc = VirtualAllocator;
219218
assert_eq!(0, core::mem::size_of::<VirtualAllocator>());
220219
let zero_sized_layout = Layout::new::<VirtualAllocator>();
221-
_ = alloc.allocate(zero_sized_layout).unwrap_err();
220+
_ = VirtualAllocator.allocate(zero_sized_layout).unwrap_err();
222221
}
223222

224223
#[test]
@@ -228,8 +227,7 @@ mod tests {
228227
let too_large_layout = Layout::from_size_align(1, too_large)
229228
.unwrap()
230229
.pad_to_align();
231-
let alloc = VirtualAllocator;
232-
_ = alloc.allocate(too_large_layout).unwrap_err();
230+
_ = VirtualAllocator.allocate(too_large_layout).unwrap_err();
233231
}
234232

235233
#[test]
@@ -266,9 +264,8 @@ mod tests {
266264
#[track_caller]
267265
fn realistic_size(size: usize) {
268266
let page_size = os::page_size().unwrap();
269-
let alloc = VirtualAllocator;
270267
let layout = Layout::from_size_align(size, page_size).unwrap();
271-
let wide_ptr = alloc.allocate(layout).unwrap();
268+
let wide_ptr = VirtualAllocator.allocate(layout).unwrap();
272269
let actual_size = wide_ptr.len();
273270

274271
// Should be a multiple of page size.
@@ -277,7 +274,7 @@ mod tests {
277274
// Shouldn't ever be smaller than what was asked for.
278275
assert!(actual_size >= size);
279276

280-
unsafe { alloc.deallocate(wide_ptr.cast(), layout) };
277+
unsafe { VirtualAllocator.deallocate(wide_ptr.cast(), layout) };
281278
}
282279

283280
#[test]

datadog-profiling-ffi/src/profiles/pprof_builder/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ where
187187
buffer,
188188
endpoints_stats: Default::default(),
189189
};
190-
unsafe { out_profile.write(Handle::from(encoded)) };
190+
let h = Handle::try_new(encoded).ok_or(ProfileError::other(
191+
"out of memory: failed to allocate handle for the EncodedProfile",
192+
))?;
193+
unsafe { out_profile.write(h) };
191194
Ok(())
192195
}())
193196
}

ddcommon-ffi/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ crossbeam-queue = "0.3.11"
2626
ddcommon = { path = "../ddcommon" }
2727
hyper = { version = "1.6", features = ["http1", "client"] }
2828
serde = "1.0"
29-
thiserror = "1"
3029

3130
[dev-dependencies]
3231
bolero = "0.13"

ddcommon-ffi/src/handle.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use ddcommon::error::FfiSafeErrorMessage;
5+
use std::ffi::CStr;
6+
use std::fmt::{Display, Formatter};
47
use std::ptr::null_mut;
58

69
/// Represents an object that should only be referred to by its handle.
@@ -11,6 +14,13 @@ pub struct Handle<T> {
1114
inner: *mut T,
1215
}
1316

17+
#[repr(C)]
18+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
19+
pub enum HandleError {
20+
OuterNullPtr,
21+
InnerNullPtr,
22+
}
23+
1424
impl<T> Handle<T> {
1525
pub fn empty() -> Self {
1626
Self { inner: null_mut() }
@@ -23,22 +33,29 @@ impl<T> Handle<T> {
2333
let inner = allocator_api2::boxed::Box::into_raw(uninit).cast();
2434
Some(Self { inner })
2535
}
36+
}
2637

27-
pub fn as_inner(&self) -> Result<&T, HandleError> {
28-
// SAFETY: the Handle owns the data.
29-
unsafe { self.inner.as_ref() }.ok_or(HandleError::InnerNullPtr)
38+
/// # Safety
39+
/// All cases use c-str literals to satisfy conditions.
40+
unsafe impl FfiSafeErrorMessage for HandleError {
41+
fn as_ffi_str(&self) -> &'static CStr {
42+
match self {
43+
HandleError::OuterNullPtr => c"handle is null",
44+
HandleError::InnerNullPtr => {
45+
c"handle's interior pointer is null, indicates use-after-free"
46+
}
47+
}
3048
}
3149
}
3250

33-
#[repr(C)]
34-
#[derive(Debug, thiserror::Error)]
35-
pub enum HandleError {
36-
#[error("handle is null")]
37-
OuterNullPtr,
38-
#[error("handle's interior pointer is null, indicates use-after-free")]
39-
InnerNullPtr,
51+
impl Display for HandleError {
52+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53+
self.as_rust_str().fmt(f)
54+
}
4055
}
4156

57+
impl core::error::Error for HandleError {}
58+
4259
pub trait ToInner<T> {
4360
/// # Safety
4461
/// The Handle must hold a valid `inner` which has been allocated and not freed.

0 commit comments

Comments
 (0)