convert from svn repository: remove tags directory
[lttv.git] / trunk / obsolete / genevent / gentest.c
CommitLineData
6ef4987c 1
e277c2f1 2#define __KERNEL__
3
6ef4987c 4#include <assert.h>
5#include <sys/types.h>
6#include <stdint.h>
7#include <stdlib.h>
3e090fd3 8#include <string.h>
6ef4987c 9
e277c2f1 10#include <linux/compiler.h>
11
6ef4987c 12#define min(a,b) (((a)<(b))?a:b)
13#define max(a,b) (((a)>(b))?a:b)
14#define BUG_ON(a) assert(!(a))
15
5a9aecad 16// Useful outside __KERNEL__. Not used here because inline is already redefined.
1a42a214 17#define force_inline inline __attribute__((always_inline))
18
6ef4987c 19/* Calculate the offset needed to align the type */
e277c2f1 20static inline unsigned int ltt_align(size_t align_drift,
6ef4987c 21 size_t size_of_type)
22{
23 size_t alignment = min(sizeof(void*), size_of_type);
24
25 return ((alignment - align_drift) & (alignment-1));
26}
27
28
29/* TEMPLATE */
30
31enum lttng_tasklet_priority {
32 LTTNG_LOW,
33 LTTNG_HIGH,
34};
35
36enum lttng_irq_mode {
37 LTTNG_user,
38 LTTNG_kernel,
39};
40
41struct lttng_mystruct2 {
42 unsigned int irq_id;
43 enum lttng_irq_mode mode;
44 //struct lttng_mystruct teststr1;
45};
46
0d16d1c4 47#if 0
e277c2f1 48static inline size_t lttng_get_size_mystruct2(
257d03bc 49 struct lttng_mystruct2 * obj)
6ef4987c 50{
51 size_t size=0, locsize;
52
53 locsize = sizeof(unsigned int);
54 size += ltt_align(size, locsize) + locsize;
55
56 locsize = sizeof(enum lttng_irq_mode);
57 size += ltt_align(size, locsize) + locsize;
58
59 BUG_ON(sizeof(struct lttng_mystruct2) != size);
60
61 return sizeof(struct lttng_mystruct2);
62}
0d16d1c4 63#endif //0
6ef4987c 64
e277c2f1 65static inline size_t lttng_get_alignment_mystruct2(
6ef4987c 66 struct lttng_mystruct2 *obj)
67{
68 size_t align=0, localign;
69
70 localign = sizeof(unsigned int);
71 align = max(align, localign);
72
73 localign = sizeof(enum lttng_irq_mode);
74 align = max(align, localign);
75
76 return align;
77}
78
e277c2f1 79static inline void lttng_write_mystruct2(
0d16d1c4 80 void *buffer,
81 size_t *to_base,
257d03bc 82 size_t *to,
6ef4987c 83 void **from,
84 size_t *len,
85 struct lttng_mystruct2 *obj)
86{
3e090fd3 87 size_t align, size;
6ef4987c 88
89 align = lttng_get_alignment_mystruct2(obj);
0d16d1c4 90 //size = lttng_get_size_mystruct2(obj);
91 size = sizeof(struct lttng_mystruct2);
6ef4987c 92
93 if(*len == 0) {
1c341713 94 *to += ltt_align(*to, align); /* align output */
6ef4987c 95 } else {
1c341713 96 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 97 }
98
99 *len += size;
100}
101
102
103
104#define LTTNG_ARRAY_SIZE_mystruct_myarray 10
105typedef uint64_t lttng_array_mystruct_myarray[LTTNG_ARRAY_SIZE_mystruct_myarray];
106
0d16d1c4 107#if 0
e277c2f1 108static inline size_t lttng_get_size_array_mystruct_myarray(
6ef4987c 109 lttng_array_mystruct_myarray obj)
110{
111 size_t size=0, locsize;
112
113 locsize = sizeof(uint64_t);
114 /* ltt_align == 0 always*/
115 //size += ltt_align(size, locsize) + (LTTNG_ARRAY_SIZE_mystruct_myarray * locsize);
116 BUG_ON(ltt_align(size, locsize) != 0);
117 size += LTTNG_ARRAY_SIZE_mystruct_myarray * locsize;
118
ae9eea8c 119 BUG_ON(sizeof(lttng_array_mystruct_myarray) != size);
6ef4987c 120
ae9eea8c 121 return sizeof(lttng_array_mystruct_myarray);
6ef4987c 122}
0d16d1c4 123#endif //0
6ef4987c 124
e277c2f1 125static inline size_t lttng_get_alignment_array_mystruct_myarray(
6ef4987c 126 lttng_array_mystruct_myarray obj)
127{
128 size_t align=0, localign;
129
130 localign = sizeof(uint64_t);
131 align = max(align, localign);
132
133 return align;
134}
135
136
e277c2f1 137static inline void lttng_write_array_mystruct_myarray(
0d16d1c4 138 void *buffer,
139 size_t *to_base,
257d03bc 140 size_t *to,
6ef4987c 141 void **from,
142 size_t *len,
143 lttng_array_mystruct_myarray obj)
144{
1c341713 145 size_t align, size;
6ef4987c 146
147 align = lttng_get_alignment_array_mystruct_myarray(obj);
0d16d1c4 148 //size = lttng_get_size_array_mystruct_myarray(obj);
149 size = sizeof(lttng_array_mystruct_myarray);
6ef4987c 150
151 if(*len == 0) {
1c341713 152 *to += ltt_align(*to, align); /* align output */
6ef4987c 153 } else {
1c341713 154 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 155 }
156
157 *len += size;
0d16d1c4 158#if 0
159 /* For varlen child : let the child align itself. */
160 for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_mystruct_myarray; i++) {
161 lttng_write_child(buffer, to_base, to, from, len, obj[i]);
162 }
163#endif //0
164
6ef4987c 165}
166
167
168typedef struct lttng_sequence_mystruct_mysequence lttng_sequence_mystruct_mysequence;
169struct lttng_sequence_mystruct_mysequence {
170 unsigned int len;
171 double *array;
172};
173
0d16d1c4 174#if 0
e277c2f1 175static inline size_t lttng_get_size_sequence_mystruct_mysequence(
6ef4987c 176 lttng_sequence_mystruct_mysequence *obj)
177{
178 size_t size=0, locsize;
179
180 locsize = sizeof(unsigned int);
181 size += ltt_align(size, locsize) + locsize;
182
183 locsize = sizeof(double);
184 size += ltt_align(size, locsize) + (obj->len * locsize);
185
5ec61509 186 /* Realign on arch size */
187 locsize = sizeof(void *);
188 size += ltt_align(size, locsize);
189
6ef4987c 190 return size;
191}
0d16d1c4 192#endif //0
6ef4987c 193
e277c2f1 194static inline size_t lttng_get_alignment_sequence_mystruct_mysequence(
6ef4987c 195 lttng_sequence_mystruct_mysequence *obj)
196{
197 size_t align=0, localign;
198
199 localign = sizeof(unsigned int);
200 align = max(align, localign);
201
202 localign = sizeof(double);
203 align = max(align, localign);
204
205 return align;
206}
207
208
e277c2f1 209static inline void lttng_write_sequence_mystruct_mysequence(
0d16d1c4 210 void *buffer,
211 size_t *to_base,
257d03bc 212 size_t *to,
6ef4987c 213 void **from,
214 size_t *len,
215 lttng_sequence_mystruct_mysequence *obj)
216{
257d03bc 217 size_t align;
0d16d1c4 218 size_t size;
6ef4987c 219
220 /* Flush pending memcpy */
221 if(*len != 0) {
0d16d1c4 222 if(buffer != NULL)
223 memcpy(buffer+*to_base+*to, *from, *len);
6ef4987c 224 }
0d16d1c4 225 *to += *len;
226 *len = 0;
6ef4987c 227
228 align = lttng_get_alignment_sequence_mystruct_mysequence(obj);
229 //no need size = lttng_get_size_sequence_mystruct_mysequence(obj);
230
231 /* Align output */
1c341713 232 *to += ltt_align(*to, align); /* *len = 0 in this function */
6ef4987c 233
234 /* Copy members */
0d16d1c4 235 size = sizeof(unsigned int);
236 *to += ltt_align(*to, size);
237 if(buffer != NULL)
238 memcpy(buffer+*to_base+*to, &obj->len, size);
239 *to += size;
240
241 size = sizeof(double);
242 *to += ltt_align(*to, size);
243 size = obj->len * sizeof(double);
244 if(buffer != NULL)
245 memcpy(buffer+*to_base+*to, obj->array, size);
246 *to += size;
247#if 0
248 /* If varlen child : let child align itself */
249 for(unsigned int i=0; i<obj->len; i++) {
250 lttng_write_child(buffer, to_base, to, from, len, obj->array[i]);
251 }
252#endif //0
253
6ef4987c 254
1c341713 255 /* Realign the *to_base on arch size, set *to to 0 */
256 *to = ltt_align(*to, sizeof(void *));
257 *to_base = *to_base+*to;
258 *to = 0;
a6cf4981 259
6ef4987c 260 /* Put source *from just after the C sequence */
261 *from = obj+1;
262}
263
264
265
266union lttng_mystruct_myunion {
267 double myfloat;
268 unsigned long myulong;
269};
270
0d16d1c4 271#if 0
e277c2f1 272static inline size_t lttng_get_size_mystruct_myunion(
6ef4987c 273 union lttng_mystruct_myunion *obj)
274{
275 size_t size=0, locsize;
276
277 locsize = sizeof(double);
278 size = max(size, locsize);
279
280 locsize = sizeof(unsigned long);
281 size = max(size, locsize);
282
283 BUG_ON(size != sizeof(union lttng_mystruct_myunion));
284
ae9eea8c 285 return sizeof(union lttng_mystruct_myunion);
6ef4987c 286}
0d16d1c4 287#endif //0
6ef4987c 288
e277c2f1 289static inline size_t lttng_get_alignment_mystruct_myunion(
6ef4987c 290 union lttng_mystruct_myunion *obj)
291{
292 size_t align=0, localign;
293
294 localign = sizeof(double);
295 align = max(align, localign);
296
297 localign = sizeof(unsigned long);
298 align = max(align, localign);
299
300 return align;
301}
302
303
e277c2f1 304static inline void lttng_write_mystruct_myunion(
0d16d1c4 305 void *buffer,
306 size_t *to_base,
257d03bc 307 size_t *to,
6ef4987c 308 void **from,
309 size_t *len,
310 union lttng_mystruct_myunion *obj)
311{
1c341713 312 size_t align, size;
6ef4987c 313
314 align = lttng_get_alignment_mystruct_myunion(obj);
0d16d1c4 315 //size = lttng_get_size_mystruct_myunion(obj);
316 size = sizeof(union lttng_mystruct_myunion);
6ef4987c 317
318 if(*len == 0) {
1c341713 319 *to += ltt_align(*to, align); /* align output */
6ef4987c 320 } else {
1c341713 321 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 322 }
323
324 *len += size;
a3e6ce64 325
326 /* Assert : no varlen child. */
6ef4987c 327}
328
329
330struct lttng_mystruct {
331 unsigned int irq_id;
332 enum lttng_irq_mode mode;
333 struct lttng_mystruct2 teststr;
334 lttng_array_mystruct_myarray myarray;
335 lttng_sequence_mystruct_mysequence mysequence;
336 union lttng_mystruct_myunion myunion;
337};
338
0d16d1c4 339#if 0
e277c2f1 340static inline size_t lttng_get_size_mystruct(
6ef4987c 341 struct lttng_mystruct *obj)
342{
343 size_t size=0, locsize, localign;
344
345 locsize = sizeof(unsigned int);
346 size += ltt_align(size, locsize) + locsize;
347
348 locsize = sizeof(enum lttng_irq_mode);
349 size += ltt_align(size, locsize) + locsize;
350
351 localign = lttng_get_alignment_mystruct2(&obj->teststr);
352 locsize = lttng_get_size_mystruct2(&obj->teststr);
353 size += ltt_align(size, localign) + locsize;
354
355 localign = lttng_get_alignment_array_mystruct_myarray(obj->myarray);
356 locsize = lttng_get_size_array_mystruct_myarray(obj->myarray);
357 size += ltt_align(size, localign) + locsize;
358
359 localign = lttng_get_alignment_sequence_mystruct_mysequence(&obj->mysequence);
360 locsize = lttng_get_size_sequence_mystruct_mysequence(&obj->mysequence);
361 size += ltt_align(size, localign) + locsize;
362
363 localign = lttng_get_alignment_mystruct_myunion(&obj->myunion);
364 locsize = lttng_get_size_mystruct_myunion(&obj->myunion);
365 size += ltt_align(size, localign) + locsize;
366
367 return size;
368}
0d16d1c4 369#endif //0
6ef4987c 370
e277c2f1 371static inline size_t lttng_get_alignment_mystruct(
6ef4987c 372 struct lttng_mystruct *obj)
373{
374 size_t align=0, localign;
375
376 localign = sizeof(unsigned int);
377 align = max(align, localign);
378
379 localign = sizeof(enum lttng_irq_mode);
380 align = max(align, localign);
381
382 localign = lttng_get_alignment_mystruct2(&obj->teststr);
383 align = max(align, localign);
384
385 localign = lttng_get_alignment_array_mystruct_myarray(obj->myarray);
386 align = max(align, localign);
387
388 localign = lttng_get_alignment_sequence_mystruct_mysequence(&obj->mysequence);
389 align = max(align, localign);
390
391 localign = lttng_get_alignment_mystruct_myunion(&obj->myunion);
392 align = max(align, localign);
393
394 return align;
395}
396
e277c2f1 397static inline void lttng_write_mystruct(
0d16d1c4 398 void *buffer,
399 size_t *to_base,
257d03bc 400 size_t *to,
6ef4987c 401 void **from,
402 size_t *len,
403 struct lttng_mystruct *obj)
404{
1c341713 405 size_t align, size;
6ef4987c 406
407 align = lttng_get_alignment_mystruct(obj);
408 // no need : contains variable size fields.
409 // locsize = lttng_get_size_mystruct(obj);
410
411 if(*len == 0) {
257d03bc 412 *to += ltt_align(*to, align); /* align output */
6ef4987c 413 } else {
257d03bc 414 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 415 }
416
417 /* Contains variable sized fields : must explode the structure */
418
1c341713 419 size = sizeof(unsigned int);
420 size += ltt_align(*to+*len, size) + size;
421 *len += size;
6ef4987c 422
1c341713 423 size = sizeof(enum lttng_irq_mode);
424 size += ltt_align(*to+*len, size) + size;
425 *len += size;
6ef4987c 426
0d16d1c4 427 lttng_write_mystruct2(buffer, to_base, to, from, len, &obj->teststr);
6ef4987c 428
0d16d1c4 429 lttng_write_array_mystruct_myarray(buffer, to_base, to, from, len, obj->myarray);
6ef4987c 430
431 /* Variable length field */
0d16d1c4 432 lttng_write_sequence_mystruct_mysequence(buffer, to_base, to, from, len, &obj->mysequence);
a3e6ce64 433 /* After this previous write, we are sure that *to is 0, *len is 0 and
434 * *to_base is aligned on the architecture size : to rest of alignment will
435 * be calculated statically. */
1c341713 436
0d16d1c4 437 lttng_write_mystruct_myunion(buffer, to_base, to, from, len, &obj->myunion);
6ef4987c 438
439 /* Don't forget to flush last write..... */
440}
441
442
443
444
445
446
257d03bc 447//void main()
448void test()
6ef4987c 449{
450 struct lttng_mystruct test;
451 test.mysequence.len = 20;
452 test.mysequence.array = malloc(20);
453
0d16d1c4 454 //size_t size = lttng_get_size_mystruct(&test);
455 //size_t align = lttng_get_alignment_mystruct(&test);
456 //
457 size_t to_base = 0; /* the buffer is allocated on arch_size alignment */
257d03bc 458 size_t to = 0;
6ef4987c 459 void *from = &test;
460 size_t len = 0;
461
0d16d1c4 462 /* Get size */
463 lttng_write_mystruct(NULL, &to_base, &to, &from, &len, &test);
464 /* Size = to_base + to + len */
465
466 void *buffer = malloc(to_base + to + len);
467 to_base = 0; /* the buffer is allocated on arch_size alignment */
468 to = 0;
469 from = &test;
470 len = 0;
471
472 lttng_write_mystruct(buffer, &to_base, &to, &from, &len, &test);
6ef4987c 473 /* Final flush */
474 /* Flush pending memcpy */
475 if(len != 0) {
0d16d1c4 476 // Assert buffer != NULL */
477 memcpy(buffer+to_base+to, from, len);
6ef4987c 478 to += len;
479 from += len;
480 len = 0;
481 }
482
483 free(test.mysequence.array);
0d16d1c4 484 free(buffer);
6ef4987c 485}
This page took 0.055764 seconds and 4 git commands to generate.