Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/rcutils/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* \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
Expand Down
15 changes: 15 additions & 0 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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;
}

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;
}

Expand Down