LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2010, 06:59 AM   #1
skaushal_lk
LQ Newbie
 
Registered: Jul 2010
Distribution: Ubuntu, CentOS
Posts: 27

Rep: Reputation: 4
making dynamic memory as 64kB aligned in C lang


Hi All,

I doing malloc and getting the chunk of dynamic memory. Now I want it to align that memory to 64KB.

This means that the address of the memory starts from 64KB or multiple of 64KB.

Any suggestions on how to align it to 64KB?

Thanks.
 
Old 08-17-2010, 07:07 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
To get 64KB alignment, you ought to skip malloc and manage (at least part of) your virtual address space yourself and use mmap to request chunks of memory at specific virtual addresses.
 
Old 08-17-2010, 07:07 AM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
get a 128K size chunk and then use a pointer and some math to give you the address of the first 64K aligned spot in the chunk assign the pointer to that spot and your good to go
 
Old 08-17-2010, 07:23 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by estabroo View Post
get a 128K size chunk and then use a pointer and some math to give you the address of the first 64K aligned spot in the chunk assign the pointer to that spot and your good to go
I don't think skaushal_lk said that the desired size is 64KB, just that the desired alignment is 64K. The size might be far larger than 64KB.

But your suggestion contains the basic idea of one (somewhat wasteful) way to solve the problem:

If you request a very big chunk from malloc, the result would be 4K aligned. If you request a chunk that is 60KB larger than the amount you actually want, then the first 64K aligned address within that chunk (which can be computed easily with pointer masking) is the start of a sub chunk of at least the desired size.

In the unlikely case malloc is configured so that some of your chunks (plus 60KB) are still not large enough to to trigger 4K alignment, you could add 0xFFF8 to the desired size, so (with the 8 byte alignment used for small chunks) you have enough extra.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic shared memory user777 Linux - General 6 02-16-2009 07:36 AM
get a 'n' byte aligned memory from malloc kapsikum Programming 1 05-30-2008 01:51 PM
[C++] Classes and dynamic memory Ephracis Programming 17 12-30-2006 01:55 AM
LXer: Exploit 64KB Page Support with DB2 Enterprise 9 LXer Syndicated Linux News 0 06-22-2006 04:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration