force inlining
[lttv.git] / genevent-new / 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
e277c2f1 16// Useful outsize __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
47
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}
63
e277c2f1 64static inline size_t lttng_get_alignment_mystruct2(
6ef4987c 65 struct lttng_mystruct2 *obj)
66{
67 size_t align=0, localign;
68
69 localign = sizeof(unsigned int);
70 align = max(align, localign);
71
72 localign = sizeof(enum lttng_irq_mode);
73 align = max(align, localign);
74
75 return align;
76}
77
e277c2f1 78static inline void lttng_write_mystruct2(
1c341713 79 void **to_base,
257d03bc 80 size_t *to,
6ef4987c 81 void **from,
82 size_t *len,
83 struct lttng_mystruct2 *obj)
84{
3e090fd3 85 size_t align, size;
6ef4987c 86
87 align = lttng_get_alignment_mystruct2(obj);
88 size = lttng_get_size_mystruct2(obj);
89
90 if(*len == 0) {
1c341713 91 *to += ltt_align(*to, align); /* align output */
6ef4987c 92 } else {
1c341713 93 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 94 }
95
96 *len += size;
97}
98
99
100
101#define LTTNG_ARRAY_SIZE_mystruct_myarray 10
102typedef uint64_t lttng_array_mystruct_myarray[LTTNG_ARRAY_SIZE_mystruct_myarray];
103
e277c2f1 104static inline size_t lttng_get_size_array_mystruct_myarray(
6ef4987c 105 lttng_array_mystruct_myarray obj)
106{
107 size_t size=0, locsize;
108
109 locsize = sizeof(uint64_t);
110 /* ltt_align == 0 always*/
111 //size += ltt_align(size, locsize) + (LTTNG_ARRAY_SIZE_mystruct_myarray * locsize);
112 BUG_ON(ltt_align(size, locsize) != 0);
113 size += LTTNG_ARRAY_SIZE_mystruct_myarray * locsize;
114
115 BUG_ON(size != LTTNG_ARRAY_SIZE_mystruct_myarray * sizeof(uint64_t));
116
117 return size;
118}
119
e277c2f1 120static inline size_t lttng_get_alignment_array_mystruct_myarray(
6ef4987c 121 lttng_array_mystruct_myarray obj)
122{
123 size_t align=0, localign;
124
125 localign = sizeof(uint64_t);
126 align = max(align, localign);
127
128 return align;
129}
130
131
e277c2f1 132static inline void lttng_write_array_mystruct_myarray(
1c341713 133 void **to_base,
257d03bc 134 size_t *to,
6ef4987c 135 void **from,
136 size_t *len,
137 lttng_array_mystruct_myarray obj)
138{
1c341713 139 size_t align, size;
6ef4987c 140
141 align = lttng_get_alignment_array_mystruct_myarray(obj);
142 size = lttng_get_size_array_mystruct_myarray(obj);
143
144 if(*len == 0) {
1c341713 145 *to += ltt_align(*to, align); /* align output */
6ef4987c 146 } else {
1c341713 147 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 148 }
149
150 *len += size;
151}
152
153
154typedef struct lttng_sequence_mystruct_mysequence lttng_sequence_mystruct_mysequence;
155struct lttng_sequence_mystruct_mysequence {
156 unsigned int len;
157 double *array;
158};
159
160
e277c2f1 161static inline size_t lttng_get_size_sequence_mystruct_mysequence(
6ef4987c 162 lttng_sequence_mystruct_mysequence *obj)
163{
164 size_t size=0, locsize;
165
166 locsize = sizeof(unsigned int);
167 size += ltt_align(size, locsize) + locsize;
168
169 locsize = sizeof(double);
170 size += ltt_align(size, locsize) + (obj->len * locsize);
171
172 return size;
173}
174
e277c2f1 175static inline size_t lttng_get_alignment_sequence_mystruct_mysequence(
6ef4987c 176 lttng_sequence_mystruct_mysequence *obj)
177{
178 size_t align=0, localign;
179
180 localign = sizeof(unsigned int);
181 align = max(align, localign);
182
183 localign = sizeof(double);
184 align = max(align, localign);
185
186 return align;
187}
188
189
e277c2f1 190static inline void lttng_write_sequence_mystruct_mysequence(
1c341713 191 void **to_base,
257d03bc 192 size_t *to,
6ef4987c 193 void **from,
194 size_t *len,
195 lttng_sequence_mystruct_mysequence *obj)
196{
257d03bc 197 size_t align;
6ef4987c 198 void *varfrom;
199 size_t varlen=0;
200
201 /* Flush pending memcpy */
202 if(*len != 0) {
1c341713 203 memcpy(*to_base+*to, *from, *len);
6ef4987c 204 *to += *len;
205 *len = 0;
206 }
207
208 align = lttng_get_alignment_sequence_mystruct_mysequence(obj);
209 //no need size = lttng_get_size_sequence_mystruct_mysequence(obj);
210
211 /* Align output */
1c341713 212 *to += ltt_align(*to, align); /* *len = 0 in this function */
6ef4987c 213
214 /* Copy members */
1c341713 215 *to += ltt_align(*to, sizeof(unsigned int));
6ef4987c 216 varfrom = &obj->len;
217 varlen += sizeof(unsigned int);
1c341713 218 memcpy(*to_base+*to, varfrom, varlen);
219 *to += varlen;
6ef4987c 220 varlen = 0;
221
1c341713 222 *to += ltt_align(*to, sizeof(double));
6ef4987c 223 varfrom = obj->array;
224 varlen += obj->len * sizeof(double);
1c341713 225 memcpy(*to_base+*to, varfrom, varlen);
226 *to += varlen;
6ef4987c 227 varlen = 0;
228
1c341713 229 /* Realign the *to_base on arch size, set *to to 0 */
230 *to = ltt_align(*to, sizeof(void *));
231 *to_base = *to_base+*to;
232 *to = 0;
a6cf4981 233
6ef4987c 234 /* Put source *from just after the C sequence */
235 *from = obj+1;
236}
237
238
239
240union lttng_mystruct_myunion {
241 double myfloat;
242 unsigned long myulong;
243};
244
245
e277c2f1 246static inline size_t lttng_get_size_mystruct_myunion(
6ef4987c 247 union lttng_mystruct_myunion *obj)
248{
249 size_t size=0, locsize;
250
251 locsize = sizeof(double);
252 size = max(size, locsize);
253
254 locsize = sizeof(unsigned long);
255 size = max(size, locsize);
256
257 BUG_ON(size != sizeof(union lttng_mystruct_myunion));
258
259 return size;
260}
261
262
e277c2f1 263static inline size_t lttng_get_alignment_mystruct_myunion(
6ef4987c 264 union lttng_mystruct_myunion *obj)
265{
266 size_t align=0, localign;
267
268 localign = sizeof(double);
269 align = max(align, localign);
270
271 localign = sizeof(unsigned long);
272 align = max(align, localign);
273
274 return align;
275}
276
277
e277c2f1 278static inline void lttng_write_mystruct_myunion(
1c341713 279 void **to_base,
257d03bc 280 size_t *to,
6ef4987c 281 void **from,
282 size_t *len,
283 union lttng_mystruct_myunion *obj)
284{
1c341713 285 size_t align, size;
6ef4987c 286
287 align = lttng_get_alignment_mystruct_myunion(obj);
288 size = lttng_get_size_mystruct_myunion(obj);
289
290 if(*len == 0) {
1c341713 291 *to += ltt_align(*to, align); /* align output */
6ef4987c 292 } else {
1c341713 293 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 294 }
295
296 *len += size;
297}
298
299
300struct lttng_mystruct {
301 unsigned int irq_id;
302 enum lttng_irq_mode mode;
303 struct lttng_mystruct2 teststr;
304 lttng_array_mystruct_myarray myarray;
305 lttng_sequence_mystruct_mysequence mysequence;
306 union lttng_mystruct_myunion myunion;
307};
308
e277c2f1 309static inline size_t lttng_get_size_mystruct(
6ef4987c 310 struct lttng_mystruct *obj)
311{
312 size_t size=0, locsize, localign;
313
314 locsize = sizeof(unsigned int);
315 size += ltt_align(size, locsize) + locsize;
316
317 locsize = sizeof(enum lttng_irq_mode);
318 size += ltt_align(size, locsize) + locsize;
319
320 localign = lttng_get_alignment_mystruct2(&obj->teststr);
321 locsize = lttng_get_size_mystruct2(&obj->teststr);
322 size += ltt_align(size, localign) + locsize;
323
324 localign = lttng_get_alignment_array_mystruct_myarray(obj->myarray);
325 locsize = lttng_get_size_array_mystruct_myarray(obj->myarray);
326 size += ltt_align(size, localign) + locsize;
327
328 localign = lttng_get_alignment_sequence_mystruct_mysequence(&obj->mysequence);
329 locsize = lttng_get_size_sequence_mystruct_mysequence(&obj->mysequence);
330 size += ltt_align(size, localign) + locsize;
331
332 localign = lttng_get_alignment_mystruct_myunion(&obj->myunion);
333 locsize = lttng_get_size_mystruct_myunion(&obj->myunion);
334 size += ltt_align(size, localign) + locsize;
335
336 return size;
337}
338
339
e277c2f1 340static inline size_t lttng_get_alignment_mystruct(
6ef4987c 341 struct lttng_mystruct *obj)
342{
343 size_t align=0, localign;
344
345 localign = sizeof(unsigned int);
346 align = max(align, localign);
347
348 localign = sizeof(enum lttng_irq_mode);
349 align = max(align, localign);
350
351 localign = lttng_get_alignment_mystruct2(&obj->teststr);
352 align = max(align, localign);
353
354 localign = lttng_get_alignment_array_mystruct_myarray(obj->myarray);
355 align = max(align, localign);
356
357 localign = lttng_get_alignment_sequence_mystruct_mysequence(&obj->mysequence);
358 align = max(align, localign);
359
360 localign = lttng_get_alignment_mystruct_myunion(&obj->myunion);
361 align = max(align, localign);
362
363 return align;
364}
365
e277c2f1 366static inline void lttng_write_mystruct(
1c341713 367 void **to_base,
257d03bc 368 size_t *to,
6ef4987c 369 void **from,
370 size_t *len,
371 struct lttng_mystruct *obj)
372{
1c341713 373 size_t align, size;
6ef4987c 374
375 align = lttng_get_alignment_mystruct(obj);
376 // no need : contains variable size fields.
377 // locsize = lttng_get_size_mystruct(obj);
378
379 if(*len == 0) {
257d03bc 380 *to += ltt_align(*to, align); /* align output */
6ef4987c 381 } else {
257d03bc 382 *len += ltt_align(*to+*len, align); /* C alignment, ok to do a memcpy of it */
6ef4987c 383 }
384
385 /* Contains variable sized fields : must explode the structure */
386
1c341713 387 size = sizeof(unsigned int);
388 size += ltt_align(*to+*len, size) + size;
389 *len += size;
6ef4987c 390
1c341713 391 size = sizeof(enum lttng_irq_mode);
392 size += ltt_align(*to+*len, size) + size;
393 *len += size;
6ef4987c 394
1c341713 395 lttng_write_mystruct2(to_base, to, from, len, &obj->teststr);
6ef4987c 396
1c341713 397 lttng_write_array_mystruct_myarray(to_base, to, from, len, obj->myarray);
6ef4987c 398
399 /* Variable length field */
1c341713 400 lttng_write_sequence_mystruct_mysequence(to_base, to, from, len, &obj->mysequence);
53f58ee1 401 //*to = 0; /* Force the compiler to know it's 0 */
1c341713 402 /* After this previous write, we are sure that *to is 0, and *to_base is
403 * aligned on the architecture size : to rest of alignment will be calculated
404 * statically. */
405
1c341713 406 lttng_write_mystruct_myunion(to_base, to, from, len, &obj->myunion);
6ef4987c 407
408 /* Don't forget to flush last write..... */
409}
410
411
412
413
414
415
257d03bc 416//void main()
417void test()
6ef4987c 418{
419 struct lttng_mystruct test;
420 test.mysequence.len = 20;
421 test.mysequence.array = malloc(20);
422
423 size_t size = lttng_get_size_mystruct(&test);
424 size_t align = lttng_get_alignment_mystruct(&test);
425
426 void *buf = malloc(align + size);
1c341713 427 void *to_base = buf; /* the buffer is allocated on arch_size alignment */
257d03bc 428 size_t to = 0;
6ef4987c 429 void *from = &test;
430 size_t len = 0;
431
1c341713 432 lttng_write_mystruct(&to_base, &to, &from, &len, &test);
6ef4987c 433 /* Final flush */
434 /* Flush pending memcpy */
435 if(len != 0) {
1c341713 436 memcpy(to_base+to, from, len);
6ef4987c 437 to += len;
438 from += len;
439 len = 0;
440 }
441
442 free(test.mysequence.array);
443 free(buf);
444}
This page took 0.040549 seconds and 4 git commands to generate.