view test/strinit.c @ 896:d712ee10feb7

local_nptr for locally defined struct
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 06 Apr 2014 19:20:58 +0900
parents ece860823251
children b9d8ad7ea0ad
line wrap: on
line source

#include <stdio.h>

#ifndef INLINE
#define INLINE
#endif

INLINE void main7();
INLINE int main8();

int b[3] = {1,2,3};

struct temp {
   long a;
   long b;
   long c;
   long d;
    struct hoge {
        long k;
        long j;
    } m;
   long e;
} temp1 = {
//   101,
//   102,
//   103,
//   104,
//   105,
   .e = 5,
   .a = 3
};

struct temp temp3 = {
   .c = (long)&b,
   .d = -10,
   .a = (long)b
};

struct temp temp4 = { 1,2,3,4,5,6,7};
struct temp temp7 = { 1,2,3,4,{5,6},7};

#if 1
struct temp temp10 = {
    .m = (struct hoge){ .k = 3 },    // not allowed in mc
    // .c = (int)&b,                 // int/long trancation is not allowed in global
};
#endif 

INLINE void
linux_kernel();

struct test{
    int a,b,c;
} huga = {
    0,0,0,
};

typedef unsigned int __u32;
typedef unsigned short __u16;
typedef unsigned char __u8;
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
struct in6_addr { char hoge[128]; };

struct flowi {
        int oif;
        int iif;

        union {
                struct {
                        __u32 daddr;
                        __u32 saddr;
                        __u32 fwmark;
                        __u8 tos;
                        __u8 scope;
                } ip4_u;

                struct {
                        struct in6_addr daddr;
                        struct in6_addr saddr;
                        __u32 flowlabel;
                } ip6_u;

                struct {
                        __u16 daddr;
                        __u16 saddr;
                        __u32 fwmark;
                        __u8 scope;
                } dn_u;
        } nl_u;
        __u8 proto;
        __u8 flags;
        union {
                struct {
                        __u16 sport;
                        __u16 dport;
                } ports;

                struct {
                        __u8 type;
                        __u8 code__;
                } icmpt;

                struct {
                        __u16 sport;
                        __u16 dport;

                        __u8 objnum;
                        __u8 objnamel;
                        __u8 objname[16];
                } dnports;

                __u32 spi;
        } uli_u;
} __attribute__((__aligned__(32/8)));


INLINE static /* __inline__ __attribute__((always_inline)) __attribute__((always_inline))  */
void ip_route_connect(/* struct rtable **rp, */ u32 dst,
                                   u32 src, u32 tos, int oif, u8 protocol,
                                   u16 sport, u16 dport /*, struct sock *sk */)
{
        struct flowi fl = { .oif = oif,
                            .nl_u = { .ip4_u = { .daddr = dst,
                                                 .saddr = src,
                                                 .tos = tos } },
                            .proto = protocol,
                            .uli_u = { .ports =
                                       { .sport = sport,
                                         .dport = dport } } };

        printf("#0131:%x %x\n",fl.oif,fl.uli_u.ports.dport);
}

INLINE static 
void ip_route_connect0(/* struct rtable **rp, */ u32 dst,
                                   u32 src, u32 tos, int oif, u8 protocol,
                                   u16 sport, u16 dport /*, struct sock *sk */)
{
        struct flowi fl = { .oif = oif,
                            .iif = 55,
                            .nl_u = { .ip4_u = { .daddr = dst,
                                                 .saddr = 66,
                                                 .tos = tos } },
                            .proto = protocol,
                            .uli_u = { .ports =
                                       { .sport = sport,
                                         .dport = dport } } };

        printf("#0149:%x %x\n",fl.oif,fl.uli_u.ports.dport);
        printf("#0150:%x %x\n",fl.iif,fl.nl_u.ip4_u.saddr);
}

INLINE static 
void ip_route_connect1(/* struct rtable **rp, */ u32 dst,
                                   u32 src, u32 tos, int oif, u8 protocol,
                                   u16 sport, u16 dport /*, struct sock *sk */)
{
        struct flowi fl, *flp; 
      fl = (struct flowi){ .oif = oif,
                            .iif = 55,
                            .nl_u = { .ip4_u = { .daddr = dst,
                                                 .saddr = 66,
                                                 .tos = tos } },
                            .proto = protocol,
                            .uli_u = { .ports =
                                       { .sport = sport,
                                         .dport = dport } } };
       flp = &fl;

        printf("#0170:%d %d\n",flp->oif,flp->uli_u.ports.dport);
        printf("#0171:%d %d\n",flp->iif,flp->nl_u.ip4_u.saddr);
}

INLINE void
linux_kernel()
{
    int i=1,j=2;
    printf("#0178:%d %d\n",i,j);
    ip_route_connect0(1,2,3,4,5,6,7);
    ip_route_connect(1,2,3,4,5,6,7);
    ip_route_connect(11,12,13,14,15,16,17);
    ip_route_connect1(1,2,3,4,5,6,7);
}

struct st_child {
  int a,b;
  char c;
};

struct st_parent{
  char ch;
  struct st_child s1;
  int i,j;
};

