Solved

# 16 byte aligned

Posted on 2006-05-30
467 Views
Hi all,
What is memory that is 16 byte aligned? Thanks.
0
Question by:valleytech

LVL 12

Expert Comment

0

LVL 23

Expert Comment

It is memory whose address is a multiple of 16.  (The address%16 returns 0).
If such a memory address is viewed as a hexadecimal value, then the last digit would always be 0.

0

Author Comment

could you please give me any idea how to drive a function that can return a pointer to memory that is 16 byte aligned and at least numbytes round up to next 32 in length?. We can use malloc() that return numbytes of data with no alignment.
Thanks.
0

LVL 11

Assisted Solution

you can use something like this:

void *aligned16Malloc( unsigned size, unsigned char **alignedPtr )
{
unsigned char *myptr;
unsigned offset;

myptr = (unsigned char *)malloc( size + 15 );
offset = (unsigned)myptr & 0x0F;
if( offset != 0 )
*alignedPtr = myptr + (16 - offset);
else
*alignedPtr = myptr;
return( (void *)myptr );
}
---------------------------------------------------------------------------
And to get aligned pointer:
void *ptrForFree;
unsigned char *alignedPtr;

ptrForFree = aligned16Malloc( size, &alignedPtr );
... use of pointer ...
free( ptrForFree);
0

LVL 23

Expert Comment

You could use Doug Lea's malloc implementation.  It is not only fast, but it allows you to specify alignment  [minimum allocation size] via MALLOC_ALIGNMENT configuration parameter.

ftp://gee.cs.oswego.edu/pub/misc/malloc.c

0

LVL 22

Accepted Solution

Slightly simpler than the above fine example:

void * aligned16Malloc( unsigned size, unsigned char **alignedPtr )
{
unsigned char *    myptr = (unsigned char *) malloc( size + 15 );
* alignedPtr = ( myptr + 15 ) & ~0xF;
return( (void *) myptr );
}
0

Author Comment

First of all, i need to increase to 500 points. I am reading those functions and try to understand them. Thanks a lot.
0

Author Comment

regarding to

void *aligned16Malloc( unsigned size, unsigned char **alignedPtr )
{
unsigned char *myptr;
unsigned offset;

myptr = (unsigned char *)malloc( size + 15 );
offset = (unsigned)myptr & 0x0F;
if( offset != 0 )
*alignedPtr = myptr + (16 - offset);
else
*alignedPtr = myptr;
return( (void *)myptr );
}

==> that is the void function. How can it return?
0

LVL 11

Expert Comment

(Void *) is POINTER to "undefined type" - one that malloc() returns...
To use it you need to do casting, as I did to (unsigned char *)...
free() accepts (void *) to free the memory...
0

Author Comment

oh i see.
One more question, how can you  & 0x0F? That's the reason to use 0x0F? Thanks.
0

LVL 11

Expert Comment

(num & 0x0F) is "effective" from CPU point of view way of (num % 16).
So offset = (unsigned)myptr & 0x0F; contains number of bytes from "aligned 16" address...
0

Author Comment

I am just curious 1 more thing hihi.
If  num& 0x0F is "effective" like num%16, can we use directly num%16 somehow instead? I can't find a way to work around it because myptr is a pointer to point to 1 space; not the number.Please let me know if you have another way.

PS: i want to use some popular operator in this function. Thanks.
0

LVL 11

Expert Comment

Ooh, sure you can use:
offset = (unsigned)myptr % 16;
0

Author Comment

Hi dimitry,
myptr = (unsigned char *)malloc( size + 15 ); < == why do you need to add 15? THanks.
0

LVL 11

Expert Comment

You want aligned memory... Assume malloc for 1 KB will return you address 0x12345.
So you need to align it to 0x12350... But from 0x12350 there is no 1 KB anymore, so you need to ask "more" in advance...
0

Author Comment

so i need to ask for some "more" extra in order to make it align. Just wonder whether i can ask for 20 or 30 or not . Thanks.
0

LVL 11

Expert Comment

You can... 15 is a minimum...
0

## Join & Write a Comment Already a member? Login.

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

#### 733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!