Re: Why is the kfree() argument const?

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Andy Lutomirski
Date: Friday, January 18, 2008 - 6:53 am

Giacomo Catenazzi wrote:

I'd say this implies the exact opposite.  It almost sounds like the 
compiler is free to change:

void foo(const int *x);
foo(x);
printf("%d", x);

to:

void foo(const int *x);
printf("%d", x);
foo(x);

especially if it can prove that the pointer to x doesn't otherwise 
escape or that foo doesn't call anything that could see the pointer (and 
given that gcc has special magical markings for malloc, one way this 
could be "proven" is to have x be some freshly malloced object.

If foo is kfree, then the above transformation is clearly invalid.

(Note that this isn't just a problem for optimizers -- a programmer 
might expect that passing a pointer to a function that takes a const 
pointer argument does not, in and of itself, change the pointed-to 
value.  Given that const certainly does not mean that no one else 
changes the object, I'm not sure what else it could mean.  kfree does 
not have either property, so I'm don't think it makes sense for it to 
take a const argument.)

--Andy


--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
RE: Why is the kfree() argument const?, Linus Torvalds, (Thu Jan 17, 2:25 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Thu Jan 17, 3:28 pm)
RE: Why is the kfree() argument const?, Linus Torvalds, (Thu Jan 17, 4:10 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Thu Jan 17, 5:56 pm)
RE: Why is the kfree() argument const?, Linus Torvalds, (Thu Jan 17, 6:15 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Thu Jan 17, 10:02 pm)
Re: Why is the kfree() argument const?, Giacomo Catenazzi, (Fri Jan 18, 1:20 am)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 1:30 am)
Re: Why is the kfree() argument const?, Giacomo A. Catenazzi, (Fri Jan 18, 4:47 am)
Re: Why is the kfree() argument const?, Björn, (Fri Jan 18, 6:31 am)
Re: Why is the kfree() argument const?, Andy Lutomirski, (Fri Jan 18, 6:53 am)
Re: Why is the kfree() argument const?, Andy Lutomirski, (Fri Jan 18, 6:54 am)
Re: Why is the kfree() argument const?, Jakob Oestergaard, (Fri Jan 18, 7:39 am)
Re: Why is the kfree() argument const?, Chris Friesen, (Fri Jan 18, 8:38 am)
RE: Why is the kfree() argument const?, Linus Torvalds, (Fri Jan 18, 9:10 am)
Re: Why is the kfree() argument const?, Olivier Galibert, (Fri Jan 18, 10:24 am)
Re: Why is the kfree() argument const?, Olivier Galibert, (Fri Jan 18, 10:37 am)
Re: Why is the kfree() argument const?, DM, (Fri Jan 18, 11:06 am)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 12:06 pm)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 12:14 pm)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 12:55 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Fri Jan 18, 1:55 pm)
Re: Why is the kfree() argument const?, J.A. , (Fri Jan 18, 3:29 pm)
Re: Why is the kfree() argument const?, Krzysztof Halasa, (Fri Jan 18, 4:44 pm)