unable to resize mmap object

F

Fabiano Sidler

Hi folks!

I created an mmap object like so:
--- snip ---
from mmap import mmap,MAP_ANONYMOUS,MAP_PRIVATE
fl = file('/dev/zero','rw')
mm = mmap(fl.fileno(), 1, MAP_PRIVATE|MAP_ANONYMOUS)
--- snap ---

Now, when I try to resize mm to 10 byte
--- snip ---
mm.resize(10)
--- snap ---
I get an EnvironmentError:[Errno 22] Invalid argument.

How can I implement a resizeable anonymous memory mapping?
Thanks for your reply!

F. Sidler
 
S

Serge Orlov

Fabiano said:
Hi folks!

I created an mmap object like so:
--- snip ---
from mmap import mmap,MAP_ANONYMOUS,MAP_PRIVATE
fl = file('/dev/zero','rw')
mm = mmap(fl.fileno(), 1, MAP_PRIVATE|MAP_ANONYMOUS)
--- snap ---

Now, when I try to resize mm to 10 byte
--- snip ---
mm.resize(10)
--- snap ---
I get an EnvironmentError:[Errno 22] Invalid argument.

Just a guess: try a new size argument that is multiple of page size.
 
F

Fabiano Sidler

Fabiano said:
Now, when I try to resize mm to 10 byte
--- snip ---
mm.resize(10)
--- snap ---
I get an EnvironmentError:[Errno 22] Invalid argument.

Just a guess: try a new size argument that is multiple of page size.

No, doesn't work neitzer. :(

Thank you anyway for the idea!
 
C

Carl Banks

Fabiano said:
Hi folks!

I created an mmap object like so:
--- snip ---
from mmap import mmap,MAP_ANONYMOUS,MAP_PRIVATE
fl = file('/dev/zero','rw')
mm = mmap(fl.fileno(), 1, MAP_PRIVATE|MAP_ANONYMOUS)
--- snap ---

Now, when I try to resize mm to 10 byte
--- snip ---
mm.resize(10)
--- snap ---
I get an EnvironmentError:[Errno 22] Invalid argument.

How can I implement a resizeable anonymous memory mapping?
Thanks for your reply!

Bug in Python. Here's the reason (from the changelog):

- Bug #728515: mmap.resize() now resizes the file on Unix as it did
on Windows.

The mmap module resizes the file in question by calling ftruncate.
Only problem is, you can't truncate /dev/zero. I'm going to file this
as a bug; probably on Unix mmap should only resize the file if it's a
regular file (so fstat it first).

Frankly, I'm not so sure matching Windows behavior is a great idea.
mmap module seems to be having an identity crisis. Is it a low-level
system call, or a high-level, OS-independent way to access files as
blocks of memory? The modules is moving towards the latter (what with
Unix mmap accepting ACCESS-style flags, and now this file-resizing
behavior). I actually favor a two-level approach similar to file I/O:
there would low-level system calls in the os module, and high-level
mmap object. (The high-level object would go all the way. It would
accept a filename rather than a file descriptor, anonymous blocks would
be handled OS-independently, rather than mapping /dev/zero, and so on.)

Carl Banks
 
G

Georg Brandl

Carl said:
Frankly, I'm not so sure matching Windows behavior is a great idea.
mmap module seems to be having an identity crisis. Is it a low-level
system call, or a high-level, OS-independent way to access files as
blocks of memory? The modules is moving towards the latter (what with
Unix mmap accepting ACCESS-style flags, and now this file-resizing
behavior). I actually favor a two-level approach similar to file I/O:
there would low-level system calls in the os module, and high-level
mmap object. (The high-level object would go all the way. It would
accept a filename rather than a file descriptor, anonymous blocks would
be handled OS-independently, rather than mapping /dev/zero, and so on.)

I'm sure that we will gladly accept a patch implementing this approach.

Cheers,
Georg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top