Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-2058

alignment declarations don't work if they precede the struct keyword

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • WT2.7.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      WiredTiger isn't aligning mutex and log structures.

      Here's a test program:

      $ cat t.c
      #include <inttypes.h>
      #include <stdio.h>
      #include <stdlib.h>
      
      __attribute__((aligned(64))) struct xx {
      	int a;
      };
      typedef struct xx XX;
      
      struct __attribute__((aligned(64))) yy {
      	int a;
      };
      typedef struct yy YY;
      
      int
      main(void)
      {
      	XX *a;
      	YY *b;
      
              printf("sizeof XX: %zu\n", sizeof(XX));
              a = malloc(10 * sizeof(XX));
              printf("XX array element size: %zu\n", 
                  (char *)&a[1] - (char *)&a[0]);
      
              printf("sizeof YY: %zu\n", sizeof(YY));
              b = malloc(10 * sizeof(YY));
              printf("YY array element size: %zu\n", 
                  (char *)&b[1] - (char *)&b[0]);
      
      	return (0);
      }
      $ gcc48 t.c; ./a.out
      sizeof XX: 4
      XX array element size: 4
      sizeof YY: 64
      YY array element size: 64
      $ gcc5 t.c; ./a.out
      sizeof XX: 4
      XX array element size: 4
      sizeof YY: 64
      YY array element size: 64
      $ clang t.c; ./a.out
      t.c:5:16: warning: attribute 'aligned' is ignored,
      place it after "struct" to apply attribute to type
      declaration [-Wignored-attributes]
      __attribute__((aligned(64))) struct xx {
                     ^
      1 warning generated.
      sizeof XX: 4
      XX array element size: 4
      sizeof YY: 64
      YY array element size: 64
      

      Note clang complains; however, if you precede the struct declaration with a typedef:

      typedef __attribute__((aligned(64))) struct xx {
              int a;
      } XX;
      
      typedef struct __attribute__((aligned(64))) yy {
              int a;
      } YY;
      

      then clang no longer detects the problem.

      $ clang t.c; ./a.out
      sizeof XX: 4
      XX array element size: 4
      sizeof YY: 64
      YY array element size: 64
      

      WiredTiger's page-locks and log slots are not being aligned to 64B boundaries because of this problem.

        1. t.c
          0.5 kB

            Assignee:
            keith.bostic@mongodb.com Keith Bostic (Inactive)
            Reporter:
            keith.bostic@mongodb.com Keith Bostic (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: