-
Notifications
You must be signed in to change notification settings - Fork 436
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
Expose allocator interface to publishers and subscriptions #117
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f6481ee
Expose template in publisher/subscription create
9bf74c5
allocator_traits
2588c51
Use allocators to replace MessagePoolMemoryStrategy
28a1dab
Deleter template
6bfa7ed
Missing allocator_deleter
d20cd35
Add allocator_wrapper to clean up passing deleter
989d9f0
Uncrustify
e8834f9
remove message memory strategy
61eef06
remove references to message memory strategy
cf73f0b
Use construct
62003bd
add any subscription callback types for const
792d385
Remove shared_ptr references
449aa4f
Remove references from unique_ptrs
be2d7ad
Check for const ptr callback
7f33929
Merge branch 'const_shared_ptr' into rt_intra_process
874084a
Add check for type of second argument
1f4f618
Merge branch 'const_shared_ptr' into rt_intra_process
567e77c
Fix test mock
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
This file contains 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,56 @@ | ||
// Copyright 2015 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RCLCPP_RCLCPP_ALLOCATOR_DELETER_HPP_ | ||
#define RCLCPP_RCLCPP_ALLOCATOR_DELETER_HPP_ | ||
|
||
#include <memory> | ||
|
||
template<typename T, typename Allocator> | ||
class AllocatorDeleter | ||
{ | ||
public: | ||
AllocatorDeleter() | ||
: allocator_(new Allocator) | ||
{ | ||
} | ||
|
||
AllocatorDeleter(Allocator * a) | ||
: allocator_(a) | ||
{ | ||
} | ||
|
||
template<typename U, typename B> | ||
AllocatorDeleter(const AllocatorDeleter<U, B> & a) | ||
{ | ||
allocator_ = a.get_allocator(); | ||
} | ||
|
||
void operator()(T * ptr) | ||
{ | ||
std::allocator_traits<Allocator>::destroy(*allocator_, ptr); | ||
std::allocator_traits<Allocator>::deallocate(*allocator_, ptr, sizeof(T)); | ||
ptr = NULL; | ||
} | ||
|
||
Allocator * get_allocator() const | ||
{ | ||
return allocator_; | ||
} | ||
|
||
private: | ||
Allocator * allocator_; | ||
}; | ||
|
||
#endif |
This file contains 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,141 @@ | ||
// Copyright 2014 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RCLCPP_RCLCPP_ALLOCATOR_WRAPPER_HPP_ | ||
#define RCLCPP_RCLCPP_ALLOCATOR_WRAPPER_HPP_ | ||
|
||
#include <memory> | ||
#include <iostream> | ||
#include <rclcpp/allocator_deleter.hpp> | ||
|
||
|
||
template<typename Alloc, typename T, typename D> | ||
void initialize_deleter(D * deleter, Alloc * alloc) | ||
{ | ||
(void) deleter; | ||
(void) alloc; | ||
throw std::runtime_error("Reached unexpected template specialization"); | ||
} | ||
|
||
template<typename T> | ||
void initialize_deleter(std::default_delete<T> * deleter, std::allocator<T> * alloc) | ||
{ | ||
(void) alloc; | ||
deleter = new std::default_delete<T>; | ||
if (!deleter) { | ||
throw std::runtime_error("initialize_deleter failed"); | ||
} | ||
} | ||
|
||
template<typename Alloc, typename T> | ||
void initialize_deleter(AllocatorDeleter<T, Alloc> * deleter, Alloc * alloc) | ||
{ | ||
if (!alloc) { | ||
throw std::invalid_argument("Allocator argument was NULL"); | ||
} | ||
deleter = new AllocatorDeleter<T, Alloc>(alloc); | ||
if (!deleter) { | ||
throw std::runtime_error("initialize_deleter failed"); | ||
} | ||
} | ||
|
||
template<typename T, typename Alloc> | ||
class AllocatorWrapper | ||
{ | ||
public: | ||
using Deleter = typename std::conditional< | ||
std::is_same<Alloc, std::allocator<T>>::value, | ||
std::default_delete<T>, | ||
AllocatorDeleter<T, Alloc> | ||
>::type; | ||
|
||
AllocatorWrapper(Alloc * allocator) | ||
: allocator_(allocator) | ||
{ | ||
if (!allocator_) { | ||
throw std::invalid_argument("Allocator argument was NULL"); | ||
} | ||
initialize_deleter(deleter_, allocator_); | ||
if (deleter_ == NULL) { | ||
throw std::invalid_argument("Failed to initialize deleter"); | ||
} | ||
} | ||
|
||
AllocatorWrapper(Alloc * allocator, Deleter * deleter) | ||
: allocator_(allocator), deleter_(deleter) | ||
{ | ||
if (!allocator_) { | ||
throw std::invalid_argument("Allocator argument was NULL"); | ||
} | ||
if (!deleter_) { | ||
throw std::invalid_argument("Deleter argument was NULL"); | ||
} | ||
} | ||
|
||
AllocatorWrapper(Alloc & allocator) | ||
{ | ||
allocator_ = &allocator; | ||
if (!allocator_) { | ||
throw std::invalid_argument("Allocator argument was NULL"); | ||
} | ||
initialize_deleter(deleter_, allocator_); | ||
if (!deleter_) { | ||
throw std::invalid_argument("Failed to initialize deleter"); | ||
} | ||
} | ||
|
||
AllocatorWrapper() | ||
{ | ||
allocator_ = new Alloc(); | ||
initialize_deleter(deleter_, allocator_); | ||
if (!deleter_) { | ||
//throw std::invalid_argument("Failed to initialize deleter"); | ||
deleter_ = new Deleter; | ||
} | ||
} | ||
|
||
T * allocate(size_t size) | ||
{ | ||
return std::allocator_traits<Alloc>::allocate(*allocator_, size); | ||
} | ||
|
||
T * deallocate(void * pointer, size_t size) | ||
{ | ||
std::allocator_traits<Alloc>::deallocate(*allocator_, pointer, size); | ||
} | ||
|
||
template<class ... Args> | ||
void construct(T * pointer, Args && ... args) | ||
{ | ||
std::allocator_traits<Alloc>::construct(*allocator_, pointer, std::forward<Args>(args)...); | ||
} | ||
|
||
Deleter * get_deleter() const | ||
{ | ||
return deleter_; | ||
} | ||
Alloc * get_underlying_allocator() const | ||
{ | ||
return allocator_; | ||
} | ||
|
||
private: | ||
Alloc * allocator_; | ||
Deleter * deleter_; | ||
}; | ||
|
||
template<typename T> | ||
using DefaultAllocator = AllocatorWrapper<T, std::allocator<T>>; | ||
|
||
#endif |
This file contains 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 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 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.
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.
Can this be private / protected?