Show More
@@ -440,13 +440,18 b' class bufferedinputpipe:' | |||||
440 | return data |
|
440 | return data | |
441 |
|
441 | |||
442 |
|
442 | |||
443 | def mmapread(fp, size=None): |
|
443 | def mmapread(fp, size=None, pre_populate=True): | |
444 | """Read a file content using mmap |
|
444 | """Read a file content using mmap | |
445 |
|
445 | |||
446 | The responsability of checking the file system is mmap safe is the |
|
446 | The responsability of checking the file system is mmap safe is the | |
447 | responsability of the caller. |
|
447 | responsability of the caller (see `vfs.is_mmap_safe`). | |
448 |
|
448 | |||
449 | In some case, a normal string might be returned. |
|
449 | In some case, a normal string might be returned. | |
|
450 | ||||
|
451 | If `pre_populate` is True (the default), the mmapped data will be | |||
|
452 | pre-populated in memory if the system support this option, this slow down | |||
|
453 | the initial mmaping but avoid potentially crippling page fault on later | |||
|
454 | access. If this is not the desired behavior, set `pre_populate` to False. | |||
450 | """ |
|
455 | """ | |
451 | if size == 0: |
|
456 | if size == 0: | |
452 | # size of 0 to mmap.mmap() means "all data" |
|
457 | # size of 0 to mmap.mmap() means "all data" | |
@@ -455,8 +460,12 b' def mmapread(fp, size=None):' | |||||
455 | elif size is None: |
|
460 | elif size is None: | |
456 | size = 0 |
|
461 | size = 0 | |
457 | fd = getattr(fp, 'fileno', lambda: fp)() |
|
462 | fd = getattr(fp, 'fileno', lambda: fp)() | |
|
463 | flags = mmap.MAP_PRIVATE | |||
|
464 | if pre_populate: | |||
|
465 | flags |= getattr(mmap, 'MAP_POPULATE', 0) | |||
458 | try: |
|
466 | try: | |
459 |
|
|
467 | m = mmap.mmap(fd, size, flags=flags, prot=mmap.PROT_READ) | |
|
468 | return m | |||
460 | except ValueError: |
|
469 | except ValueError: | |
461 | # Empty files cannot be mmapped, but mmapread should still work. Check |
|
470 | # Empty files cannot be mmapped, but mmapread should still work. Check | |
462 | # if the file is empty, and if so, return an empty buffer. |
|
471 | # if the file is empty, and if so, return an empty buffer. |
General Comments 0
You need to be logged in to leave comments.
Login now