Skip to content
Permalink
Browse files

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 <npitre@baylibre.com>
  • Loading branch information...
Nicolas Pitre authored and andrewboie committed May 21, 2019
1 parent 5f5c9a5 commit 6311766d9ab244846600353b1e40d741230a431e
@@ -145,7 +145,7 @@ void philosopher(void *id, void *unused1, void *unused2)
fork_t fork1;
fork_t fork2;

int my_id = (int)id;
int my_id = POINTER_TO_INT(id);

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

k_thread_create(&threads[i], &stacks[i][0], STACK_SIZE,
philosopher, (void *)i, NULL, NULL, prio,
K_USER, K_FOREVER);
philosopher, INT_TO_POINTER(i), NULL, NULL,
prio, K_USER, K_FOREVER);

k_object_access_grant(fork(i), &threads[i]);
k_object_access_grant(fork((i + 1) % NUM_PHIL), &threads[i]);
@@ -135,7 +135,7 @@ void philosopher(void const *id)
fork_t fork1;
fork_t fork2;

int my_id = (int)id;
int my_id = POINTER_TO_INT(id);

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

for (int i = 0; i < NUM_PHIL; i++) {
int prio = new_prio(i);
id = osThreadCreate(osThread(philosopher), (void *)i);
id = osThreadCreate(osThread(philosopher), INT_TO_POINTER(i));
osThreadSetPriority(id, prio);
}
}
@@ -171,7 +171,7 @@ void philosopher(void *id)
fork_t fork1;
fork_t fork2;

int my_id = (int)id;
int my_id = POINTER_TO_INT(id);

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

@@ -1156,9 +1156,9 @@ static void kill_handler(const struct shell *shell)
void shell_thread(void *shell_handle, void *arg_log_backend,
void *arg_log_level)
{
struct shell *shell = (struct shell *)shell_handle;
struct shell *shell = shell_handle;
bool log_backend = (bool)arg_log_backend;
u32_t log_level = (u32_t)arg_log_level;
u32_t log_level = POINTER_TO_UINT(arg_log_level);
int err;

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

shell->ctx->tid = tid;
@@ -42,7 +42,7 @@ void lifo_thread1(void *par1, void *par2, void *par3)
int element_a[2];
int element_b[2];
int *pelement;
int num_loops = (int) par2;
int num_loops = POINTER_TO_INT(par2);

ARG_UNUSED(par1);

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

for (i = 0; i < num_loops; i++) {
element[1] = i;
@@ -114,8 +114,8 @@ void lifo_thread3(void *par1, void *par2, void *par3)
int i;
int element[2];
int *pelement;
int *pcounter = (int *)par1;
int num_loops = (int) par2;
int *pcounter = par1;
int num_loops = POINTER_TO_INT(par2);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, lifo_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, lifo_thread2,
(void *) &i, (void *) number_of_loops, NULL,
&i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, lifo_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, lifo_thread3,
(void *) &i, (void *) number_of_loops, NULL,
&i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, lifo_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
for (i = 0; i < number_of_loops / 2U; i++) {
int element[2];
@@ -43,7 +43,7 @@ void fifo_thread1(void *par1, void *par2, void *par3)
int i;
int element[2];
int *pelement;
int num_loops = (int) par2;
int num_loops = POINTER_TO_INT(par2);

ARG_UNUSED(par1);
ARG_UNUSED(par3);
@@ -76,8 +76,8 @@ void fifo_thread2(void *par1, void *par2, void *par3)
int i;
int element[2];
int *pelement;
int *pcounter = (int *) par1;
int num_loops = (int) par2;
int *pcounter = par1;
int num_loops = POINTER_TO_INT(par2);

ARG_UNUSED(par3);

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

ARG_UNUSED(par3);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, fifo_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, fifo_thread2,
(void *) &i, (void *) number_of_loops, NULL,
&i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

i = 0;
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, fifo_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, fifo_thread3,
(void *) &i, (void *) number_of_loops, NULL,
&i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, fifo_thread1,
NULL, (void *) (number_of_loops / 2U), NULL,
NULL, INT_TO_POINTER(number_of_loops / 2U), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, fifo_thread1,
NULL, (void *) (number_of_loops / 2U), NULL,
NULL, INT_TO_POINTER(number_of_loops / 2U), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
for (i = 0; i < number_of_loops / 2U; i++) {
int element[2];
@@ -37,7 +37,7 @@ void sema_test_init(void)
void sema_thread1(void *par1, void *par2, void *par3)
{
int i;
int num_loops = (int) par2;
int num_loops = POINTER_TO_INT(par2);

ARG_UNUSED(par1);
ARG_UNUSED(par3);
@@ -63,7 +63,7 @@ void sema_thread2(void *par1, void *par2, void *par3)
{
int i;
int *pcounter = (int *)par1;
int num_loops = (int) par2;
int num_loops = POINTER_TO_INT(par2);

ARG_UNUSED(par3);

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

ARG_UNUSED(par3);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, sema_thread2,
(void *) &i, (void *) number_of_loops, NULL,
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, sema_thread3,
(void *) &i, (void *) number_of_loops, NULL,
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, sema_thread1,
NULL, (void *) number_of_loops, NULL,
NULL, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
for (i = 0; i < number_of_loops; i++) {
k_sem_give(&sem1);
@@ -41,7 +41,7 @@ void stack_test_init(void)
*/
void stack_thread1(void *par1, void *par2, void *par3)
{
int num_loops = ((int) par2 / 2);
int num_loops = POINTER_TO_INT(par2) / 2;
int i;
u32_t data;

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

ARG_UNUSED(par3);

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

ARG_UNUSED(par3);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, stack_thread1,
0, (void *) number_of_loops, NULL,
0, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, stack_thread2,
(void *) &i, (void *) number_of_loops, NULL,
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

i = 0;
k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, stack_thread1,
0, (void *) number_of_loops, NULL,
0, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);
k_thread_create(&thread_data2, thread_stack2, STACK_SIZE, stack_thread3,
(void *) &i, (void *) number_of_loops, NULL,
(void *) &i, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

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

k_thread_create(&thread_data1, thread_stack1, STACK_SIZE, stack_thread1,
0, (void *) number_of_loops, NULL,
0, INT_TO_POINTER(number_of_loops), NULL,
K_PRIO_COOP(3), 0, K_NO_WAIT);

for (i = 0; i < number_of_loops / 2U; i++) {
@@ -39,8 +39,11 @@ struct result result[N_THREADS];

struct k_fifo fifo;

static void errno_thread(int n, int my_errno)
static void errno_thread(void *_n, void *_my_errno, void *_unused)
{
int n = POINTER_TO_INT(_n);
int my_errno = POINTER_TO_INT(_my_errno);

errno = my_errno;

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

@@ -22,7 +22,7 @@ volatile u32_t sentinel;

static void offload_function(void *param)
{
u32_t x = (u32_t)param;
u32_t x = POINTER_TO_INT(param);

/* Make sure we're in IRQ context */
zassert_true(z_is_in_isr(), "Not in IRQ context!");
@@ -55,7 +55,8 @@ void test_timeout_order(void)

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

0 comments on commit 6311766

Please sign in to comment.
You can’t perform that action at this time.