X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=genevent-new%2Fgentest.c;h=ae71da7250f04d6b4a971156959051b4ae4ef9da;hb=1a42a214239da3d0f285e88f04f736d2481d0f48;hp=cfb6c73a705d110b4c3d0b92f49a34fea73e042c;hpb=257d03bcccad69422301b366600f36b8ea271526;p=lttv.git diff --git a/genevent-new/gentest.c b/genevent-new/gentest.c index cfb6c73a..ae71da72 100644 --- a/genevent-new/gentest.c +++ b/genevent-new/gentest.c @@ -3,13 +3,16 @@ #include #include #include +#include #define min(a,b) (((a)<(b))?a:b) #define max(a,b) (((a)>(b))?a:b) #define BUG_ON(a) assert(!(a)) +#define force_inline inline __attribute__((always_inline)) + /* Calculate the offset needed to align the type */ -static inline unsigned int ltt_align(size_t align_drift, +static force_inline unsigned int ltt_align(size_t align_drift, size_t size_of_type) { size_t alignment = min(sizeof(void*), size_of_type); @@ -37,7 +40,7 @@ struct lttng_mystruct2 { }; -static inline size_t lttng_get_size_mystruct2( +static force_inline size_t lttng_get_size_mystruct2( struct lttng_mystruct2 * obj) { size_t size=0, locsize; @@ -53,7 +56,7 @@ static inline size_t lttng_get_size_mystruct2( return sizeof(struct lttng_mystruct2); } -static inline size_t lttng_get_alignment_mystruct2( +static force_inline size_t lttng_get_alignment_mystruct2( struct lttng_mystruct2 *obj) { size_t align=0, localign; @@ -67,29 +70,25 @@ static inline size_t lttng_get_alignment_mystruct2( return align; } -static inline size_t lttng_write_mystruct2( - void *buf, +static force_inline void lttng_write_mystruct2( + void **to_base, size_t *to, void **from, size_t *len, struct lttng_mystruct2 *obj) { - size_t align, size, varalign; + size_t align, size; align = lttng_get_alignment_mystruct2(obj); size = lttng_get_size_mystruct2(obj); if(*len == 0) { - varalign = ltt_align(*to, align); /* align output */ - *to += varalign; + *to += ltt_align(*to, align); /* align output */ } else { - varalign = ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ - *len += varalign; + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } *len += size; - - return varalign+size; } @@ -97,7 +96,7 @@ static inline size_t lttng_write_mystruct2( #define LTTNG_ARRAY_SIZE_mystruct_myarray 10 typedef uint64_t lttng_array_mystruct_myarray[LTTNG_ARRAY_SIZE_mystruct_myarray]; -static inline size_t lttng_get_size_array_mystruct_myarray( +static force_inline size_t lttng_get_size_array_mystruct_myarray( lttng_array_mystruct_myarray obj) { size_t size=0, locsize; @@ -113,7 +112,7 @@ static inline size_t lttng_get_size_array_mystruct_myarray( return size; } -static inline size_t lttng_get_alignment_array_mystruct_myarray( +static force_inline size_t lttng_get_alignment_array_mystruct_myarray( lttng_array_mystruct_myarray obj) { size_t align=0, localign; @@ -125,29 +124,25 @@ static inline size_t lttng_get_alignment_array_mystruct_myarray( } -static inline size_t lttng_write_array_mystruct_myarray( - void *buf, +static force_inline void lttng_write_array_mystruct_myarray( + void **to_base, size_t *to, void **from, size_t *len, lttng_array_mystruct_myarray obj) { - size_t align, size, varalign; + size_t align, size; align = lttng_get_alignment_array_mystruct_myarray(obj); size = lttng_get_size_array_mystruct_myarray(obj); if(*len == 0) { - varalign = ltt_align(*to, align); /* align output */ - *to += varalign; + *to += ltt_align(*to, align); /* align output */ } else { - varalign = ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ - *len += varalign; + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } *len += size; - - return varalign + size; /* offset in the from */ } @@ -158,7 +153,7 @@ struct lttng_sequence_mystruct_mysequence { }; -static inline size_t lttng_get_size_sequence_mystruct_mysequence( +static force_inline size_t lttng_get_size_sequence_mystruct_mysequence( lttng_sequence_mystruct_mysequence *obj) { size_t size=0, locsize; @@ -172,7 +167,7 @@ static inline size_t lttng_get_size_sequence_mystruct_mysequence( return size; } -static inline size_t lttng_get_alignment_sequence_mystruct_mysequence( +static force_inline size_t lttng_get_alignment_sequence_mystruct_mysequence( lttng_sequence_mystruct_mysequence *obj) { size_t align=0, localign; @@ -187,22 +182,20 @@ static inline size_t lttng_get_alignment_sequence_mystruct_mysequence( } -static inline size_t lttng_write_sequence_mystruct_mysequence( - void *buf, +static force_inline void lttng_write_sequence_mystruct_mysequence( + void **to_base, size_t *to, void **from, size_t *len, lttng_sequence_mystruct_mysequence *obj) { size_t align; - size_t size=0; void *varfrom; size_t varlen=0; - size_t varalign=0; /* Flush pending memcpy */ if(*len != 0) { - memcpy(buf+*to, *from, *len); + memcpy(*to_base+*to, *from, *len); *to += *len; *len = 0; } @@ -211,32 +204,30 @@ static inline size_t lttng_write_sequence_mystruct_mysequence( //no need size = lttng_get_size_sequence_mystruct_mysequence(obj); /* Align output */ - varalign = ltt_align((size_t)(*to), align); - size += varalign; + *to += ltt_align(*to, align); /* *len = 0 in this function */ /* Copy members */ - varalign = ltt_align(size, sizeof(unsigned int)); - size += varalign; + *to += ltt_align(*to, sizeof(unsigned int)); varfrom = &obj->len; varlen += sizeof(unsigned int); - memcpy(buf+*to+size, varfrom, varlen); - size += varlen; + memcpy(*to_base+*to, varfrom, varlen); + *to += varlen; varlen = 0; - varalign = ltt_align(size, sizeof(double)); - size += varalign; + *to += ltt_align(*to, sizeof(double)); varfrom = obj->array; varlen += obj->len * sizeof(double); - memcpy(buf+*to+size, varfrom, varlen); - size += varlen; + memcpy(*to_base+*to, varfrom, varlen); + *to += varlen; varlen = 0; - *to += size; + /* Realign the *to_base on arch size, set *to to 0 */ + *to = ltt_align(*to, sizeof(void *)); + *to_base = *to_base+*to; + *to = 0; /* Put source *from just after the C sequence */ *from = obj+1; - - return size; } @@ -247,7 +238,7 @@ union lttng_mystruct_myunion { }; -static inline size_t lttng_get_size_mystruct_myunion( +static force_inline size_t lttng_get_size_mystruct_myunion( union lttng_mystruct_myunion *obj) { size_t size=0, locsize; @@ -264,7 +255,7 @@ static inline size_t lttng_get_size_mystruct_myunion( } -static inline size_t lttng_get_alignment_mystruct_myunion( +static force_inline size_t lttng_get_alignment_mystruct_myunion( union lttng_mystruct_myunion *obj) { size_t align=0, localign; @@ -279,29 +270,25 @@ static inline size_t lttng_get_alignment_mystruct_myunion( } -static inline size_t lttng_write_mystruct_myunion( - void *buf, +static force_inline void lttng_write_mystruct_myunion( + void **to_base, size_t *to, void **from, size_t *len, union lttng_mystruct_myunion *obj) { - size_t align, size, varalign; + size_t align, size; align = lttng_get_alignment_mystruct_myunion(obj); size = lttng_get_size_mystruct_myunion(obj); if(*len == 0) { - varalign = ltt_align(*to, align); /* align output */ - *to += varalign; + *to += ltt_align(*to, align); /* align output */ } else { - varalign = ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ - *len += varalign; + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } *len += size; - - return varalign + size; } @@ -314,7 +301,7 @@ struct lttng_mystruct { union lttng_mystruct_myunion myunion; }; -static inline size_t lttng_get_size_mystruct( +static force_inline size_t lttng_get_size_mystruct( struct lttng_mystruct *obj) { size_t size=0, locsize, localign; @@ -345,7 +332,7 @@ static inline size_t lttng_get_size_mystruct( } -static inline size_t lttng_get_alignment_mystruct( +static force_inline size_t lttng_get_alignment_mystruct( struct lttng_mystruct *obj) { size_t align=0, localign; @@ -371,14 +358,14 @@ static inline size_t lttng_get_alignment_mystruct( return align; } -static inline void lttng_write_mystruct( - void *buf, +static force_inline void lttng_write_mystruct( + void **to_base, size_t *to, void **from, size_t *len, struct lttng_mystruct *obj) { - size_t align, size=0, locsize; + size_t align, size; align = lttng_get_alignment_mystruct(obj); // no need : contains variable size fields. @@ -392,24 +379,26 @@ static inline void lttng_write_mystruct( /* Contains variable sized fields : must explode the structure */ - locsize = sizeof(unsigned int); - locsize += ltt_align(size, locsize) + locsize; - *len += locsize; - size += locsize; + size = sizeof(unsigned int); + size += ltt_align(*to+*len, size) + size; + *len += size; - locsize = sizeof(enum lttng_irq_mode); - locsize += ltt_align(size, locsize) + locsize; - *len += locsize; - size += locsize; + size = sizeof(enum lttng_irq_mode); + size += ltt_align(*to+*len, size) + size; + *len += size; - size += lttng_write_mystruct2(buf, to, from, len, &obj->teststr); + lttng_write_mystruct2(to_base, to, from, len, &obj->teststr); - size += lttng_write_array_mystruct_myarray(buf, to, from, len, obj->myarray); + lttng_write_array_mystruct_myarray(to_base, to, from, len, obj->myarray); /* Variable length field */ - size += lttng_write_sequence_mystruct_mysequence(buf, to, from, len, &obj->mysequence); - - size += lttng_write_mystruct_myunion(buf, to, from, len, &obj->myunion); + lttng_write_sequence_mystruct_mysequence(to_base, to, from, len, &obj->mysequence); + *to = 0; /* Force the compiler to know it's 0 */ + /* After this previous write, we are sure that *to is 0, and *to_base is + * aligned on the architecture size : to rest of alignment will be calculated + * statically. */ + + lttng_write_mystruct_myunion(to_base, to, from, len, &obj->myunion); /* Don't forget to flush last write..... */ } @@ -430,15 +419,16 @@ void test() size_t align = lttng_get_alignment_mystruct(&test); void *buf = malloc(align + size); + void *to_base = buf; /* the buffer is allocated on arch_size alignment */ size_t to = 0; void *from = &test; size_t len = 0; - lttng_write_mystruct(buf, &to, &from, &len, &test); + lttng_write_mystruct(&to_base, &to, &from, &len, &test); /* Final flush */ /* Flush pending memcpy */ if(len != 0) { - memcpy(buf+to, from, len); + memcpy(to_base+to, from, len); to += len; from += len; len = 0;