Skip to content
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

Add "MemoryStrategy" to Executor with dep. injection #56

Merged
merged 6 commits into from
Jul 15, 2015

Conversation

jacquelinekay
Copy link
Contributor

This pull request introduces the MemoryStrategy class to delegate memory management on the execution path of Executor.

By default, MemoryStrategy is stateless and handles all delegated functions with dynamic memory management (malloc, free, new), but all functions are overrideable. An example subclass of MemoryStrategy, StaticMemoryStrategy, shows how these functions can be overridden and state can be added to implement fully static memory management.

The following functions are delegated to MemoryStrategy:

borrow_handles: returns a pointer to memory reserved for subscription, service, client or guard condition handles.
return_handles: cleans up memory instantiated in borrow_handles, by deleting it or resetting to NULL.
instantiate_next_executable: instantiates the next AnyExecutable object and passes it as a shared pointer.

In order to manage circular dependencies, I moved the AnyExecutable struct from a protected member of Executor to its own header file.

An example of how the memory strategy can be specified dynamically is in the examples/memory_dependency_injection:
https://github.com/ros2/examples/blob/memory_dependency_injection/rclcpp_examples/src/topics/listener_exec.cpp

@jacquelinekay jacquelinekay added the in progress Actively being worked on (Kanban column) label Jul 13, 2015
@jacquelinekay jacquelinekay self-assigned this Jul 13, 2015
@jacquelinekay jacquelinekay added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Jul 13, 2015
{
namespace executor
{
struct AnyExecutable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the RCLCPP_MAKE_SHARED_DEFINITIONS macro here to add the static function make_shared to the AnyExecutable class.

@wjwwood
Copy link
Member

wjwwood commented Jul 13, 2015

+1

@jacquelinekay
Copy link
Contributor Author

Updates based on William's comments in 541385a:

use RCLCPP_MAKE_SHARED for shared pointer typedefs for new classes
namespace/struct newline placement
change map to unordered_map and reserve memory in constructor of StaticMemoryAllocator

@@ -91,7 +98,7 @@ class Executor
{
this->add_node(node);
// non-blocking = true
std::shared_ptr<AnyExecutable> any_exec = get_next_executable(nonblocking);
AnyExecutable::SharedPtr any_exec = get_next_executable(nonblocking);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use auto.

@wjwwood
Copy link
Member

wjwwood commented Jul 13, 2015

Thanks @jacquelinekay, 541385a lgtm.

size_t _pool_seq;
size_t _exec_seq;

std::unordered_map<void *, size_t> _memory_map;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All those member variables should have a trailing underscore and no leading underscore.


void ** borrow_handles(HandleType type, size_t number_of_handles)
{
switch (type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the code check that number_of_handles is within the preallocated limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, definitely. Done in e751530.

@dirk-thomas
Copy link
Member

LGTM.

@jacquelinekay
Copy link
Contributor Author

@tfoote Let me know if you're planning to take a look at this PR, otherwise I will merge it soon (next few days).

@tfoote
Copy link
Contributor

tfoote commented Jul 15, 2015

+1

return std::malloc(size);
}

virtual void free(void * ptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to name this dealloc, just to mirror alloc.

@esteve
Copy link
Member

esteve commented Jul 15, 2015

+1

jacquelinekay added a commit that referenced this pull request Jul 15, 2015
Add "MemoryStrategy" to Executor with dep. injection
@jacquelinekay jacquelinekay merged commit 5a426d3 into master Jul 15, 2015
@jacquelinekay jacquelinekay removed the in review Waiting for review (Kanban column) label Jul 15, 2015
@jacquelinekay jacquelinekay deleted the memory_dependency_injection branch July 15, 2015 16:47
mauropasse pushed a commit to mauropasse/rclcpp that referenced this pull request Apr 1, 2021
nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
* ros2GH-112 Open storage for reading handing in rmw_identifier

* ros2GH-113 Cleanup: better naming

* ros2GH-113 Introduce interface for StorageFactory to allow mocks in tests

* ros2GH-113 Add test for SequentialReader for using converters

- Added mocks for storage and converters (and factories)

* ros2GH-113 Implement skeleton convert function

- Use convert only if necessary (different input and output formats),
  converters are only loaded if really necessary.
- Allocate_ros2_message is public to enable extensive tests for this function.
- Helper function to get any typesupport by name
- Helper function for empty ros2_message

* ros2GH-113 Implement allocate_ros2_message

- Treats most messages already.
- Some combinations of nested messages with arrays are still missing
- Cleanup of DynamicArrayNested messages is failing
- Main difficulty is the cleanup of the allocated ros2_message which
needs to be done manually
- The test_ros2_message is intended to be run with valgrind and there
should be no leaks or problems with free!

* ros2GH-113 Fix DynamicArrayNested deallocation

Swapping with empty container seems more stable than deleting the data
pointer of the container.

* ros2GH-113 Add test for BoundedArrayNested deallocation

* ros2GH-113 Refactoring of deallocation code

* ros2GH-113 Fix string initialization in all types

* ros2GH-113 Fix vector<bool> initialization

* ros2GH-113 Add test for deallocation of topic name + Refactoring

* ros2GH-113 Minor refactoring of converter

* ros2GH-113 Make sure to throw an error if converters do not exist

* ros2GH-113 Delete superfluous imports

* ros2GH-113 Small fix for deleting vectors

* ros2GH-113 Fix build after rebase

* ros2GH-30 Minor refactoring

- The TODO comments have been removed because they're no longer relevant: they have been discussed in the PR review

* ros2GH-30 Give an allocator as parameter to allocate_ros2_message()

* ros2GH-111 Add missing test dependencies for CDR converter test

* ros2GH-128 Extend message allocation test to also cover big strings

- Big strings are not treated with small string optimization and need
  to be checked, too.

* ros2GH-128 Add tests for nested arrays

* ros2GH-128 always initialize vectors with a placement new

* pass by ref

* use new getter functions

* consistent function naming

*  uncrustify

* ros2GH-30 Fix windows build

* use visibility macros on all functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants