Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

16 byte aligned

Hi all,
 What is memory that is 16 byte aligned? Thanks.
0
valleytech
Asked:
valleytech
  • 7
  • 6
  • 2
  • +2
2 Solutions
 
rajeev_devinCommented:
0
 
brettmjohnsonCommented:
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
 
valleytechAuthor Commented:
 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
dimitryCommented:
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
 
brettmjohnsonCommented:
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
 
grg99Commented:
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
 
valleytechAuthor Commented:
First of all, i need to increase to 500 points. I am reading those functions and try to understand them. Thanks a lot.
0
 
valleytechAuthor Commented:
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
 
dimitryCommented:
(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
 
valleytechAuthor Commented:
oh i see.
 One more question, how can you  & 0x0F? That's the reason to use 0x0F? Thanks.
0
 
dimitryCommented:
(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
 
valleytechAuthor Commented:
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
 
dimitryCommented:
Ooh, sure you can use:
  offset = (unsigned)myptr % 16;
instead....  
0
 
valleytechAuthor Commented:
Hi dimitry,
 myptr = (unsigned char *)malloc( size + 15 ); < == why do you need to add 15? THanks.
0
 
dimitryCommented:
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
 
valleytechAuthor Commented:
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
 
dimitryCommented:
You can... 15 is a minimum...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now