Skip to content

Commit 6311766

Browse files
Nicolas Pitreandrewboie
Nicolas Pitre
authored andcommitted
pointer-type args: cast appropriately to be 64-bit compatible
Using void pointers as universal arguments is widely used. However, when compiling a 64-bit target, the compiler doesn't like when an int is converted to a pointer and vice versa despite the presence of a cast. This is due to a width mismatch between ints (32 bits) and pointers (64 bits). The trick is to cast to a widening integer type such as intptr_t and then cast to void*. When appropriate, the INT_TO_POINTER macro is used instead of this double cast to make things clearer. The converse with POINTER_TO_INT is also done which also serves as good code annotations. While at it, remove unneeded casts to specific pointer types from void* in the vicinity, and move to typed variable upon function entry to make the code cleaner. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent 5f5c9a5 commit 6311766

File tree

24 files changed

+120
-112
lines changed

24 files changed

+120
-112
lines changed

samples/philosophers/src/main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void philosopher(void *id, void *unused1, void *unused2)
145145
fork_t fork1;
146146
fork_t fork2;
147147

148-
int my_id = (int)id;
148+
int my_id = POINTER_TO_INT(id);
149149

150150
/* Djkstra's solution: always pick up the lowest numbered fork first */
151151
if (is_last_philosopher(my_id)) {
@@ -217,8 +217,8 @@ static void start_threads(void)
217217
int prio = new_prio(i);
218218

219219
k_thread_create(&threads[i], &stacks[i][0], STACK_SIZE,
220-
philosopher, (void *)i, NULL, NULL, prio,
221-
K_USER, K_FOREVER);
220+
philosopher, INT_TO_POINTER(i), NULL, NULL,
221+
prio, K_USER, K_FOREVER);
222222

223223
k_object_access_grant(fork(i), &threads[i]);
224224
k_object_access_grant(fork((i + 1) % NUM_PHIL), &threads[i]);

samples/portability/cmsis_rtos_v1/philosophers/src/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void philosopher(void const *id)
135135
fork_t fork1;
136136
fork_t fork2;
137137

138-
int my_id = (int)id;
138+
int my_id = POINTER_TO_INT(id);
139139

140140
/* Djkstra's solution: always pick up the lowest numbered fork first */
141141
if (is_last_philosopher(my_id)) {
@@ -186,7 +186,7 @@ static void start_threads(void)
186186

187187
for (int i = 0; i < NUM_PHIL; i++) {
188188
int prio = new_prio(i);
189-
id = osThreadCreate(osThread(philosopher), (void *)i);
189+
id = osThreadCreate(osThread(philosopher), INT_TO_POINTER(i));
190190
osThreadSetPriority(id, prio);
191191
}
192192
}

samples/portability/cmsis_rtos_v2/philosophers/src/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void philosopher(void *id)
171171
fork_t fork1;
172172
fork_t fork2;
173173

174-
int my_id = (int)id;
174+
int my_id = POINTER_TO_INT(id);
175175

176176
/* Djkstra's solution: always pick up the lowest numbered fork first */
177177
if (is_last_philosopher(my_id)) {
@@ -226,7 +226,7 @@ static void start_threads(void)
226226
for (int i = 0; i < NUM_PHIL; i++) {
227227
int prio = new_prio(i);
228228
thread_attr[i].priority = prio;
229-
osThreadNew(philosopher, (void *)i, &thread_attr[i]);
229+
osThreadNew(philosopher, INT_TO_POINTER(i), &thread_attr[i]);
230230
}
231231
}
232232

