Handle the LB_GETTEXT and CB_GETLBTEXT cases for 32W to 16 mapping.
[wine.git] / memory / virtual.c
blob846820c037057e15e97742b69452aca9287f0c19
1 /*
2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #ifdef HAVE_SYS_ERRNO_H
12 #include <sys/errno.h>
13 #endif
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23 #include "winbase.h"
24 #include "wine/exception.h"
25 #include "winerror.h"
26 #include "file.h"
27 #include "process.h"
28 #include "global.h"
29 #include "server.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(virtual);
33 DECLARE_DEBUG_CHANNEL(module);
35 #ifndef MS_SYNC
36 #define MS_SYNC 0
37 #endif
39 /* File view */
40 typedef struct _FV
42 struct _FV *next; /* Next view */
43 struct _FV *prev; /* Prev view */
44 UINT base; /* Base address */
45 UINT size; /* Size in bytes */
46 UINT flags; /* Allocation flags */
47 HANDLE mapping; /* Handle to the file mapping */
48 HANDLERPROC handlerProc; /* Fault handler */
49 LPVOID handlerArg; /* Fault handler argument */
50 BYTE protect; /* Protection for all pages at allocation time */
51 BYTE prot[1]; /* Protection byte for each page */
52 } FILE_VIEW;
54 /* Per-view flags */
55 #define VFLAG_SYSTEM 0x01
57 /* Conversion from VPROT_* to Win32 flags */
58 static const BYTE VIRTUAL_Win32Flags[16] =
60 PAGE_NOACCESS, /* 0 */
61 PAGE_READONLY, /* READ */
62 PAGE_READWRITE, /* WRITE */
63 PAGE_READWRITE, /* READ | WRITE */
64 PAGE_EXECUTE, /* EXEC */
65 PAGE_EXECUTE_READ, /* READ | EXEC */
66 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
67 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
68 PAGE_WRITECOPY, /* WRITECOPY */
69 PAGE_WRITECOPY, /* READ | WRITECOPY */
70 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
71 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
75 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
79 static FILE_VIEW *VIRTUAL_FirstView;
81 #ifdef __i386__
82 /* These are always the same on an i386, and it will be faster this way */
83 # define page_mask 0xfff
84 # define page_shift 12
85 #else
86 static UINT page_shift;
87 static UINT page_mask;
88 #endif /* __i386__ */
89 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
91 #define ROUND_ADDR(addr) \
92 ((UINT)(addr) & ~page_mask)
94 #define ROUND_SIZE(addr,size) \
95 (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
97 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
98 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
101 /* filter for page-fault exceptions */
102 static WINE_EXCEPTION_FILTER(page_fault)
104 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
105 return EXCEPTION_EXECUTE_HANDLER;
106 return EXCEPTION_CONTINUE_SEARCH;
109 /***********************************************************************
110 * VIRTUAL_GetProtStr
112 static const char *VIRTUAL_GetProtStr( BYTE prot )
114 static char buffer[6];
115 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
116 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
117 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
118 buffer[3] = (prot & VPROT_WRITE) ?
119 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
120 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
121 buffer[5] = 0;
122 return buffer;
126 /***********************************************************************
127 * VIRTUAL_DumpView
129 static void VIRTUAL_DumpView( FILE_VIEW *view )
131 UINT i, count;
132 UINT addr = view->base;
133 BYTE prot = view->prot[0];
135 DPRINTF( "View: %08x - %08x%s",
136 view->base, view->base + view->size - 1,
137 (view->flags & VFLAG_SYSTEM) ? " (system)" : "" );
138 if (view->mapping)
139 DPRINTF( " %d\n", view->mapping );
140 else
141 DPRINTF( " (anonymous)\n");
143 for (count = i = 1; i < view->size >> page_shift; i++, count++)
145 if (view->prot[i] == prot) continue;
146 DPRINTF( " %08x - %08x %s\n",
147 addr, addr + (count << page_shift) - 1,
148 VIRTUAL_GetProtStr(prot) );
149 addr += (count << page_shift);
150 prot = view->prot[i];
151 count = 0;
153 if (count)
154 DPRINTF( " %08x - %08x %s\n",
155 addr, addr + (count << page_shift) - 1,
156 VIRTUAL_GetProtStr(prot) );
160 /***********************************************************************
161 * VIRTUAL_Dump
163 void VIRTUAL_Dump(void)
165 FILE_VIEW *view = VIRTUAL_FirstView;
166 DPRINTF( "\nDump of all virtual memory views:\n\n" );
167 while (view)
169 VIRTUAL_DumpView( view );
170 view = view->next;
175 /***********************************************************************
176 * VIRTUAL_FindView
178 * Find the view containing a given address.
180 * RETURNS
181 * View: Success
182 * NULL: Failure
184 static FILE_VIEW *VIRTUAL_FindView(
185 UINT addr /* [in] Address */
187 FILE_VIEW *view = VIRTUAL_FirstView;
188 while (view)
190 if (view->base > addr) return NULL;
191 if (view->base + view->size > addr) return view;
192 view = view->next;
194 return NULL;
198 /***********************************************************************
199 * VIRTUAL_CreateView
201 * Create a new view and add it in the linked list.
203 static FILE_VIEW *VIRTUAL_CreateView( UINT base, UINT size, UINT flags,
204 BYTE vprot, HANDLE mapping )
206 FILE_VIEW *view, *prev;
208 /* Create the view structure */
210 assert( !(base & page_mask) );
211 assert( !(size & page_mask) );
212 size >>= page_shift;
213 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
214 view->base = base;
215 view->size = size << page_shift;
216 view->flags = flags;
217 view->mapping = mapping;
218 view->protect = vprot;
219 view->handlerProc = NULL;
220 memset( view->prot, vprot, size );
222 /* Duplicate the mapping handle */
224 if ((view->mapping != -1) &&
225 !DuplicateHandle( GetCurrentProcess(), view->mapping,
226 GetCurrentProcess(), &view->mapping,
227 0, FALSE, DUPLICATE_SAME_ACCESS ))
229 free( view );
230 return NULL;
233 /* Insert it in the linked list */
235 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
237 view->next = VIRTUAL_FirstView;
238 view->prev = NULL;
239 if (view->next) view->next->prev = view;
240 VIRTUAL_FirstView = view;
242 else
244 prev = VIRTUAL_FirstView;
245 while (prev->next && (prev->next->base < base)) prev = prev->next;
246 view->next = prev->next;
247 view->prev = prev;
248 if (view->next) view->next->prev = view;
249 prev->next = view;
251 VIRTUAL_DEBUG_DUMP_VIEW( view );
252 return view;
256 /***********************************************************************
257 * VIRTUAL_DeleteView
258 * Deletes a view.
260 * RETURNS
261 * None
263 static void VIRTUAL_DeleteView(
264 FILE_VIEW *view /* [in] View */
266 if (!(view->flags & VFLAG_SYSTEM))
267 FILE_munmap( (void *)view->base, 0, view->size );
268 if (view->next) view->next->prev = view->prev;
269 if (view->prev) view->prev->next = view->next;
270 else VIRTUAL_FirstView = view->next;
271 if (view->mapping) CloseHandle( view->mapping );
272 free( view );
276 /***********************************************************************
277 * VIRTUAL_GetUnixProt
279 * Convert page protections to protection for mmap/mprotect.
281 static int VIRTUAL_GetUnixProt( BYTE vprot )
283 int prot = 0;
284 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
286 if (vprot & VPROT_READ) prot |= PROT_READ;
287 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
288 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
289 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
291 return prot;
295 /***********************************************************************
296 * VIRTUAL_GetWin32Prot
298 * Convert page protections to Win32 flags.
300 * RETURNS
301 * None
303 static void VIRTUAL_GetWin32Prot(
304 BYTE vprot, /* [in] Page protection flags */
305 DWORD *protect, /* [out] Location to store Win32 protection flags */
306 DWORD *state /* [out] Location to store mem state flag */
308 if (protect) {
309 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
310 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
311 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
313 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
316 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
320 /***********************************************************************
321 * VIRTUAL_GetProt
323 * Build page protections from Win32 flags.
325 * RETURNS
326 * Value of page protection flags
328 static BYTE VIRTUAL_GetProt(
329 DWORD protect /* [in] Win32 protection flags */
331 BYTE vprot;
333 switch(protect & 0xff)
335 case PAGE_READONLY:
336 vprot = VPROT_READ;
337 break;
338 case PAGE_READWRITE:
339 vprot = VPROT_READ | VPROT_WRITE;
340 break;
341 case PAGE_WRITECOPY:
342 vprot = VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
343 break;
344 case PAGE_EXECUTE:
345 vprot = VPROT_EXEC;
346 break;
347 case PAGE_EXECUTE_READ:
348 vprot = VPROT_EXEC | VPROT_READ;
349 break;
350 case PAGE_EXECUTE_READWRITE:
351 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
352 break;
353 case PAGE_EXECUTE_WRITECOPY:
354 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
355 break;
356 case PAGE_NOACCESS:
357 default:
358 vprot = 0;
359 break;
361 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
362 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
363 return vprot;
367 /***********************************************************************
368 * VIRTUAL_SetProt
370 * Change the protection of a range of pages.
372 * RETURNS
373 * TRUE: Success
374 * FALSE: Failure
376 static BOOL VIRTUAL_SetProt(
377 FILE_VIEW *view, /* [in] Pointer to view */
378 UINT base, /* [in] Starting address */
379 UINT size, /* [in] Size in bytes */
380 BYTE vprot /* [in] Protections to use */
382 TRACE("%08x-%08x %s\n",
383 base, base + size - 1, VIRTUAL_GetProtStr( vprot ) );
385 if (mprotect( (void *)base, size, VIRTUAL_GetUnixProt(vprot) ))
386 return FALSE; /* FIXME: last error */
388 memset( view->prot + ((base - view->base) >> page_shift),
389 vprot, size >> page_shift );
390 VIRTUAL_DEBUG_DUMP_VIEW( view );
391 return TRUE;
395 /***********************************************************************
396 * map_image
398 * Map an executable (PE format) image into memory.
400 static LPVOID map_image( HANDLE hmapping, int fd, char *base, DWORD total_size,
401 DWORD header_size, HANDLE shared_file, DWORD shared_size )
403 IMAGE_DOS_HEADER *dos;
404 IMAGE_NT_HEADERS *nt;
405 IMAGE_SECTION_HEADER *sec;
406 int i, pos;
407 DWORD err = GetLastError();
408 FILE_VIEW *view = NULL;
409 char *ptr;
410 int shared_fd = -1;
412 SetLastError( ERROR_BAD_EXE_FORMAT ); /* generic error */
414 /* zero-map the whole range */
416 if ((ptr = FILE_dommap( -1, base, 0, total_size, 0, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
417 MAP_PRIVATE )) == (char *)-1)
419 ptr = FILE_dommap( -1, NULL, 0, total_size, 0, 0,
420 PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE );
421 if (ptr == (char *)-1)
423 ERR_(module)("Not enough memory for module (%ld bytes)\n", total_size);
424 goto error;
427 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
429 if (!(view = VIRTUAL_CreateView( (UINT)ptr, total_size, 0,
430 VPROT_COMMITTED|VPROT_READ|VPROT_WRITE|VPROT_WRITECOPY,
431 hmapping )))
433 FILE_munmap( ptr, 0, total_size );
434 SetLastError( ERROR_OUTOFMEMORY );
435 goto error;
438 /* map the header */
440 if (FILE_dommap( fd, ptr, 0, header_size, 0, 0,
441 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED ) == (char *)-1) goto error;
442 dos = (IMAGE_DOS_HEADER *)ptr;
443 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
444 if ((char *)(nt + 1) > ptr + header_size) goto error;
446 sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
447 if ((char *)(sec + nt->FileHeader.NumberOfSections) > ptr + header_size) goto error;
449 /* check the architecture */
451 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
453 MESSAGE("Trying to load PE image for unsupported architecture (");
454 switch (nt->FileHeader.Machine)
456 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
457 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
458 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
459 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
460 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
461 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
462 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
463 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
465 MESSAGE(")\n");
466 goto error;
469 /* retrieve the shared sections file */
471 if (shared_size)
473 struct get_read_fd_request *req = get_req_buffer();
474 req->handle = shared_file;
475 server_call_fd( REQ_GET_READ_FD, -1, &shared_fd );
476 if (shared_fd == -1) goto error;
477 CloseHandle( shared_file ); /* we no longer need it */
478 shared_file = INVALID_HANDLE_VALUE;
481 /* map all the sections */
483 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
485 DWORD size;
487 /* a few sanity checks */
488 size = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
489 if (sec->VirtualAddress > total_size || size > total_size || size < sec->VirtualAddress)
491 ERR_(module)( "Section %.8s too large (%lx+%lx/%lx)\n",
492 sec->Name, sec->VirtualAddress, sec->Misc.VirtualSize, total_size );
493 goto error;
496 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
497 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
499 size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
500 TRACE_(module)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
501 sec->Name, (char *)ptr + sec->VirtualAddress,
502 sec->PointerToRawData, pos, sec->SizeOfRawData,
503 size, sec->Characteristics );
504 if (FILE_dommap( shared_fd, (char *)ptr + sec->VirtualAddress, 0, size,
505 0, pos, PROT_READ|PROT_WRITE|PROT_EXEC,
506 MAP_SHARED|MAP_FIXED ) == (void *)-1)
508 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
509 goto error;
511 pos += size;
512 continue;
515 if (sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
516 if (!sec->PointerToRawData || !sec->SizeOfRawData) continue;
518 TRACE_(module)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
519 sec->Name, (char *)ptr + sec->VirtualAddress,
520 sec->PointerToRawData, sec->SizeOfRawData,
521 sec->Characteristics );
523 /* Note: if the section is not aligned properly FILE_dommap will magically
524 * fall back to read(), so we don't need to check anything here.
526 if (FILE_dommap( fd, (char *)ptr + sec->VirtualAddress, 0, sec->SizeOfRawData,
527 0, sec->PointerToRawData, PROT_READ|PROT_WRITE|PROT_EXEC,
528 MAP_PRIVATE | MAP_FIXED ) == (void *)-1)
530 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
531 goto error;
534 if ((sec->SizeOfRawData < sec->Misc.VirtualSize) && (sec->SizeOfRawData & page_mask))
536 DWORD end = ROUND_SIZE( 0, sec->SizeOfRawData );
537 if (end > sec->Misc.VirtualSize) end = sec->Misc.VirtualSize;
538 TRACE_(module)("clearing %p - %p\n",
539 (char *)ptr + sec->VirtualAddress + sec->SizeOfRawData,
540 (char *)ptr + sec->VirtualAddress + end );
541 memset( (char *)ptr + sec->VirtualAddress + sec->SizeOfRawData, 0,
542 end - sec->SizeOfRawData );
546 SetLastError( err ); /* restore last error */
547 close( fd );
548 if (shared_fd != -1) close( shared_fd );
549 return ptr;
551 error:
552 if (view) VIRTUAL_DeleteView( view );
553 close( fd );
554 if (shared_fd != -1) close( shared_fd );
555 if (shared_file != INVALID_HANDLE_VALUE) CloseHandle( shared_file );
556 return NULL;
560 /***********************************************************************
561 * VIRTUAL_Init
563 #ifndef page_mask
564 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init)
566 DWORD page_size;
568 # ifdef HAVE_GETPAGESIZE
569 page_size = getpagesize();
570 # else
571 # ifdef __svr4__
572 page_size = sysconf(_SC_PAGESIZE);
573 # else
574 # error Cannot get the page size on this platform
575 # endif
576 # endif
577 page_mask = page_size - 1;
578 /* Make sure we have a power of 2 */
579 assert( !(page_size & page_mask) );
580 page_shift = 0;
581 while ((1 << page_shift) != page_size) page_shift++;
583 #endif /* page_mask */
586 /***********************************************************************
587 * VIRTUAL_GetPageSize
589 DWORD VIRTUAL_GetPageSize(void)
591 return 1 << page_shift;
595 /***********************************************************************
596 * VIRTUAL_GetGranularity
598 DWORD VIRTUAL_GetGranularity(void)
600 return granularity_mask + 1;
604 /***********************************************************************
605 * VIRTUAL_SetFaultHandler
607 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
609 FILE_VIEW *view;
611 if (!(view = VIRTUAL_FindView((UINT)addr))) return FALSE;
612 view->handlerProc = proc;
613 view->handlerArg = arg;
614 return TRUE;
617 /***********************************************************************
618 * VIRTUAL_HandleFault
620 DWORD VIRTUAL_HandleFault( LPCVOID addr )
622 FILE_VIEW *view = VIRTUAL_FindView((UINT)addr);
623 DWORD ret = EXCEPTION_ACCESS_VIOLATION;
625 if (view)
627 if (view->handlerProc)
629 if (view->handlerProc(view->handlerArg, addr)) ret = 0; /* handled */
631 else
633 BYTE vprot = view->prot[((UINT)addr - view->base) >> page_shift];
634 UINT page = (UINT)addr & ~page_mask;
635 char *stack = (char *)NtCurrentTeb()->stack_base + SIGNAL_STACK_SIZE + page_mask + 1;
636 if (vprot & VPROT_GUARD)
638 VIRTUAL_SetProt( view, page, page_mask + 1, vprot & ~VPROT_GUARD );
639 ret = STATUS_GUARD_PAGE_VIOLATION;
641 /* is it inside the stack guard pages? */
642 if (((char *)addr >= stack) && ((char *)addr < stack + 2*(page_mask+1)))
643 ret = STATUS_STACK_OVERFLOW;
646 return ret;
650 /***********************************************************************
651 * VirtualAlloc (KERNEL32.548)
652 * Reserves or commits a region of pages in virtual address space
654 * RETURNS
655 * Base address of allocated region of pages
656 * NULL: Failure
658 LPVOID WINAPI VirtualAlloc(
659 LPVOID addr, /* [in] Address of region to reserve or commit */
660 DWORD size, /* [in] Size of region */
661 DWORD type, /* [in] Type of allocation */
662 DWORD protect /* [in] Type of access protection */
664 FILE_VIEW *view;
665 UINT base, ptr, view_size;
666 BYTE vprot;
668 TRACE("%08x %08lx %lx %08lx\n",
669 (UINT)addr, size, type, protect );
671 /* Round parameters to a page boundary */
673 if (size > 0x7fc00000) /* 2Gb - 4Mb */
675 SetLastError( ERROR_OUTOFMEMORY );
676 return NULL;
678 if (addr)
680 if (type & MEM_RESERVE) /* Round down to 64k boundary */
681 base = (UINT)addr & ~granularity_mask;
682 else
683 base = ROUND_ADDR( addr );
684 size = (((UINT)addr + size + page_mask) & ~page_mask) - base;
685 if ((base <= granularity_mask) || (base + size < base))
687 /* disallow low 64k and wrap-around */
688 SetLastError( ERROR_INVALID_PARAMETER );
689 return NULL;
692 else
694 base = 0;
695 size = (size + page_mask) & ~page_mask;
698 if (type & MEM_TOP_DOWN) {
699 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
700 * Is there _ANY_ way to do it with UNIX mmap()?
702 WARN("MEM_TOP_DOWN ignored\n");
703 type &= ~MEM_TOP_DOWN;
705 /* Compute the protection flags */
707 if (!(type & (MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)) ||
708 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)))
710 SetLastError( ERROR_INVALID_PARAMETER );
711 return NULL;
713 if (type & (MEM_COMMIT | MEM_SYSTEM))
714 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
715 else vprot = 0;
717 /* Reserve the memory */
719 if ((type & MEM_RESERVE) || !base)
721 view_size = size + (base ? 0 : granularity_mask + 1);
722 if (type & MEM_SYSTEM)
723 ptr = base;
724 else
725 ptr = (UINT)FILE_dommap( -1, (LPVOID)base, 0, view_size, 0, 0,
726 VIRTUAL_GetUnixProt( vprot ), MAP_PRIVATE );
727 if (ptr == (UINT)-1)
729 SetLastError( ERROR_OUTOFMEMORY );
730 return NULL;
732 if (!base)
734 /* Release the extra memory while keeping the range */
735 /* starting on a 64k boundary. */
737 if (ptr & granularity_mask)
739 UINT extra = granularity_mask + 1 - (ptr & granularity_mask);
740 FILE_munmap( (void *)ptr, 0, extra );
741 ptr += extra;
742 view_size -= extra;
744 if (view_size > size)
745 FILE_munmap( (void *)(ptr + size), 0, view_size - size );
747 else if (ptr != base)
749 /* We couldn't get the address we wanted */
750 FILE_munmap( (void *)ptr, 0, view_size );
751 SetLastError( ERROR_INVALID_ADDRESS );
752 return NULL;
754 if (!(view = VIRTUAL_CreateView( ptr, size, (type & MEM_SYSTEM) ?
755 VFLAG_SYSTEM : 0, vprot, -1 )))
757 FILE_munmap( (void *)ptr, 0, size );
758 SetLastError( ERROR_OUTOFMEMORY );
759 return NULL;
761 VIRTUAL_DEBUG_DUMP_VIEW( view );
762 return (LPVOID)ptr;
765 /* Commit the pages */
767 if (!(view = VIRTUAL_FindView( base )) ||
768 (base + size > view->base + view->size))
770 SetLastError( ERROR_INVALID_ADDRESS );
771 return NULL;
774 if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
775 return (LPVOID)base;
779 /***********************************************************************
780 * VirtualFree (KERNEL32.550)
781 * Release or decommits a region of pages in virtual address space.
783 * RETURNS
784 * TRUE: Success
785 * FALSE: Failure
787 BOOL WINAPI VirtualFree(
788 LPVOID addr, /* [in] Address of region of committed pages */
789 DWORD size, /* [in] Size of region */
790 DWORD type /* [in] Type of operation */
792 FILE_VIEW *view;
793 UINT base;
795 TRACE("%08x %08lx %lx\n",
796 (UINT)addr, size, type );
798 /* Fix the parameters */
800 size = ROUND_SIZE( addr, size );
801 base = ROUND_ADDR( addr );
803 if (!(view = VIRTUAL_FindView( base )) ||
804 (base + size > view->base + view->size))
806 SetLastError( ERROR_INVALID_PARAMETER );
807 return FALSE;
810 /* Compute the protection flags */
812 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
814 SetLastError( ERROR_INVALID_PARAMETER );
815 return FALSE;
818 /* Free the pages */
820 if (type == MEM_RELEASE)
822 if (size || (base != view->base))
824 SetLastError( ERROR_INVALID_PARAMETER );
825 return FALSE;
827 VIRTUAL_DeleteView( view );
828 return TRUE;
831 /* Decommit the pages by remapping zero-pages instead */
833 if (FILE_dommap( -1, (LPVOID)base, 0, size, 0, 0,
834 VIRTUAL_GetUnixProt( 0 ), MAP_PRIVATE|MAP_FIXED )
835 != (LPVOID)base)
836 ERR( "Could not remap pages, expect trouble\n" );
837 return VIRTUAL_SetProt( view, base, size, 0 );
841 /***********************************************************************
842 * VirtualLock (KERNEL32.551)
843 * Locks the specified region of virtual address space
845 * NOTE
846 * Always returns TRUE
848 * RETURNS
849 * TRUE: Success
850 * FALSE: Failure
852 BOOL WINAPI VirtualLock(
853 LPVOID addr, /* [in] Address of first byte of range to lock */
854 DWORD size /* [in] Number of bytes in range to lock */
856 return TRUE;
860 /***********************************************************************
861 * VirtualUnlock (KERNEL32.556)
862 * Unlocks a range of pages in the virtual address space
864 * NOTE
865 * Always returns TRUE
867 * RETURNS
868 * TRUE: Success
869 * FALSE: Failure
871 BOOL WINAPI VirtualUnlock(
872 LPVOID addr, /* [in] Address of first byte of range */
873 DWORD size /* [in] Number of bytes in range */
875 return TRUE;
879 /***********************************************************************
880 * VirtualProtect (KERNEL32.552)
881 * Changes the access protection on a region of committed pages
883 * RETURNS
884 * TRUE: Success
885 * FALSE: Failure
887 BOOL WINAPI VirtualProtect(
888 LPVOID addr, /* [in] Address of region of committed pages */
889 DWORD size, /* [in] Size of region */
890 DWORD new_prot, /* [in] Desired access protection */
891 LPDWORD old_prot /* [out] Address of variable to get old protection */
893 FILE_VIEW *view;
894 UINT base, i;
895 BYTE vprot, *p;
897 TRACE("%08x %08lx %08lx\n",
898 (UINT)addr, size, new_prot );
900 /* Fix the parameters */
902 size = ROUND_SIZE( addr, size );
903 base = ROUND_ADDR( addr );
905 if (!(view = VIRTUAL_FindView( base )) ||
906 (base + size > view->base + view->size))
908 SetLastError( ERROR_INVALID_PARAMETER );
909 return FALSE;
912 /* Make sure all the pages are committed */
914 p = view->prot + ((base - view->base) >> page_shift);
915 for (i = size >> page_shift; i; i--, p++)
917 if (!(*p & VPROT_COMMITTED))
919 SetLastError( ERROR_INVALID_PARAMETER );
920 return FALSE;
924 VIRTUAL_GetWin32Prot( view->prot[0], old_prot, NULL );
925 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
926 return VIRTUAL_SetProt( view, base, size, vprot );
930 /***********************************************************************
931 * VirtualProtectEx (KERNEL32.553)
932 * Changes the access protection on a region of committed pages in the
933 * virtual address space of a specified process
935 * RETURNS
936 * TRUE: Success
937 * FALSE: Failure
939 BOOL WINAPI VirtualProtectEx(
940 HANDLE handle, /* [in] Handle of process */
941 LPVOID addr, /* [in] Address of region of committed pages */
942 DWORD size, /* [in] Size of region */
943 DWORD new_prot, /* [in] Desired access protection */
944 LPDWORD old_prot /* [out] Address of variable to get old protection */ )
946 if (MapProcessHandle( handle ) == GetCurrentProcessId())
947 return VirtualProtect( addr, size, new_prot, old_prot );
948 ERR("Unsupported on other process\n");
949 return FALSE;
953 /***********************************************************************
954 * VirtualQuery (KERNEL32.554)
955 * Provides info about a range of pages in virtual address space
957 * RETURNS
958 * Number of bytes returned in information buffer
960 DWORD WINAPI VirtualQuery(
961 LPCVOID addr, /* [in] Address of region */
962 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
963 DWORD len /* [in] Size of buffer */
965 FILE_VIEW *view = VIRTUAL_FirstView;
966 UINT base = ROUND_ADDR( addr );
967 UINT alloc_base = 0;
968 UINT size = 0;
970 /* Find the view containing the address */
972 for (;;)
974 if (!view)
976 size = 0xffff0000 - alloc_base;
977 break;
979 if (view->base > base)
981 size = view->base - alloc_base;
982 view = NULL;
983 break;
985 if (view->base + view->size > base)
987 alloc_base = view->base;
988 size = view->size;
989 break;
991 alloc_base = view->base + view->size;
992 view = view->next;
995 /* Fill the info structure */
997 if (!view)
999 info->State = MEM_FREE;
1000 info->Protect = 0;
1001 info->AllocationProtect = 0;
1002 info->Type = 0;
1004 else
1006 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
1007 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
1008 for (size = base - alloc_base; size < view->size; size += page_mask+1)
1009 if (view->prot[size >> page_shift] != vprot) break;
1010 info->AllocationProtect = view->protect;
1011 info->Type = MEM_PRIVATE; /* FIXME */
1014 info->BaseAddress = (LPVOID)base;
1015 info->AllocationBase = (LPVOID)alloc_base;
1016 info->RegionSize = size - (base - alloc_base);
1017 return sizeof(*info);
1021 /***********************************************************************
1022 * VirtualQueryEx (KERNEL32.555)
1023 * Provides info about a range of pages in virtual address space of a
1024 * specified process
1026 * RETURNS
1027 * Number of bytes returned in information buffer
1029 DWORD WINAPI VirtualQueryEx(
1030 HANDLE handle, /* [in] Handle of process */
1031 LPCVOID addr, /* [in] Address of region */
1032 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
1033 DWORD len /* [in] Size of buffer */ )
1035 if (MapProcessHandle( handle ) == GetCurrentProcessId())
1036 return VirtualQuery( addr, info, len );
1037 ERR("Unsupported on other process\n");
1038 return 0;
1042 /***********************************************************************
1043 * IsBadReadPtr (KERNEL32.354)
1045 * RETURNS
1046 * FALSE: Process has read access to entire block
1047 * TRUE: Otherwise
1049 BOOL WINAPI IsBadReadPtr(
1050 LPCVOID ptr, /* Address of memory block */
1051 UINT size ) /* Size of block */
1053 __TRY
1055 volatile const char *p = ptr;
1056 volatile const char *end = p + size - 1;
1057 char dummy;
1059 while (p < end)
1061 dummy = *p;
1062 p += page_mask + 1;
1064 dummy = *end;
1066 __EXCEPT(page_fault) { return TRUE; }
1067 __ENDTRY
1068 return FALSE;
1072 /***********************************************************************
1073 * IsBadWritePtr (KERNEL32.357)
1075 * RETURNS
1076 * FALSE: Process has write access to entire block
1077 * TRUE: Otherwise
1079 BOOL WINAPI IsBadWritePtr(
1080 LPVOID ptr, /* [in] Address of memory block */
1081 UINT size ) /* [in] Size of block in bytes */
1083 __TRY
1085 volatile char *p = ptr;
1086 volatile char *end = p + size - 1;
1088 while (p < end)
1090 *p |= 0;
1091 p += page_mask + 1;
1093 *end |= 0;
1095 __EXCEPT(page_fault) { return TRUE; }
1096 __ENDTRY
1097 return FALSE;
1101 /***********************************************************************
1102 * IsBadHugeReadPtr (KERNEL32.352)
1103 * RETURNS
1104 * FALSE: Process has read access to entire block
1105 * TRUE: Otherwise
1107 BOOL WINAPI IsBadHugeReadPtr(
1108 LPCVOID ptr, /* [in] Address of memory block */
1109 UINT size /* [in] Size of block */
1111 return IsBadReadPtr( ptr, size );
1115 /***********************************************************************
1116 * IsBadHugeWritePtr (KERNEL32.353)
1117 * RETURNS
1118 * FALSE: Process has write access to entire block
1119 * TRUE: Otherwise
1121 BOOL WINAPI IsBadHugeWritePtr(
1122 LPVOID ptr, /* [in] Address of memory block */
1123 UINT size /* [in] Size of block */
1125 return IsBadWritePtr( ptr, size );
1129 /***********************************************************************
1130 * IsBadCodePtr (KERNEL32.351)
1132 * RETURNS
1133 * FALSE: Process has read access to specified memory
1134 * TRUE: Otherwise
1136 BOOL WINAPI IsBadCodePtr( FARPROC ptr ) /* [in] Address of function */
1138 return IsBadReadPtr( ptr, 1 );
1142 /***********************************************************************
1143 * IsBadStringPtrA (KERNEL32.355)
1145 * RETURNS
1146 * FALSE: Read access to all bytes in string
1147 * TRUE: Else
1149 BOOL WINAPI IsBadStringPtrA(
1150 LPCSTR str, /* [in] Address of string */
1151 UINT max ) /* [in] Maximum size of string */
1153 __TRY
1155 volatile const char *p = str;
1156 while (p < str + max) if (!*p++) break;
1158 __EXCEPT(page_fault) { return TRUE; }
1159 __ENDTRY
1160 return FALSE;
1164 /***********************************************************************
1165 * IsBadStringPtrW (KERNEL32.356)
1166 * See IsBadStringPtrA
1168 BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT max )
1170 __TRY
1172 volatile const WCHAR *p = str;
1173 while (p < str + max) if (!*p++) break;
1175 __EXCEPT(page_fault) { return TRUE; }
1176 __ENDTRY
1177 return FALSE;
1181 /***********************************************************************
1182 * CreateFileMappingA (KERNEL32.46)
1183 * Creates a named or unnamed file-mapping object for the specified file
1185 * RETURNS
1186 * Handle: Success
1187 * 0: Mapping object does not exist
1188 * NULL: Failure
1190 HANDLE WINAPI CreateFileMappingA(
1191 HFILE hFile, /* [in] Handle of file to map */
1192 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
1193 DWORD protect, /* [in] Protection for mapping object */
1194 DWORD size_high, /* [in] High-order 32 bits of object size */
1195 DWORD size_low, /* [in] Low-order 32 bits of object size */
1196 LPCSTR name /* [in] Name of file-mapping object */ )
1198 struct create_mapping_request *req = get_req_buffer();
1199 BYTE vprot;
1201 /* Check parameters */
1203 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1204 hFile, sa, protect, size_high, size_low, debugstr_a(name) );
1206 vprot = VIRTUAL_GetProt( protect );
1207 if (protect & SEC_RESERVE)
1209 if (hFile != INVALID_HANDLE_VALUE)
1211 SetLastError( ERROR_INVALID_PARAMETER );
1212 return 0;
1215 else vprot |= VPROT_COMMITTED;
1216 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1217 if (protect & SEC_IMAGE) vprot |= VPROT_IMAGE;
1219 /* Create the server object */
1221 req->file_handle = hFile;
1222 req->size_high = size_high;
1223 req->size_low = size_low;
1224 req->protect = vprot;
1225 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1226 server_strcpyAtoW( req->name, name );
1227 SetLastError(0);
1228 server_call( REQ_CREATE_MAPPING );
1229 if (req->handle == -1) return 0;
1230 return req->handle;
1234 /***********************************************************************
1235 * CreateFileMappingW (KERNEL32.47)
1236 * See CreateFileMappingA
1238 HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES sa,
1239 DWORD protect, DWORD size_high,
1240 DWORD size_low, LPCWSTR name )
1242 struct create_mapping_request *req = get_req_buffer();
1243 BYTE vprot;
1245 /* Check parameters */
1247 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1248 hFile, sa, protect, size_high, size_low, debugstr_w(name) );
1250 vprot = VIRTUAL_GetProt( protect );
1251 if (protect & SEC_RESERVE)
1253 if (hFile != INVALID_HANDLE_VALUE)
1255 SetLastError( ERROR_INVALID_PARAMETER );
1256 return 0;
1259 else vprot |= VPROT_COMMITTED;
1260 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1261 if (protect & SEC_IMAGE) vprot |= VPROT_IMAGE;
1263 /* Create the server object */
1265 req->file_handle = hFile;
1266 req->size_high = size_high;
1267 req->size_low = size_low;
1268 req->protect = vprot;
1269 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1270 server_strcpyW( req->name, name );
1271 SetLastError(0);
1272 server_call( REQ_CREATE_MAPPING );
1273 if (req->handle == -1) return 0;
1274 return req->handle;
1278 /***********************************************************************
1279 * OpenFileMappingA (KERNEL32.397)
1280 * Opens a named file-mapping object.
1282 * RETURNS
1283 * Handle: Success
1284 * NULL: Failure
1286 HANDLE WINAPI OpenFileMappingA(
1287 DWORD access, /* [in] Access mode */
1288 BOOL inherit, /* [in] Inherit flag */
1289 LPCSTR name ) /* [in] Name of file-mapping object */
1291 struct open_mapping_request *req = get_req_buffer();
1293 req->access = access;
1294 req->inherit = inherit;
1295 server_strcpyAtoW( req->name, name );
1296 server_call( REQ_OPEN_MAPPING );
1297 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
1298 return req->handle;
1302 /***********************************************************************
1303 * OpenFileMappingW (KERNEL32.398)
1304 * See OpenFileMappingA
1306 HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
1308 struct open_mapping_request *req = get_req_buffer();
1310 req->access = access;
1311 req->inherit = inherit;
1312 server_strcpyW( req->name, name );
1313 server_call( REQ_OPEN_MAPPING );
1314 if (req->handle == -1) return 0; /* must return 0 on failure, not -1 */
1315 return req->handle;
1319 /***********************************************************************
1320 * MapViewOfFile (KERNEL32.385)
1321 * Maps a view of a file into the address space
1323 * RETURNS
1324 * Starting address of mapped view
1325 * NULL: Failure
1327 LPVOID WINAPI MapViewOfFile(
1328 HANDLE mapping, /* [in] File-mapping object to map */
1329 DWORD access, /* [in] Access mode */
1330 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1331 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1332 DWORD count /* [in] Number of bytes to map */
1334 return MapViewOfFileEx( mapping, access, offset_high,
1335 offset_low, count, NULL );
1339 /***********************************************************************
1340 * MapViewOfFileEx (KERNEL32.386)
1341 * Maps a view of a file into the address space
1343 * RETURNS
1344 * Starting address of mapped view
1345 * NULL: Failure
1347 LPVOID WINAPI MapViewOfFileEx(
1348 HANDLE handle, /* [in] File-mapping object to map */
1349 DWORD access, /* [in] Access mode */
1350 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1351 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1352 DWORD count, /* [in] Number of bytes to map */
1353 LPVOID addr /* [in] Suggested starting address for mapped view */
1355 FILE_VIEW *view;
1356 UINT ptr = (UINT)-1, size = 0;
1357 int flags = MAP_PRIVATE;
1358 int unix_handle = -1;
1359 int prot;
1360 struct get_mapping_info_request *req = get_req_buffer();
1362 /* Check parameters */
1364 if ((offset_low & granularity_mask) ||
1365 (addr && ((UINT)addr & granularity_mask)))
1367 SetLastError( ERROR_INVALID_PARAMETER );
1368 return NULL;
1371 req->handle = handle;
1372 if (server_call_fd( REQ_GET_MAPPING_INFO, -1, &unix_handle )) goto error;
1373 prot = req->protect;
1375 if (prot & VPROT_IMAGE)
1376 return map_image( handle, unix_handle, req->base, req->size_low, req->header_size,
1377 req->shared_file, req->shared_size );
1379 if (req->size_high || offset_high)
1380 ERR("Offsets larger than 4Gb not supported\n");
1382 if ((offset_low >= req->size_low) ||
1383 (count > req->size_low - offset_low))
1385 SetLastError( ERROR_INVALID_PARAMETER );
1386 goto error;
1388 if (count) size = ROUND_SIZE( offset_low, count );
1389 else size = req->size_low - offset_low;
1391 switch(access)
1393 case FILE_MAP_ALL_ACCESS:
1394 case FILE_MAP_WRITE:
1395 case FILE_MAP_WRITE | FILE_MAP_READ:
1396 if (!(prot & VPROT_WRITE))
1398 SetLastError( ERROR_INVALID_PARAMETER );
1399 goto error;
1401 flags = MAP_SHARED;
1402 /* fall through */
1403 case FILE_MAP_READ:
1404 case FILE_MAP_COPY:
1405 case FILE_MAP_COPY | FILE_MAP_READ:
1406 if (prot & VPROT_READ) break;
1407 /* fall through */
1408 default:
1409 SetLastError( ERROR_INVALID_PARAMETER );
1410 goto error;
1413 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1414 * which has a view of this mapping commits some pages, they will
1415 * appear commited in all other processes, which have the same
1416 * view created. Since we don`t support this yet, we create the
1417 * whole mapping commited.
1419 prot |= VPROT_COMMITTED;
1421 /* Map the file */
1423 TRACE("handle=%x size=%x offset=%lx\n", handle, size, offset_low );
1425 ptr = (UINT)FILE_dommap( unix_handle, addr, 0, size, 0, offset_low,
1426 VIRTUAL_GetUnixProt( prot ), flags );
1427 if (ptr == (UINT)-1) {
1428 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1429 * Platform Differences":
1430 * Windows NT: ERROR_INVALID_PARAMETER
1431 * Windows 95: ERROR_INVALID_ADDRESS.
1432 * FIXME: So should we add a module dependend check here? -MM
1434 if (errno==ENOMEM)
1435 SetLastError( ERROR_OUTOFMEMORY );
1436 else
1437 SetLastError( ERROR_INVALID_PARAMETER );
1438 goto error;
1441 if (!(view = VIRTUAL_CreateView( ptr, size, 0, prot, handle )))
1443 SetLastError( ERROR_OUTOFMEMORY );
1444 goto error;
1446 if (unix_handle != -1) close( unix_handle );
1447 return (LPVOID)ptr;
1449 error:
1450 if (unix_handle != -1) close( unix_handle );
1451 if (ptr != (UINT)-1) FILE_munmap( (void *)ptr, 0, size );
1452 return NULL;
1456 /***********************************************************************
1457 * FlushViewOfFile (KERNEL32.262)
1458 * Writes to the disk a byte range within a mapped view of a file
1460 * RETURNS
1461 * TRUE: Success
1462 * FALSE: Failure
1464 BOOL WINAPI FlushViewOfFile(
1465 LPCVOID base, /* [in] Start address of byte range to flush */
1466 DWORD cbFlush /* [in] Number of bytes in range */
1468 FILE_VIEW *view;
1469 UINT addr = ROUND_ADDR( base );
1471 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1472 base, cbFlush );
1474 if (!(view = VIRTUAL_FindView( addr )))
1476 SetLastError( ERROR_INVALID_PARAMETER );
1477 return FALSE;
1479 if (!cbFlush) cbFlush = view->size;
1480 if (!msync( (void *)addr, cbFlush, MS_SYNC )) return TRUE;
1481 SetLastError( ERROR_INVALID_PARAMETER );
1482 return FALSE;
1486 /***********************************************************************
1487 * UnmapViewOfFile (KERNEL32.540)
1488 * Unmaps a mapped view of a file.
1490 * NOTES
1491 * Should addr be an LPCVOID?
1493 * RETURNS
1494 * TRUE: Success
1495 * FALSE: Failure
1497 BOOL WINAPI UnmapViewOfFile(
1498 LPVOID addr /* [in] Address where mapped view begins */
1500 FILE_VIEW *view;
1501 UINT base = ROUND_ADDR( addr );
1502 if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1504 SetLastError( ERROR_INVALID_PARAMETER );
1505 return FALSE;
1507 VIRTUAL_DeleteView( view );
1508 return TRUE;
1511 /***********************************************************************
1512 * VIRTUAL_MapFileW
1514 * Helper function to map a file to memory:
1515 * name - file name
1516 * [RETURN] ptr - pointer to mapped file
1518 LPVOID VIRTUAL_MapFileW( LPCWSTR name )
1520 HANDLE hFile, hMapping;
1521 LPVOID ptr = NULL;
1523 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
1524 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
1525 if (hFile != INVALID_HANDLE_VALUE)
1527 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
1528 CloseHandle( hFile );
1529 if (hMapping)
1531 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
1532 CloseHandle( hMapping );
1535 return ptr;