Skip to content

Commit

Permalink
msync(), MinGW64 fixes
Browse files Browse the repository at this point in the history
git-svn-id: https://plibc.svn.sourceforge.net/svnroot/plibc/trunk/plibc@84 1596915d-9e13-0410-ab7f-96e59408e0f0
  • Loading branch information
ndurner committed Nov 30, 2011
1 parent 4a2a8bb commit 9978eb8
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 16 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2011-11-30 LRN
* msync()
* MinGW64 fixes

2011-11-25 Nils Durner
* nl_item
* fix cross-platform build
Expand Down
2 changes: 1 addition & 1 deletion configure.in
Expand Up @@ -64,7 +64,7 @@ dnl Checks for libraries.
dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h sys/param.h sys/time.h time.h sys/mkdev.h sys/sysmacros.h string.h memory.h fcntl.h dirent.h sys/ndir.h ndir.h alloca.h locale.h )
AC_CHECK_FUNCS(ftruncate)
AC_CHECK_FUNCS(ftruncate getnameinfo gettimeofday)

AC_HEADER_MAJOR
AC_FUNC_ALLOCA
Expand Down
4 changes: 4 additions & 0 deletions src/gettimeofday.c
Expand Up @@ -26,13 +26,17 @@

void _win_gettimeofday(struct timeval *tp, void *tzp)
{
#ifdef HAVE_GETTIMEOFDAY
gettimeofday (tp, tzp);
#else
struct _timeb theTime;

errno = 0;

_ftime(&theTime);
tp->tv_sec = theTime.time;
tp->tv_usec = theTime.millitm * 1000;
#endif
}

/* end of gettimeofday.c */
8 changes: 8 additions & 0 deletions src/include/langinfo.h
Expand Up @@ -25,6 +25,10 @@
#ifndef _LANGINFO_H_
#define _LANGINFO_H_

#ifdef __cplusplus
extern "C" {
#endif

typedef int nl_item;

/* Enumeration of locale items that can be queried with `nl_langinfo'. */
Expand Down Expand Up @@ -160,4 +164,8 @@ enum

char *nl_langinfo(int item);

#ifdef __cplusplus
}
#endif

