Malloc Allignment
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 48

Thread: Malloc Allignment

  1. #1
    Join Date
    Jun 2009
    Posts
    89

    Malloc Allignment

    Hi,

    consider that you have a function that gets size of bytes , and returns a pointer to the beginning of the block.


    void* Malloc_Alllign(size_t size)
    {

    }



    How can we make sure that our function will allocate a block of bytes in the memory , and the address of the beginning of the block will always be divided by 16.??

    Thank you

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,145

    Re: Malloc Allignment

    I must admit, I don't really understand your questions. So my answers may not be what you are looking for...

    Quote Originally Posted by wael.salman
    How can we make sure that our function will allocate a block of bytes in the memory
    If you use malloc, then unless it returns NULL then you can be sure that it has successfully allocated the requested number of bytes in one contiguous block.

    Quote Originally Posted by wael.salman
    and the address of the beginning of the block will always be divided by 16.??
    Ok, I really don't understand this part of the question. Do you really mean that the address is to be divisible by 16??? If so, that is a really odd requirement! The only thing that I can think of, is that you are asking how to allocate successive adjacent blocks of 16 bytes (making sure they sit next to eachother in memory)... if that is correct, then you could create a memory pool. That way you can control the the layout of the dynamic memory for your application.

    Please can you give a better explanation of what you require.

  3. #3
    Join Date
    Jun 2009
    Posts
    89

    Re: Malloc Allignment

    Ok , I will explain.

    when you use malloc, you allocate a bloc of memory, and returns pointer that holds that address of the block's beginning. That address can be anything.

    consider that you want to allocate memory block exactly in an address that is divisible by 16.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Malloc Allignment

    Quote Originally Posted by wael.salman View Post
    Ok , I will explain.

    when you use malloc, you allocate a bloc of memory, and returns pointer that holds that address of the block's beginning. That address can be anything.

    consider that you want to allocate memory block exactly in an address that is divisible by 16.
    We know this already. The question is -- why do you want to do this?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,145

    Re: Malloc Allignment

    But why would you want to do that? It is a very strange requirement. The fact that you want to do it is a little concerning.

  6. #6
    Join Date
    Jun 2009
    Posts
    89

    Re: Malloc Allignment

    I do not need it . i just want to know if I can do that or C, C++ has the ability to provide thing like that.

  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,145

    Re: Malloc Allignment

    It doesn't make sense to do it. Why would you want to do it?

    The location of the memory allocation is up to the system on which the application is running. As such, under normal circumstances where you have no control of where the system allocates memory, it is not possible to specify that the address of the first block of the allocated memory is divisible by 16. This is not only true for C and C++ but any language that I have ever worked with.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: Malloc Allignment

    There may be a need to do it in something like a device driver where, say, a DMA controller can only be given an address that lays on a certain alignment boundary.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Jun 2009
    Posts
    89

    Re: Malloc Allignment

    Thanx Jhon...

    can you tell me how to do that please??

  10. #10
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Malloc Allignment

    Quote Originally Posted by wael.salman View Post
    Thanx Jhon...

    can you tell me how to do that please??
    So JohnW and you work together? Why did you not answer the question?

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Jul 2006
    Posts
    21

    Re: Malloc Allignment

    Embedded system programming does see a lot of alignment requirements like this when dealing with some devices.

    If you need to create a 16-byte aligned block of memory, you will have to do it yourself as the only constraint on malloc is that it returns a pointer that is suitably aligned to reference any such object allocated. Typically this will be a 4-byte or 8-byte aligned address, depending upon the architecture.

    To guarentee 16-byte alignment, you should allocate a block of memory with an extra 15 bytes (which is used as padding). The aligned pointer can be obtained by ((original_ptr + 15) & ~0xF).

    Please note that you have to be sure to use the original_ptr when calling free(). Since you need to keep track of both the aligned pointer and the original pointer, it is best to encapsulate everything into a class which will handle the allocation and alignment, instead of emulating the malloc() routine with Malloc_Align().

    Mark Pauna

  12. #12
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,145

    Re: Malloc Allignment

    Quote Originally Posted by John@Wessex
    There may be a need to do it in something like a device driver where, say, a DMA controller can only be given an address that lays on a certain alignment boundary.
    That's a fair enough point.

    However, the literal answer to the OP's question "How can we make sure that our function will allocate a block of bytes in the memory , and the address of the beginning of the block will always be divided by 16?" is that you cannot do it.

    I like Mark Pauna's solution that supplies an equivalent with a disclaimer for freeing the memory. I must admit though, if stuck with C then I would probably employ a memory pool to handle the returning of aligned pointers to dynamically allocated memory - for some reason it feels like a more contained solution. But I agree that if in C++ then a simple class for handling the memory management would be the most suitable solution.

  13. #13
    Join Date
    Jun 2009
    Posts
    89

    Re: Malloc Allignment

    Hii

    You said

    ((original_ptr + 15) & ~0xF).

    why + 15??
    what & ~0xF gives you ??

  14. #14
    Join Date
    Jul 2006
    Posts
    21

    Re: Malloc Allignment

    15 is the maximum number of padding bytes required to get to a 16-byte aligned address.
    ~0xF is equivalent to 0xFFFFFFF0 (or 0xFFFFFFFFFFFFFFF0, depending upon your architecture) and is a mask which can be used to force an alignment to a 16-byte boundary.

    Examples:
    malloc returns 0x00000000 -> 0x00000000 + 15 = 0x0000000F & ~0x00000000F = 0x00000000
    malloc returns 0x00000001 -> 0x00000001 + 15 = 0x00000010 & ~0x00000000F = 0x00000010
    malloc returns 0x00000002 -> 0x00000002 + 15 = 0x00000011 & ~0x00000000F = 0x00000010
    malloc returns 0x00000003 -> 0x00000003 + 15 = 0x00000012 & ~0x00000000F = 0x00000010
    ...
    malloc returns 0x0000000F -> 0x0000000F + 15 = 0x0000001E & ~0x00000000F = 0x00000010
    malloc returns 0x00000010 -> 0x00000010 + 15 = 0x0000001F & ~0x00000000F = 0x00000010
    malloc returns 0x00000011 -> 0x00000011 + 15 = 0x00000020 & ~0x00000000F = 0x00000020

    In general, to force an alignment to any power of 2 bytes, use the formula:
    ((char*)unaligned_addr + (alignment-1)) & ~ (size_t)(alignment-1)

    Notes: The (char*)unaligned_addr cast is required so that the additive operator is in bytes.
    The (size_t)(alignment-1) cast is required so that the complement operator argumentis the same size as a pointer.

    Mark Pauna
    Last edited by mpauna; July 6th, 2009 at 03:00 PM.

  15. #15
    Join Date
    Jun 2009
    Posts
    89

    Re: Malloc Allignment

    waoo.. This is great. Thank you so much - So clear

Page 1 of 4 1234 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center