Created
July 23, 2012 19:34
-
-
Save eklitzke/3165699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void | |
| decide_maybe_mmap (_IO_FILE *fp) | |
| { | |
| /* We use the file in read-only mode. This could mean we can | |
| mmap the file and use it without any copying. But not all | |
| file descriptors are for mmap-able objects and on 32-bit | |
| machines we don't want to map files which are too large since | |
| this would require too much virtual memory. */ | |
| struct _G_stat64 st; | |
| if (_IO_SYSSTAT (fp, &st) == 0 | |
| && S_ISREG (st.st_mode) && st.st_size != 0 | |
| /* Limit the file size to 1MB for 32-bit machines. */ | |
| && (sizeof (ptrdiff_t) > 4 || st.st_size < 1*1024*1024) | |
| /* Sanity check. */ | |
| && (fp->_offset == _IO_pos_BAD || fp->_offset <= st.st_size)) | |
| { | |
| /* Try to map the file. */ | |
| void *p; | |
| # ifdef _G_MMAP64 | |
| p = _G_MMAP64 (NULL, st.st_size, PROT_READ, MAP_SHARED, fp->_fileno, 0); | |
| # else | |
| p = __mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fp->_fileno, 0); | |
| # endif | |
| if (p != MAP_FAILED) | |
| { | |
| /* OK, we managed to map the file. Set the buffer up and use a | |
| special jump table with simplified underflow functions which | |
| never tries to read anything from the file. */ | |
| if ( | |
| # ifdef _G_LSEEK64 | |
| _G_LSEEK64 | |
| # else | |
| __lseek | |
| # endif | |
| (fp->_fileno, st.st_size, SEEK_SET) != st.st_size) | |
| { | |
| (void) __munmap (p, st.st_size); | |
| fp->_offset = _IO_pos_BAD; | |
| } | |
| else | |
| { | |
| _IO_setb (fp, p, (char *) p + st.st_size, 0); | |
| if (fp->_offset == _IO_pos_BAD) | |
| fp->_offset = 0; | |
| _IO_setg (fp, p, p + fp->_offset, p + st.st_size); | |
| fp->_offset = st.st_size; | |
| if (fp->_mode <= 0) | |
| _IO_JUMPS ((struct _IO_FILE_plus *)fp) = &_IO_file_jumps_mmap; | |
| else | |
| _IO_JUMPS ((struct _IO_FILE_plus *)fp) = &_IO_wfile_jumps_mmap; | |
| fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_mmap; | |
| return; | |
| } | |
| } | |
| } | |
| /* We couldn't use mmap, so revert to the vanilla file operations. */ | |
| if (fp->_mode <= 0) | |
| _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps; | |
| else | |
| _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_wfile_jumps; | |
| fp->_wide_data->_wide_vtable = &_IO_wfile_jumps; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment