Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * dataalign.c -- show alignment needs
  3  *
  4  * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
  5  * Copyright (C) 2001 O'Reilly & Associates
  6  *
  7  * The source code in this file can be freely used, adapted,
  8  * and redistributed in source or binary form, so long as an
  9  * acknowledgment appears in derived source files.  The citation
 10  * should list that the code comes from the book "Linux Device
 11  * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 12  * by O'Reilly & Associates.   No warranty is attached;
 13  * we cannot take responsibility for errors or fitness for use.
 14  *
 15  * This runs with any Linux kernel (not any Unix, because of <linux/types.h>)
 16  */
 17 #include <stdio.h>
 18 #include <sys/utsname.h>
 19 #include <linux/types.h>
 20 
 21 /*
 22  * Define several data structures, all of them start with a lone char
 23  * in order to present an unaligned offset for the next field
 24  */
 25 struct c   {char c;  char      t;} c;
 26 struct s   {char c;  short     t;} s;
 27 struct i   {char c;  int       t;} i;
 28 struct l   {char c;  long      t;} l;
 29 struct ll  {char c;  long long t;} ll;
 30 struct p   {char c;  void *    t;} p;
 31 struct u1b {char c;  __u8      t;} u1b;
 32 struct u2b {char c;  __u16     t;} u2b;
 33 struct u4b {char c;  __u32     t;} u4b;
 34 struct u8b {char c;  __u64     t;} u8b;
 35 
 36 int main(int argc, char **argv)
 37 {
 38     struct utsname name;
 39 
 40     uname(&name); /* never fails :) */
 41     printf("arch  Align:  char  short  int  long   ptr long-long "
 42            " u8 u16 u32 u64\n");
 43     printf(       "%-12s  %3i   %3i   %3i   %3i   %3i   %3i      "
 44            "%3i %3i %3i %3i\n",
 45            name.machine,
 46            /* note that gcc can subtract void * values, but it's not ansi */
 47            (int)((void *)(&c.t)   - (void *)&c),
 48            (int)((void *)(&s.t)   - (void *)&s),
 49            (int)((void *)(&i.t)   - (void *)&i),
 50            (int)((void *)(&l.t)   - (void *)&l),
 51            (int)((void *)(&p.t)   - (void *)&p),
 52            (int)((void *)(&ll.t)  - (void *)&ll),
 53            (int)((void *)(&u1b.t) - (void *)&u1b),
 54            (int)((void *)(&u2b.t) - (void *)&u2b),
 55            (int)((void *)(&u4b.t) - (void *)&u4b),
 56            (int)((void *)(&u8b.t) - (void *)&u8b));
 57     return 0;
 58 }
 59 
  This page was automatically generated by the LXR engine.