-
Notifications
You must be signed in to change notification settings - Fork 117
feat: ability to override the default allocator #508
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
base: rolling
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,21 @@ RCUTILS_WARN_UNUSED | |
| rcutils_allocator_t | ||
| rcutils_get_default_allocator(void); | ||
|
|
||
| /// Override the default allocator returned by rcutils_get_default_allocator. | ||
| /** | ||
| * Attribute | Adherence | ||
| * ------------------ | ------------- | ||
| * Allocates Memory | No | ||
| * Thread-Safe | No | ||
| * Uses Atomics | No | ||
| * Lock-Free | Yes | ||
|
Comment on lines
+116
to
+118
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit concerned about having this be non-thread-safe, because it's unlikely that calling code will have the ability to synchronize with other calling threads and/or with other threads reading the value. My guess is that for this to be useful, it needs to be thread-safe and that to do so, using atomics and no locks (assuming the atomics are lock-free) would be ideal. |
||
| * | ||
| * \param[in] override_allocator The allocator to set as the default. | ||
| */ | ||
| RCUTILS_PUBLIC | ||
| void | ||
| rcutils_set_default_allocator(rcutils_allocator_t override_allocator); | ||
|
|
||
| /// Return true if the given allocator has non-null function pointers. | ||
| /** | ||
| * \param[in] allocator to be checked by the function | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,16 +69,31 @@ rcutils_get_zero_initialized_allocator(void) | |
| return zero_allocator; | ||
| } | ||
|
|
||
| static rcutils_allocator_t rcutils_override_default_allocator = {0}; | ||
|
|
||
| void | ||
| rcutils_set_default_allocator(rcutils_allocator_t override_allocator) | ||
| { | ||
| if (rcutils_allocator_is_valid(&override_allocator)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this check on the setter, is it impossible to reset the default allocator to be the "real default"? I would imagine, you could do something like |
||
| rcutils_override_default_allocator = override_allocator; | ||
| } | ||
| } | ||
|
|
||
| rcutils_allocator_t | ||
| rcutils_get_default_allocator(void) | ||
| { | ||
| if (rcutils_allocator_is_valid(&rcutils_override_default_allocator)) { | ||
| return rcutils_override_default_allocator; | ||
| } | ||
|
Comment on lines
+85
to
+87
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Three concerns here:
|
||
|
|
||
| static rcutils_allocator_t default_allocator = { | ||
| .allocate = __default_allocate, | ||
| .deallocate = __default_deallocate, | ||
| .reallocate = __default_reallocate, | ||
| .zero_allocate = __default_zero_allocate, | ||
| .state = NULL, | ||
| }; | ||
|
|
||
| return default_allocator; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.