Thread: how to align linux address which is created using kmalloc

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    how to align linux address which is created using kmalloc

    I was given a task where I need to send the address(ex: 0x2add0010) to the firmware in which the lower 6 bits are reserved I need to send the address accordingly.

    Could you please tell how to align this one as I got this address when I requested the memory through kmalloc()

    Thank you,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose you could do something like this.
    Code:
    struct alignedmem {
        char alignment[64];  // for 6 bits
        char mem[];  // C99 Flexible Array Member
    };
    
    // then if size is the amount you want to malloc, then do
    struct alignedmem *mem = kmalloc( sizeof(*mem) + size );
    Now one element of alignment will have an address where the bottom 6 bits has the pattern you desire.
    All you then do is pass &mem->alignment[x] to your device.

    When you're done, you kfree(mem);
    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.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    Code:
    struct alignedmem {
        char alignment[64];  // for 6 bits
        char mem[];  // C99 Flexible Array Member
    };
    The alignment[] array actually only has to be 63 bytes long. If you need to offset by 64 bytes to align, then you're already aligned...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change file date created or modified (linux/unix/osx)
    By therealmuffin in forum C Programming
    Replies: 1
    Last Post: 03-30-2012, 03:40 AM
  2. structs created in same address?
    By nepper271 in forum C Programming
    Replies: 2
    Last Post: 06-07-2010, 08:11 PM
  3. right align
    By ke121885 in forum Linux Programming
    Replies: 1
    Last Post: 09-27-2009, 07:18 PM
  4. right align
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2009, 07:20 PM
  5. align syntax
    By mynickmynick in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2008, 04:54 AM