X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=genevent-new%2Fgentest.c;h=ae71da7250f04d6b4a971156959051b4ae4ef9da;hb=1a42a214239da3d0f285e88f04f736d2481d0f48;hp=4f12814c0e79c684974715f0ec552fc41b6f5fb6;hpb=6ef4987c3c6a9fbf5e9742bb679f1632ea7733f5;p=lttv.git diff --git a/genevent-new/gentest.c b/genevent-new/gentest.c index 4f12814c..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,8 +40,8 @@ struct lttng_mystruct2 { }; -size_t lttng_get_size_mystruct2( - struct lttng_mystruct2 *obj) +static force_inline size_t lttng_get_size_mystruct2( + struct lttng_mystruct2 * obj) { size_t size=0, locsize; @@ -53,7 +56,7 @@ size_t lttng_get_size_mystruct2( return sizeof(struct lttng_mystruct2); } -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,7 +70,9 @@ size_t lttng_get_alignment_mystruct2( return align; } -void lttng_write_mystruct2(void **to, +static force_inline void lttng_write_mystruct2( + void **to_base, + size_t *to, void **from, size_t *len, struct lttng_mystruct2 *obj) @@ -78,9 +83,9 @@ void lttng_write_mystruct2(void **to, size = lttng_get_size_mystruct2(obj); if(*len == 0) { - *to += ltt_align((size_t)(*to), align); /* align output */ + *to += ltt_align(*to, align); /* align output */ } else { - *len += ltt_align((size_t)(*to+*len), align); /* C alignment, ok to do a memcpy of it */ + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } *len += size; @@ -91,7 +96,7 @@ void lttng_write_mystruct2(void **to, #define LTTNG_ARRAY_SIZE_mystruct_myarray 10 typedef uint64_t lttng_array_mystruct_myarray[LTTNG_ARRAY_SIZE_mystruct_myarray]; -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; @@ -107,7 +112,7 @@ size_t lttng_get_size_array_mystruct_myarray( return size; } -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; @@ -119,7 +124,9 @@ size_t lttng_get_alignment_array_mystruct_myarray( } -void lttng_write_array_mystruct_myarray(void **to, +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) @@ -130,9 +137,9 @@ void lttng_write_array_mystruct_myarray(void **to, size = lttng_get_size_array_mystruct_myarray(obj); if(*len == 0) { - *to += ltt_align((size_t)(*to), align); /* align output */ + *to += ltt_align(*to, align); /* align output */ } else { - *len += ltt_align((size_t)(*to+*len), align); /* C alignment, ok to do a memcpy of it */ + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } *len += size; @@ -146,7 +153,7 @@ struct lttng_sequence_mystruct_mysequence { }; -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; @@ -160,7 +167,7 @@ size_t lttng_get_size_sequence_mystruct_mysequence( return size; } -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; @@ -175,18 +182,20 @@ size_t lttng_get_alignment_sequence_mystruct_mysequence( } -void lttng_write_sequence_mystruct_mysequence(void **to, +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; + size_t align; void *varfrom; size_t varlen=0; /* Flush pending memcpy */ if(*len != 0) { - memcpy(*to, *from, *len); + memcpy(*to_base+*to, *from, *len); *to += *len; *len = 0; } @@ -195,23 +204,28 @@ void lttng_write_sequence_mystruct_mysequence(void **to, //no need size = lttng_get_size_sequence_mystruct_mysequence(obj); /* Align output */ - *to += ltt_align((size_t)(*to), align); + *to += ltt_align(*to, align); /* *len = 0 in this function */ /* Copy members */ - *to += ltt_align((size_t)*to, sizeof(unsigned int)); + *to += ltt_align(*to, sizeof(unsigned int)); varfrom = &obj->len; varlen += sizeof(unsigned int); - memcpy(*to, varfrom, varlen); + memcpy(*to_base+*to, varfrom, varlen); *to += varlen; varlen = 0; - *to += ltt_align((size_t)*to, sizeof(double)); + *to += ltt_align(*to, sizeof(double)); varfrom = obj->array; varlen += obj->len * sizeof(double); - memcpy(*to, varfrom, varlen); + memcpy(*to_base+*to, varfrom, varlen); *to += varlen; varlen = 0; + /* 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; } @@ -224,7 +238,7 @@ union lttng_mystruct_myunion { }; -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; @@ -241,7 +255,7 @@ size_t lttng_get_size_mystruct_myunion( } -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; @@ -256,7 +270,9 @@ size_t lttng_get_alignment_mystruct_myunion( } -void lttng_write_mystruct_myunion(void **to, +static force_inline void lttng_write_mystruct_myunion( + void **to_base, + size_t *to, void **from, size_t *len, union lttng_mystruct_myunion *obj) @@ -267,9 +283,9 @@ void lttng_write_mystruct_myunion(void **to, size = lttng_get_size_mystruct_myunion(obj); if(*len == 0) { - *to += ltt_align((size_t)(*to), align); /* align output */ + *to += ltt_align(*to, align); /* align output */ } else { - *len += ltt_align((size_t)(*to+*len), align); /* C alignment, ok to do a memcpy of it */ + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } *len += size; @@ -285,7 +301,7 @@ struct lttng_mystruct { union lttng_mystruct_myunion myunion; }; -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; @@ -316,7 +332,7 @@ size_t lttng_get_size_mystruct( } -size_t lttng_get_alignment_mystruct( +static force_inline size_t lttng_get_alignment_mystruct( struct lttng_mystruct *obj) { size_t align=0, localign; @@ -342,7 +358,9 @@ size_t lttng_get_alignment_mystruct( return align; } -void lttng_write_mystruct(void **to, +static force_inline void lttng_write_mystruct( + void **to_base, + size_t *to, void **from, size_t *len, struct lttng_mystruct *obj) @@ -354,27 +372,33 @@ void lttng_write_mystruct(void **to, // locsize = lttng_get_size_mystruct(obj); if(*len == 0) { - *to += ltt_align((size_t)(*to), align); /* align output */ + *to += ltt_align(*to, align); /* align output */ } else { - *len += ltt_align((size_t)(*to+*len), align); /* C alignment, ok to do a memcpy of it */ + *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */ } /* Contains variable sized fields : must explode the structure */ size = sizeof(unsigned int); - *len += ltt_align((size_t)(*to+*len), size) + size; + size += ltt_align(*to+*len, size) + size; + *len += size; size = sizeof(enum lttng_irq_mode); - *len += ltt_align((size_t)(*to+*len), size) + size; + size += ltt_align(*to+*len, size) + size; + *len += size; - lttng_write_mystruct2(to, from, len, &obj->teststr); + lttng_write_mystruct2(to_base, to, from, len, &obj->teststr); - lttng_write_array_mystruct_myarray(to, from, len, obj->myarray); + lttng_write_array_mystruct_myarray(to_base, to, from, len, obj->myarray); /* Variable length field */ - lttng_write_sequence_mystruct_mysequence(to, from, len, &obj->mysequence); - - lttng_write_mystruct_myunion(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..... */ } @@ -384,7 +408,8 @@ void lttng_write_mystruct(void **to, -void main() +//void main() +void test() { struct lttng_mystruct test; test.mysequence.len = 20; @@ -394,15 +419,16 @@ void main() size_t align = lttng_get_alignment_mystruct(&test); void *buf = malloc(align + size); - void *to = buf; + 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(&to, &from, &len, &test); + lttng_write_mystruct(&to_base, &to, &from, &len, &test); /* Final flush */ /* Flush pending memcpy */ if(len != 0) { - memcpy(to, from, len); + memcpy(to_base+to, from, len); to += len; from += len; len = 0;