Re: [PATCH 0/1] Constify struct address_space_operations for2.6.32-git-053fe57ac v2

From: Pavel Machek
Date: Tue Dec 15 2009 - 13:14:21 EST


Hi!

> Arjan van de Ven wrote:
> > On Mon, 14 Dec 2009 22:25:26 +0100
> > Pavel Machek <pavel@xxxxxx> wrote:
> >
> >> On Mon 2009-12-14 08:00:49, Arjan van de Ven wrote:
> >>> On Mon, 14 Dec 2009 12:26:56 +0100
> >>> Pavel Machek <pavel@xxxxxx> wrote:
> >> I certainly object "constify ops... as much as possible". If it
> >> uglifies the code, it should not be done. If it is as simple as adding
> >> const to few lines, its probably ok.
> >>
> >> But .... the patch contained huge load of
> >>
> >> - int (* resume)()
> >> + int (* const resume)()
> >>
> >> What is that?
> >
> > the ops stuct instantiation itself should be const.
> > the members not so much; that makes no sense.
>
> Consitfying the structure fields prevents direct modifications of runtime
> allocated ops structures therefore it gives a strong signal to the programmer
> that he's trying to do something undesired (this approach is in fact already
> used in the kernel, see: iwl_ops).

One const in structure declaration seems to be just enough, see:

const struct a {
void (* f)(void);
void (* const g)(void);
} s;

void h(void)
{
struct a *p = &s;
s.f = 0;
s.g = 0;
p->f = 0;
p->g = 0;
}


delme.c: In function 'h':
delme.c:8: warning: initialization discards qualifiers from pointer target type
delme.c:9: error: assignment of read-only variable 's'
delme.c:10: error: assignment of read-only variable 's'
delme.c:12: error: assignment of read-only member 'g'

You get clean-enough warnings.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/