Added new library.h header for libwine definitions.

Added getpagesize() and wine_anon_mmap() portability functions.
This commit is contained in:
Alexandre Julliard 2000-11-08 04:28:54 +00:00
parent 8d1462b614
commit 85423c6a6b
5 changed files with 95 additions and 45 deletions

30
include/wine/library.h Normal file
View File

@ -0,0 +1,30 @@
/*
* Definitions for the Wine library
*
* Copyright 2000 Alexandre Julliard
*/
#ifndef __WINE_WINE_LIBRARY_H
#define __WINE_WINE_LIBRARY_H
#include <sys/types.h>
/* dll loading */
struct _IMAGE_NT_HEADERS;
typedef void (*load_dll_callback_t)( const struct _IMAGE_NT_HEADERS *, const char * );
extern void wine_dll_set_callback( load_dll_callback_t load );
extern void *wine_dll_load( const char *filename );
extern void wine_dll_unload( void *handle );
/* debugging */
extern void wine_dbg_add_option( const char *name, unsigned char set, unsigned char clear );
/* portability */
extern void *wine_anon_mmap( void *start, size_t size, int prot, int flags );
#endif /* __WINE_WINE_LIBRARY_H */

View File

@ -77,6 +77,10 @@ int getsockopt(int socket, int level, int option_name, void *option_value, size_
void *memmove(void *dest, const void *src, unsigned int len);
#endif /* !defined(HAVE_MEMMOVE) */
#ifndef HAVE_GETPAGESIZE
size_t getpagesize(void);
#endif /* HAVE_GETPAGESIZE */
#ifndef HAVE_INET_NETWORK
unsigned long inet_network(const char *cp);
#endif /* !defined(HAVE_INET_NETWORK) */

View File

@ -23,6 +23,9 @@
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#ifdef HAVE_LIBIO_H
# include <libio.h>
#endif
@ -103,6 +106,22 @@ const char *strerror( int err )
}
#endif /* HAVE_STRERROR */
/***********************************************************************
* getpagesize
*/
#ifndef HAVE_GETPAGESIZE
size_t getpagesize(void)
{
# ifdef __svr4__
return sysconf(_SC_PAGESIZE);
# else
# error Cannot get the page size on this platform
# endif
}
#endif /* HAVE_GETPAGESIZE */
/***********************************************************************
* clone
*/
@ -339,3 +358,35 @@ int statfs(const char *name, struct statfs *info)
#endif /* defined(__BEOS__) */
}
#endif /* !defined(HAVE_STATFS) */
/***********************************************************************
* wine_anon_mmap
*
* Portable wrapper for anonymous mmaps
*/
void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
{
static int fdzero = -1;
#ifdef MAP_ANON
flags |= MAP_ANON;
#else
if (fdzero == -1)
{
if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
{
perror( "/dev/zero: open" );
exit(1);
}
}
#endif /* MAP_ANON */
/* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
#ifdef MAP_SHARED
flags &= ~MAP_SHARED;
#endif
#ifdef MAP_PRIVATE
flags |= MAP_PRIVATE;
#endif
return mmap( start, size, prot, flags, fdzero, 0 );
}

View File

@ -24,6 +24,8 @@
#include "winbase.h"
#include "wine/exception.h"
#include "wine/unicode.h"
#include "wine/library.h"
#include "wine/port.h"
#include "winerror.h"
#include "file.h"
#include "process.h"
@ -433,10 +435,10 @@ static LPVOID map_image( HANDLE hmapping, int fd, char *base, DWORD total_size,
/* zero-map the whole range */
if ((ptr = VIRTUAL_mmap( -1, base, total_size, 0,
if ((ptr = wine_anon_mmap( base, total_size,
PROT_READ | PROT_WRITE | PROT_EXEC, 0 )) == (char *)-1)
{
ptr = VIRTUAL_mmap( -1, NULL, total_size, 0,
ptr = wine_anon_mmap( NULL, total_size,
PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
if (ptr == (char *)-1)
{
@ -580,15 +582,7 @@ static LPVOID map_image( HANDLE hmapping, int fd, char *base, DWORD total_size,
#ifndef page_mask
DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init)
{
# ifdef HAVE_GETPAGESIZE
page_size = getpagesize();
# else
# ifdef __svr4__
page_size = sysconf(_SC_PAGESIZE);
# else
# error Cannot get the page size on this platform
# endif
# endif
page_mask = page_size - 1;
/* Make sure we have a power of 2 */
assert( !(page_size & page_mask) );
@ -668,39 +662,13 @@ DWORD VIRTUAL_HandleFault( LPCVOID addr )
* Wrapper for mmap() that handles anonymous mappings portably,
* and falls back to read if mmap of a file fails.
*/
LPVOID VIRTUAL_mmap( int unix_handle, LPVOID start, DWORD size,
LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size,
DWORD offset, int prot, int flags )
{
int fd = -1;
int pos;
LPVOID ret;
if (unix_handle == -1)
{
#ifdef MAP_ANON
flags |= MAP_ANON;
#else
static int fdzero = -1;
if (fdzero == -1)
{
if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
{
perror( "/dev/zero: open" );
ExitProcess(1);
}
}
fd = fdzero;
#endif /* MAP_ANON */
/* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
#ifdef MAP_SHARED
flags &= ~MAP_SHARED;
#endif
#ifdef MAP_PRIVATE
flags |= MAP_PRIVATE;
#endif
}
else fd = unix_handle;
if (fd == -1) return wine_anon_mmap( start, size, prot, flags );
if ((ret = mmap( start, size, prot, flags, fd, offset )) != (LPVOID)-1)
return ret;
@ -709,7 +677,6 @@ LPVOID VIRTUAL_mmap( int unix_handle, LPVOID start, DWORD size,
/* page-aligned (EINVAL), or because the underlying filesystem */
/* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
if (unix_handle == -1) return ret;
if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return ret;
if (prot & PROT_WRITE)
{
@ -723,7 +690,7 @@ LPVOID VIRTUAL_mmap( int unix_handle, LPVOID start, DWORD size,
}
/* Reserve the memory with an anonymous mmap */
ret = VIRTUAL_mmap( -1, start, size, 0, PROT_READ | PROT_WRITE, flags );
ret = wine_anon_mmap( start, size, PROT_READ | PROT_WRITE, flags );
if (ret == (LPVOID)-1) return ret;
/* Now read in the file */
if ((pos = lseek( fd, offset, SEEK_SET )) == -1)
@ -814,8 +781,7 @@ LPVOID WINAPI VirtualAlloc(
if (type & MEM_SYSTEM)
ptr = base;
else
ptr = (UINT)VIRTUAL_mmap( -1, (LPVOID)base, view_size, 0,
VIRTUAL_GetUnixProt( vprot ), 0 );
ptr = (UINT)wine_anon_mmap( (LPVOID)base, view_size, VIRTUAL_GetUnixProt( vprot ), 0 );
if (ptr == (UINT)-1)
{
SetLastError( ERROR_OUTOFMEMORY );
@ -941,8 +907,7 @@ BOOL WINAPI VirtualFree(
/* Decommit the pages by remapping zero-pages instead */
if (VIRTUAL_mmap( -1, (LPVOID)base, size, 0, VIRTUAL_GetUnixProt( 0 ),
MAP_FIXED ) != (LPVOID)base)
if (wine_anon_mmap( (LPVOID)base, size, VIRTUAL_GetUnixProt(0), MAP_FIXED ) != (LPVOID)base)
ERR( "Could not remap pages, expect trouble\n" );
return VIRTUAL_SetProt( view, base, size, 0 );
}

View File

@ -9,6 +9,7 @@
#include <stdlib.h>
#include "winbase.h"
#include "wine/library.h"
#include "main.h"
#include "options.h"
#include "version.h"
@ -160,7 +161,6 @@ static void do_config( const char *arg )
static void do_debugmsg( const char *arg )
{
extern void wine_dbg_add_option( const char *name, unsigned char set, unsigned char clear );
static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
char *opt, *options = strdup(arg);