From 2652bfba735913bfcf22e77240c5e1f2e677357a Mon Sep 17 00:00:00 2001 From: Maurizio Monge Date: Wed, 10 Oct 2007 23:35:28 +0200 Subject: [PATCH] Implemented mmap wrapper for windows. on wine it works, i hope on windows too. --- book.cpp | 33 +++++++++++--------- moveparse.cpp | 4 +-- platform.cpp | 99 +++++++++++++++++++++++++++++++++++++++++------------------ platform.h | 4 +-- 4 files changed, 92 insertions(+), 48 deletions(-) diff --git a/book.cpp b/book.cpp index 5541052..3e8a3f2 100644 --- a/book.cpp +++ b/book.cpp @@ -263,18 +263,19 @@ bool Engine::open_book(const char *f) { close_book(); -// book_mmap = map_file( f, &book_size); -// if(!book_mmap) -// { -// output("Error, could not open and mmap the book file \"%s\".\n", f); -// return false; -// } -// else -// { -// output("Book file \"%s\" opened and mapped, good!\n", f); -// return true; -// } - +#if 1 + book_mmap = map_file(f, &book_size); + if(!book_mmap) + { + output("Error, could not open and mmap the book file \"%s\".\n", f); + return false; + } + else + { + output("Book file \"%s\" opened and mapped, good!\n", f); + return true; + } +#else FILE *file = fopen(f, "r"); if(!file) { @@ -293,14 +294,18 @@ bool Engine::open_book(const char *f) output("Book file \"%s\" opened and mapped, good!\n", f); return true; } +#endif } void Engine::close_book() { -// if(book_mmap) -// unmap_file(book_mmap, book_size); +#if 1 + if(book_mmap) + unmap_file(book_mmap, book_size); +#else if(book_mmap) free(book_mmap); +#endif book_mmap = NULL; } diff --git a/moveparse.cpp b/moveparse.cpp index 563c535..f44a632 100644 --- a/moveparse.cpp +++ b/moveparse.cpp @@ -318,8 +318,8 @@ int Engine::run_epd_prog(const char* prog, int time, const char* file, bool quie fprintf(out, "post\n"); fprintf(out, "go\n"); - sleep(1); - fprintf(out, "?\n"); + //sleep(1); + //fprintf(out, "?\n"); while(fgets(buf, 1024, in)) { diff --git a/platform.cpp b/platform.cpp index d130310..d5d9a2c 100644 --- a/platform.cpp +++ b/platform.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "platform.h" static bool pipe; static HANDLE handle; @@ -70,6 +71,45 @@ bool start_engine(const char *prog, FILE **in, FILE **out) return false; } +void *map_file(const char *file_name, uint32_t *size) +{ + HANDLE file, fmap; + uint64_t offset = 0; + DWORD hiword, loword; + void *retv; + + file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if(file==INVALID_HANDLE_VALUE) + return NULL; + + loword = GetFileSize(file, &hiword); + if(loword == INVALID_FILE_SIZE) + { + CloseHandle(file); + return(NULL); + } + *size = loword | ((uint64_t)hiword<<32); + + fmap = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); + if(fmap == INVALID_HANDLE_VALUE) + { + CloseHandle(file); + return NULL; + } + + retv = MapViewOfFile(fmap, FILE_MAP_READ, (DWORD)(offset >> 32), (DWORD)(offset % 0xffffffff), 0); + + CloseHandle(fmap); + CloseHandle(file); + return retv; +} + +void unmap_file(void *addr, uint32_t size) +{ + UnmapViewOfFile(addr); +} + #else //defined(_WIN32) || defined(_WIN64) #include @@ -114,36 +154,35 @@ int current_time() return tv.tv_sec*100 + tv.tv_usec/10000; } -// void *map_file(const char *file_name, uint32_t *size) -// { -// struct stat st; -// void *addr; -// int fd; -// -// fd = open( file_name, O_RDONLY); -// if(fd == -1) -// return NULL; -// -// fstat(fd, &st); -// if(size) -// *size = st.st_size; -// -// addr = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); -// if(addr == (void*)-1) -// { -// close(fd); -// return NULL; -// } -// -// close(fd); -// return addr; -// -// } -// -// void unmap_file(void*addr, uint32_t size) -// { -// munmap(addr, size); -// } +void *map_file(const char *file_name, uint32_t *size) +{ + struct stat st; + void *addr; + int fd; + + fd = open( file_name, O_RDONLY); + if(fd == -1) + return NULL; + + fstat(fd, &st); + if(size) + *size = st.st_size; + + addr = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if(addr == (void*)-1) + { + close(fd); + return NULL; + } + + close(fd); + return addr; +} + +void unmap_file(void*addr, uint32_t size) +{ + munmap(addr, size); +} bool start_engine(const char *prog, FILE **in, FILE **out) { diff --git a/platform.h b/platform.h index 96226c4..df27f04 100644 --- a/platform.h +++ b/platform.h @@ -22,8 +22,8 @@ #include /* map and unmap a file in memory. */ -// void *map_file(const char *file_name, uint32_t* size); -// void unmap_file(void*addr, uint32_t size); +void *map_file(const char *file_name, uint32_t* size); +void unmap_file(void*addr, uint32_t size); /* return the current time in centiseconds */ int current_time(); -- 2.11.4.GIT