-
Notifications
You must be signed in to change notification settings - Fork 16
feat(profiling-ffi): Profile_add2 & Profile_with_dictionary #1406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0520ea9
feat(profiling-ffi): ProfilesDictionary
morrisonlevi 49e4563
chore: add cbindgen renames for ProfilesDictionary FFI types
morrisonlevi adb785d
refactor: avoid unnecessary unsafe in StringRef -> StringId2
morrisonlevi 94d5e4d
docs: ArcHandle::as_inner safety
morrisonlevi a0b4576
refactor: ProfilesDictionary error messages
morrisonlevi 6289fee
refactor: use thiserror to shorten code, even though it duplicates er…
morrisonlevi f208cb6
feat(profiling-ffi): Profile_add2 & Profile_with_dictionary
morrisonlevi 0618837
fix: label.num_unit conversion
morrisonlevi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use crate::profile_error::ProfileError; | ||
| use crate::EmptyHandleError; | ||
| use libdd_profiling::profiles::collections::Arc; | ||
| use std::ptr::{null_mut, NonNull}; | ||
|
|
||
| /// Opaque FFI handle to an `Arc<T>`'s inner `T`. | ||
| /// | ||
| /// Safety rules for implementors/callers: | ||
| /// - Do not create multiple owning `Arc<T>`s from the same raw pointer. | ||
| /// - Always restore the original `Arc` with `into_raw` after any `from_raw`. | ||
| /// - Use `as_inner()` to validate non-null before performing raw round-trips. | ||
| /// | ||
| /// From Rust, use [`ArcHandle::try_clone`] to make a reference-counted copy. | ||
| /// From the C FFI, the handle should probably be renamed to avoid generics | ||
| /// bloat garbage, and a *_try_clone API should be provided. | ||
| /// | ||
| /// Use [`ArcHandle::drop_resource`] to drop the resource and move this handle | ||
| /// into the empty handle state, which is the default state. | ||
| #[repr(transparent)] | ||
| #[derive(Debug)] | ||
| pub struct ArcHandle<T>(*mut T); | ||
|
|
||
| impl<T> Default for ArcHandle<T> { | ||
| fn default() -> Self { | ||
| Self(null_mut()) | ||
| } | ||
| } | ||
|
|
||
| impl<T> ArcHandle<T> { | ||
| /// Constructs a new handle by allocating an `ArcHandle<T>` and returning | ||
| /// its inner pointer as a handle. | ||
| /// | ||
| /// Returns OutOfMemory on allocation failure. | ||
| pub fn new(value: T) -> Result<Self, ProfileError> { | ||
| let arc = Arc::try_new(value)?; | ||
| let ptr = Arc::into_raw(arc).as_ptr(); | ||
| Ok(Self(ptr)) | ||
| } | ||
|
|
||
| pub fn try_clone_into_arc(&self) -> Result<Arc<T>, ProfileError> { | ||
| let clone = self.try_clone()?; | ||
| // SAFETY: try_clone succeeded so it must not be null. | ||
| let nn = unsafe { NonNull::new_unchecked(clone.0) }; | ||
| // SAFETY: validated that it isn't null, should otherwise be an Arc. | ||
| Ok(unsafe { Arc::from_raw(nn) }) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn as_inner(&self) -> Result<&T, EmptyHandleError> { | ||
| // SAFETY: If non-null, self.0 was created from Arc and remains valid, | ||
| // at least as long as we can trust the C side to not do insane things. | ||
| unsafe { self.0.as_ref() }.ok_or(EmptyHandleError) | ||
| } | ||
|
|
||
| /// Tries to clone the resource this handle points to, and returns a new | ||
| /// handle to it. | ||
| pub fn try_clone(&self) -> Result<Self, ProfileError> { | ||
| let nn = NonNull::new(self.0).ok_or(EmptyHandleError)?; | ||
| // SAFETY: ArcHandle uses a pointer to T as its repr, and as long as | ||
| // callers have upheld safety requirements elsewhere, including the | ||
| // FFI, then there will be a valid object with refcount > 0. | ||
| unsafe { Arc::try_increment_count(nn.as_ptr())? }; | ||
| Ok(Self(self.0)) | ||
| } | ||
|
|
||
| /// Drops the resource that this handle refers to. It will remain alive if | ||
| /// there are other handles to the resource which were created by | ||
| /// successful calls to try_clone. This handle will now be empty and | ||
| /// operations on it will fail. | ||
| pub fn drop_resource(&mut self) { | ||
| // pointers aren't default until Rust 1.88. | ||
| let ptr = core::mem::replace(&mut self.0, null_mut()); | ||
| if let Some(nn) = NonNull::new(ptr) { | ||
| drop(unsafe { Arc::from_raw(nn) }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be a named constant to be clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or a helper function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You specifically mean the
0for the name? It was chosen specifically because environments like Java and sometimes .NET don't get to use the C macros--the detail needs to be concrete.But we can still add a constant, it just needs to be guaranteed to also be zero or else there could be issues.
To be honest, we could probably simplify this stuff. Rely on
nullto mean no error, and then use the lowest bit for "allocated or not".