align.c

text/plain

Filename: align.c
Type: text/plain
Part: 0
Message: Re: Relation bulk write facility
#include <stdio.h>
#include <stdint.h>

/* add a byte so the compiler has to go out of its way to achieve alignment for
   consecutive instances */
typedef union PGIOAlignedBlock
{
	__attribute__((aligned(4096)))
	char		data[1 + 8192];
	double		force_align_d;
	uint64_t	force_align_i64;
} PGIOAlignedBlock;

char		pad;

/* with and without each of: static, const, initializer */
PGIOAlignedBlock al4096;
PGIOAlignedBlock al4096_initialized = {{0}};
const PGIOAlignedBlock al4096_const;
const PGIOAlignedBlock al4096_const_initialized = {{0}};
static PGIOAlignedBlock al4096_static;
static PGIOAlignedBlock al4096_static_initialized = {{0}};
static const PGIOAlignedBlock al4096_static_const;
static const PGIOAlignedBlock al4096_static_const_initialized = {{0}};

/* in case last position is special */
static const PGIOAlignedBlock last;
static const PGIOAlignedBlock last_initialized = {{0}};

#define DUMP(want_align, var) dump_internal(#var, (want_align), &(var))

static void
dump_internal(const char *ident, unsigned want_align, const void *addr)
{
	unsigned	mod = (uintptr_t) addr % want_align;

	printf("%-32s %4u @ 0x%p (mod %u%s)\n", ident, want_align, addr,
		   mod, mod ? " - BUG" : "");
}

int
main(int argc, char **argv)
{
	DUMP(4096, al4096);
	DUMP(4096, al4096_initialized);
	DUMP(4096, al4096_const);
	DUMP(4096, al4096_const_initialized);
	DUMP(4096, al4096_static);
	DUMP(4096, al4096_static_initialized);
	DUMP(4096, al4096_static_const);
	DUMP(4096, al4096_static_const_initialized);
	return 0;
}