Re: __initdata and string tables, usage consensus?

Jamie Lokier (lkd@tantalophile.demon.co.uk)
Tue, 14 Jul 1998 14:54:03 +0100


I wrote:
> I've just tried this __initstr macro, it works on strings inside
> functions, but you can't use it to initialise static or global tables of
> strings :-( Might give someone some ideas though.

To be more specific, you can't my __initstr to initialise tables like
this. The compiler complains that the initialisers aren't constant. If
you use the C++ compiler {:-)} this is allowed, but it's worse as you
get two copies of each string in the output, one __initdata and one not.
(Using EGCS 1.0.2).

static const char * my_table [] =
{ __initstr ("First"), __initstr ("Second") };

But you can always initialise string tables like this:

static const char my_table [][7] __initdata =
{ "First", "Second" };

The latter generally wastes space, but being __initdata it is only
temporarily wasted. I'd say it's ok for small tables but not large ones.
You have to be careful with the string length ([7]), as you don't get an
error or a warning if this is large enough to hold the longest string
without the trailing zero. If it matters.

Constructs like this that aren't static/global initialisers work fine
with __initstr:

const char * breakage =
broken ? __initstr ("totally broken") : __initstr ("working great");

printk (__initstr ("Your %s is %s."), device, breakage);

-- Jamie

> #include <asm/init.h>
> #include <stdio.h>
>
> #define __initstr(__s) \
> ({ static const char __str [] __initdata = (__s); __str; })
> #define __initstrw(__s) \
> ({ static char __str [] __initdata = (__s); __str; })
>
> __initfunc (static void init (void))
> {
> printf (__initstr ("Hi there!\n"));
> }

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html