INLINE void
main7()
{
  struct st_parent st1 = {'A',{1,2,'E'},5,6};
  struct st_child st2 = {10,11,'F'};
  printf("#0201:st1.ch   = %c\n",st1.ch);
  printf("#0202:st1.s1.a = %d\n",st1.s1.a);
  printf("#0203:st1.s1.b = %d\n",st1.s1.b);
  printf("#0204:st1.s1.c = %c\n",st1.s1.c);
  printf("#0205:st1.i    = %d\n",st1.i);
  printf("#0206:st1.j    = %d\n\n",st1.j);

  printf("#0208:st2.a = %d\n",st2.a);
  printf("#0209:st2.b = %d\n",st2.b);
  printf("#0210:st2.c = %c\n",st2.c);
  
  return;

}

struct arg { int hage, fuga; int aho; };



INLINE void f(int i, struct arg h) {
    printf("#0221:%d=%d %d %d\n",i,h.hage,h.fuga,h.aho);
}

INLINE int main8() {
     struct arg a = {11,22,33};
     struct arg b = {.fuga=44,.aho=55};
     struct arg c;
     struct arg d = {};
     struct arg e = (struct arg){};
     f(0,a);
     f(1,b);
     // f(2,c);    indeterminate output
     f(3,d);
     f(4,e);
     f(5,(struct arg){.fuga = 3,.aho=5});
     f(6,(struct arg){.hage = 3});
     f(7,(struct arg){});
     printf("#0238:%d %d\n",a.aho,((struct arg){.aho=120, .hage=55}).aho); 
     printf("#0239:%d %d\n",a.aho,((struct arg){.aho=120, .hage=55}).fuga); 
     return 0;
     printf("#0241:%d %d\n",a.aho,((struct arg){.aho=120, .hage=55}).fuga);   // unexecuted code
}

int
main()
{
    struct temp temp2 = { .c = 5, .e=99 };
#ifdef WRONG
    struct temp temp5 = { 1,2,3,4,5,6,7,8};
    struct temp temp6 = { 1,2,3,4,5,6};
#else
    struct temp temp5 = { 1,2,3,4,5,6,7};
    struct temp temp6 = { 1,2,3,4,5,6,7};
    struct temp temp8 = { 1,2,3,4,{5,6},7};
#endif
    struct temp temp11 = {
       .m = (struct hoge){},
       .c = (long)&b,
    };
    struct temp temp12 = {
       .m = (struct hoge){1,3},
       .c = (long)&temp4,
    };
    int c[3] = {1,2,3};

    printf("#0266:1: %ld\n",temp1.a);
    printf("#0267:1: %ld\n",temp1.e);
    printf("#0268:1: %ld\n",temp1.b);
    printf("#0269:2: %ld\n",temp2.c);
    printf("#0270:2: %ld\n",temp2.e);
    printf("#0271:2: %ld\n",temp2.b); // non explict initialized  value
    printf("#0272:2: %d\n",(void*)temp3.c==b);
    printf("#0273:2: %d\n",temp3.c==(long)b);
    printf("#0274:2: %d\n",temp3.a==(long)&b);
    printf("#0275:2: %ld\n",temp4.m.j);
    printf("#0276:2: %ld\n",temp5.m.j);
    printf("#0277:2: %ld\n",temp11.m.j);
    printf("#0278:2: %ld\n",temp12.m.k);
    printf("#0279:2: %ld\n",temp12.m.j);
    printf("#0280:2: %d\n",temp11.c==(long)&b);
    printf("#0281:2: %ld\n",temp8.e);
    printf("#0282:2: %ld\n",temp6.e);
    main7();
    main8();
    linux_kernel();
    int i=1,j=2;
    printf("#0287:%d %d\n",i,j);

    printf("#0289:b %d %d %d\n",b[0],b[1],b[2]);
    printf("#0290:c %d %d %d\n",c[0],c[1],c[2]);

    struct test t = {1,2,3};
    printf("#0293:t %d %d %d\n",t.a,t.b,t.c);

    struct test3 {
        int a,b,c;
    } *p, q, d = {
        1,2,3,
    };
    printf("#0300:d %d %d %d\n",d.a,d.b,d.c);
    p = & (struct test3) {1,2,3};
    printf("#0302:p %d %d %d\n",p->a,p->b,p->c);
    q =  (struct test3) {1,2,3};
    printf("#0304:q %d %d %d\n",q.a,q.b,q.c);

    struct test4 {
        struct test5 {
            int a,b,c;
        } d; 
    } u = { .d = {1,2,3} };
    struct test4 v = { .d = (struct test5){1,2,3} };
    
    printf("#0313:u %d %d %d\n",u.d.a,u.d.b,u.d.c);

    printf("#0315:v %d %d %d\n",v.d.a,v.d.b,v.d.c);

    printf("#0317:v %ld\n",temp10.a);
    printf("#0318:v %ld\n",temp10.m.k);
    printf("#0319:v %ld\n",temp10.m.j);
    printf("#0320:v %ld\n",temp10.e);


    return 0;
}



// end