Thread: __attribute__((aligned(x))) - Declare that a struct should be aligned to x

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    __attribute__((aligned(x))) - Declare that a struct should be aligned to x

    I need to define a structure who's elements are unaligned(no space in between like with __attribute__((packed))) but the structure itself needs to be 4kb aligned. How do I do that?

    Here's my first guess, but I don't know how to check to see if it's aligned correctly:
    Code:
    typedef struct page_struct{
        unsigned int entry[1024] __attribute__((packed));
    } __attribute__((aligned(4096))) page_dir_t page_table_t;

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You shouldn't need any special declarations. Just make sure that when you allocate space for the page table that the space is page aligned.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Print the address (in hex), and make sure the right-most 12 bits are zero

    It should be 0x?????000

    BTW, a struct with only one element (even an array) will be packed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    > You shouldn't need any special declarations. Just make sure that when you allocate space for the page table that the space is page aligned.
    That's the point of trying to align it to 4kb. I don't plan on ever enabling PAE, so I should be fine with 4kb.

    > Print the address (in hex), and make sure the right-most 12 bits are zero
    Yeah... Sorry. I made a test prog and it works.

    > BTW, a struct with only one element (even an array) will be packed.
    Brain fart. I though for some reason that GCC might align the elements, I just realized that that GCC doing that would break all constructs based around the concept of arrays and pointers.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by User Name: View Post
    > You shouldn't need any special declarations. Just make sure that when you allocate space for the page table that the space is page aligned.
    That's the point of trying to align it to 4kb. I don't plan on ever enabling PAE, so I should be fine with 4kb.
    I'm not sure how you expect alignment attributes to cause the structure itself to be 4k aligned. Alignment affects the members of the struct. If you want the entire struct to be aligned on a 4k boundary then you simply need to allocate it that way.

    Suppose you used malloc() to allocate one of these structures (you probably aren't, since I have a good idea what you are doing here, but for instance). malloc() will align allocations to 8 bytes on its own, but it has no idea that the thing you are trying to allocate has a stricter alignment requirement than that. It's your own responsibility to make sure it aligns and the compiler can't really help you with it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    > Alignment affects the members of the struct. If you want the entire struct to be aligned on a 4k boundary then you simply need to allocate it that way.
    If declared at compile time, it affects the structure alignment as well. See
    Code:
    typedef struct page_struct{
    	unsigned int entry[1024];
    }__attribute__((aligned(4096))) page_dir_t, page_table_t; /* athough their uses are different, they have the same structure */
    
    int main()
    {
    	page_table_t t;
    	printf("%p", &t);
        return 0;
    }
    Always prints 0x*000.

    BTW, I don't even have a malloc yet. I'll probably ask you quite a few questions when I start on it. Such as: Does malloc return addresses from a common pool of memory(system wide) or process specific pool?
    Last edited by User Name:; 07-23-2010 at 10:55 AM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by User Name: View Post
    If declared at compile time, it affects the structure alignment as well.
    Static alignment requirements are usually handled by declaring the structure in it's own .c file and then using linker directives to make sure the linker places it at the appropriate location. As you get deeper into this you'll probably want to control not only the alignment but the exact address where it goes, and ONLY the linker can do that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Request two pages worth of storage from malloc(), then plant the struct at the address that's divisible by 4k.
    Quote Originally Posted by User Name: View Post
    Does malloc return addresses from a common pool of memory(system wide) or process specific pool?
    The malloc() family taps the data segment of a process for the requested storage.

  9. #9
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Yeah, I've become quite familiar with ld linker scripts lately.

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    > The malloc() family taps the data segment of a process for the requested storage.
    Thanks, I'll have to think about how to implement it like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Realloc()
    By krazyxazn in forum C Programming
    Replies: 10
    Last Post: 01-27-2010, 10:05 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM