-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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) 3e133b2 afb72ef 0a53658 8d0acff 35ee9e3 * fix rmw_take_serialized() -> rmw_take_serialized_message() * use size_t (#139)
- Loading branch information
1 parent
8ea66db
commit 229ab5b
Showing
7 changed files
with
407 additions
and
0 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2018 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 RMW__SERIALIZED_MESSAGE_H_ | ||
#define RMW__SERIALIZED_MESSAGE_H_ | ||
|
||
#if __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
#include "rcutils/allocator.h" | ||
|
||
#include "rmw/macros.h" | ||
#include "rmw/types.h" | ||
#include "rmw/visibility_control.h" | ||
|
||
/// Return a zero initialized serialized message struct. | ||
/** | ||
* \return rmw_serialized_message_t a zero initialized serialized message struct | ||
*/ | ||
RMW_PUBLIC | ||
RMW_WARN_UNUSED | ||
rmw_serialized_message_t | ||
rmw_get_zero_initialized_serialized_message(void); | ||
|
||
/// Initialize a zero initialized serialized message struct. | ||
/** | ||
* This function may leak if the serialized message struct is already | ||
* pre-initialized. | ||
* If the capacity is set to 0, no memory is allocated and the internal buffer | ||
* is still NULL. | ||
* | ||
* \param msg a pointer to the to be initialized serialized message struct | ||
* \param buffer_capacity the size of the memory to allocate for the byte stream | ||
* \param allocator the allocator to use for the memory allocation | ||
* \return `RMW_RET_OK` if successful, or | ||
* \return `RMW_RET_ERROR` if an unexpected error occurs | ||
*/ | ||
RMW_PUBLIC | ||
RMW_WARN_UNUSED | ||
rmw_ret_t | ||
rmw_serialized_message_init( | ||
rmw_serialized_message_t * msg, | ||
size_t buffer_capacity, | ||
const rcutils_allocator_t * allocator); | ||
|
||
/// Finalize a serialized message struct. | ||
/** | ||
* Cleans up and deallocates any resources used in a rmw_message_serialized_t. | ||
* Passing a rmw_serialized_message_t which has not been zero initialized using | ||
* rmw_get_zero_initialized_serialized_message() to this function is undefined | ||
* behavior. | ||
* | ||
* \param msg pointer to the serialized message to be cleaned up | ||
* \return `RMW_RET_OK` if successful, or | ||
* \return `RMW_RET_BAD_ALLOC` if memory allocation failed, or | ||
* \return `RMW_RET_ERROR` if an unexpected error occurs | ||
*/ | ||
RMW_PUBLIC | ||
RMW_WARN_UNUSED | ||
rmw_ret_t | ||
rmw_serialized_message_fini(rmw_serialized_message_t * msg); | ||
|
||
/// Resize the internal buffer for the message byte stream. | ||
/** | ||
* The internal buffer of the serialized message can be resized dynamically if needed. | ||
* If the new size is smaller than the current capacity, then the memory is | ||
* truncated. | ||
* Be aware, that this will deallocate the memory and therefore invalidates any | ||
* pointers to this storage. | ||
* If the new size is larger, new memory is getting allocated and the existing | ||
* content is copied over. | ||
* | ||
* \param msg pointer to the instance of rmw_serialized_message_t which is being resized | ||
* \param new_size the new size of the internal buffer | ||
* \return `RMW_RET_OK` if successful, or | ||
* \return `RMW_RET_BAD_ALLOC` if memory allocation failed, or | ||
* \return `RMW_RET_ERROR` if an unexpected error occurs | ||
*/ | ||
RMW_PUBLIC | ||
RMW_WARN_UNUSED | ||
rmw_ret_t | ||
rmw_serialized_message_resize(rmw_serialized_message_t * msg, size_t new_size); | ||
|
||
#if __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // RMW__SERIALIZED_MESSAGE_H_ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2018 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. | ||
|
||
#include "rcutils/error_handling.h" | ||
|
||
#include "rmw/error_handling.h" | ||
#include "rmw/serialized_message.h" | ||
|
||
rmw_serialized_message_t | ||
rmw_get_zero_initialized_serialized_message(void) | ||
{ | ||
static rmw_serialized_message_t serialized_message = { | ||
.buffer = NULL, | ||
.buffer_length = 0u, | ||
.buffer_capacity = 0u | ||
}; | ||
serialized_message.allocator = rcutils_get_zero_initialized_allocator(); | ||
return serialized_message; | ||
} | ||
|
||
rmw_ret_t | ||
rmw_serialized_message_init( | ||
rmw_serialized_message_t * msg, | ||
size_t buffer_capacity, | ||
const rcutils_allocator_t * allocator) | ||
{ | ||
rcutils_allocator_t error_msg_allocator = rcutils_get_default_allocator(); | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
msg, "serialized message pointer is null", return RMW_RET_ERROR, error_msg_allocator); | ||
|
||
if (!rcutils_allocator_is_valid(allocator)) { | ||
RMW_SET_ERROR_MSG("serialized message has no valid allocator"); | ||
return RMW_RET_ERROR; | ||
} | ||
|
||
msg->buffer_length = 0; | ||
msg->buffer_capacity = buffer_capacity; | ||
msg->allocator = *allocator; | ||
|
||
if (buffer_capacity > 0u) { | ||
msg->buffer = (char *)allocator->allocate(buffer_capacity * sizeof(char), allocator->state); | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
msg->buffer, | ||
"failed to allocate memory for serialized message", | ||
return RMW_RET_BAD_ALLOC, | ||
*allocator); | ||
} | ||
|
||
return RMW_RET_OK; | ||
} | ||
|
||
rmw_ret_t | ||
rmw_serialized_message_fini(rmw_serialized_message_t * msg) | ||
{ | ||
rcutils_allocator_t error_msg_allocator = rcutils_get_default_allocator(); | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
msg, "serialized message pointer is null", return RMW_RET_ERROR, error_msg_allocator); | ||
|
||
rcutils_allocator_t * allocator = &msg->allocator; | ||
if (!rcutils_allocator_is_valid(allocator)) { | ||
RMW_SET_ERROR_MSG("serialized message has no valid allocator"); | ||
return RMW_RET_ERROR; | ||
} | ||
|
||
allocator->deallocate(msg->buffer, allocator->state); | ||
msg->buffer = NULL; | ||
msg->buffer_length = 0u; | ||
msg->buffer_capacity = 0u; | ||
|
||
return RMW_RET_OK; | ||
} | ||
|
||
rmw_ret_t | ||
rmw_serialized_message_resize(rmw_serialized_message_t * msg, size_t new_size) | ||
{ | ||
rcutils_allocator_t error_msg_allocator = rcutils_get_default_allocator(); | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
msg, "serialized message pointer is null", return RMW_RET_ERROR, error_msg_allocator); | ||
|
||
rcutils_allocator_t * allocator = &msg->allocator; | ||
if (!rcutils_allocator_is_valid(allocator)) { | ||
RMW_SET_ERROR_MSG("serialized message has no valid allocator"); | ||
return RMW_RET_ERROR; | ||
} | ||
|
||
if (new_size == msg->buffer_capacity) { | ||
// nothing to do here | ||
return RMW_RET_OK; | ||
} | ||
|
||
msg->buffer = rcutils_reallocf(msg->buffer, new_size * sizeof(char), allocator); | ||
RCUTILS_CHECK_FOR_NULL_WITH_MSG( | ||
msg->buffer, | ||
"failed to reallocate memory for serialized message", | ||
return RMW_RET_BAD_ALLOC, | ||
*allocator); | ||
|
||
msg->buffer_capacity = new_size; | ||
if (new_size < msg->buffer_length) { | ||
msg->buffer_length = new_size; | ||
} | ||
|
||
return RMW_RET_OK; | ||
} |
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.