Browse Source

[core] _WIN32 impl of read-only mmap(), munmap()

master
Glenn Strauss 11 months ago
parent
commit
86ede08134
  1. 10
      src/chunk.c
  2. 4
      src/mod_webdav.c
  3. 22
      src/stream.c
  4. 52
      src/sys-mmap.h

10
src/chunk.c

@ -955,7 +955,7 @@ int chunkqueue_open_file_chunk(chunkqueue * const restrict cq, log_error_st * co
}
#if defined(HAVE_MMAP)
#if defined(HAVE_MMAP) || defined(_WIN32) /*(see local sys-mmap.h)*/
__attribute_cold__
#endif
__attribute_noinline__
@ -975,14 +975,18 @@ chunkqueue_write_chunk_file_intermed (const int fd, chunk * const restrict c, lo
}
#if defined(HAVE_MMAP)
#if defined(HAVE_MMAP) || defined(_WIN32) /*(see local sys-mmap.h)*/
/*(improved from network_write_mmap.c)*/
static off_t
mmap_align_offset (off_t start)
{
static off_t pagemask = 0;
if (0 == pagemask) {
#ifndef _WIN32
long pagesize = sysconf(_SC_PAGESIZE);
#else
long pagesize = -1; /*(not implemented (yet))*/
#endif
if (-1 == pagesize) pagesize = 4096;
pagemask = ~((off_t)pagesize - 1); /* pagesize always power-of-2 */
}
@ -1021,7 +1025,7 @@ chunkqueue_write_chunk_file (const int fd, chunk * const restrict c, log_error_s
if (wr < 0 && (errno == EINVAL || errno == ENOSYS))
#endif
{
#if defined(HAVE_MMAP)
#if defined(HAVE_MMAP) || defined(_WIN32) /*(see local sys-mmap.h)*/
/*(caller is responsible for handling SIGBUS if chunkqueue might contain
* untrusted file, i.e. any file other than lighttpd-created tempfile)*/
/*(tempfiles are expected for input, MAP_PRIVATE used for portability)*/

4
src/mod_webdav.c

@ -3477,7 +3477,7 @@ webdav_mmap_file_rd (void ** const addr, const size_t length,
return 0;
}
#ifdef HAVE_MMAP
#if defined(HAVE_MMAP) || defined(_WIN32) /*(see local sys-mmap.h)*/
*addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
if (*addr == MAP_FAILED && errno == EINVAL)
@ -3486,6 +3486,8 @@ webdav_mmap_file_rd (void ** const addr, const size_t length,
#else
UNUSED(fd);
UNUSED(offset);
return -1;
#endif

22
src/stream.c

@ -11,6 +11,11 @@
#include "sys-mmap.h"
#ifdef _WIN32
#include <Windows.h>
#include <MemoryAPI.h>
#endif
#ifndef O_BINARY
# define O_BINARY 0
#endif
@ -70,9 +75,9 @@ int stream_open(stream *f, const char *fn) {
#elif defined __WIN32
HANDLE *fh, *mh;
HANDLE fh, mh;
void *p;
LARGE_INTEGER fsize;
int64_t fsize;
f->start = NULL;
f->size = 0;
@ -87,7 +92,7 @@ int stream_open(stream *f, const char *fn) {
if (!fh) return -1;
if (0 != GetFileSizeEx(fh, &fsize)) {
if (!GetFileSizeEx(fh, (PLARGE_INTEGER)&fsize)) {
CloseHandle(fh);
return -1;
}
@ -105,17 +110,6 @@ int stream_open(stream *f, const char *fn) {
NULL);
if (!mh) {
/*
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
*/
CloseHandle(fh);
return -1;
}

52
src/sys-mmap.h

@ -3,11 +3,57 @@
#include "first.h"
#if defined(HAVE_SYS_MMAN_H)
# include <sys/mman.h>
#else /* HAVE_SYS_MMAN_H */
# define PROT_SHARED 0
#include <sys/mman.h>
#elif defined(_WIN32)
#include <Windows.h>
#include <HandleAPI.h>
#include <io.h>
#include <MemoryAPI.h>
#ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
#ifndef MAP_PRIVATE
#define MAP_PRIVATE 0
#endif
#ifndef MAP_SHARED
#define MAP_SHARED 0
#endif
#ifndef PROT_READ
#define PROT_READ PAGE_READONLY
#endif
#define munmap(addr, length) UnmapViewOfFile((LPCVOID)(addr))
static inline void *
mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
/* XXX: this limited implementation maps args only to read-only mmap */
if (prot != PAGE_READONLY) /*(for PAGE_READONLY and FILE_MAP_READ)*/
return MAP_FAILED;
UNUSED(flags);
HANDLE mh = CreateFileMapping((HANDLE) _get_osfhandle(fd),
NULL, PAGE_READONLY,
(sizeof(size_t) > 4) ? length >> 32 : 0,
length & 0xffffffff, NULL);
if (NULL == mh)
return MAP_FAILED;
LPVOID p = MapViewOfFileEx(mh, FILE_MAP_READ,
(sizeof(off_t) > 4) ? offset >> 32 : 0,
offset & 0xffffffff, length, addr);
CloseHandle(mh);
return (NULL != p) ? (void *)p : MAP_FAILED;
}
#else
# define MAP_SHARED 0
# define MAP_PRIVATE 0
# define PROT_READ 0
# define mmap(a, b, c, d, e, f) (-1)

Loading…
Cancel
Save