Skip to content

Commit c80c906

Browse files
committed
Align fixes
1 parent 9d00078 commit c80c906

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

RTXOff/align.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Created by Luka on 12/16/2020.
3+
//
4+
5+
#ifndef MBED_BENCHTEST_ALIGN_H
6+
#define MBED_BENCHTEST_ALIGN_H
7+
8+
#if RTXOFF_USE_32BIT
9+
template <typename T>
10+
constexpr uint32_t align(T x) {
11+
return (x + 3U) & ~3UL;
12+
}
13+
14+
template<void *>
15+
constexpr bool is_aligned(void *x) {
16+
return (reinterpret_cast<uint64_t>(x) & 3U) == 0;
17+
}
18+
19+
template<typename T>
20+
constexpr bool is_aligned(T x) {
21+
return (x & 3U) == 0;
22+
}
23+
#else
24+
25+
template<typename T>
26+
constexpr inline uint64_t align(T x) {
27+
return (x + 7U) & ~7UL;
28+
}
29+
30+
static inline bool is_aligned_p(void *x) {
31+
return (reinterpret_cast<uint64_t>(x) & 7U) == 0;
32+
}
33+
34+
template<typename T>
35+
constexpr inline bool is_aligned(T x) {
36+
return (x & 7U) == 0;
37+
}
38+
39+
#endif
40+
41+
#endif //MBED_BENCHTEST_ALIGN_H

RTXOff/rtxoff_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "cmsis_os.h"
99
#include "rtxoff_os.h"
10+
#include "align.h"
1011

1112
#include <string>
1213
#include <iostream>

RTXOff/rtxoff_mempool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew(uint32_t block_count, uint32_t block
150150
return nullptr;
151151
}
152152
b_count = block_count;
153-
b_size = (block_size + 3U) & ~3UL;
153+
b_size = align(block_size);
154154
if ((__builtin_clz(b_count) + __builtin_clz(b_size)) < 32U) {
155155
// add event
156156
// EvrRtxMemoryPoolError(nullptr, (int32_t) osErrorParameter);
@@ -170,7 +170,7 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew(uint32_t block_count, uint32_t block
170170
mp_size = attr->mp_size;
171171
if (mp != nullptr) {
172172
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
173-
if ((((uint64_t) mp & 3U) != 0U) || (attr->cb_size < sizeof(osRtxMemoryPool_t))) {
173+
if (!is_aligned_p(mp) || (attr->cb_size < sizeof(osRtxMemoryPool_t))) {
174174
// add event
175175
// EvrRtxMemoryPoolError(nullptr, osRtxErrorInvalidControlBlock);
176176
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
@@ -186,7 +186,7 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew(uint32_t block_count, uint32_t block
186186
}
187187
if (mp_mem != nullptr) {
188188
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
189-
if ((((uint64_t) mp_mem & 3U) != 0U) || (mp_size < size)) {
189+
if (!is_aligned_p(mp_mem) || (mp_size < size)) {
190190
// add event
191191
// EvrRtxMemoryPoolError(nullptr, osRtxErrorInvalidDataMemory);
192192
//lint -e{904} "Return statement before end of function" [MISRA Note 1]

RTXOff/rtxoff_msgqueue.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,9 @@ osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, cons
192192
return nullptr;
193193
}
194194

195-
#if RTXOFF_USE_32BIT
196-
block_size = ((msg_size + 3U) & ~3UL) + sizeof(osRtxMessage_t);
197-
#else
198-
block_size = ((msg_size + 7U) & ~7UL) + sizeof(osRtxMessage_t);
199-
#endif
195+
// needs to hold at least a full pointer
196+
block_size = align(msg_size) + sizeof(osRtxMessage_t);
197+
200198
if ((__builtin_clz(msg_count) + __builtin_clz(block_size)) < 32U) {
201199

202200
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
@@ -215,7 +213,7 @@ osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, cons
215213
mq_size = attr->mq_size;
216214
if (mq != nullptr) {
217215
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
218-
if (((reinterpret_cast<uint64_t>(mq) & 3U) != 0U) || (attr->cb_size < sizeof(osRtxMessageQueue_t))) {
216+
if (!is_aligned_p(mq) || (attr->cb_size < sizeof(osRtxMessageQueue_t))) {
219217

220218
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
221219
return nullptr;
@@ -229,7 +227,7 @@ osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, cons
229227
}
230228
if (mq_mem != nullptr) {
231229
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
232-
if (((reinterpret_cast<uint64_t>(mq_mem) & 3U) != 0U) || (mq_size < size)) {
230+
if (!is_aligned_p(mq_mem) || (mq_size < size)) {
233231

234232
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
235233
return nullptr;

RTXOff/rtxoff_timer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, c
150150
timer = static_cast<osRtxTimer_t *>(attr->cb_mem);
151151
if (timer != nullptr) {
152152
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
153-
if ((((uint64_t) timer & 3U) != 0U) || (attr->cb_size < sizeof(osRtxTimer_t))) {
153+
if (!is_aligned_p(timer) || (attr->cb_size < sizeof(osRtxTimer_t))) {
154154
return nullptr;
155155
}
156156
} else {

tests/arm-cmsis-rtos-validator/Source/RV_Semaphore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ void TC_SemaphoreWaitTimeout (void) {
467467
break;
468468
}
469469
}
470-
ASSERT_TRUE (i <= 10); // QEMU max seen value was 8
470+
ASSERT_TRUE (i <= 12); // I encountered 11
471471

472472
if (evt.status == osEventSignal) {
473473
ASSERT_TRUE (evt.value.signals == 0x02);

0 commit comments

Comments
 (0)