Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update configure and warning, and fix format #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ for opt do
;;
--prefix=*)
PREFIX="$optval"
bindir="${PREFIX}/bin"
libdir="${PREFIX}/lib"
incdir="${PREFIX}/include/sys"
;;
--libdir=*)
libdir="$optval"
Expand Down
58 changes: 32 additions & 26 deletions mman.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,48 @@ static int __map_mman_error(const DWORD err, const int deferr)
static DWORD __map_mmap_prot_page(const int prot)
{
DWORD protect = 0;

if (prot == PROT_NONE)
return protect;

if ((prot & PROT_EXEC) != 0)
{
protect = ((prot & PROT_WRITE) != 0) ?
protect = ((prot & PROT_WRITE) != 0) ?
PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
}
else
{
protect = ((prot & PROT_WRITE) != 0) ?
PAGE_READWRITE : PAGE_READONLY;
}

return protect;
}

static DWORD __map_mmap_prot_file(const int prot)
{
DWORD desiredAccess = 0;

if (prot == PROT_NONE)
return desiredAccess;

if ((prot & PROT_READ) != 0)
desiredAccess |= FILE_MAP_READ;
if ((prot & PROT_WRITE) != 0)
desiredAccess |= FILE_MAP_WRITE;
if ((prot & PROT_EXEC) != 0)
desiredAccess |= FILE_MAP_EXECUTE;

return desiredAccess;
}

MMANSHARED_EXPORT
void* mmap(void *addr, size_t len, int prot, int flags, int fildes, OffsetType off)
{
HANDLE fm, h;

void * map = MAP_FAILED;

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4293)
Expand All @@ -85,8 +86,8 @@ void* mmap(void *addr, size_t len, int prot, int flags, int fildes, OffsetType o
#endif

errno = 0;
if (len == 0

if (len == 0
/* Unsupported flag combinations */
|| (flags & MAP_FIXED) != 0
/* Usupported protection combinations */
Expand All @@ -95,8 +96,8 @@ void* mmap(void *addr, size_t len, int prot, int flags, int fildes, OffsetType o
errno = EINVAL;
return MAP_FAILED;
}
h = ((flags & MAP_ANONYMOUS) == 0) ?

h = ((flags & MAP_ANONYMOUS) == 0) ?
(HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE;

if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE)
Expand All @@ -112,11 +113,11 @@ void* mmap(void *addr, size_t len, int prot, int flags, int fildes, OffsetType o
errno = __map_mman_error(GetLastError(), EPERM);
return MAP_FAILED;
}

map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len);

CloseHandle(fm);

if (map == NULL)
{
errno = __map_mman_error(GetLastError(), EPERM);
Expand All @@ -126,55 +127,60 @@ void* mmap(void *addr, size_t len, int prot, int flags, int fildes, OffsetType o
return map;
}

MMANSHARED_EXPORT
int munmap(void *addr, size_t len)
{
if (UnmapViewOfFile(addr))
return 0;

errno = __map_mman_error(GetLastError(), EPERM);

return -1;
}

MMANSHARED_EXPORT
int _mprotect(void *addr, size_t len, int prot)
{
DWORD newProtect = __map_mmap_prot_page(prot);
DWORD oldProtect = 0;

if (VirtualProtect(addr, len, newProtect, &oldProtect))
return 0;

errno = __map_mman_error(GetLastError(), EPERM);

return -1;
}

MMANSHARED_EXPORT
int msync(void *addr, size_t len, int flags)
{
if (FlushViewOfFile(addr, len))
return 0;

errno = __map_mman_error(GetLastError(), EPERM);

return -1;
}

MMANSHARED_EXPORT
int mlock(const void *addr, size_t len)
{
if (VirtualLock((LPVOID)addr, len))
return 0;

errno = __map_mman_error(GetLastError(), EPERM);

return -1;
}

MMANSHARED_EXPORT
int munlock(const void *addr, size_t len)
{
if (VirtualUnlock((LPVOID)addr, len))
return 0;

errno = __map_mman_error(GetLastError(), EPERM);

return -1;
}