Change MAPID_ERROR to MAP_FAILED to be closer to POSIX.
return 1;
}
- if (!mmap (fd, ACTUAL, strlen (sample)))
+ if (mmap (fd, ACTUAL) == MAP_FAILED)
{
printf ("(child-mm-wrt) mmap() failed\n");
return 1;
main (void)
{
int fd;
+ mapid_t map;
printf ("(mmap-close) begin\n");
return 1;
}
- if (!mmap (fd, ACTUAL, strlen (sample)))
+ map = mmap (fd, ACTUAL);
+ if (map == MAP_FAILED)
{
printf ("(mmap-close) mmap() failed\n");
return 1;
return 1;
}
- munmap (ACTUAL, strlen (sample));
+ munmap (map);
/* Done. */
printf ("(mmap-close) end\n");
printf ("(mmap-overlap) open() failed\n");
return 1;
}
- if (!mmap (fd[i], start, 8192))
+ if (mmap (fd[i], start) == MAP_FAILED)
{
if (i == 1)
return 0;
main (void)
{
int fd;
+ mapid_t map;
printf ("(mmap-read) begin\n");
return 1;
}
- if (!mmap (fd, ACTUAL, strlen (sample)))
+ map = mmap (fd, ACTUAL);
+ if (map == MAP_FAILED)
{
printf ("(mmap-read) mmap() failed\n");
return 1;
return 1;
}
- if (!munmap (ACTUAL, strlen (sample)))
+ if (!munmap (map))
{
printf ("(mmap-read) munmap() failed\n");
return 1;
return 1;
}
- if (!mmap (fd, buf, SIZE))
+ if (mmap (fd, buf) == MAP_FAILED)
{
printf ("(mmap-shuffle) mmap() failed\n");
return 1;
printf ("(mmap-twice) open() #%zu failed\n", i);
return 1;
}
- if (!mmap (fd[i], actual[i], 8192))
+ if (mmap (fd[i], actual[i]) == MAP_FAILED)
{
printf ("(mmap-twice) mmap() #%zu failed\n", i);
return 1;
main (void)
{
int fd;
+ mapid_t map;
printf ("(mmap-unmap) begin\n");
return 1;
}
- if (!mmap (fd, ACTUAL, strlen (sample)))
+ map = mmap (fd, ACTUAL);
+ if (map == MAP_FAILED)
{
printf ("(mmap-unmap) mmap() failed\n");
return 1;
}
- munmap (ACTUAL, strlen (sample));
+ munmap (map);
printf ("(mmap-unmap) FAIL: unmapped memory is readable (%d)\n",
*(int *) ACTUAL);
main (void)
{
int fd;
+ mapid_t map;
char buf[1024];
printf ("(mmap-write) begin\n");
return 1;
}
- if (!mmap (fd, ACTUAL, strlen (sample)))
+ map = mmap (fd, ACTUAL);
+ if (map == MAP_FAILED)
{
printf ("(mmap-write) mmap() failed\n");
return 1;
}
memcpy (ACTUAL, sample, strlen (sample));
- munmap (ACTUAL, strlen (sample));
+ munmap (map);
/* Read back via read(). */
read (fd, buf, strlen (sample));
#include <dirent.h>
+#include <round.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
pintos_filesize (int fd)
{
- off_t old = lseek (fd, 0, SEEK_CUR);
- off_t size = lseek (fd, 0, SEEK_END);
- lseek (fd, old, SEEK_SET);
- return size;
+ struct stat s;
+ fstat (fd, &s);
+ return s.st_size;
}
int
close (fd);
}
-bool
-pintos_mmap (int fd, void *addr, unsigned length)
+mapid_t
+pintos_mmap (int fd, void *addr)
{
- return mmap (addr, length, PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_FIXED, fd, 0) == addr;
+ size_t page_size = getpagesize ();
+ int length = pintos_filesize (fd);
+ uintptr_t ptr = (uintptr_t) mmap (addr, length, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FIXED, fd, 0);
+ return ptr | DIV_ROUND_UP (size, page_size);
}
bool
pintos_munmap (void *addr, unsigned length)
{
- return munmap (addr, length) == 0;
+ size_t page_size = getpagesize ();
+ uintptr_t page_mask = page_size - 1;
+ void *base = (void *) ((uintptr_t) addr & ~page_mask);
+ size_t length = ((uintptr_t) addr & page_mask) * page_size;
+ return munmap (base, length);
}
bool
#define PID_ERROR ((pid_t) -1)
typedef int mapid_t;
-#define MAPID_ERROR ((mapid_t) -1)
+#define MAP_FAILED ((mapid_t) -1)
void halt (void) NO_RETURN;
void exit (int status) NO_RETURN;