minimal changes to make the runtime compilable on Windows

This commit is contained in:
krangelov
2021-11-10 15:52:02 +01:00
parent 81eb2217ac
commit c806ce2d26
6 changed files with 99 additions and 28 deletions

View File

@@ -8,6 +8,10 @@
#include "pgf.h"
#ifdef _WIN32
#include <windows.h>
#endif
class PGF_INTERNAL_DECL pgf_error : public std::runtime_error {
public:
pgf_error(const char *msg) : std::runtime_error(msg)

View File

@@ -1,18 +1,18 @@
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#ifndef _WIN32
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#else
#include <io.h>
#include <windows.h>
static
size_t getpagesize()
{
@@ -24,7 +24,6 @@ size_t getpagesize()
#define ftruncate _chsize
#endif
#include "data.h"
#include "ipc.h"
PGF_INTERNAL __thread unsigned char* current_base __attribute__((tls_model("initial-exec"))) = NULL;
@@ -345,6 +344,7 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
this->filepath = strdup(filepath);
}
#ifndef _WIN32
#ifndef MREMAP_MAYMOVE
if (fd >= 0) {
ms = (malloc_state*)
@@ -357,6 +357,7 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
ms = (malloc_state*)
mmap(NULL, file_size, PROT_READ | PROT_WRITE, mflags, fd, 0);
#endif
if (ms == MAP_FAILED || ms == NULL) {
ms = NULL; // mark that ms is not created.
::free((void *) this->filepath);
@@ -364,6 +365,33 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
close(fd);
throw pgf_systemerror(code, filepath);
}
#else
if (fd >= 0) {
hMap = CreateFileMapping((HANDLE) _get_osfhandle(fd),
NULL,
PAGE_READWRITE,
HIWORD(file_size), LOWORD(file_size),
NULL);
if (hMap != NULL) {
ms = (malloc_state*) MapViewOfFile(hMap,
FILE_MAP_WRITE,
0,0,file_size);
} else {
hMap = INVALID_HANDLE_VALUE;
ms = NULL;
}
} else {
hMap = INVALID_HANDLE_VALUE;
ms = (malloc_state*) ::malloc(file_size);
}
if (ms == NULL) {
::free((void *) this->filepath);
int code = errno;
close(fd);
throw pgf_systemerror(code, filepath);
}
#endif
try {
rwlock = ipc_new_file_rwlock(this->filepath, &is_first);
@@ -417,12 +445,21 @@ PgfDB::~PgfDB() {
size_t size =
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
#ifndef _WIN32
#ifndef MREMAP_MAYMOVE
if (fd < 0) {
::free(ms);
} else
#endif
munmap(ms,size);
#else
if (fd < 0) {
::free(ms);
} else {
UnmapViewOfFile(ms);
CloseHandle(hMap);
}
#endif
}
if (fd >= 0)
@@ -940,6 +977,7 @@ object PgfDB::malloc_internal(size_t bytes)
}
malloc_state* new_ms;
#ifndef _WIN32
// OSX mman and mman-win32 do not implement mremap or MREMAP_MAYMOVE
#ifndef MREMAP_MAYMOVE
if (fd >= 0) {
@@ -956,6 +994,8 @@ object PgfDB::malloc_internal(size_t bytes)
#endif
if (new_ms == MAP_FAILED)
throw pgf_systemerror(errno);
#else
#endif
ms = new_ms;
current_base = (unsigned char*) ms;
@@ -1162,6 +1202,7 @@ void PgfDB::sync()
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
int res;
#ifndef _WIN32
#ifndef MREMAP_MAYMOVE
if (current_db->fd < 0) {
res = 0;
@@ -1170,6 +1211,8 @@ void PgfDB::sync()
res = msync((void *) ms, size, MS_SYNC | MS_INVALIDATE);
if (res != 0)
throw pgf_systemerror(errno);
#else
#endif
}
DB_scope::DB_scope(PgfDB *db, DB_scope_mode m)

View File

@@ -62,6 +62,10 @@ private:
pthread_rwlock_t *rwlock;
#ifdef _WIN32
HANDLE hMap;
#endif
friend class PgfReader;
public:

View File

@@ -1,10 +1,3 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
//#define DEBUG_IPC
#ifdef DEBUG_IPC
@@ -27,6 +20,14 @@ void ipc_toomany() {
#define ipc_toomany() throw pgf_error("Too many open grammars")
#endif
#ifndef _WIN32
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#define ptr_t(x) size_t
#define ptr(o,T) (o ? (T*) (((uint8_t*) locks) + o) : NULL)
#define offs(p) (((uint8_t*) p) - ((uint8_t*) locks))
@@ -321,6 +322,20 @@ void ipc_release_file_rwlock(const char* file_path,
pthread_mutex_unlock(&locks->mutex);
}
#else
PGF_INTERNAL_DECL
pthread_rwlock_t *ipc_new_file_rwlock(const char* file_path,
bool *is_first)
{
return NULL;
}
PGF_INTERNAL_DECL
void ipc_release_file_rwlock(const char* file_path,
pthread_rwlock_t *rwlock)
{
}
#endif
#ifdef DEBUG_IPC
int main(int argc, char *argv[])

View File

@@ -1,6 +1,7 @@
#include <fcntl.h>
#include <math.h>
#include <errno.h>
#include <io.h>
#include "data.h"
#include "reader.h"
@@ -81,7 +82,13 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
FILE *in = NULL;
PGF_API_BEGIN {
db = new PgfDB(ngf_path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
db = new PgfDB(ngf_path, O_CREAT | O_EXCL | O_RDWR,
#ifndef _WIN32
S_IRUSR | S_IWUSR
#else
0
#endif
);
in = fopen(pgf_path, "rb");
if (!in) {
@@ -160,7 +167,13 @@ PgfDB *pgf_new_ngf(PgfText *abstract_name,
PgfDB *db = NULL;
PGF_API_BEGIN {
db = new PgfDB(fpath, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
db = new PgfDB(fpath, O_CREAT | O_EXCL | O_RDWR,
#ifndef _WIN32
S_IRUSR | S_IWUSR
#else
0
#endif
);
{
DB_scope scope(db, WRITER_SCOPE);

View File

@@ -7,11 +7,11 @@
#define EXTERN_C
#endif
#if defined(_MSC_VER)
#if defined(_MSC_VER) || defined(__MINGW32__)
#if defined(COMPILING_PGF)
#define PGF_API_DECL __declspec(dllexport) EXTERN_C
#define PGF_API __declspec(dllexport) EXTERN_C
#define PGF_API_DECL EXTERN_C __declspec(dllexport)
#define PGF_API EXTERN_C __declspec(dllexport)
#else
#define PGF_API_DECL __declspec(dllimport)
#define PGF_API ERROR_NOT_COMPILING_LIBPGF
@@ -19,14 +19,6 @@
#define PGF_INTERNAL_DECL
#define PGF_INTERNAL
#elif defined(__MINGW32__)
#define PGF_API_DECL EXTERN_C
#define PGF_API EXTERN_C
#define PGF_INTERNAL_DECL
#define PGF_INTERNAL
#else
#define PGF_API_DECL EXTERN_C
@@ -37,7 +29,7 @@
#endif
#include<stdint.h>
#include <stdint.h>
#include <sys/types.h>
#define PGF_MAJOR_VERSION 2
@@ -259,7 +251,7 @@ PgfDB *pgf_read_ngf(const char* fpath,
* Aside from the name, the grammar is otherwise empty but can be later
* populated with new functions and categories. If fpath is NULL then
* the file is not stored on the disk but only in memory. */
PGF_API
PGF_API_DECL
PgfDB *pgf_new_ngf(PgfText *abstract_name,
const char *fpath,
PgfRevision *revision,
@@ -289,7 +281,7 @@ PGF_API_DECL
void pgf_iter_categories(PgfDB *db, PgfRevision revision,
PgfItor *itor, PgfExn *err);
PGF_API
PGF_API_DECL
void pgf_iter_concretes(PgfDB *db, PgfRevision revision,
PgfItor *itor, PgfExn *err);