Re: p = kmalloc(sizeof(*p), )

From: Matthias Urlichs
Date: Mon Sep 19 2005 - 16:24:38 EST


Hi, Linus Torvalds wrote:

> Well, to be slightly more positive: it's not a very easy feature to do
> properly.

It's apparently an easy feature to do decidedly suboptimally.

This sample code

#include <malloc.h>
struct foo {
char fill1[1000];
int bar;
char fill2[1000];
int baz;
char fill3[1000];
};
struct foo *get_foo() {
struct foo *res = malloc(sizeof(struct foo));
*res = (struct foo){.bar=5, .baz=7};
return res;
}

calls memcpy _twice_ -- it initializes the object on the stack from a
couple of mostly-zero bytes in .rodata, and then memcpy's the thing to the
heap.

> So considering that almost nobody does this (certainly not SpecInt), and
> it would probably require re-organizations at many levels, I'm not
> surprised it hasn't gotten a lot of attention.
>
This is gcc 4.0. Optimization levels (I tried 0, 3, and s) don't affect
this -- which surprised me; I'd have thought that gcc would decide on
the proper trade-off between programmed and static initialization a bit
later.

--
Matthias Urlichs | {M:U} IT Design @ m-u-it.de | smurf@xxxxxxxxxxxxxx
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
- -
O that my tongue were in the thunder's mouth! Then with a passion would I
shake the world.
-- Shakespeare


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/