how to get aliagned memory using malloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dhirendra Pal Singh

    #1

    how to get aliagned memory using malloc

    Hi all,
    I have couple of questions, I hope I can find the answers
    here...(assumin g the question are okay with this group)

    A) what is memory alignment? I have a rough idea but cant clearly
    understand it clearly...? If anyone has a pointer on net please tell
    me.. I would be grateful...
    Some told me that for memory to be aligned, the last 6 digits of the
    address should be zero? is this correct?


    B) so how can I write a function for example...
    void * align_malloc(si ze_t size_of_memory_ required, int
    bit_alignement_ required)

    Any help would be appreciated..

    Thanks and advance

    Dp
  • Lew Pitcher

    #2
    Re: how to get aliagned memory using malloc

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Dhirendra Pal Singh wrote:[color=blue]
    > Hi all,
    > I have couple of questions, I hope I can find the answers
    > here...(assumin g the question are okay with this group)
    >
    > A) what is memory alignment? I have a rough idea but cant clearly
    > understand it clearly...? If anyone has a pointer on net please tell
    > me.. I would be grateful...
    > Some told me that for memory to be aligned, the last 6 digits of the
    > address should be zero? is this correct?[/color]

    Perhaps. Perhaps not. It depends on the internals of your platform.

    "Memory alignment" is the recognition that the underlying platform may
    have restrictions on where certain data types may be placed, in relation
    to the space available in which to place them. Some platforms require
    that certain data types be placed so that they start only on 'even
    number addresses', or 'addresses that are divisible by 4' or some other
    requirement. Some platforms have no requirements of this nature. The
    requirement that limits the addresses that a data type can be placed at
    is called "memory alignment".

    The C standard specifies that malloc() and family will allocate memory
    so that the address can be used for any data type. This means that
    malloc() and family must be familiar with and able to accomodate the
    restrictions the underlying platform place on the arrangement of data
    items in memory. Not an easy task for portable code to accomplish, which
    is why it is part of the implementation of a compiler.
    [color=blue]
    >
    > B) so how can I write a function for example...
    > void * align_malloc(si ze_t size_of_memory_ required, int
    > bit_alignement_ required)[/color]

    First off, your function will, by necessity, be specific to your
    platform, and will not be portable.

    Before you write such a function, you need to know
    a) the mechanics of how your compiler arranges for a program to store
    each data item. Typically, this will be an examination of the
    assembly or machine code that your compiler generates, to determine
    data type size, and store/load instruction type.
    b) what restrictions your platform places on the placement of each data
    type manipulated by the instructions generated by your compiler.

    - From these, you can determine what machine restrictions your compiler
    will have to contend with when accessing data types stored in an
    align_malloc()' ed block of memory.

    Once you have determined the restrictions, code your function to
    allocate a block of memory, such that the block of memory starts at an
    address that satisfies /all/ the alignment restrictions imposed by your
    compiler on your platform. Then, return the address that the block
    starts at.

    [color=blue]
    > Any help would be appreciated..
    >
    > Thanks and advance
    >
    > Dp[/color]


    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFCE5D1agV FX4UWr64RAqK2AK DnZcStlcuRODzkJ 95P5r9QOfq20gCg inP0
    y7+QXCh6gDBSU7K xuxQK4JU=
    =SDVo
    -----END PGP SIGNATURE-----

    Comment

    • Dhirendra Pal Singh

      #3
      Re: how to get aliagned memory using malloc



      Lew Pitcher wrote:[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Dhirendra Pal Singh wrote:
      >[color=green]
      >>Hi all,
      >>I have couple of questions, I hope I can find the answers
      >>here...(assum ing the question are okay with this group)
      >>
      >>A) what is memory alignment? I have a rough idea but cant clearly
      >>understand it clearly...? If anyone has a pointer on net please tell
      >>me.. I would be grateful...
      >>Some told me that for memory to be aligned, the last 6 digits of the
      >>address should be zero? is this correct?[/color]
      >
      >
      > Perhaps. Perhaps not. It depends on the internals of your platform.[/color]

      So if I am using intel pentimum chipset, then would be correct? ie the
      last 6 digits would be 0...
      Actually someone asked me in an interview, so I was wondering what would
      be best answere, asuming I am working on Intel Pentinum?
      [color=blue]
      >
      > "Memory alignment" is the recognition that the underlying platform may
      > have restrictions on where certain data types may be placed, in relation
      > to the space available in which to place them. Some platforms require
      > that certain data types be placed so that they start only on 'even
      > number addresses', or 'addresses that are divisible by 4' or some other
      > requirement. Some platforms have no requirements of this nature. The
      > requirement that limits the addresses that a data type can be placed at
      > is called "memory alignment".
      >
      > The C standard specifies that malloc() and family will allocate memory
      > so that the address can be used for any data type. This means that
      > malloc() and family must be familiar with and able to accomodate the
      > restrictions the underlying platform place on the arrangement of data
      > items in memory. Not an easy task for portable code to accomplish, which
      > is why it is part of the implementation of a compiler.
      >
      >[color=green]
      >>B) so how can I write a function for example...
      >>void * align_malloc(si ze_t size_of_memory_ required, int
      >>bit_alignemen t_required)[/color]
      >
      >
      > First off, your function will, by necessity, be specific to your
      > platform, and will not be portable.
      >
      > Before you write such a function, you need to know
      > a) the mechanics of how your compiler arranges for a program to store
      > each data item. Typically, this will be an examination of the
      > assembly or machine code that your compiler generates, to determine
      > data type size, and store/load instruction type.
      > b) what restrictions your platform places on the placement of each data
      > type manipulated by the instructions generated by your compiler.
      >
      > - From these, you can determine what machine restrictions your compiler
      > will have to contend with when accessing data types stored in an
      > align_malloc()' ed block of memory.
      >
      > Once you have determined the restrictions, code your function to
      > allocate a block of memory, such that the block of memory starts at an
      > address that satisfies /all/ the alignment restrictions imposed by your
      > compiler on your platform. Then, return the address that the block
      > starts at.
      >
      >
      >[color=green]
      >>Any help would be appreciated..
      >>
      >>Thanks and advance
      >>
      >>Dp[/color]
      >
      >
      >
      > - --
      >
      > Lew Pitcher, IT Specialist, Enterprise Data Systems
      > Enterprise Technology Solutions, TD Bank Financial Group
      >
      > (Opinions expressed here are my own, not my employer's)
      > -----BEGIN PGP SIGNATURE-----
      > Version: GnuPG v1.2.4 (MingW32)
      >
      > iD8DBQFCE5D1agV FX4UWr64RAqK2AK DnZcStlcuRODzkJ 95P5r9QOfq20gCg inP0
      > y7+QXCh6gDBSU7K xuxQK4JU=
      > =SDVo
      > -----END PGP SIGNATURE-----[/color]

      Comment

      • Thomas Matthews

        #4
        Re: how to get aliagned memory using malloc

        Dhirendra Pal Singh wrote:[color=blue]
        > Hi all,
        > I have couple of questions, I hope I can find the answers
        > here...(assumin g the question are okay with this group)
        >
        > A) what is memory alignment? I have a rough idea but cant clearly
        > understand it clearly...? If anyone has a pointer on net please tell
        > me.. I would be grateful...
        > Some told me that for memory to be aligned, the last 6 digits of the
        > address should be zero? is this correct?[/color]

        Use your favorite search engine and search the newsgroups
        for "alignment Thomas Matthews". I already posted a lengthy
        dissertation on alignment.

        [color=blue]
        > B) so how can I write a function for example...
        > void * align_malloc(si ze_t size_of_memory_ required, int
        > bit_alignement_ required)[/color]

        The C language does not require that malloc return a
        pointer to an aligned area of memory.

        The only guaranteed method is to allocate more memory than
        required and create a pointer to the first aligned address
        within that memory space.
        [color=blue]
        >
        > Any help would be appreciated..
        >
        > Thanks and advance
        >
        > Dp[/color]



        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book
        http://www.sgi.com/tech/stl -- Standard Template Library

        Comment

        • Keith Thompson

          #5
          Re: how to get aliagned memory using malloc

          Thomas Matthews <Thomas_Matthew sSpamBotsSuck@s bcglobal.net> writes:[color=blue]
          > Dhirendra Pal Singh wrote:[/color]
          [...][color=blue][color=green]
          >> B) so how can I write a function for example...
          >> void * align_malloc(si ze_t size_of_memory_ required, int
          >> bit_alignement_ required)[/color]
          >
          > The C language does not require that malloc return a
          > pointer to an aligned area of memory.
          >
          > The only guaranteed method is to allocate more memory than
          > required and create a pointer to the first aligned address
          > within that memory space.[/color]

          It guarantees that the allocated memory is suitably aligned for any
          type of object. If you need stricter alignment than that for some
          reason, you'll need to do something system-specific.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Mark McIntyre

            #6
            Re: how to get aliagned memory using malloc

            On Thu, 17 Feb 2005 17:43:18 GMT, in comp.lang.c , Dhirendra Pal Singh
            <abcd@sbcglobal .net> wrote:
            [color=blue]
            >Lew Pitcher wrote:[color=green]
            >> Dhirendra Pal Singh wrote:[color=darkred]
            >>>Some told me that for memory to be aligned, the last 6 digits of the
            >>>address should be zero? is this correct?[/color]
            >>
            >> Perhaps. Perhaps not. It depends on the internals of your platform.[/color]
            >
            >So if I am using intel pentimum chipset, then would be correct? ie the
            >last 6 digits would be 0...[/color]

            Perhaps, perhaps not. You'd need to ask in a newsgroup which knew about
            your hardware and OS.


            --
            Mark McIntyre
            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

            ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
            http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
            ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

            Comment

            • Lawrence Kirby

              #7
              Re: how to get aliagned memory using malloc

              On Thu, 17 Feb 2005 17:43:18 +0000, Dhirendra Pal Singh wrote:
              [color=blue]
              >
              >
              > Lew Pitcher wrote:[color=green]
              >> -----BEGIN PGP SIGNED MESSAGE-----
              >> Hash: SHA1
              >>
              >> Dhirendra Pal Singh wrote:
              >>[color=darkred]
              >>>Hi all,
              >>>I have couple of questions, I hope I can find the answers
              >>>here...(assu ming the question are okay with this group)
              >>>
              >>>A) what is memory alignment? I have a rough idea but cant clearly
              >>>understand it clearly...? If anyone has a pointer on net please tell
              >>>me.. I would be grateful...
              >>>Some told me that for memory to be aligned, the last 6 digits of the
              >>>address should be zero? is this correct?[/color]
              >>
              >>
              >> Perhaps. Perhaps not. It depends on the internals of your platform.[/color]
              >
              > So if I am using intel pentimum chipset, then would be correct? ie the
              > last 6 digits would be 0...[/color]

              That would be up to the specific compiler/library but it is unlikely. For
              example 32 bit values are likely to be 4 byte aligned i.e. the address is
              a multiple of 4 which would typically mean the last 2 bits are zero.
              [color=blue]
              > Actually someone asked me in an interview, so I was wondering what would
              > be best answere, asuming I am working on Intel Pentinum?[/color]

              Alignment requirements are type-specific. x86 processors can access values
              at any address but aligned access is faster so x86 compilers tend to
              align objects.

              Lawrence

              Comment

              • Chris Torek

                #8
                Re: how to get aliagned memory using malloc

                In article <pan.2005.02.18 .12.03.36.12500 0@netactive.co. uk>
                Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=blue]
                >Alignment requirements are type-specific. x86 processors can access values
                >at any address ...[/color]

                (except SSE instructions)
                [color=blue]
                >but aligned access is faster so x86 compilers tend to align objects.[/color]

                Yes.

                It is also worth noting (despite its off-topic-ness) that many
                architectures have stricter "super-alignment" requirements for
                types and/or operations that are not generated by C compilers.
                Even the x86 recommends (I think) 128-byte alignment for some
                multiprocessor synchronization operations (mutexes in MP systems).
                (These are usually connected to cache line sizes, which tend to
                run 16, 32, or 64 bytes today.)
                --
                In-Real-Life: Chris Torek, Wind River Systems
                Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                email: forget about it http://web.torek.net/torek/index.html
                Reading email is like searching for food in the garbage, thanks to spammers.

                Comment

                Working...