Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By brewbuck

how to align linux address which is created using kmalloc

This is a discussion on how to align linux address which is created using kmalloc within the Linux Programming forums, part of the Platform Specific Boards category; I was given a task where I need to send the address(ex: 0x2add00 10 ) to the firmware in which ...

  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
    32,832
    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);
    vart likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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...
    Mario F. likes this.
    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, 04:40 AM
  2. structs created in same address?
    By nepper271 in forum C Programming
    Replies: 2
    Last Post: 06-07-2010, 09:11 PM
  3. right align
    By ke121885 in forum Linux Programming
    Replies: 1
    Last Post: 09-27-2009, 08:18 PM
  4. right align
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2009, 08:20 PM
  5. align syntax
    By mynickmynick in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2008, 05:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21