Created
November 15, 2016 23:31
-
-
Save grooverdan/3cda0a8ed62f05f897f24c34b3a6ed08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat align-test.c | |
#include <stdlib.h> | |
/* from include/my_compiler.h */ | |
#if defined __GNUC__ || defined __SUNPRO_C || defined __SUNPRO_CC | |
/* Specifies the minimum alignment of a type. */ | |
# define MY_ALIGNOF(type) __alignof__(type) | |
/* Determine the alignment requirement of a type. */ | |
# define MY_ALIGNED(n) __attribute__((__aligned__((n)))) | |
/* Microsoft Visual C++ */ | |
#elif defined _MSC_VER | |
# define MY_ALIGNOF(type) __alignof(type) | |
# define MY_ALIGNED(n) __declspec(align(n)) | |
#else /* Make sure they are defined for other compilers. */ | |
# define MY_ALIGNOF(type) | |
# define MY_ALIGNED(size) | |
#endif | |
#ifndef CPU_LEVEL1_DCACHE_LINESIZE | |
#if defined(__s390__) | |
#define CPU_LEVEL1_DCACHE_LINESIZE 256 | |
#elif defined(__powerpc__) || defined(__aarch64__) | |
#define CPU_LEVEL1_DCACHE_LINESIZE 128 | |
#else | |
#define CPU_LEVEL1_DCACHE_LINESIZE 64 | |
#endif | |
#endif | |
struct s_one { | |
char first[30] MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE); | |
}; | |
struct s_multiple { | |
char first; | |
unsigned long two; | |
char three; | |
char four[40] MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE); | |
char last; | |
}; | |
struct s_one one_arr[2]; | |
struct s_multiple multiple_arr[2]; | |
struct s_simple { | |
char first[30]; | |
}; | |
struct s_simple simple_arr[2]; | |
#include <stdio.h> | |
#include <stddef.h> | |
int main() | |
{ | |
printf("Alignment %x\n\n", CPU_LEVEL1_DCACHE_LINESIZE); | |
printf("sizeof(one) %zx\n", sizeof(struct s_one)); | |
printf("one[0] %p\n", &one_arr[0].first); | |
printf("one[1] %p\n", &one_arr[1].first); | |
printf("one ptrdiff %tx\n", (ptrdiff_t) (&one_arr[1].first - &one_arr[0].first)); | |
printf("\nsizeof(multiple) %zx\n", sizeof(struct s_multiple)); | |
printf("multiple[0].first %p\n", &multiple_arr[0].first); | |
printf("multiple[0].four %p\n", &multiple_arr[0].four); | |
printf("multiple[1].first %p\n", &multiple_arr[1].first); | |
printf("multiple diff %tx\n", (ptrdiff_t) (&multiple_arr[1].first - &multiple_arr[0].first)); | |
printf("\nsizeof(simple) %zx\n", sizeof(struct s_simple)); | |
printf("simple[0] %p\n", &simple_arr[0].first); | |
printf("simple[1] %p\n", &simple_arr[1].first); | |
printf("simple diff %tx\n", (ptrdiff_t) (&simple_arr[1].first - &simple_arr[0].first)); | |
struct s_multiple *dmultiple_arr, *d2multiple_arr; | |
dmultiple_arr=malloc(sizeof(struct s_multiple)*2); | |
printf("\ndmultiple[0].first %p\n", &dmultiple_arr[0].first); | |
printf("dmultiple[0].four %p\n", &dmultiple_arr[0].four); | |
printf("dmultiple[1].first %p\n", &dmultiple_arr[1].first); | |
printf("dmultiple[1].last %p\n", &dmultiple_arr[1].last); | |
printf("dmultiple diff %tx\n", (ptrdiff_t) (&dmultiple_arr[1].first - &dmultiple_arr[0].first)); | |
d2multiple_arr=malloc(sizeof(struct s_multiple)*2); | |
printf("\nd2multiple[0].first %p\n", &d2multiple_arr[0].first); | |
printf("d2multiple[0].four %p\n", &d2multiple_arr[0].four); | |
printf("d2multiple[1].first %p\n", &d2multiple_arr[1].first); | |
printf("d2multiple diff %tx\n", (ptrdiff_t) (&d2multiple_arr[1].first - &d2multiple_arr[0].first)); | |
printf("\nd2multiple.first - dmultiple.last diff %tx\n", (ptrdiff_t) (&d2multiple_arr[1].first - &dmultiple_arr[0].last)); | |
free(dmultiple_arr); | |
free(d2multiple_arr); | |
} | |
uname -m | |
x86_64 | |
gcc -o align-test align-test.c && ./align-test | |
Alignment 40 | |
sizeof(one) 40 | |
one[0] 0x6011c0 | |
one[1] 0x601200 | |
one ptrdiff 2 | |
sizeof(multiple) 80 | |
multiple[0].first 0x6010c0 | |
multiple[0].four 0x601100 | |
multiple[1].first 0x601140 | |
multiple diff 80 | |
sizeof(simple) 1e | |
simple[0] 0x601080 | |
simple[1] 0x60109e | |
simple diff 1 | |
dmultiple[0].first 0x104b420 | |
dmultiple[0].four 0x104b460 | |
dmultiple[1].first 0x104b4a0 | |
dmultiple[1].last 0x104b508 | |
dmultiple diff 80 | |
d2multiple[0].first 0x104b530 | |
d2multiple[0].four 0x104b570 | |
d2multiple[1].first 0x104b5b0 | |
d2multiple diff 80 | |
d2multiple.first - dmultiple.last diff 128 | |
gcc -o align-test align-test.c -ljemalloc && ./align-test | |
Alignment 40 | |
sizeof(one) 40 | |
one[0] 0x6011c0 | |
one[1] 0x601200 | |
one ptrdiff 2 | |
sizeof(multiple) 80 | |
multiple[0].first 0x6010c0 | |
multiple[0].four 0x601100 | |
multiple[1].first 0x601140 | |
multiple diff 80 | |
sizeof(simple) 1e | |
simple[0] 0x601080 | |
simple[1] 0x60109e | |
simple diff 1 | |
dmultiple[0].first 0x7f1b5a819000 | |
dmultiple[0].four 0x7f1b5a819040 | |
dmultiple[1].first 0x7f1b5a819080 | |
dmultiple[1].last 0x7f1b5a8190e8 | |
dmultiple diff 80 | |
d2multiple[0].first 0x7f1b5a819100 | |
d2multiple[0].four 0x7f1b5a819140 | |
d2multiple[1].first 0x7f1b5a819180 | |
d2multiple diff 80 | |
d2multiple.first - dmultiple.last diff 118 | |
ppc64le | |
gcc -o align-test align-test.c && ./align-test | |
Alignment 80 | |
sizeof(one) 80 | |
one[0] 0x10020380 | |
one[1] 0x10020400 | |
one ptrdiff 4 | |
sizeof(multiple) 100 | |
multiple[0].first 0x10020180 | |
multiple[0].four 0x10020200 | |
multiple[1].first 0x10020280 | |
multiple diff 100 | |
sizeof(simple) 1e | |
simple[0] 0x10020100 | |
simple[1] 0x1002011e | |
simple diff 1 | |
dmultiple[0].first 0x1001a310420 | |
dmultiple[0].four 0x1001a3104a0 | |
dmultiple[1].first 0x1001a310520 | |
dmultiple[1].last 0x1001a3105c8 | |
dmultiple diff 100 | |
d2multiple[0].first 0x1001a310630 | |
d2multiple[0].four 0x1001a3106b0 | |
d2multiple[1].first 0x1001a310730 | |
d2multiple diff 100 | |
d2multiple.first - dmultiple.last diff 268 | |
gcc -o align-test align-test.c -ljemalloc && ./align-test | |
Alignment 40 | |
sizeof(one) 40 | |
one[0] 0x6011c0 | |
one[1] 0x601200 | |
one ptrdiff 2 | |
sizeof(multiple) 80 | |
multiple[0].first 0x6010c0 | |
multiple[0].four 0x601100 | |
multiple[1].first 0x601140 | |
multiple diff 80 | |
sizeof(simple) 1e | |
simple[0] 0x601080 | |
simple[1] 0x60109e | |
simple diff 1 | |
dmultiple[0].first 0x7f1b5a819000 | |
dmultiple[0].four 0x7f1b5a819040 | |
dmultiple[1].first 0x7f1b5a819080 | |
dmultiple[1].last 0x7f1b5a8190e8 | |
dmultiple diff 80 | |
d2multiple[0].first 0x7f1b5a819100 | |
d2multiple[0].four 0x7f1b5a819140 | |
d2multiple[1].first 0x7f1b5a819180 | |
d2multiple diff 80 | |
d2multiple.first - dmultiple.last diff 118 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment