Skip to content

Commit

Permalink
Enforce power-of-two for history setting in CMakeLists (#271)
Browse files Browse the repository at this point in the history
* STREAM_HISTORY option requires power-of-two value

Signed-off-by: gavanderhoorn <[email protected]>

* Also mention power-of-two requirement in readme

Signed-off-by: gavanderhoorn <[email protected]>

* cmake: enforce power-of-two for history setting

Signed-off-by: gavanderhoorn <[email protected]>

Signed-off-by: gavanderhoorn <[email protected]>
  • Loading branch information
gavanderhoorn authored Dec 12, 2022
1 parent a2a7484 commit fb2dca6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ All the configurable parameters are:
| RMW_UXRCE_ENTITY_CREATION_TIMEOUT | This value sets the maximum time to wait for an XRCE entity creation </br> in milliseconds. If set to 0 best effort is used. | 1000 |
| RMW_UXRCE_ENTITY_DESTROY_TIMEOUT | This value sets the maximum time to wait for an XRCE entity destroy </br> in milliseconds. If set to 0 best effort is used. | 1000 |
| RMW_UXRCE_PUBLISH_RELIABLE_TIMEOUT | This value sets the default time to wait for a publication in a </br> reliable mode in milliseconds. | 1000 |
| RMW_UXRCE_STREAM_HISTORY | This value sets the number of MTUs to buffer, both input and output. | 4 |
| RMW_UXRCE_STREAM_HISTORY_INPUT | This value sets the number of MTUs to input buffer. </br> It will be ignored if RMW_UXRCE_STREAM_HISTORY_OUTPUT is blank. | - |
| RMW_UXRCE_STREAM_HISTORY_OUTPUT | This value sets the number of MTUs to output buffer. </br> It will be ignored if RMW_UXRCE_STREAM_HISTORY_INPUT is blank. | - |
| RMW_UXRCE_STREAM_HISTORY | This value sets the number of MTUs to buffer, both input and output. Must be a power-of-two. | 4 |
| RMW_UXRCE_STREAM_HISTORY_INPUT | This value sets the number of MTUs to input buffer. </br> It will be ignored if RMW_UXRCE_STREAM_HISTORY_OUTPUT is blank. If set, must be a power-of-two. | - |
| RMW_UXRCE_STREAM_HISTORY_OUTPUT | This value sets the number of MTUs to output buffer. </br> It will be ignored if RMW_UXRCE_STREAM_HISTORY_INPUT is blank. If set, must be a power-of-two. | - |
| RMW_UXRCE_GRAPH | Allows to perform graph-related operations to the user | OFF |
| RMW_UXRCE_ALLOW_DYNAMIC_ALLOCATIONS | Enables increasing static pools with dynamic allocation when needed. | OFF |

Expand Down
35 changes: 31 additions & 4 deletions rmw_microxrcedds_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,44 @@ if(RMW_UXRCE_ENTITY_DESTROY_TIMEOUT STREQUAL "")
set(RMW_UXRCE_ENTITY_DESTROY_TIMEOUT ${RMW_UXRCE_ENTITY_CREATION_DESTROY_TIMEOUT})
endif()

set(RMW_UXRCE_STREAM_HISTORY "4" CACHE STRING "This value sets the number of MTUs to buffer, both input and output.")
set(RMW_UXRCE_STREAM_HISTORY "4" CACHE STRING
"This value sets the number of MTUs to buffer, both input and output. Must be a power of two.")
set(RMW_UXRCE_STREAM_HISTORY_INPUT "" CACHE STRING
"This value sets the number of MTUs to input buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_OUTPUT is blank.")
"This value sets the number of MTUs to input buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_OUTPUT is blank. \
If set, must be a power of two.")
set(RMW_UXRCE_STREAM_HISTORY_OUTPUT "" CACHE STRING
"This value sets the number of MTUs to output buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_INPUT is blank.")
"This value sets the number of MTUs to output buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_INPUT is blank. \
If set, must be a power of two.")

if(RMW_UXRCE_STREAM_HISTORY_INPUT STREQUAL "" OR RMW_UXRCE_STREAM_HISTORY_OUTPUT STREQUAL "")
unset(RMW_UXRCE_STREAM_HISTORY_INPUT)
unset(RMW_UXRCE_STREAM_HISTORY_OUTPUT)

# check power-of-two requirement
# from: http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
math(EXPR __RMW_UXRCE_STREAM_HISTORY_CHECK "${RMW_UXRCE_STREAM_HISTORY} & (${RMW_UXRCE_STREAM_HISTORY} - 1)")
if(${__RMW_UXRCE_STREAM_HISTORY_CHECK} GREATER 0)
message(FATAL_ERROR "STREAM_HISTORY must be a power-of-two (not: ${RMW_UXRCE_STREAM_HISTORY})")
endif()
unset(__RMW_UXRCE_STREAM_HISTORY_CHECK)

else()
unset(RMW_UXRCE_STREAM_HISTORY)

# check power-of-two requirement
# from: http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
math(EXPR __RMW_UXRCE_STREAM_HISTORY_INPUT_CHECK "${RMW_UXRCE_STREAM_HISTORY_INPUT} & (${RMW_UXRCE_STREAM_HISTORY_INPUT} - 1)")
math(EXPR __RMW_UXRCE_STREAM_HISTORY_OUTPUT_CHECK "${RMW_UXRCE_STREAM_HISTORY_OUTPUT} & (${RMW_UXRCE_STREAM_HISTORY_OUTPUT} - 1)")

if(${__RMW_UXRCE_STREAM_HISTORY_INPUT_CHECK} GREATER 0)
message(FATAL_ERROR "STREAM_HISTORY_INPUT must be a power-of-two (not: ${RMW_UXRCE_STREAM_HISTORY_INPUT})")
endif()
if(${__RMW_UXRCE_STREAM_HISTORY_OUTPUT_CHECK} GREATER 0)
message(FATAL_ERROR "STREAM_HISTORY_OUTPUT must be a power-of-two (not: ${RMW_UXRCE_STREAM_HISTORY_OUTPUT})")
endif()

unset(__RMW_UXRCE_STREAM_HISTORY_INPUT_CHECK)
unset(__RMW_UXRCE_STREAM_HISTORY_OUTPUT_CHECK)
endif()

# Transport handle define macros.
Expand Down Expand Up @@ -402,4 +429,4 @@ if(BUILD_DOCUMENTATION)
install(DIRECTORY ${PROJECT_BINARY_DIR}/doc/api_reference
DESTINATION ${DOC_INSTALL_DIR}
COMPONENT documentation)
endif()
endif()

0 comments on commit fb2dca6

Please sign in to comment.