English 中文(简体)
How do I mmap a _particular_ region in memory?
原标题:
  • 时间:2010-01-17 07:21:56
  •  标签:
  • c++
  • c
  • mmap

I have a program. I want it to be able to mmap a particular region of memory over different runs.

  1. I have the source code of the program. C/C++
  2. I control how the program is compiled. gcc
  3. I control how the program is linked. gcc
  4. I control how the program is run (Linux).

I just want to have this particular region of memory, say 0xabcdabcd to 0xdeadbeef that I mmap to a particular file. Is there anyway to guarantee this? (I have to somehow make sure that other things aren t loaded into this particular region).

EDIT:

How do I make sure that nothing else takes this particular region in memory?

最佳回答

You cannot make sure that nothing else takes that area of memory - first come, first served. However, as you need a particular part of the memory, I m guessing that you have a pretty specialized environment, so you simply need to make sure that you are first (using start scripts)

问题回答

You need to do two things:

  1. Specify the starting address as the first argument to mmap.
  2. Include the MAP_FIXED flag.

For the starting address, you need to make sure it s a multiple of the pagesize. To get the pagesize, use the call sysconf(_SC_PAGESIZE) (that s the appropriate call on Linux, other platforms may be different).

Pass the address to map in addr. Try to get it on a 8KB boundary. You could try mlock() instead though.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签