Re: [PATCH v2] clarify usage expectations for cnt32_to_63()

Andrew Morton
2008-11-10T04:44:40Z

On Sun, 09 Nov 2008 23:20:00 -0500 (EST) Nicolas Pitre <nico@cam.org> wrote:

> On Sun, 9 Nov 2008, Mathieu Desnoyers wrote:
>
> > * Nicolas Pitre (nico@cam.org) wrote:
> > > Do you really have such instances where multiple call sites are needed?
> > > That sounds even more confusing to me than the current model. Better
> > > encapsulate the usage of this macro within some function which has a
> > > stronger meaning, such as sched_clock(), and call _that_ from multiple
> > > sites instead.
> >
> > I see a few reasons for it :
> >
> > - If we want to inline the whole read function so we don't pay the extra
> > runtime cost of a function call, this would become required.
>
> You can inline it as you want as long as it remains in the same .c file.
> The static variable is still shared amongst all call sites in that case.

Please don't rely upon deep compiler behaviour like that. It is
unobvious to the reader and it might break if someone uses it incorrectly,
or if the compiler implementation changes, or if a non-gcc compiler is
used, etc.

It is far better to make the management of the state explicit and at
the control of the caller. Get the caller to allocate the state and
pass its address into this function. Simple, clear, explicit and
robust.