| 1 | /* $Id: virtual.cpp,v 1.3 1999-08-25 08:55:19 phaller Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 virtual memory functions
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 8 | * Parts (VIRTUAL_MapFileA/W) based on Wine code (memory\virtual.c):
|
|---|
| 9 | *
|
|---|
| 10 | * Copyright 1997 Alexandre Julliard
|
|---|
| 11 | *
|
|---|
| 12 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | #include <os2win.h>
|
|---|
| 17 | #include <stdlib.h>
|
|---|
| 18 | #include <string.h>
|
|---|
| 19 | #include <win\virtual.h>
|
|---|
| 20 | #include <heapstring.h>
|
|---|
| 21 | #include "mmap.h"
|
|---|
| 22 | #include <handlemanager.h>
|
|---|
| 23 |
|
|---|
| 24 | /***********************************************************************
|
|---|
| 25 | * CreateFileMapping32A (KERNEL32.46)
|
|---|
| 26 | * Creates a named or unnamed file-mapping object for the specified file
|
|---|
| 27 | *
|
|---|
| 28 | * RETURNS
|
|---|
| 29 | * Handle: Success
|
|---|
| 30 | * 0: Mapping object does not exist
|
|---|
| 31 | * NULL: Failure
|
|---|
| 32 | */
|
|---|
| 33 | HANDLE WINAPI CreateFileMappingA(
|
|---|
| 34 | HFILE hFile, /* [in] Handle of file to map */
|
|---|
| 35 | SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
|
|---|
| 36 | DWORD protect, /* [in] Protection for mapping object */
|
|---|
| 37 | DWORD size_high, /* [in] High-order 32 bits of object size */
|
|---|
| 38 | DWORD size_low, /* [in] Low-order 32 bits of object size */
|
|---|
| 39 | LPCSTR name /* [in] Name of file-mapping object */ )
|
|---|
| 40 | {
|
|---|
| 41 | return HMCreateFileMapping(hFile, sa, protect, size_high, size_low, name);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /***********************************************************************
|
|---|
| 46 | * CreateFileMapping32W (KERNEL32.47)
|
|---|
| 47 | * See CreateFileMapping32A
|
|---|
| 48 | */
|
|---|
| 49 | HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES attr,
|
|---|
| 50 | DWORD protect, DWORD size_high,
|
|---|
| 51 | DWORD size_low, LPCWSTR name )
|
|---|
| 52 | {
|
|---|
| 53 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
|
|---|
| 54 | HANDLE ret = CreateFileMappingA( hFile, attr, protect,
|
|---|
| 55 | size_high, size_low, nameA );
|
|---|
| 56 | HeapFree( GetProcessHeap(), 0, nameA );
|
|---|
| 57 | return ret;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | /***********************************************************************
|
|---|
| 62 | * OpenFileMapping32A (KERNEL32.397)
|
|---|
| 63 | * Opens a named file-mapping object.
|
|---|
| 64 | *
|
|---|
| 65 | * RETURNS
|
|---|
| 66 | * Handle: Success
|
|---|
| 67 | * NULL: Failure
|
|---|
| 68 | */
|
|---|
| 69 | HANDLE WINAPI OpenFileMappingA(
|
|---|
| 70 | DWORD access, /* [in] Access mode */
|
|---|
| 71 | BOOL inherit, /* [in] Inherit flag */
|
|---|
| 72 | LPCSTR name ) /* [in] Name of file-mapping object */
|
|---|
| 73 | {
|
|---|
| 74 | return (HMOpenFileMapping(access, inherit, name));
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | /***********************************************************************
|
|---|
| 79 | * OpenFileMapping32W (KERNEL32.398)
|
|---|
| 80 | * See OpenFileMapping32A
|
|---|
| 81 | */
|
|---|
| 82 | HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
|
|---|
| 83 | {
|
|---|
| 84 | LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
|
|---|
| 85 | HANDLE ret = OpenFileMappingA( access, inherit, nameA );
|
|---|
| 86 | HeapFree( GetProcessHeap(), 0, nameA );
|
|---|
| 87 | return ret;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | /***********************************************************************
|
|---|
| 92 | * MapViewOfFile (KERNEL32.385)
|
|---|
| 93 | * Maps a view of a file into the address space
|
|---|
| 94 | *
|
|---|
| 95 | * RETURNS
|
|---|
| 96 | * Starting address of mapped view
|
|---|
| 97 | * NULL: Failure
|
|---|
| 98 | */
|
|---|
| 99 | LPVOID WINAPI MapViewOfFile(
|
|---|
| 100 | HANDLE mapping, /* [in] File-mapping object to map */
|
|---|
| 101 | DWORD access, /* [in] Access mode */
|
|---|
| 102 | DWORD offset_high, /* [in] High-order 32 bits of file offset */
|
|---|
| 103 | DWORD offset_low, /* [in] Low-order 32 bits of file offset */
|
|---|
| 104 | DWORD count /* [in] Number of bytes to map */
|
|---|
| 105 | )
|
|---|
| 106 | {
|
|---|
| 107 | return HMMapViewOfFile( mapping, access, offset_high,
|
|---|
| 108 | offset_low, count);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | /***********************************************************************
|
|---|
| 113 | * MapViewOfFileEx (KERNEL32.386)
|
|---|
| 114 | * Maps a view of a file into the address space
|
|---|
| 115 | *
|
|---|
| 116 | * RETURNS
|
|---|
| 117 | * Starting address of mapped view
|
|---|
| 118 | * NULL: Failure
|
|---|
| 119 | */
|
|---|
| 120 | LPVOID WINAPI MapViewOfFileEx(
|
|---|
| 121 | HANDLE handle, /* [in] File-mapping object to map */
|
|---|
| 122 | DWORD access, /* [in] Access mode */
|
|---|
| 123 | DWORD offset_high, /* [in] High-order 32 bits of file offset */
|
|---|
| 124 | DWORD offset_low, /* [in] Low-order 32 bits of file offset */
|
|---|
| 125 | DWORD count, /* [in] Number of bytes to map */
|
|---|
| 126 | LPVOID addr /* [in] Suggested starting address for mapped view */
|
|---|
| 127 | )
|
|---|
| 128 | {
|
|---|
| 129 | return HMMapViewOfFileEx( handle, access, offset_high,
|
|---|
| 130 | offset_low, count, addr);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | /***********************************************************************
|
|---|
| 135 | * FlushViewOfFile (KERNEL32.262)
|
|---|
| 136 | * Writes to the disk a byte range within a mapped view of a file
|
|---|
| 137 | *
|
|---|
| 138 | * RETURNS
|
|---|
| 139 | * TRUE: Success
|
|---|
| 140 | * FALSE: Failure
|
|---|
| 141 | */
|
|---|
| 142 | BOOL WINAPI FlushViewOfFile(
|
|---|
| 143 | LPCVOID base, /* [in] Start address of byte range to flush */
|
|---|
| 144 | DWORD cbFlush /* [in] Number of bytes in range */
|
|---|
| 145 | )
|
|---|
| 146 | {
|
|---|
| 147 | return HMFlushViewOfFile( (LPVOID)base, cbFlush);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /***********************************************************************
|
|---|
| 152 | * UnmapViewOfFile (KERNEL32.540)
|
|---|
| 153 | * Unmaps a mapped view of a file.
|
|---|
| 154 | *
|
|---|
| 155 | * NOTES
|
|---|
| 156 | * Should addr be an LPCVOID?
|
|---|
| 157 | *
|
|---|
| 158 | * RETURNS
|
|---|
| 159 | * TRUE: Success
|
|---|
| 160 | * FALSE: Failure
|
|---|
| 161 | */
|
|---|
| 162 | BOOL WINAPI UnmapViewOfFile(LPVOID addr /* [in] Address where mapped view begins */
|
|---|
| 163 | )
|
|---|
| 164 | {
|
|---|
| 165 | return HMUnmapViewOfFile( addr );
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | /***********************************************************************
|
|---|
| 170 | * VIRTUAL_MapFileW
|
|---|
| 171 | *
|
|---|
| 172 | * Helper function to map a file to memory:
|
|---|
| 173 | * name - file name
|
|---|
| 174 | * [RETURN] ptr - pointer to mapped file
|
|---|
| 175 | */
|
|---|
| 176 | LPVOID WINAPI VIRTUAL_MapFileW( LPCWSTR name )
|
|---|
| 177 | {
|
|---|
| 178 | HANDLE hFile, hMapping;
|
|---|
| 179 | LPVOID ptr = NULL;
|
|---|
| 180 |
|
|---|
| 181 | hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|---|
| 182 | OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
|
|---|
| 183 | if (hFile != INVALID_HANDLE_VALUE)
|
|---|
| 184 | {
|
|---|
| 185 | hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
|
|---|
| 186 | CloseHandle( hFile );
|
|---|
| 187 | if (hMapping)
|
|---|
| 188 | {
|
|---|
| 189 | ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
|
|---|
| 190 | CloseHandle( hMapping );
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 | return ptr;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /***********************************************************************
|
|---|
| 197 | * VIRTUAL_MapFileA
|
|---|
| 198 | *
|
|---|
| 199 | * Helper function to map a file to memory:
|
|---|
| 200 | * name - file name
|
|---|
| 201 | * [RETURN] ptr - pointer to mapped file
|
|---|
| 202 | */
|
|---|
| 203 | LPVOID WINAPI VIRTUAL_MapFileA( LPCSTR name )
|
|---|
| 204 | {
|
|---|
| 205 | HANDLE hFile, hMapping;
|
|---|
| 206 | LPVOID ptr = NULL;
|
|---|
| 207 |
|
|---|
| 208 | hFile = CreateFileA(name, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|---|
| 209 | OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
|
|---|
| 210 | if (hFile != INVALID_HANDLE_VALUE)
|
|---|
| 211 | {
|
|---|
| 212 | hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
|
|---|
| 213 | CloseHandle( hFile );
|
|---|
| 214 | if (hMapping)
|
|---|
| 215 | {
|
|---|
| 216 | ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
|
|---|
| 217 | CloseHandle( hMapping );
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 | return ptr;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|