anonymous memory mapping
Ben Caradoc-Davies
ben at wintersun.org
Mon Mar 20 18:03:44 EST 2006
Fabiano Sidler wrote:
> 2006/3/14, Fabiano Sidler <fabianosidler at gmail.com>:
>>Ok, sorry! I wanted to do this:
>>
>>--- snip ---
>>from mmap import mmap, MAP_ANONYMOUS
>>mm = mmap(-1, 1024, MAP_ANONYMOUS)
>>--- snap ---
>>
>>But I got an EnvironmentError, "[Errno 22] Invalid argument" (on
>>Linux2.6, btw.). The reason why I want to use anonymous mapping is
>>that it only allocates the memory it actually uses.
>
> Hello? Nobody out there who can answer this question?
From the Linux mmap(2) man page:
http://www.die.net/doc/linux/man/man2/mmap.2.html
"You must specify exactly one of MAP_SHARED and MAP_PRIVATE."
From the /usr/include/bits/mman.h:
/* Sharing types (must choose one and only one of these). */
#define MAP_SHARED 0x01 /* Share changes. */
#define MAP_PRIVATE 0x02 /* Changes are private. */
/* ... later ... */
# define MAP_ANONYMOUS 0x20 /* Don't use a file. */
# define MAP_ANON MAP_ANONYMOUS
The problem is that python mmap defaults to MAP_SHARED, but when you
pass in (only) MAP_ANONYMOUS as the access parameter, you are silently
unsetting the MAP_SHARED bit and not setting the MAP_PRIVATE bit. Linux
wants to know which you want.
With Python 2.4 on Linux 2.6, both work:
from mmap import mmap, MAP_ANONYMOUS, MAP_SHARED, MAP_PRIVATE
m1 = mmap(-1, 1024, MAP_ANONYMOUS|MAP_PRIVATE) # no error
m2 = mmap(-1, 1024, MAP_ANONYMOUS|MAP_SHARED) # no error
--
Ben Caradoc-Davies <ben at wintersun.org>
http://wintersun.org/
"Those who deny freedom to others deserve it not for themselves."
- Abraham Lincoln
More information about the Python-list
mailing list