[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC] kern: simple futex for gnumach (version 11)

From: Diego Nieto Cid
Subject: Re: [RFC] kern: simple futex for gnumach (version 11)
Date: Thu, 9 Jan 2014 14:36:18 -0200


I don't really know about futexes so I will only comment on C language stuff.

2014/1/8 Marin Ramesa <mpr@hi.t-com.hr>
> +
> +static unsigned long futex_init(task_t task, vm_offset_t address, boolean_t 
> private_futex, struct futex *futex)
> +{
> +       unsigned long node_slot = 0;
> +
> +       futex = (struct futex *)kalloc(sizeof(*futex));

This only changes the value of the parameter. "futex" could very well
be a local variable instead.

If what you want is to update the value seen by the caller you should
declare futex as a "struct futex **" and then do something like this:

    *futex = (struct futex *)kalloc(sizeof(**futex));

> +void futex_wait(task_t task, vm_offset_t address, int value, int /* TODO Use 
> time_value_t */ msec, boolean_t private_futex)
> +{
> +       unsigned long node_slot = 0;
> +
> +       node_slot = futex_lookup_address(address);
> +       if (node_slot == 0) {
> +               if (private_futex) {
> +                       pfutexes = (struct futex *)kalloc(sizeof(pfutexes));
> +                       if (pfutexes == NULL)
> +                               return;
> +                       node_slot = futex_init(task, address, TRUE, 
> &pfutexes[ARRAY_SIZE(pfutexes) - 1]);

I don't really get this stuff.

First of all, you are allocating "sizeof(pfutexes)" and pfutexes is
defined as "struct futex *". Thus, you are just allocating memory for
a pointer (4 bytes in a 32-bit machine) and then casting it to a
pointer to "struct futex". So, if you dereference pfutexes to access
some member of the futex structure you will probably go past the end
of the allocated space (for the futex structure is bigger than a

Secondly, you are calling the macro ARRAY_SIZE with pfutexes as
parameter. This will be evaluated to 0. Here's why:

ARRAY_SIZE is defined as (sizeof(x) / sizeof(x[0])). When applied to
pfutexes, it results in sizeof(struct futex *) / sizeof(struct futex).
Since the numerator is less than the denominator, the integer division
evaluates to 0.

Finally, why is pfutexes an array? You are always using it's last
element (that is if the ARRAY_SIZE macro worked as intended).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]