#endif //_LANGINFO_H_
20 changes: 14 additions & 6 deletions src/include/plibc.h
Expand Up @@ -50,8 +50,8 @@ extern "C" {
#include "langinfo.h"
#endif

#include <windows.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
Expand All @@ -70,7 +70,7 @@ extern "C" {
/* Convert LARGE_INTEGER to double */
#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + \
(double)((x).LowPart))

#ifndef __MINGW64__
struct stat64
{
_dev_t st_dev;
Expand All @@ -85,7 +85,7 @@ struct stat64
__time64_t st_mtime;
__time64_t st_ctime;
};

#endif
typedef unsigned int sa_family_t;

struct sockaddr_un {
Expand Down Expand Up @@ -226,8 +226,13 @@ enum
#define MAP_SHARED 0x1
#define MAP_PRIVATE 0x2 /* unsupported */
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20 /* unsupported */
#define MAP_FAILED ((void *)-1)

#define MS_ASYNC 1 /* sync memory asynchronously */
#define MS_INVALIDATE 2 /* invalidate the caches */
#define MS_SYNC 4 /* synchronous memory sync */

struct statfs
{
long f_type; /* type of filesystem (see below) */
Expand Down Expand Up @@ -334,7 +339,7 @@ BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest);
BOOL _plibc_DereferenceShortcut(char *pszShortcut);
char *plibc_ChooseDir(char *pszTitle, unsigned long ulFlags);
char *plibc_ChooseFile(char *pszTitle, unsigned long ulFlags);
long QueryRegistry(HKEY hMainKey, char *pszKey, char *pszSubKey,
long QueryRegistry(HKEY hMainKey, const char *pszKey, const char *pszSubKey,
char *pszBuffer, long *pdLength);

BOOL __win_IsHandleMarkedAsBlocking(int hHandle);
Expand Down Expand Up @@ -406,6 +411,7 @@ size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream );
int _win_symlink(const char *path1, const char *path2);
void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
unsigned long long offset);
int _win_msync(void *start, size_t length, int flags);
int _win_munmap(void *start, size_t length);
int _win_lstat(const char *path, struct stat *buf);
int _win_lstat64(const char *path, struct stat64 *buf);
Expand Down Expand Up @@ -462,10 +468,10 @@ size_t strnlen (const char *str, size_t maxlen);
#endif
char *stpcpy(char *dest, const char *src);
char *strcasestr(const char *haystack_start, const char *needle_start);

#ifndef __MINGW64__
#define strcasecmp(a, b) stricmp(a, b)
#define strncasecmp(a, b, c) strnicmp(a, b, c)

#endif
#endif /* WINDOWS */

#ifndef WINDOWS
Expand Down Expand Up @@ -508,6 +514,7 @@ char *strcasestr(const char *haystack_start, const char *needle_start);
#define SYMLINK(a, b) symlink(a, b)
#define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o)
#define MKFIFO(p, m) mkfifo(p, m)
#define MSYNC(s, l, f) msync(s, l, f)
#define MUNMAP(s, l) munmap(s, l)
#define STRERROR(i) strerror(i)
#define RANDOM() random()
Expand Down Expand Up @@ -604,6 +611,7 @@ char *strcasestr(const char *haystack_start, const char *needle_start);
#define SYMLINK(a, b) _win_symlink(a, b)
#define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o)
#define MKFIFO(p, m) _win_mkfifo(p, m)
#define MSYNC(s, l, f) _win_msync(s, l, f)
#define MUNMAP(s, l) _win_munmap(s, l)
#define STRERROR(i) _win_strerror(i)
#define READLINK(p, b, s) _win_readlink(p, b, s)
Expand Down
1 change: 1 addition & 0 deletions src/include/plibc_private.h
Expand Up @@ -49,6 +49,7 @@
typedef struct {
char *pStart;
HANDLE hMapping;
HANDLE hFile;
} TMapping;

typedef struct
Expand Down
81 changes: 78 additions & 3 deletions src/mmap.c
Expand Up @@ -111,6 +111,16 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
if (! bFound)
{
int inserted = 0;
HANDLE hOwnFile;

if (!DuplicateHandle (GetCurrentProcess (), hFile, GetCurrentProcess (),
&hOwnFile, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
ReleaseMutex(hMappingsLock);
SetErrnoFromWinError(GetLastError());
CloseHandle(h);
return MAP_FAILED;
}

uiIndex = 0;

Expand All @@ -120,7 +130,7 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
{
pMappings[uiIndex].pStart = base;
pMappings[uiIndex].hMapping = h;

pMappings[uiIndex].hFile = hOwnFile;
inserted = 1;
}
if (uiIndex == uiMappingsCount)
Expand All @@ -137,6 +147,53 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
return base;
}

int _win_msync(void *start, size_t length, int flags)
{
unsigned uiIndex;
/* Can't have sync and async at the same time */
if ((flags & MS_SYNC) && (flags & MS_ASYNC))
{
errno = EINVAL;
return -1;
}
/* Not sure what to make of it. It's either the default, or unsupported */
if (flags & MS_INVALIDATE)
{
errno = ENOSYS;
return -1;
}

if (FlushViewOfFile (start, length))
{
BOOL success = TRUE;
errno = 0;

if (flags & MS_SYNC)
{
/* Flush to the file */
WaitForSingleObject(hMappingsLock, INFINITE);

for(uiIndex = 0; uiIndex <= uiMappingsCount; uiIndex++)
{
if (pMappings[uiIndex].pStart == start)
{
success = FlushFileBuffers (pMappings[uiIndex].hFile);
SetErrnoFromWinError(GetLastError());
break;
}
}

ReleaseMutex(hMappingsLock);
}
return success ? 0 : -1;
}
else
{
SetErrnoFromWinError(GetLastError());
return -1;
}
}

/**
* @brief Unmap files from memory
* @author Cygwin team
Expand All @@ -158,10 +215,28 @@ int _win_munmap(void *start, size_t length)
{
if (pMappings[uiIndex].pStart == start)
{
success = CloseHandle(pMappings[uiIndex].hMapping);
SetErrnoFromWinError(GetLastError());
DWORD error;

error = NO_ERROR;

if (!CloseHandle(pMappings[uiIndex].hMapping))
{
success = FALSE;
error = GetLastError();
}

if (!CloseHandle(pMappings[uiIndex].hFile))
{
success = FALSE;
error = GetLastError();
}

if (error != NO_ERROR)
SetErrnoFromWinError(error);

pMappings[uiIndex].pStart = NULL;
pMappings[uiIndex].hMapping = NULL;
pMappings[uiIndex].hFile = NULL;

break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/printf.c
Expand Up @@ -668,7 +668,7 @@ int _win_vsprintf(char *dest, const char *format, va_list arg_ptr)
return _win_vsnprintf(dest,(size_t)-1,format,arg_ptr);
}

static int __fwrite(void*ptr, size_t nmemb, int fd) {
static int __fwrite(void*ptr, size_t nmemb, void *fd) {
return fwrite(ptr, 1, nmemb, fd);
}

Expand Down
2 changes: 1 addition & 1 deletion src/registry.c
Expand Up @@ -33,7 +33,7 @@
* @param pdLength receives size of returned string
* @return Error code from winerror.h, ERROR_SUCCESS on success
*/
long QueryRegistry(HKEY hMainKey, char *pszKey, char *pszSubKey,
long QueryRegistry(HKEY hMainKey, const char *pszKey, const char *pszSubKey,
char *pszBuffer, long *pdLength)
{
HKEY hKey;
Expand Down
5 changes: 3 additions & 2 deletions src/resolv_ms.c
Expand Up @@ -15,6 +15,7 @@ Revision History:
--*/

#include "plibc_private.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h> // sprintf()
Expand Down Expand Up @@ -949,7 +950,7 @@ getaddrinfo(
}



#ifndef HAVE_GETNAMEINFO
__inline
int
getnameinfo (
Expand All @@ -968,7 +969,7 @@ getnameinfo (
return ((*pfGetNameInfo)
(sa, salen, host, hostlen, serv, servlen, flags));
}

#endif


__inline
Expand Down
5 changes: 3 additions & 2 deletions src/shortcut.c
Expand Up @@ -31,6 +31,7 @@ DEFINE_OLEGUID(IID_IPersistFile, 0x0000010BL, 0, 0);

#include "plibc_private.h"

#include <shlguid.h>
#include <shlobj.h>
#include <objbase.h>

Expand Down Expand Up @@ -101,8 +102,8 @@ BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest)

free(pwszDest);

pFile->lpVtbl->Release(pLink);
pLink->lpVtbl->Release(pFile);
pFile->lpVtbl->Release(pFile);
pLink->lpVtbl->Release(pLink);
CoUninitialize();
errno = 0;

Expand Down

0 comments on commit 9978eb8

Please sign in to comment.