subsys/shell/shell.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1156,9 +1156,9 @@ static void kill_handler(const struct shell *shell)
11561156
void shell_thread(void *shell_handle, void *arg_log_backend,
11571157
void *arg_log_level)
11581158
{
1159-
struct shell *shell = (struct shell *)shell_handle;
1159+
struct shell *shell = shell_handle;
11601160
bool log_backend = (bool)arg_log_backend;
1161-
u32_t log_level = (u32_t)arg_log_level;
1161+
u32_t log_level = POINTER_TO_UINT(arg_log_level);
11621162
int err;
11631163

11641164
err = shell->iface->api->enable(shell->iface, false);
@@ -1220,7 +1220,7 @@ int shell_init(const struct shell *shell, const void *transport_config,
12201220
k_tid_t tid = k_thread_create(shell->thread,
12211221
shell->stack, CONFIG_SHELL_STACK_SIZE,
12221222
shell_thread, (void *)shell, (void *)log_backend,
1223-
(void *)init_log_level,
1223+
UINT_TO_POINTER(init_log_level),
12241224
K_LOWEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT);
12251225

12261226
shell->ctx->tid = tid;

tests/benchmarks/sys_kernel/src/lifo.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void lifo_thread1(void *par1, void *par2, void *par3)
4242
int element_a[2];
4343
int element_b[2];
4444
int *pelement;
45-
int num_loops = (int) par2;
45+
int num_loops = POINTER_TO_INT(par2);
4646

4747
ARG_UNUSED(par1);
4848

@@ -82,8 +82,8 @@ void lifo_thread2(void *par1, void *par2, void *par3)
8282
int i;
8383
int element[2];
8484
int *pelement;
85-
int *pcounter = (int *)par1;
86-
int num_loops = (int) par2;
85+
int *pcounter = par1;
86+
int num_loops = POINTER_TO_INT(par2);
8787

8888
for (i = 0; i < num_loops; i++) {
8989
element[1] = i;
@@ -114,8 +114,8 @@ void lifo_thread3(void *par1, void *par2, void *par3)
114114
int i;
115115
int element[2];
116116
int *pelement;
117-
int *pcounter = (int *)par1;
118-
int num_loops = (int) par2;
117+
int *pcounter = par1;
118+
int num_loops = POINTER_TO_INT(par2);
119119

120120
for (i = 0; i < num_loops; i++) {
121121
element[1] = i;
@@ -163,11 +163,11 @@ int lifo_test(void)
163163
t = BENCH_START();
164164

165165
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, lifo_thread1,
166-
NULL, (void *) number_of_loops, NULL,
166+
NULL, INT_TO_POINTER(number_of_loops), NULL,
167167
K_PRIO_COOP(3), 0, K_NO_WAIT);
168168

169169
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, lifo_thread2,
170-
(void *) &i, (void *) number_of_loops, NULL,
170+
&i, INT_TO_POINTER(number_of_loops), NULL,
171171
K_PRIO_COOP(3), 0, K_NO_WAIT);
172172

173173
t = TIME_STAMP_DELTA_GET(t);
@@ -197,11 +197,11 @@ int lifo_test(void)
197197
i = 0;
198198

199199
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, lifo_thread1,
200-
NULL, (void *) number_of_loops, NULL,
200+
NULL, INT_TO_POINTER(number_of_loops), NULL,
201201
K_PRIO_COOP(3), 0, K_NO_WAIT);
202202

203203
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, lifo_thread3,
204-
(void *) &i, (void *) number_of_loops, NULL,
204+
&i, INT_TO_POINTER(number_of_loops), NULL,
205205
K_PRIO_COOP(3), 0, K_NO_WAIT);
206206

207207
t = TIME_STAMP_DELTA_GET(t);
@@ -229,7 +229,7 @@ int lifo_test(void)
229229
t = BENCH_START();
230230

231231
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, lifo_thread1,
232-
NULL, (void *) number_of_loops, NULL,
232+
NULL, INT_TO_POINTER(number_of_loops), NULL,
233233
K_PRIO_COOP(3), 0, K_NO_WAIT);
234234
for (i = 0; i < number_of_loops / 2U; i++) {
235235
int element[2];

tests/benchmarks/sys_kernel/src/mwfifo.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void fifo_thread1(void *par1, void *par2, void *par3)
4343
int i;
4444
int element[2];
4545
int *pelement;
46-
int num_loops = (int) par2;
46+
int num_loops = POINTER_TO_INT(par2);
4747

4848
ARG_UNUSED(par1);
4949
ARG_UNUSED(par3);
@@ -76,8 +76,8 @@ void fifo_thread2(void *par1, void *par2, void *par3)
7676
int i;
7777
int element[2];
7878
int *pelement;
79-
int *pcounter = (int *) par1;
80-
int num_loops = (int) par2;
79+
int *pcounter = par1;
80+
int num_loops = POINTER_TO_INT(par2);
8181

8282
ARG_UNUSED(par3);
8383

@@ -111,8 +111,8 @@ void fifo_thread3(void *par1, void *par2, void *par3)
111111
int i;
112112
int element[2];
113113
int *pelement;
114-
int *pcounter = (int *)par1;
115-
int num_loops = (int) par2;
114+
int *pcounter = par1;
115+
int num_loops = POINTER_TO_INT(par2);
116116

117117
ARG_UNUSED(par3);
118118

@@ -163,10 +163,10 @@ int fifo_test(void)
163163
t = BENCH_START();
164164

165165
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, fifo_thread1,
166-
NULL, (void *) number_of_loops, NULL,
166+
NULL, INT_TO_POINTER(number_of_loops), NULL,
167167
K_PRIO_COOP(3), 0, K_NO_WAIT);
168168
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, fifo_thread2,
169-
(void *) &i, (void *) number_of_loops, NULL,
169+
&i, INT_TO_POINTER(number_of_loops), NULL,
170170
K_PRIO_COOP(3), 0, K_NO_WAIT);
171171

