RE: Why is the kfree() argument const?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: David Schwartz <davids@...>
Cc: Johannes Weiner <hannes@...>, Linux Kernel Mailing List <linux-kernel@...>, <clameter@...>, <penberg@...>
Date: Thursday, January 17, 2008 - 5:25 pm

On Thu, 17 Jan 2008, David Schwartz wrote:

No. It's the *pointer* that is no longer valid.

There's definitely a difference between "exists and is changed" and 
"doesn't exist any more".


It's not a change to the data behind it, it's a change to the *metadata*. 
Which is somethign that "const" doesn't talk about at all.


No, it's why I'm right.

"kmalloc/kfree" (or any memory manager) by definition has to play games 
with pointers and do things like cast them. But the users shouldn't need 
to, not for something like this.


No. 

You are continuing to make the mistake that you think that "const" means 
that the memory behind the pointer is not going to change.

Why do you make that mistake, when it is PROVABLY NOT TRUE!

Try this trivial program:

	int main(int argc, char **argv)
	{
	        int i;
	        const int *c;
	
	        i = 5;
	        c = &i;
	        i = 10;
	        return *c;
	}

and realize that according to the C rules, if it returns anything but 10, 
the compiler is *buggy*.

The fact is, that in spite of us having a "const int *", the data behind 
that pointer may change.

So it doesn't matter ONE WHIT if you pass in a "const *" to "kfree()": it 
does not guarantee that the data doesn't change, because the object you 
point to has other pointers pointing to it.

This isn't worth discussing. It's really simple: a conforming program 
CANNOT POSSIBLY TELL whether "kfree()" modified the data or not. As such, 
AS FAR AS THE PROGRAM IS CONCERNED, kfree() takes a const pointer, and the 
rule that "if it can be considered const, it should be marked const" comes 
and says that kfree() should take a const pointer.

In other words - anythign that could ever disagree with "const *" is BY 
DEFINITION buggy.

It really is that simple. 

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