]> git.ozlabs.org Git - ccan/blob - ccan/alignof/_info
htable: fix tools/speed.
[ccan] / ccan / alignof / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * alignof - ALIGNOF() macro to determine alignment of a type.
7  *
8  * Many platforms have requirements that certain types must be aligned
9  * to certain address boundaries, such as ints needing to be on 4-byte
10  * boundaries.  Attempting to access variables with incorrect
11  * alignment may cause performance loss or even program failure (eg. a
12  * bus signal).
13  *
14  * There are times which it's useful to be able to programatically
15  * access these requirements, such as for dynamic allocators.
16  *
17  * Example:
18  *      #include <stdio.h>
19  *      #include <stdlib.h>
20  *      #include <ccan/alignof/alignof.h>
21  *
22  *      // Output contains "ALIGNOF(char) == 1"
23  *      // Will also print out whether an onstack char array can hold a long.
24  *      int main(int argc, char *argv[])
25  *      {
26  *              char arr[sizeof(int)];
27  *
28  *              printf("ALIGNOF(char) == %zu\n", ALIGNOF(char));
29  *              if ((unsigned long)arr % ALIGNOF(int)) {
30  *                      printf("arr %p CANNOT hold an int\n", arr);
31  *                      exit(1);
32  *              } else {
33  *                      printf("arr %p CAN hold an int\n", arr);
34  *                      exit(0);
35  *              }
36  *      }
37  *
38  * License: Public domain
39  * Author: Rusty Russell <rusty@rustcorp.com.au>
40  */
41 int main(int argc, char *argv[])
42 {
43         if (argc != 2)
44                 return 1;
45
46         if (strcmp(argv[1], "depends") == 0) {
47                 printf("ccan/build_assert\n");
48                 return 0;
49         }
50
51         return 1;
52 }