mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
minimal changes to make the runtime compilable on Windows
This commit is contained in:
@@ -8,6 +8,10 @@
|
|||||||
|
|
||||||
#include "pgf.h"
|
#include "pgf.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
class PGF_INTERNAL_DECL pgf_error : public std::runtime_error {
|
class PGF_INTERNAL_DECL pgf_error : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
pgf_error(const char *msg) : std::runtime_error(msg)
|
pgf_error(const char *msg) : std::runtime_error(msg)
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include <io.h>
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
static
|
static
|
||||||
size_t getpagesize()
|
size_t getpagesize()
|
||||||
{
|
{
|
||||||
@@ -24,7 +24,6 @@ size_t getpagesize()
|
|||||||
#define ftruncate _chsize
|
#define ftruncate _chsize
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "data.h"
|
|
||||||
#include "ipc.h"
|
#include "ipc.h"
|
||||||
|
|
||||||
PGF_INTERNAL __thread unsigned char* current_base __attribute__((tls_model("initial-exec"))) = NULL;
|
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);
|
this->filepath = strdup(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
#ifndef MREMAP_MAYMOVE
|
#ifndef MREMAP_MAYMOVE
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
ms = (malloc_state*)
|
ms = (malloc_state*)
|
||||||
@@ -357,6 +357,7 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
|
|||||||
ms = (malloc_state*)
|
ms = (malloc_state*)
|
||||||
mmap(NULL, file_size, PROT_READ | PROT_WRITE, mflags, fd, 0);
|
mmap(NULL, file_size, PROT_READ | PROT_WRITE, mflags, fd, 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (ms == MAP_FAILED || ms == NULL) {
|
if (ms == MAP_FAILED || ms == NULL) {
|
||||||
ms = NULL; // mark that ms is not created.
|
ms = NULL; // mark that ms is not created.
|
||||||
::free((void *) this->filepath);
|
::free((void *) this->filepath);
|
||||||
@@ -364,6 +365,33 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
|
|||||||
close(fd);
|
close(fd);
|
||||||
throw pgf_systemerror(code, filepath);
|
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 {
|
try {
|
||||||
rwlock = ipc_new_file_rwlock(this->filepath, &is_first);
|
rwlock = ipc_new_file_rwlock(this->filepath, &is_first);
|
||||||
@@ -417,12 +445,21 @@ PgfDB::~PgfDB() {
|
|||||||
size_t size =
|
size_t size =
|
||||||
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
|
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
#ifndef MREMAP_MAYMOVE
|
#ifndef MREMAP_MAYMOVE
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
::free(ms);
|
::free(ms);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
munmap(ms,size);
|
munmap(ms,size);
|
||||||
|
#else
|
||||||
|
if (fd < 0) {
|
||||||
|
::free(ms);
|
||||||
|
} else {
|
||||||
|
UnmapViewOfFile(ms);
|
||||||
|
CloseHandle(hMap);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd >= 0)
|
if (fd >= 0)
|
||||||
@@ -940,6 +977,7 @@ object PgfDB::malloc_internal(size_t bytes)
|
|||||||
}
|
}
|
||||||
|
|
||||||
malloc_state* new_ms;
|
malloc_state* new_ms;
|
||||||
|
#ifndef _WIN32
|
||||||
// OSX mman and mman-win32 do not implement mremap or MREMAP_MAYMOVE
|
// OSX mman and mman-win32 do not implement mremap or MREMAP_MAYMOVE
|
||||||
#ifndef MREMAP_MAYMOVE
|
#ifndef MREMAP_MAYMOVE
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
@@ -956,6 +994,8 @@ object PgfDB::malloc_internal(size_t bytes)
|
|||||||
#endif
|
#endif
|
||||||
if (new_ms == MAP_FAILED)
|
if (new_ms == MAP_FAILED)
|
||||||
throw pgf_systemerror(errno);
|
throw pgf_systemerror(errno);
|
||||||
|
#else
|
||||||
|
#endif
|
||||||
|
|
||||||
ms = new_ms;
|
ms = new_ms;
|
||||||
current_base = (unsigned char*) ms;
|
current_base = (unsigned char*) ms;
|
||||||
@@ -1162,6 +1202,7 @@ void PgfDB::sync()
|
|||||||
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
|
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
|
||||||
|
|
||||||
int res;
|
int res;
|
||||||
|
#ifndef _WIN32
|
||||||
#ifndef MREMAP_MAYMOVE
|
#ifndef MREMAP_MAYMOVE
|
||||||
if (current_db->fd < 0) {
|
if (current_db->fd < 0) {
|
||||||
res = 0;
|
res = 0;
|
||||||
@@ -1170,6 +1211,8 @@ void PgfDB::sync()
|
|||||||
res = msync((void *) ms, size, MS_SYNC | MS_INVALIDATE);
|
res = msync((void *) ms, size, MS_SYNC | MS_INVALIDATE);
|
||||||
if (res != 0)
|
if (res != 0)
|
||||||
throw pgf_systemerror(errno);
|
throw pgf_systemerror(errno);
|
||||||
|
#else
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
DB_scope::DB_scope(PgfDB *db, DB_scope_mode m)
|
DB_scope::DB_scope(PgfDB *db, DB_scope_mode m)
|
||||||
|
|||||||
@@ -62,6 +62,10 @@ private:
|
|||||||
|
|
||||||
pthread_rwlock_t *rwlock;
|
pthread_rwlock_t *rwlock;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
HANDLE hMap;
|
||||||
|
#endif
|
||||||
|
|
||||||
friend class PgfReader;
|
friend class PgfReader;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -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
|
//#define DEBUG_IPC
|
||||||
|
|
||||||
#ifdef DEBUG_IPC
|
#ifdef DEBUG_IPC
|
||||||
@@ -27,6 +20,14 @@ void ipc_toomany() {
|
|||||||
#define ipc_toomany() throw pgf_error("Too many open grammars")
|
#define ipc_toomany() throw pgf_error("Too many open grammars")
|
||||||
#endif
|
#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_t(x) size_t
|
||||||
#define ptr(o,T) (o ? (T*) (((uint8_t*) locks) + o) : NULL)
|
#define ptr(o,T) (o ? (T*) (((uint8_t*) locks) + o) : NULL)
|
||||||
#define offs(p) (((uint8_t*) p) - ((uint8_t*) locks))
|
#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);
|
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
|
#ifdef DEBUG_IPC
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <io.h>
|
||||||
|
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "reader.h"
|
#include "reader.h"
|
||||||
@@ -81,7 +82,13 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
|||||||
FILE *in = NULL;
|
FILE *in = NULL;
|
||||||
|
|
||||||
PGF_API_BEGIN {
|
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");
|
in = fopen(pgf_path, "rb");
|
||||||
if (!in) {
|
if (!in) {
|
||||||
@@ -160,7 +167,13 @@ PgfDB *pgf_new_ngf(PgfText *abstract_name,
|
|||||||
PgfDB *db = NULL;
|
PgfDB *db = NULL;
|
||||||
|
|
||||||
PGF_API_BEGIN {
|
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);
|
DB_scope scope(db, WRITER_SCOPE);
|
||||||
|
|||||||
@@ -7,11 +7,11 @@
|
|||||||
#define EXTERN_C
|
#define EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||||
|
|
||||||
#if defined(COMPILING_PGF)
|
#if defined(COMPILING_PGF)
|
||||||
#define PGF_API_DECL __declspec(dllexport) EXTERN_C
|
#define PGF_API_DECL EXTERN_C __declspec(dllexport)
|
||||||
#define PGF_API __declspec(dllexport) EXTERN_C
|
#define PGF_API EXTERN_C __declspec(dllexport)
|
||||||
#else
|
#else
|
||||||
#define PGF_API_DECL __declspec(dllimport)
|
#define PGF_API_DECL __declspec(dllimport)
|
||||||
#define PGF_API ERROR_NOT_COMPILING_LIBPGF
|
#define PGF_API ERROR_NOT_COMPILING_LIBPGF
|
||||||
@@ -19,14 +19,6 @@
|
|||||||
#define PGF_INTERNAL_DECL
|
#define PGF_INTERNAL_DECL
|
||||||
#define PGF_INTERNAL
|
#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
|
#else
|
||||||
|
|
||||||
#define PGF_API_DECL EXTERN_C
|
#define PGF_API_DECL EXTERN_C
|
||||||
@@ -37,7 +29,7 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include<stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#define PGF_MAJOR_VERSION 2
|
#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
|
* Aside from the name, the grammar is otherwise empty but can be later
|
||||||
* populated with new functions and categories. If fpath is NULL then
|
* populated with new functions and categories. If fpath is NULL then
|
||||||
* the file is not stored on the disk but only in memory. */
|
* the file is not stored on the disk but only in memory. */
|
||||||
PGF_API
|
PGF_API_DECL
|
||||||
PgfDB *pgf_new_ngf(PgfText *abstract_name,
|
PgfDB *pgf_new_ngf(PgfText *abstract_name,
|
||||||
const char *fpath,
|
const char *fpath,
|
||||||
PgfRevision *revision,
|
PgfRevision *revision,
|
||||||
@@ -289,7 +281,7 @@ PGF_API_DECL
|
|||||||
void pgf_iter_categories(PgfDB *db, PgfRevision revision,
|
void pgf_iter_categories(PgfDB *db, PgfRevision revision,
|
||||||
PgfItor *itor, PgfExn *err);
|
PgfItor *itor, PgfExn *err);
|
||||||
|
|
||||||
PGF_API
|
PGF_API_DECL
|
||||||
void pgf_iter_concretes(PgfDB *db, PgfRevision revision,
|
void pgf_iter_concretes(PgfDB *db, PgfRevision revision,
|
||||||
PgfItor *itor, PgfExn *err);
|
PgfItor *itor, PgfExn *err);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user