-
Notifications
You must be signed in to change notification settings - Fork 70
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
*_raw function #125
*_raw function #125
Conversation
17cff09
to
ba32172
Compare
ba32172
to
5550e60
Compare
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.
bike shed: how attached are you to the _raw
name? I was thinking rmw_take_serialized
and rmw_publish_serialized
and rmw_serialized_message_t
, but I'm also not tied to that. I just think raw
doesn't describe what's going on very well, and we know what it means, but for a non-native speaker it's probably doesn't imply what's actually happening properly.
rmw/src/raw_message.c
Outdated
char * new_buffer = allocator->allocate(new_size * sizeof(char), allocator->state); | ||
if (!new_buffer) { | ||
RMW_SET_ERROR_MSG("failed to allocate memory for resizing raw message"); | ||
return RMW_RET_ERROR; |
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.
Could be RMW_RET_BAD_ALLOC
rmw/src/raw_message.c
Outdated
{ | ||
msg->buffer = (char *)allocator->allocate(buffer_capacity * sizeof(char), allocator->state); | ||
if (!msg->buffer) { | ||
return RMW_RET_ERROR; |
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.
Could be RMW_RET_BAD_ALLOC
rmw/src/raw_message.c
Outdated
return RMW_RET_ERROR; | ||
} | ||
if (new_size < msg->buffer_capacity) { | ||
memcpy(new_buffer, msg->buffer, new_size); |
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.
Do the bytes beyond msg->buffer_length
need to be copied?
rmw/src/raw_message.c
Outdated
return RMW_RET_OK; | ||
} | ||
|
||
char * new_buffer = allocator->allocate(new_size * sizeof(char), allocator->state); |
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.
The documentation for rcutils
allocator->reallocate()
says it behaves like std::realloc
, which I think means it copies the memory too. It could be used in place of this and the memcpy
calls below.
rmw/test/test_raw_message.cpp
Outdated
auto allocator = rcutils_get_default_allocator(); | ||
EXPECT_EQ(RMW_RET_OK, rmw_initialize_raw_message(&raw_msg, 0, &allocator)); | ||
EXPECT_EQ(0u, raw_msg.buffer_capacity); | ||
EXPECT_TRUE(raw_msg.buffer); |
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.
It doesn't appear to be safe to assume buffer
is not NULL
using the default allocator. The documentation for malloc()
says
If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage, but has to be passed to free).
How about making rmw_initialize_raw_message()
not call allocator->allocate()
if the size is zero, and instead expect the buffer to be NULL
?
rmw/test/test_raw_message.cpp
Outdated
TEST(test_raw_message, resize) { | ||
auto raw_msg = rmw_get_zero_initialized_raw_message(); | ||
auto allocator = rcutils_get_default_allocator(); | ||
rmw_initialize_raw_message(&raw_msg, 5, &allocator); |
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.
Recommend ASSERT_EQ(RMW_RET_OK, ...)
to prevent a crash if this call fails.
rmw/include/rmw/raw_message.h
Outdated
rmw_ret_t | ||
rmw_raw_message_init( | ||
rmw_message_raw_t * msg, | ||
unsigned int buffer_capacity, |
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.
Why is this not a size_t
?
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.
same discussion here: ros2/rclcpp#388 (comment)
rmw/include/rmw/types.h
Outdated
unsigned int buffer_length; | ||
unsigned int buffer_capacity; | ||
rcutils_allocator_t allocator; | ||
} rmw_message_raw_t; |
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.
Did you consider any other names, like rmw_raw_message_t
(which is a more natural sounding to me, i.e. adjective before noun), rmw_serialized_message_t
which is more technical sounding, or just rmw_message_t
?
It's a bit late to change it now, but on the other hand changing it later is going to potentially be painful.
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.
ok. I will address the style comments first, and then rename them.
the type: rmw_serialized_message_t
what do you propose for the function?
rmw_take_serialized_message
?
rmw/src/raw_message.c
Outdated
#include "rmw/error_handling.h" | ||
#include "rmw/raw_message.h" | ||
|
||
RMW_PUBLIC |
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.
Visibility macros should not be in the .c
files.
rmw/include/rmw/raw_message.h
Outdated
*/ | ||
RMW_PUBLIC | ||
rmw_ret_t | ||
rmw_raw_message_resize(rmw_message_raw_t * msg, unsigned int new_size); |
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.
Same here, why not size_t
?
rmw/src/raw_message.c
Outdated
return RMW_RET_OK; | ||
} | ||
|
||
msg->buffer = allocator->reallocate(msg->buffer, new_size * sizeof(char), allocator->state); |
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 is dangerous, use the reallocf
style function instead or address the potential memory leak directly, see: https://github.com/ros2/rcutils/blob/3595e7c0a1b815d7ff6aa7e3d2978361ab843bc0/include/rcutils/allocator.h#L128-L136
rmw/include/rmw/raw_message.h
Outdated
* \return rmw_message_raw_t a zero initialized raw message struct | ||
*/ | ||
RMW_PUBLIC | ||
rmw_message_raw_t |
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.
Use RMW_WARN_UNUSED
on all public functions you added here, see:
Line 22 in 8ea66db
#define RMW_WARN_UNUSED RCUTILS_WARN_UNUSED |
@Karsten1987 I added back my changes from last night in c044e2a, looks like they got force pushed over (not easy to recover since it was all in the GitHub webui), please pull before pushing in the future 🙏. |
@ros2/rmw_implementations FYI these PRs added new API to publish / take raw (not deserialized) messagesto the rmw interface. For FastRTPS and Connext the new API has been implemented in the linked PRs. For OpenSplice only an issue requesting further information has been filled upstream (ADLINK-IST/opensplice#50). Other RMW implementations should be updated to implement this new API in order to stay compatible. If you are maintaining a RMW impl but are not part of the mentioned team please feel free to reach out to be added to the team. That way you can stay in the loop for future changes. |
* publish_raw function * add rmw_take_raw * extract raw message * add rmw_serialize functions * linters * address comments * rename to rmw_raw_message_init * documentation for new functions * address comments * raw->serialized * documentation fixups (restored after being force pushed) ros2@3e133b2 ros2@afb72ef ros2@0a53658 ros2@8d0acff ros2@35ee9e3 * fix rmw_take_serialized() -> rmw_take_serialized_message() * use size_t (ros2#139) Signed-off-by: Devin Bonnie <[email protected]>
Connects to ros2/demos#185