/* tag: Tom Lord Tue Dec  4 15:02:16 2001 (gen-alignment.to-c)
 */
/* gen-alignment.c - -*-c-*-
 *
 ****************************************************************
 * Copyright (C) 2000 Tom Lord
 * 
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */


#include <stdio.h>

#define TYPES \
  TYPE(char, char) \
  TYPE(short, short) \
  TYPE(int, int) \
  TYPE(long, long) \
  TYPE(long long, long_long) \
  TYPE(void *, pointer) \
  TYPE(float, float) \
  TYPE(double, double) \
  TYPE(long double, long_double)

#undef TYPE
#define TYPE(SCALAR, STRUCT) \
typedef struct \
{ \
  SCALAR a; \
  char b; \
} struct_ ## STRUCT;

TYPES


static size_t
machine_alignment (void)
{
  size_t best;
  size_t this;

  best = 1;
  
/* 
  technique borrowed from 

 Computing Memory Alignment Restrictions
     of Data Types in C and C++
          by Tom Lokovic

http://www.monkeyspeak.com/alignment/

*/
#undef TYPE
#define TYPE(SCALAR, STRUCT) \
  this = ((sizeof(struct_ ## STRUCT) > sizeof(SCALAR)) ? \
           sizeof(struct_ ## STRUCT) - sizeof(SCALAR) : sizeof(SCALAR)) ;\
  if (this > best) \
    best = this;

  TYPES
    return best;
}


int
main (int argc, char * argv[])
{
  printf ("/* Automatically generated by gen-alignment. DO NOT EDIT.\n");
  printf ("*/\n");
  printf ("#ifndef INCLUDE__MACHINE__ALIGNMENT_H\n");
  printf ("#define INCLUDE__MACHINE__ALIGNMENT_H\n");
  printf ("\n\n");
  printf ("#define MACHINE_ALIGNMENT %ld\n", (long)machine_alignment ());
  printf ("\n\n");
  printf ("#endif\n");
  return 0;
}
