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

*_raw function #125

Merged
merged 13 commits into from
Jun 16, 2018
Merged

*_raw function #125

merged 13 commits into from
Jun 16, 2018

Conversation

Karsten1987
Copy link
Contributor

@Karsten1987 Karsten1987 commented Oct 23, 2017

Connects to ros2/demos#185

@Karsten1987 Karsten1987 added the in progress Actively being worked on (Kanban column) label Oct 23, 2017
@Karsten1987 Karsten1987 self-assigned this Oct 23, 2017
@Karsten1987 Karsten1987 changed the title publish_raw function *_raw function Oct 23, 2017
Copy link
Member

@wjwwood wjwwood left a 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/include/rmw/types.h Outdated Show resolved Hide resolved
rmw/include/rmw/types.h Outdated Show resolved Hide resolved
rmw/include/rmw/rmw.h Outdated Show resolved Hide resolved
rmw/src/raw_message.c Outdated Show resolved Hide resolved
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;
Copy link
Contributor

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

{
msg->buffer = (char *)allocator->allocate(buffer_capacity * sizeof(char), allocator->state);
if (!msg->buffer) {
return RMW_RET_ERROR;
Copy link
Contributor

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

return RMW_RET_ERROR;
}
if (new_size < msg->buffer_capacity) {
memcpy(new_buffer, msg->buffer, new_size);
Copy link
Contributor

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?

return RMW_RET_OK;
}

char * new_buffer = allocator->allocate(new_size * sizeof(char), allocator->state);
Copy link
Contributor

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.

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);
Copy link
Contributor

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?

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);
Copy link
Contributor

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_ret_t
rmw_raw_message_init(
rmw_message_raw_t * msg,
unsigned int buffer_capacity,
Copy link
Member

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?

Copy link
Contributor Author

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)

unsigned int buffer_length;
unsigned int buffer_capacity;
rcutils_allocator_t allocator;
} rmw_message_raw_t;
Copy link
Member

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.

Copy link
Contributor Author

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?

#include "rmw/error_handling.h"
#include "rmw/raw_message.h"

RMW_PUBLIC
Copy link
Member

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_PUBLIC
rmw_ret_t
rmw_raw_message_resize(rmw_message_raw_t * msg, unsigned int new_size);
Copy link
Member

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?

return RMW_RET_OK;
}

msg->buffer = allocator->reallocate(msg->buffer, new_size * sizeof(char), allocator->state);
Copy link
Member

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

* \return rmw_message_raw_t a zero initialized raw message struct
*/
RMW_PUBLIC
rmw_message_raw_t
Copy link
Member

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:

#define RMW_WARN_UNUSED RCUTILS_WARN_UNUSED

@wjwwood
Copy link
Member

wjwwood commented Jun 14, 2018

@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 🙏.

@Karsten1987 Karsten1987 merged commit 229ab5b into master Jun 16, 2018
@dirk-thomas dirk-thomas removed the in progress Actively being worked on (Kanban column) label Jun 16, 2018
@Karsten1987 Karsten1987 deleted the expose_cdr branch June 16, 2018 08:37
@dirk-thomas
Copy link
Member

@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.

dabonnie pushed a commit to aws-ros-dev/rmw that referenced this pull request Apr 2, 2019
* 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 <dbbonnie@amazon.com>
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.

None yet

4 participants