172172
t = TIME_STAMP_DELTA_GET(t);
@@ -195,10 +195,10 @@ int fifo_test(void)
195195

196196
i = 0;
197197
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, fifo_thread1,
198-
NULL, (void *) number_of_loops, NULL,
198+
NULL, INT_TO_POINTER(number_of_loops), NULL,
199199
K_PRIO_COOP(3), 0, K_NO_WAIT);
200200
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, fifo_thread3,
201-
(void *) &i, (void *) number_of_loops, NULL,
201+
&i, INT_TO_POINTER(number_of_loops), NULL,
202202
K_PRIO_COOP(3), 0, K_NO_WAIT);
203203

204204
t = TIME_STAMP_DELTA_GET(t);
@@ -226,10 +226,10 @@ int fifo_test(void)
226226
t = BENCH_START();
227227

228228
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, fifo_thread1,
229-
NULL, (void *) (number_of_loops / 2U), NULL,
229+
NULL, INT_TO_POINTER(number_of_loops / 2U), NULL,
230230
K_PRIO_COOP(3), 0, K_NO_WAIT);
231231
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, fifo_thread1,
232-
NULL, (void *) (number_of_loops / 2U), NULL,
232+
NULL, INT_TO_POINTER(number_of_loops / 2U), NULL,
233233
K_PRIO_COOP(3), 0, K_NO_WAIT);
234234
for (i = 0; i < number_of_loops / 2U; i++) {
235235
int element[2];

tests/benchmarks/sys_kernel/src/sema.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void sema_test_init(void)
3737
void sema_thread1(void *par1, void *par2, void *par3)
3838
{
3939
int i;
40-
int num_loops = (int) par2;
40+
int num_loops = POINTER_TO_INT(par2);
4141

4242
ARG_UNUSED(par1);
4343
ARG_UNUSED(par3);
@@ -63,7 +63,7 @@ void sema_thread2(void *par1, void *par2, void *par3)
6363
{
6464
int i;
6565
int *pcounter = (int *)par1;
66-
int num_loops = (int) par2;
66+
int num_loops = POINTER_TO_INT(par2);
6767

6868
ARG_UNUSED(par3);
6969

@@ -88,7 +88,7 @@ void sema_thread3(void *par1, void *par2, void *par3)
8888
{
8989
int i;
9090
int *pcounter = (int *)par1;
91-
int num_loops = (int) par2;
91+
int num_loops = POINTER_TO_INT(par2);
9292

9393
ARG_UNUSED(par3);
9494

@@ -127,10 +127,10 @@ int sema_test(void)
127127
t = BENCH_START();
128128

129129
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
130-
NULL, (void *) number_of_loops, NULL,
130+
NULL, INT_TO_POINTER(number_of_loops), NULL,
131131
K_PRIO_COOP(3), 0, K_NO_WAIT);
132132
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, sema_thread2,
133-
(void *) &i, (void *) number_of_loops, NULL,
133+
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
134134
K_PRIO_COOP(3), 0, K_NO_WAIT);
135135

136136
t = TIME_STAMP_DELTA_GET(t);
@@ -152,10 +152,10 @@ int sema_test(void)
152152
t = BENCH_START();
153153

154154
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
155-
NULL, (void *) number_of_loops, NULL,
155+
NULL, INT_TO_POINTER(number_of_loops), NULL,
156156
K_PRIO_COOP(3), 0, K_NO_WAIT);
157157
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, sema_thread3,
158-
(void *) &i, (void *) number_of_loops, NULL,
158+
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
159159
K_PRIO_COOP(3), 0, K_NO_WAIT);
160160

161161
t = TIME_STAMP_DELTA_GET(t);
@@ -177,7 +177,7 @@ int sema_test(void)
177177
t = BENCH_START();
178178

