Re: Why is the kfree() argument const?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Linus Torvalds <torvalds@...>
Cc: Johannes Weiner <hannes@...>, Linux Kernel Mailing List <linux-kernel@...>, <clameter@...>, <penberg@...>
Date: Wednesday, January 16, 2008 - 6:33 pm

On Wed, Jan 16, 2008 at 10:39:00AM -0800, Linus Torvalds wrote:

I totally agree with the above.


Here, I'm not so sure.


I'm OK with this.


This too.


Err, not sure.


OK


This is where I disagree. If a struct has a constant pointer to it, then
the usage of that pointer by the struct should never modify it. If I
need to allocate memory for a name to a struct, I would not expect that
struct to ever free it.

Let's use your example. I'll assume that the struct was created by some
constructor and the destructor freed it. I'd argue the correct way would
be to have the kfree with a typecast.

Why?

  - const pointers (especially strings) should be able to point to
    static data. One thing that we would like to avoid is:

    mystruct->name = "myobj";
	...
    kfree(mystruct->name);

  - really, kfree should match kmalloc for types. What kmalloc returns
    should be what kfree accepts.  Passing in a const pointer to kfree
    *should* be a red flag that something might not be right.


    char *name = kmalloc(...);
	...
    mystruct->name = name; /* this is an implicit cast */

    So adding a cast to kfree isn't incorrect. C automatically casts
    name to a const pointer, which means if we want to free it, then
    we should cast it back.


Again, I totally agree with the above.


OK, I think that kfree should not be const, but not because of the
explanation that you gave, but because of the C type system in general.

kfree should match the kmalloc type. We don't declare

  const void *kmalloc(...)

so we shouldn't do the same with kfree. If you assign a const pointer to
something from kmalloc, C implicitly does the cast. This doesn't mean
that we should ignore doing the cast back in kfree. Especially since
this could help us avoid the kfree("mystring") issue.

Just my $0.02

-- Steve

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

Messages in current thread:
Why is the kfree() argument const?, Johannes Weiner, (Wed Jan 16, 12:32 pm)
Re: Why is the kfree() argument const?, Linus Torvalds, (Wed Jan 16, 2:39 pm)
Re: Why is the kfree() argument const?, Steven Rostedt, (Wed Jan 16, 6:33 pm)
Re: Why is the kfree() argument const?, Johannes Weiner, (Wed Jan 16, 6:19 pm)
Re: Why is the kfree() argument const?, Linus Torvalds, (Wed Jan 16, 7:16 pm)
Re: Why is the kfree() argument const?, Christoph Lameter, (Wed Jan 16, 6:20 pm)
Re: Why is the kfree() argument const?, Linus Torvalds, (Wed Jan 16, 7:18 pm)
Re: Why is the kfree() argument const?, Johannes Weiner, (Wed Jan 16, 7:13 pm)
Re: Why is the kfree() argument const?, Johannes Weiner, (Wed Jan 16, 6:37 pm)
Re: Why is the kfree() argument const?, Christoph Lameter, (Wed Jan 16, 12:48 pm)
Re: Why is the kfree() argument const?, Pekka J Enberg, (Wed Jan 16, 1:45 pm)
Re: Why is the kfree() argument const?, Bernd Petrovitsch, (Wed Jan 16, 1:34 pm)