Link to home
Start Free TrialLog in
Avatar of valleytech
valleytechFlag for United States of America

asked on

16 byte aligned

Hi all,
 What is memory that is 16 byte aligned? Thanks.
Avatar of rajeev_devin
rajeev_devin

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.



Avatar of valleytech

ASKER

 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.
SOLUTION
Avatar of dimitry
dimitry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First of all, i need to increase to 500 points. I am reading those functions and try to understand them. Thanks a lot.
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?
(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...
oh i see.
 One more question, how can you  & 0x0F? That's the reason to use 0x0F? Thanks.
(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...
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.
Ooh, sure you can use:
  offset = (unsigned)myptr % 16;
instead....  
Hi dimitry,
 myptr = (unsigned char *)malloc( size + 15 ); < == why do you need to add 15? THanks.
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...
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.
You can... 15 is a minimum...