RE: Why is the kfree() argument const?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <torvalds@...>
Cc: Johannes Weiner <hannes@...>, Linux Kernel Mailing List <linux-kernel@...>, <clameter@...>, <penberg@...>
Date: Friday, January 18, 2008 - 1:02 am

> On Thu, 17 Jan 2008, David Schwartz wrote:



Because the object ceases to exist. However, any modification requires write
access, whether or not that modification is a write.



Nobody disagrees with that.


No, I understand that.



Nonsense. The 'kfree' function *destroys* the object pointer to by the
pointer. How can you describe that as not doing anything to the object?



It destroys the object the pointer points to. Destroying an object requires
write access to it.


That is not what it does, that is how it does it. What it does is destroy
the object.


I agree, except that it destroys the object the pointer points to.


I now have a much better understanding of what you're saying, but I still
think it's nonsense.

1) An operation that modifies the logical state of an object should not
normally be done through a 'const' pointer. The reason you make a pointer
'const' is to indicate that this pointer should not be used to change the
logical state of the object pointed to.

2) The 'kfree' operation changes the logical state of the object pointed to,
as the object goes from existent to non-existent.

3) It is most useful for 'kfree' to be non-const because destroying an
object through a const pointer can easily be done in error. One of the
reasons you provide a const pointer is because you need the function you
pass the pointer to not to modify the object. Since this is an unusual
operation that could be an error, it is logical to force the person doing it
to clearly indicate that he knows the pointer is const and that he knows it
is right anyway.

I'm curious to hear how some other people on this feel. You are the first
competent coder I have *ever* heard make this argument.

By the way, I disagree with your metadata versus data argument. I would
agree that a function that changes only an object's metadata could be done
through a const pointer without needed a cast. A good example would be a
function that updates a "last time this object was read" variable.

However, *destroying* an object is not a metadata operation -- it destroys
the data as well. This is kind of a philosophical point, but an object does
not have a "does this object exist" piece of metadata. If an object does not
exist, it has no data. So destroying an object destroys the data and is thus
a write/modification operation on the data.

DS


--
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)