" -r -- map with PROT_READ protection\n"
" -w -- map with PROT_WRITE protection\n"
" -x -- map with PROT_EXEC protection\n"
+" -s <size> -- first do mmap(size)/munmap(size), try to reserve some free space\n"
" If no protection mode is specified, all are used by default.\n"
"\n"));
}
char **argv)
{
off64_t offset;
- ssize_t length;
- void *address;
+ ssize_t length = 0, length2 = 0;
+ void *address = NULL;
char *filename;
size_t blocksize, sectsize;
int c, prot = 0;
return 0;
}
- while ((c = getopt(argc, argv, "rwx")) != EOF) {
+ init_cvtnum(&blocksize, §size);
+
+ while ((c = getopt(argc, argv, "rwxs:")) != EOF) {
switch (c) {
case 'r':
prot |= PROT_READ;
case 'x':
prot |= PROT_EXEC;
break;
+ case 's':
+ length2 = cvtnum(blocksize, sectsize, optarg);
+ break;
default:
return command_usage(&mmap_cmd);
}
if (optind != argc - 2)
return command_usage(&mmap_cmd);
- init_cvtnum(&blocksize, §size);
offset = cvtnum(blocksize, sectsize, argv[optind]);
if (offset < 0) {
printf(_("non-numeric offset argument -- %s\n"), argv[optind]);
return 0;
}
- address = mmap(NULL, length, prot, MAP_SHARED, file->fd, offset);
+ /*
+ * mmap and munmap memory area of length2 region is helpful to
+ * make a region of extendible free memory. It's generally used
+ * for later mremap operation(no MREMAP_MAYMOVE flag). But there
+ * isn't guarantee that the memory after length (up to length2)
+ * will stay free.
+ */
+ if (length2 > length) {
+ address = mmap(NULL, length2, prot,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ munmap(address, length2);
+ }
+ address = mmap(address, length, prot, MAP_SHARED, file->fd, offset);
if (address == MAP_FAILED) {
perror("mmap");
free(filename);
mmap_cmd.argmin = 0;
mmap_cmd.argmax = -1;
mmap_cmd.flags = CMD_NOMAP_OK | CMD_NOFILE_OK | CMD_FOREIGN_OK;
- mmap_cmd.args = _("[N] | [-rwx] [off len]");
+ mmap_cmd.args = _("[N] | [-rwx] [-s size] [off len]");
mmap_cmd.oneline =
_("mmap a range in the current file, show mappings");
mmap_cmd.help = mmap_help;
.SH MEMORY MAPPED I/O COMMANDS
.TP
-.BI "mmap [ " N " | [[ \-rwx ] " "offset length " ]]
+.BI "mmap [ " N " | [[ \-rwx ] [\-s " size " ] " "offset length " ]]
With no arguments,
.B mmap
shows the current mappings. Specifying a single numeric argument
.RB ( \-w ),
and PROT_EXEC
.RB ( \-x ).
+.BI \-s " size"
+is used to do a mmap(size) && munmap(size) operation at first, try to reserve some
+extendible free memory space, if
+.I size
+is bigger than
+.I length
+parameter. But there's not guarantee that the memory after
+.I length
+( up to
+.I size
+) will stay free.
+.B e.g.
+"mmap -rw -s 8192 1024" will mmap 0 ~ 1024 bytes memory, but try to reserve 1024 ~ 8192
+free space(no guarantee). This free space will helpful for "mremap 8192" without
+MREMAP_MAYMOVE flag.
.TP
.B mm
See the