-
July 6th, 2009, 09:03 AM
#1
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
-
July 6th, 2009, 09:59 AM
#2
Re: Malloc Allignment
I must admit, I don't really understand your questions. So my answers may not be what you are looking for...
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.
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.
-
July 6th, 2009, 10:14 AM
#3
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.
-
July 6th, 2009, 10:17 AM
#4
Re: Malloc Allignment
Originally Posted by wael.salman
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
-
July 6th, 2009, 10:18 AM
#5
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.
-
July 6th, 2009, 10:32 AM
#6
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.
-
July 6th, 2009, 10:52 AM
#7
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.
-
July 6th, 2009, 10:59 AM
#8
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
-
July 6th, 2009, 11:02 AM
#9
Re: Malloc Allignment
Thanx Jhon...
can you tell me how to do that please??
-
July 6th, 2009, 11:06 AM
#10
Re: Malloc Allignment
Originally Posted by wael.salman
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
-
July 6th, 2009, 11:25 AM
#11
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
-
July 6th, 2009, 12:37 PM
#12
Re: Malloc Allignment
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.
-
July 6th, 2009, 01:02 PM
#13
Re: Malloc Allignment
Hii
You said
((original_ptr + 15) & ~0xF).
why + 15??
what & ~0xF gives you ??
-
July 6th, 2009, 01:55 PM
#14
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 02:00 PM.
-
July 6th, 2009, 03:01 PM
#15
Re: Malloc Allignment
waoo.. This is great. Thank you so much - So clear
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|