Skip to content

Commit fb2dca6

Browse files
Enforce power-of-two for history setting in CMakeLists (#271)
* STREAM_HISTORY option requires power-of-two value Signed-off-by: gavanderhoorn <g.a.vanderhoorn@tudelft.nl> * Also mention power-of-two requirement in readme Signed-off-by: gavanderhoorn <g.a.vanderhoorn@tudelft.nl> * cmake: enforce power-of-two for history setting Signed-off-by: gavanderhoorn <g.a.vanderhoorn@tudelft.nl> Signed-off-by: gavanderhoorn <g.a.vanderhoorn@tudelft.nl>
1 parent a2a7484 commit fb2dca6

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ All the configurable parameters are:
5151
| 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 |
5252
| 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 |
5353
| RMW_UXRCE_PUBLISH_RELIABLE_TIMEOUT | This value sets the default time to wait for a publication in a </br> reliable mode in milliseconds. | 1000 |
54-
| RMW_UXRCE_STREAM_HISTORY | This value sets the number of MTUs to buffer, both input and output. | 4 |
55-
| 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. | - |
56-
| 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. | - |
54+
| RMW_UXRCE_STREAM_HISTORY | This value sets the number of MTUs to buffer, both input and output. Must be a power-of-two. | 4 |
55+
| 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. | - |
56+
| 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. | - |
5757
| RMW_UXRCE_GRAPH | Allows to perform graph-related operations to the user | OFF |
5858
| RMW_UXRCE_ALLOW_DYNAMIC_ALLOCATIONS | Enables increasing static pools with dynamic allocation when needed. | OFF |
5959

rmw_microxrcedds_c/CMakeLists.txt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,44 @@ if(RMW_UXRCE_ENTITY_DESTROY_TIMEOUT STREQUAL "")
8484
set(RMW_UXRCE_ENTITY_DESTROY_TIMEOUT ${RMW_UXRCE_ENTITY_CREATION_DESTROY_TIMEOUT})
8585
endif()
8686

87-
set(RMW_UXRCE_STREAM_HISTORY "4" CACHE STRING "This value sets the number of MTUs to buffer, both input and output.")
87+
set(RMW_UXRCE_STREAM_HISTORY "4" CACHE STRING
88+
"This value sets the number of MTUs to buffer, both input and output. Must be a power of two.")
8889
set(RMW_UXRCE_STREAM_HISTORY_INPUT "" CACHE STRING
89-
"This value sets the number of MTUs to input buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_OUTPUT is blank.")
90+
"This value sets the number of MTUs to input buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_OUTPUT is blank. \
91+
If set, must be a power of two.")
9092
set(RMW_UXRCE_STREAM_HISTORY_OUTPUT "" CACHE STRING
91-
"This value sets the number of MTUs to output buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_INPUT is blank.")
93+
"This value sets the number of MTUs to output buffer. It will be ignored if RMW_UXRCE_STREAM_HISTORY_INPUT is blank. \
94+
If set, must be a power of two.")
9295

9396
if(RMW_UXRCE_STREAM_HISTORY_INPUT STREQUAL "" OR RMW_UXRCE_STREAM_HISTORY_OUTPUT STREQUAL "")
9497
unset(RMW_UXRCE_STREAM_HISTORY_INPUT)
9598
unset(RMW_UXRCE_STREAM_HISTORY_OUTPUT)
99+
100+
# check power-of-two requirement
101+
# from: http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
102+
math(EXPR __RMW_UXRCE_STREAM_HISTORY_CHECK "${RMW_UXRCE_STREAM_HISTORY} & (${RMW_UXRCE_STREAM_HISTORY} - 1)")
103+
if(${__RMW_UXRCE_STREAM_HISTORY_CHECK} GREATER 0)
104+
message(FATAL_ERROR "STREAM_HISTORY must be a power-of-two (not: ${RMW_UXRCE_STREAM_HISTORY})")
105+
endif()
106+
unset(__RMW_UXRCE_STREAM_HISTORY_CHECK)
107+
96108
else()
97109
unset(RMW_UXRCE_STREAM_HISTORY)
110+
111+
# check power-of-two requirement
112+
# from: http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
113+
math(EXPR __RMW_UXRCE_STREAM_HISTORY_INPUT_CHECK "${RMW_UXRCE_STREAM_HISTORY_INPUT} & (${RMW_UXRCE_STREAM_HISTORY_INPUT} - 1)")
114+
math(EXPR __RMW_UXRCE_STREAM_HISTORY_OUTPUT_CHECK "${RMW_UXRCE_STREAM_HISTORY_OUTPUT} & (${RMW_UXRCE_STREAM_HISTORY_OUTPUT} - 1)")
115+
116+
if(${__RMW_UXRCE_STREAM_HISTORY_INPUT_CHECK} GREATER 0)
117+
message(FATAL_ERROR "STREAM_HISTORY_INPUT must be a power-of-two (not: ${RMW_UXRCE_STREAM_HISTORY_INPUT})")
118+
endif()
119+
if(${__RMW_UXRCE_STREAM_HISTORY_OUTPUT_CHECK} GREATER 0)
120+
message(FATAL_ERROR "STREAM_HISTORY_OUTPUT must be a power-of-two (not: ${RMW_UXRCE_STREAM_HISTORY_OUTPUT})")
121+
endif()
122+
123+
unset(__RMW_UXRCE_STREAM_HISTORY_INPUT_CHECK)
124+
unset(__RMW_UXRCE_STREAM_HISTORY_OUTPUT_CHECK)
98125
endif()
99126

100127
# Transport handle define macros.
@@ -402,4 +429,4 @@ if(BUILD_DOCUMENTATION)
402429
install(DIRECTORY ${PROJECT_BINARY_DIR}/doc/api_reference
403430
DESTINATION ${DOC_INSTALL_DIR}
404431
COMPONENT documentation)
405-
endif()
432+
endif()

0 commit comments

Comments
 (0)