X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=genevent-new%2Fgentest.c;h=753ac4d7895663e5d0b668ce059fde17e1d9ebbb;hb=8f78c30fa8dda13373f1ba664620c65153c9558f;hp=4f12814c0e79c684974715f0ec552fc41b6f5fb6;hpb=6ef4987c3c6a9fbf5e9742bb679f1632ea7733f5;p=lttv.git diff --git a/genevent-new/gentest.c b/genevent-new/gentest.c index 4f12814c..753ac4d7 100644 --- a/genevent-new/gentest.c +++ b/genevent-new/gentest.c @@ -1,13 +1,21 @@ +#define __KERNEL__ + #include #include #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)) +// Useful outside __KERNEL__. Not used here because inline is already redefined. +#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, size_t size_of_type) @@ -36,9 +44,9 @@ struct lttng_mystruct2 { //struct lttng_mystruct teststr1; }; - -size_t lttng_get_size_mystruct2( - struct lttng_mystruct2 *obj) +#if 0 +static inline size_t lttng_get_size_mystruct2( + struct lttng_mystruct2 * obj) { size_t size=0, locsize; @@ -52,8 +60,9 @@ size_t lttng_get_size_mystruct2( return sizeof(struct lttng_mystruct2); } +#endif //0 -size_t lttng_get_alignment_mystruct2( +static inline size_t lttng_get_alignment_mystruct2( struct lttng_mystruct2 *obj) { size_t align=0, localign; @@ -67,7 +76,10 @@ size_t lttng_get_alignment_mystruct2( return align; } -void lttng_write_mystruct2(void **to, +static inline void lttng_write_mystruct2( + void *buffer, + size_t *to_base, + size_t *to, void **from, size_t *len, struct lttng_mystruct2 *obj) @@ -75,12 +87,13 @@ void lttng_write_mystruct2(void **to, size_t align, size; align = lttng_get_alignment_mystruct2(obj); - size = lttng_get_size_mystruct2(obj); + //size = lttng_get_size_mystruct2(obj); + size = sizeof(struct lttng_mystruct2); 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 +104,8 @@ 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( +#if 0 +static inline size_t lttng_get_size_array_mystruct_myarray( lttng_array_mystruct_myarray obj) { size_t size=0, locsize; @@ -102,12 +116,13 @@ size_t lttng_get_size_array_mystruct_myarray( BUG_ON(ltt_align(size, locsize) != 0); size += LTTNG_ARRAY_SIZE_mystruct_myarray * locsize; - BUG_ON(size != LTTNG_ARRAY_SIZE_mystruct_myarray * sizeof(uint64_t)); + BUG_ON(sizeof(lttng_array_mystruct_myarray) != size); - return size; + return sizeof(lttng_array_mystruct_myarray); } +#endif //0 -size_t lttng_get_alignment_array_mystruct_myarray( +static inline size_t lttng_get_alignment_array_mystruct_myarray( lttng_array_mystruct_myarray obj) { size_t align=0, localign; @@ -119,7 +134,10 @@ size_t lttng_get_alignment_array_mystruct_myarray( } -void lttng_write_array_mystruct_myarray(void **to, +static inline void lttng_write_array_mystruct_myarray( + void *buffer, + size_t *to_base, + size_t *to, void **from, size_t *len, lttng_array_mystruct_myarray obj) @@ -127,15 +145,23 @@ void lttng_write_array_mystruct_myarray(void **to, size_t align, size; align = lttng_get_alignment_array_mystruct_myarray(obj); - size = lttng_get_size_array_mystruct_myarray(obj); + //size = lttng_get_size_array_mystruct_myarray(obj); + size = sizeof(lttng_array_mystruct_myarray); 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; +#if 0 + /* For varlen child : let the child align itself. */ + for(unsigned int i=0; ilen * locsize); + /* Realign on arch size */ + locsize = sizeof(void *); + size += ltt_align(size, locsize); + return size; } +#endif //0 -size_t lttng_get_alignment_sequence_mystruct_mysequence( +static inline size_t lttng_get_alignment_sequence_mystruct_mysequence( lttng_sequence_mystruct_mysequence *obj) { size_t align=0, localign; @@ -175,42 +206,56 @@ size_t lttng_get_alignment_sequence_mystruct_mysequence( } -void lttng_write_sequence_mystruct_mysequence(void **to, +static inline void lttng_write_sequence_mystruct_mysequence( + void *buffer, + size_t *to_base, + size_t *to, void **from, size_t *len, lttng_sequence_mystruct_mysequence *obj) { - size_t align, size; - void *varfrom; - size_t varlen=0; + size_t align; + size_t size; /* Flush pending memcpy */ if(*len != 0) { - memcpy(*to, *from, *len); - *to += *len; - *len = 0; + if(buffer != NULL) + memcpy(buffer+*to_base+*to, *from, *len); } + *to += *len; + *len = 0; align = lttng_get_alignment_sequence_mystruct_mysequence(obj); //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)); - varfrom = &obj->len; - varlen += sizeof(unsigned int); - memcpy(*to, varfrom, varlen); - *to += varlen; - varlen = 0; - - *to += ltt_align((size_t)*to, sizeof(double)); - varfrom = obj->array; - varlen += obj->len * sizeof(double); - memcpy(*to, varfrom, varlen); - *to += varlen; - varlen = 0; + size = sizeof(unsigned int); + *to += ltt_align(*to, size); + if(buffer != NULL) + memcpy(buffer+*to_base+*to, &obj->len, size); + *to += size; + + size = sizeof(double); + *to += ltt_align(*to, size); + size = obj->len * sizeof(double); + if(buffer != NULL) + memcpy(buffer+*to_base+*to, obj->array, size); + *to += size; +#if 0 + /* If varlen child : let child align itself */ + for(unsigned int i=0; ilen; i++) { + lttng_write_child(buffer, to_base, to, from, len, obj->array[i]); + } +#endif //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; @@ -223,8 +268,8 @@ union lttng_mystruct_myunion { unsigned long myulong; }; - -size_t lttng_get_size_mystruct_myunion( +#if 0 +static inline size_t lttng_get_size_mystruct_myunion( union lttng_mystruct_myunion *obj) { size_t size=0, locsize; @@ -237,11 +282,11 @@ size_t lttng_get_size_mystruct_myunion( BUG_ON(size != sizeof(union lttng_mystruct_myunion)); - return size; + return sizeof(union lttng_mystruct_myunion); } +#endif //0 - -size_t lttng_get_alignment_mystruct_myunion( +static inline size_t lttng_get_alignment_mystruct_myunion( union lttng_mystruct_myunion *obj) { size_t align=0, localign; @@ -256,7 +301,10 @@ size_t lttng_get_alignment_mystruct_myunion( } -void lttng_write_mystruct_myunion(void **to, +static inline void lttng_write_mystruct_myunion( + void *buffer, + size_t *to_base, + size_t *to, void **from, size_t *len, union lttng_mystruct_myunion *obj) @@ -264,15 +312,18 @@ void lttng_write_mystruct_myunion(void **to, size_t align, size; align = lttng_get_alignment_mystruct_myunion(obj); - size = lttng_get_size_mystruct_myunion(obj); + //size = lttng_get_size_mystruct_myunion(obj); + size = sizeof(union lttng_mystruct_myunion); 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; + + /* Assert : no varlen child. */ } @@ -285,7 +336,8 @@ struct lttng_mystruct { union lttng_mystruct_myunion myunion; }; -size_t lttng_get_size_mystruct( +#if 0 +static inline size_t lttng_get_size_mystruct( struct lttng_mystruct *obj) { size_t size=0, locsize, localign; @@ -314,9 +366,9 @@ size_t lttng_get_size_mystruct( return size; } +#endif //0 - -size_t lttng_get_alignment_mystruct( +static inline size_t lttng_get_alignment_mystruct( struct lttng_mystruct *obj) { size_t align=0, localign; @@ -342,7 +394,10 @@ size_t lttng_get_alignment_mystruct( return align; } -void lttng_write_mystruct(void **to, +static inline void lttng_write_mystruct( + void *buffer, + size_t *to_base, + size_t *to, void **from, size_t *len, struct lttng_mystruct *obj) @@ -354,27 +409,32 @@ 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(buffer, to_base, to, from, len, &obj->teststr); - lttng_write_array_mystruct_myarray(to, from, len, obj->myarray); + lttng_write_array_mystruct_myarray(buffer, 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(buffer, to_base, to, from, len, &obj->mysequence); + /* After this previous write, we are sure that *to is 0, *len is 0 and + * *to_base is aligned on the architecture size : to rest of alignment will + * be calculated statically. */ + + lttng_write_mystruct_myunion(buffer, to_base, to, from, len, &obj->myunion); /* Don't forget to flush last write..... */ } @@ -384,30 +444,42 @@ void lttng_write_mystruct(void **to, -void main() +//void main() +void test() { struct lttng_mystruct test; test.mysequence.len = 20; test.mysequence.array = malloc(20); - size_t size = lttng_get_size_mystruct(&test); - size_t align = lttng_get_alignment_mystruct(&test); - - void *buf = malloc(align + size); - void *to = buf; + //size_t size = lttng_get_size_mystruct(&test); + //size_t align = lttng_get_alignment_mystruct(&test); + // + size_t to_base = 0; /* 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); + /* Get size */ + lttng_write_mystruct(NULL, &to_base, &to, &from, &len, &test); + /* Size = to_base + to + len */ + + void *buffer = malloc(to_base + to + len); + to_base = 0; /* the buffer is allocated on arch_size alignment */ + to = 0; + from = &test; + len = 0; + + lttng_write_mystruct(buffer, &to_base, &to, &from, &len, &test); /* Final flush */ /* Flush pending memcpy */ if(len != 0) { - memcpy(to, from, len); + // Assert buffer != NULL */ + memcpy(buffer+to_base+to, from, len); to += len; from += len; len = 0; } free(test.mysequence.array); - free(buf); + free(buffer); }