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