All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel]  [RFC] potential risk for macro QTAILQ_INSERT_BEFORE
@ 2011-09-28  3:38 Wayne Xia
  2011-09-28  8:43 ` Jan Kiszka
  0 siblings, 1 reply; 3+ messages in thread
From: Wayne Xia @ 2011-09-28  3:38 UTC (permalink / raw)
  To: qemu-devel

    Hi, during my coding, I found macro a bit different from other
QTAIL macros.

QTAILQ_INSERT_AFTER was defined as:
-----------------------------------------------------------
#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
                (elm)->field.tqe_next->field.tqe_prev =                 \
                    &(elm)->field.tqe_next;                             \
        else                                                            \
                (head)->tqh_last = &(elm)->field.tqe_next;              \
        (listelm)->field.tqe_next = (elm);                              \
        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
} while (/*CONSTCOND*/0)
---------------------------------------------------------
 QTAILQ_INSERT_BEFORE is defined as following:

#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {
        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
        (elm)->field.tqe_next = (listelm);                              \
        *(listelm)->field.tqe_prev = (elm);                             \
        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
} while (/*CONSTCOND*/0)
----------------------------------------------------------

    It did not take care of "head" as QTAILQ_INSERT_AFTER did, so I am
wondering what would happen if I use QTAILQ_INSERT_BEFORE to insert one
element to a queue that have only one element in it, would it happen
that the queue head pointer is not updated and the real first element
is lost? Currently some codes in qemu have used this macro.



-- 
Best Regards

Wayne Xia
mail:xiawenc@linux.vnet.ibm.com
tel:86-010-82450803

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [RFC] potential risk for macro QTAILQ_INSERT_BEFORE
  2011-09-28  3:38 [Qemu-devel] [RFC] potential risk for macro QTAILQ_INSERT_BEFORE Wayne Xia
@ 2011-09-28  8:43 ` Jan Kiszka
  2011-09-29  2:08   ` Wayne Xia
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Kiszka @ 2011-09-28  8:43 UTC (permalink / raw)
  To: Wayne Xia; +Cc: qemu-devel

On 2011-09-28 05:38, Wayne Xia wrote:
>     Hi, during my coding, I found macro a bit different from other
> QTAIL macros.
> 
> QTAILQ_INSERT_AFTER was defined as:
> -----------------------------------------------------------
> #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
>         if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
>                 (elm)->field.tqe_next->field.tqe_prev =                 \
>                     &(elm)->field.tqe_next;                             \
>         else                                                            \
>                 (head)->tqh_last = &(elm)->field.tqe_next;              \
>         (listelm)->field.tqe_next = (elm);                              \
>         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
> } while (/*CONSTCOND*/0)
> ---------------------------------------------------------
>  QTAILQ_INSERT_BEFORE is defined as following:
> 
> #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {
>         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
>         (elm)->field.tqe_next = (listelm);                              \
>         *(listelm)->field.tqe_prev = (elm);                             \
>         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
> } while (/*CONSTCOND*/0)
> ----------------------------------------------------------
> 
>     It did not take care of "head" as QTAILQ_INSERT_AFTER did, so I am
> wondering what would happen if I use QTAILQ_INSERT_BEFORE to insert one
> element to a queue that have only one element in it, would it happen
> that the queue head pointer is not updated and the real first element
> is lost? Currently some codes in qemu have used this macro.

The code is fine: a QTAILQ head consists of a dummy entry element that
looks for the first element as if a normal element would precede it. In
contrast, there is no dummy "end element", the last one just points to
NULL. Therefore we need to handle this separately.

Jan

-- 
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [RFC] potential risk for macro QTAILQ_INSERT_BEFORE
  2011-09-28  8:43 ` Jan Kiszka
@ 2011-09-29  2:08   ` Wayne Xia
  0 siblings, 0 replies; 3+ messages in thread
From: Wayne Xia @ 2011-09-29  2:08 UTC (permalink / raw)
  To: qemu-devel

于 2011-9-28 16:43, Jan Kiszka 写道:
> On 2011-09-28 05:38, Wayne Xia wrote:
>>      Hi, during my coding, I found macro a bit different from other
>> QTAIL macros.
>>
>> QTAILQ_INSERT_AFTER was defined as:
>> -----------------------------------------------------------
>> #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
>>          if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
>>                  (elm)->field.tqe_next->field.tqe_prev =                 \
>>                      &(elm)->field.tqe_next;                             \
>>          else                                                            \
>>                  (head)->tqh_last =&(elm)->field.tqe_next;              \
>>          (listelm)->field.tqe_next = (elm);                              \
>>          (elm)->field.tqe_prev =&(listelm)->field.tqe_next;             \
>> } while (/*CONSTCOND*/0)
>> ---------------------------------------------------------
>>   QTAILQ_INSERT_BEFORE is defined as following:
>>
>> #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {
>>          (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
>>          (elm)->field.tqe_next = (listelm);                              \
>>          *(listelm)->field.tqe_prev = (elm);                             \
>>          (listelm)->field.tqe_prev =&(elm)->field.tqe_next;             \
>> } while (/*CONSTCOND*/0)
>> ----------------------------------------------------------
>>
>>      It did not take care of "head" as QTAILQ_INSERT_AFTER did, so I am
>> wondering what would happen if I use QTAILQ_INSERT_BEFORE to insert one
>> element to a queue that have only one element in it, would it happen
>> that the queue head pointer is not updated and the real first element
>> is lost? Currently some codes in qemu have used this macro.
> 
> The code is fine: a QTAILQ head consists of a dummy entry element that
> looks for the first element as if a normal element would precede it. In
> contrast, there is no dummy "end element", the last one just points to
> NULL. Therefore we need to handle this separately.

Thanks Jan, made a test program showing the macro is OK, the dummy
element works like a magic.

> 
> Jan
> 


-- 
Best Regards

Wayne Xia
mail:xiawenc@linux.vnet.ibm.com
tel:86-010-82450803

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-09-29  2:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-28  3:38 [Qemu-devel] [RFC] potential risk for macro QTAILQ_INSERT_BEFORE Wayne Xia
2011-09-28  8:43 ` Jan Kiszka
2011-09-29  2:08   ` Wayne Xia

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.