Connecting Tech Pros Worldwide Help | Site Map

How to allocate memory which is 2^k aligned

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2006, 03:55 PM
Divick
Guest
 
Posts: n/a
Default How to allocate memory which is 2^k aligned

Hi,
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?

The condition is such that there should not be any memory leak and it
should not also waste memory.

Thanks,
Divick


  #2  
Old September 5th, 2006, 03:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

Divick wrote:
Quote:
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
>
The condition is such that there should not be any memory leak and it
should not also waste memory.
I think you're looking to calculate the next power of 2 from a number.
Given that you need to allocate N bytes, the N is not necessarily any
power of 2, so you need to find the next power of 2 that is not smaller
than N. You can do it in a loop.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old September 5th, 2006, 04:15 PM
Frederick Gotham
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

Divick posted:
Quote:
Can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?

"malloc" does this already -- it allocates memory whose alignment is strict
enough to satisfy the alignment of any kind of object.

You might like "aligned_storage" aswell (Google for it), although it
allocates on the stack rather than on the heap.

--

Frederick Gotham
  #4  
Old September 5th, 2006, 04:45 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

Divick wrote:
Quote:
>
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
>
The condition is such that there should not be any memory leak and it
should not also waste memory.
>
For more or less arbitrary values of k, the only way to do it is to
allocate a block that's sure to be large enough (i.e. 2^(k+1)) and pick
off an address within that block that meets your alignment requirements.

For values of k such that 2^k is the alignment of some type (typically
2, 4, 8, or 16 bytes), malloc returns a pointer that's suitably aligned
for any type, so you don't need to do anything special.

--
-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
  #5  
Old September 5th, 2006, 07:45 PM
Bart
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

Frederick Gotham wrote:
Quote:
Divick posted:
Quote:
Can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
>
"malloc" does this already -- it allocates memory whose alignment is strict
enough to satisfy the alignment of any kind of object.
But malloc only satisfies the alignment requirements of the
implementation - e.g. hardware alignment requirements. The application
may have stricter requirements depending on what you want to do.

Regards,
Bart.

  #6  
Old September 6th, 2006, 04:25 PM
Divick
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

For more or less arbitrary values of k, the only way to do it is to
Quote:
allocate a block that's sure to be large enough (i.e. 2^(k+1)) and pick
off an address within that block that meets your alignment requirements.
>
For values of k such that 2^k is the alignment of some type (typically
2, 4, 8, or 16 bytes), malloc returns a pointer that's suitably aligned
for any type, so you don't need to do anything special.
>
Since I have constraint that I have to use malloc in my application and
I also want that once that memory is passed to me I should be able to
free the memory. Thus simply returning the offset will not solve my
purpose as once the application passes the same location to me , I will
pass the location to free and free wouldn't know that it was actually
allocated. See the code below

The problem with the code below is that free does not know how to free
the memory because I pass it the address which malloc didn't return.
The other problem is that lot of memory gets wasted. Is there a way to
overcome such problems?

#define ALIGNMENT_K 3

void * allocate_memory(size_t size)
{
//allocate memory so that at least there is an address which is
2^k aligned
char * mem = (char *)malloc(size + 2^ALIGNMENT_K);

//calculate offset and if it is not 2^k aligned then
//increment the pointer till it is.
while( (*mem & ~(0xffffffff << ALIGNMENT_K)) != 0 )
{
mem++;
}

return (void *)mem;
}

//If the mem pointer is not the same that malloc returned then it
//is a problem and free won't be able to free the memory
void free_memory(void * mem)
{
free(mem);
}

I hope I am clear in my question and intent.

Thanks,
Divick

  #7  
Old September 6th, 2006, 04:25 PM
Divick
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

But malloc only satisfies the alignment requirements of the
Quote:
implementation - e.g. hardware alignment requirements. The application
may have stricter requirements depending on what you want to do.
Yup exactly that is what my problem is. Thanks for asserting in a clear
way.

Divick

  #8  
Old September 6th, 2006, 05:15 PM
peter koch
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned


Divick wrote:
Quote:
Hi,
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
>
The condition is such that there should not be any memory leak and it
should not also waste memory.
>
There is no portable way. But why do you want a specific alignment in
the first place? The reason for this must be for some nonportable
reason and the platform your program is to run on likely will support
such a call - or at least just waste virtual (not real) memory.

/Peter

  #9  
Old September 6th, 2006, 05:35 PM
Steve Pope
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

Divick <divick.kishore@gmail.comwrote:
Quote:
>Since I have constraint that I have to use malloc in my application and
>I also want that once that memory is passed to me I should be able to
>free the memory. Thus simply returning the offset will not solve my
>purpose as once the application passes the same location to me , I will
>pass the location to free and free wouldn't know that it was actually
>allocated. See the code below
>
>The problem with the code below is that free does not know how to free
>the memory because I pass it the address which malloc didn't return.
>The other problem is that lot of memory gets wasted. Is there a way to
>overcome such problems?
It seems you have to write your own version of free() that contains
a map between the original values returned by malloc() and
the aligned values passed to the application.

This should be reasonably easy using a std::map.

Steve
  #10  
Old September 7th, 2006, 12:35 PM
dasjotre
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned


Divick wrote:
Quote:
Hi,
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
>
The condition is such that there should not be any memory leak and it
should not also waste memory.
you could try something like

template <std::size_t Size, std::size_t Align>
struct aligned_str
{
boost::aligned_storage<Size, Alignaligned_;
};

typedef aligned_str<Size, Alignmy_aligned;
my_aligned * p = static_cast<my_aligned*>(malloc(sizeof(my_aligned) );
// and use
p->aligned_.address();

  #11  
Old September 7th, 2006, 12:45 PM
dasjotre
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

dasjotre wrote:
<snip>

sorry, I was beeing lazy ...

template <std::size_t Size, std::size_t Align>
class aligned_str
{
boost::aligned_storage<Size, Alignaligned_;

public:

void * operator new(std::size_t)
{
typedef aligned_str<Size, AllignT;
return malloc(sizeof(T));
}

void operator delete(void *p)
{
free(p);
}

void * address(){ return aligned_.address(); }
};

typedef aligned_str<Size, Alignmy_aligned;
::std::auto_ptr<my_alignedp (new my_aligned);
// and use
p->address();

  #12  
Old September 7th, 2006, 01:05 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: How to allocate memory which is 2^k aligned

dasjotre wrote:
Quote:
Divick wrote:
>
Quote:
>>Hi,
> can somebody help me figure out how can I make write a function
>>which inturn uses malloc routine to allocate memory which is 2^k
>>aligned?
>>
>>The condition is such that there should not be any memory leak and it
>>should not also waste memory.
>
>
you could try something like
>
template <std::size_t Size, std::size_t Align>
struct aligned_str
{
boost::aligned_storage<Size, Alignaligned_;
};
>
typedef aligned_str<Size, Alignmy_aligned;
my_aligned * p = static_cast<my_aligned*>(malloc(sizeof(my_aligned) );
// and use
p->aligned_.address();
>
I don't think that will work. aligned_storage is basically a union of a
char array of the appropriate size and some other type with suitable
alignment. For TR1 we made it clear that, essentially, the value of
Align must be a value that is the alignment of some type. It doesn't
handle arbitrary alignment values.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.