179179
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
180-
NULL, (void *) number_of_loops, NULL,
180+
NULL, INT_TO_POINTER(number_of_loops), NULL,
181181
K_PRIO_COOP(3), 0, K_NO_WAIT);
182182
for (i = 0; i < number_of_loops; i++) {
183183
k_sem_give(&sem1);

tests/benchmarks/sys_kernel/src/stack.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void stack_test_init(void)
4141
*/
4242
void stack_thread1(void *par1, void *par2, void *par3)
4343
{
44-
int num_loops = ((int) par2 / 2);
44+
int num_loops = POINTER_TO_INT(par2) / 2;
4545
int i;
4646
u32_t data;
4747

@@ -80,8 +80,8 @@ void stack_thread2(void *par1, void *par2, void *par3)
8080
{
8181
int i;
8282
u32_t data;
83-
int *pcounter = (int *)par1;
84-
int num_loops = (int) par2;
83+
int *pcounter = par1;
84+
int num_loops = POINTER_TO_INT(par2);
8585

8686
ARG_UNUSED(par3);
8787

@@ -112,8 +112,8 @@ void stack_thread3(void *par1, void *par2, void *par3)
112112
{
113113
int i;
114114
u32_t data;
115-
int *pcounter = (int *)par1;
116-
int num_loops = (int) par2;
115+
int *pcounter = par1;
116+
int num_loops = POINTER_TO_INT(par2);
117117

118118
ARG_UNUSED(par3);
119119

@@ -161,10 +161,10 @@ int stack_test(void)
161161
t = BENCH_START();
162162

163163
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, stack_thread1,
164-
0, (void *) number_of_loops, NULL,
164+
0, INT_TO_POINTER(number_of_loops), NULL,
165165
K_PRIO_COOP(3), 0, K_NO_WAIT);
166166
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, stack_thread2,
167-
(void *) &i, (void *) number_of_loops, NULL,
167+
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
168168
K_PRIO_COOP(3), 0, K_NO_WAIT);
169169

170170
t = TIME_STAMP_DELTA_GET(t);
@@ -188,10 +188,10 @@ int stack_test(void)
188188

189189
i = 0;
190190
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, stack_thread1,
191-
0, (void *) number_of_loops, NULL,
191+
0, INT_TO_POINTER(number_of_loops), NULL,
192192
K_PRIO_COOP(3), 0, K_NO_WAIT);
193193
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, stack_thread3,
194-
(void *) &i, (void *) number_of_loops, NULL,
194+
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
195195
K_PRIO_COOP(3), 0, K_NO_WAIT);
196196

197197
t = TIME_STAMP_DELTA_GET(t);
@@ -216,7 +216,7 @@ int stack_test(void)
216216
t = BENCH_START();
217217

218218
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, stack_thread1,
219-
0, (void *) number_of_loops, NULL,
219+
0, INT_TO_POINTER(number_of_loops), NULL,
220220
K_PRIO_COOP(3), 0, K_NO_WAIT);
221221

222222
for (i = 0; i < number_of_loops / 2U; i++) {

tests/kernel/common/src/errno.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ struct result result[N_THREADS];
3939

4040
struct k_fifo fifo;
4141

42-
static void errno_thread(int n, int my_errno)
42+
static void errno_thread(void *_n, void *_my_errno, void *_unused)
4343
{
44+
int n = POINTER_TO_INT(_n);
45+
int my_errno = POINTER_TO_INT(_my_errno);
46+
4447
errno = my_errno;
4548

4649
k_sleep(30 - (n * 10));
@@ -77,8 +80,8 @@ void test_thread_context(void)
7780
/**TESTPOINT: thread- threads stacks are separate */
7881
for (int ii = 0; ii < N_THREADS; ii++) {
7982
k_thread_create(&threads[ii], stacks[ii], STACK_SIZE,
80-
(k_thread_entry_t) errno_thread,
81-
(void *) ii, (void *) errno_values[ii], NULL,
83+
errno_thread, INT_TO_POINTER(ii),
84+
INT_TO_POINTER(errno_values[ii]), NULL,
8285
K_PRIO_PREEMPT(ii + 5), 0, K_NO_WAIT);
8386
}
8487

tests/kernel/common/src/irq_offload.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ volatile u32_t sentinel;
2222

2323
static void offload_function(void *param)
2424
{
25-
u32_t x = (u32_t)param;
25+
u32_t x = POINTER_TO_INT(param);
2626

2727
/* Make sure we're in IRQ context */
2828
zassert_true(z_is_in_isr(), "Not in IRQ context!");

tests/kernel/common/src/timeout_order.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ void test_timeout_order(void)
5555

5656
for (ii = 0; ii < NUM_TIMEOUTS; ii++) {
5757
(void)k_thread_create(&threads[ii], stacks[ii], STACKSIZE,
58-
thread, (void *)ii, 0, 0, prio, 0, 0);
58+
thread, INT_TO_POINTER(ii), 0, 0,
59+
prio, 0, 0);
5960
k_timer_init(&timer[ii], 0, 0);
6061
k_sem_init(&sem[ii], 0, 1);
6162
results[ii] = -1;

0 commit comments

Comments
 (0)