Shell_GetCachedImageIndex32[A|W] implemented.
[wine/multimedia.git] / memory / virtual.c
blob0950942d9110fd731f661d1aa6949092611ec252
1 /*
2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <sys/errno.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/mman.h>
16 #include "winbase.h"
17 #include "winerror.h"
18 #include "file.h"
19 #include "heap.h"
20 #include "process.h"
21 #include "xmalloc.h"
22 #include "global.h"
23 #include "server.h"
24 #include "debug.h"
26 #ifndef MS_SYNC
27 #define MS_SYNC 0
28 #endif
30 /* File mapping */
31 typedef struct
33 K32OBJ header;
34 FILE_OBJECT *file;
35 } FILE_MAPPING;
37 /* File view */
38 typedef struct _FV
40 struct _FV *next; /* Next view */
41 struct _FV *prev; /* Prev view */
42 UINT32 base; /* Base address */
43 UINT32 size; /* Size in bytes */
44 UINT32 flags; /* Allocation flags */
45 UINT32 offset; /* Offset from start of mapped file */
46 HANDLE32 mapping; /* Handle to the file mapping */
47 HANDLERPROC handlerProc; /* Fault handler */
48 LPVOID handlerArg; /* Fault handler argument */
49 BYTE protect; /* Protection for all pages at allocation time */
50 BYTE prot[1]; /* Protection byte for each page */
51 } FILE_VIEW;
53 /* Per-view flags */
54 #define VFLAG_SYSTEM 0x01
56 /* Conversion from VPROT_* to Win32 flags */
57 static const BYTE VIRTUAL_Win32Flags[16] =
59 PAGE_NOACCESS, /* 0 */
60 PAGE_READONLY, /* READ */
61 PAGE_READWRITE, /* WRITE */
62 PAGE_READWRITE, /* READ | WRITE */
63 PAGE_EXECUTE, /* EXEC */
64 PAGE_EXECUTE_READ, /* READ | EXEC */
65 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
66 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
67 PAGE_WRITECOPY, /* WRITECOPY */
68 PAGE_WRITECOPY, /* READ | WRITECOPY */
69 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
70 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
71 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
78 static FILE_VIEW *VIRTUAL_FirstView;
80 #ifdef __i386__
81 /* These are always the same on an i386, and it will be faster this way */
82 # define page_mask 0xfff
83 # define page_shift 12
84 # define granularity_mask 0xffff
85 #else
86 static UINT32 page_shift;
87 static UINT32 page_mask;
88 static UINT32 granularity_mask; /* Allocation granularity (usually 64k) */
89 #endif /* __i386__ */
91 #define ROUND_ADDR(addr) \
92 ((UINT32)(addr) & ~page_mask)
94 #define ROUND_SIZE(addr,size) \
95 (((UINT32)(size) + ((UINT32)(addr) & page_mask) + page_mask) & ~page_mask)
97 static void VIRTUAL_DestroyMapping( K32OBJ *obj );
99 const K32OBJ_OPS MEM_MAPPED_FILE_Ops =
101 VIRTUAL_DestroyMapping /* destroy */
104 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
105 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
107 /***********************************************************************
108 * VIRTUAL_GetProtStr
110 static const char *VIRTUAL_GetProtStr( BYTE prot )
112 static char buffer[6];
113 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
114 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
115 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
116 buffer[3] = (prot & VPROT_WRITE) ?
117 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
118 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
119 buffer[5] = 0;
120 return buffer;
124 /***********************************************************************
125 * VIRTUAL_DumpView
127 static void VIRTUAL_DumpView( FILE_VIEW *view )
129 UINT32 i, count;
130 UINT32 addr = view->base;
131 BYTE prot = view->prot[0];
133 DUMP( "View: %08x - %08x%s",
134 view->base, view->base + view->size - 1,
135 (view->flags & VFLAG_SYSTEM) ? " (system)" : "" );
136 if (view->mapping)
137 DUMP( " %d @ %08x\n", view->mapping, view->offset );
138 else
139 DUMP( " (anonymous)\n");
141 for (count = i = 1; i < view->size >> page_shift; i++, count++)
143 if (view->prot[i] == prot) continue;
144 DUMP( " %08x - %08x %s\n",
145 addr, addr + (count << page_shift) - 1,
146 VIRTUAL_GetProtStr(prot) );
147 addr += (count << page_shift);
148 prot = view->prot[i];
149 count = 0;
151 if (count)
152 DUMP( " %08x - %08x %s\n",
153 addr, addr + (count << page_shift) - 1,
154 VIRTUAL_GetProtStr(prot) );
158 /***********************************************************************
159 * VIRTUAL_Dump
161 void VIRTUAL_Dump(void)
163 FILE_VIEW *view = VIRTUAL_FirstView;
164 DUMP( "\nDump of all virtual memory views:\n\n" );
165 while (view)
167 VIRTUAL_DumpView( view );
168 view = view->next;
173 /***********************************************************************
174 * VIRTUAL_FindView
176 * Find the view containing a given address.
178 * RETURNS
179 * View: Success
180 * NULL: Failure
182 static FILE_VIEW *VIRTUAL_FindView(
183 UINT32 addr /* [in] Address */
185 FILE_VIEW *view = VIRTUAL_FirstView;
186 while (view)
188 if (view->base > addr) return NULL;
189 if (view->base + view->size > addr) return view;
190 view = view->next;
192 return NULL;
196 /***********************************************************************
197 * VIRTUAL_CreateView
199 * Create a new view and add it in the linked list.
201 static FILE_VIEW *VIRTUAL_CreateView( UINT32 base, UINT32 size, UINT32 offset,
202 UINT32 flags, BYTE vprot,
203 HANDLE32 mapping )
205 FILE_VIEW *view, *prev;
207 /* Create the view structure */
209 assert( !(base & page_mask) );
210 assert( !(size & page_mask) );
211 size >>= page_shift;
212 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
213 view->base = base;
214 view->size = size << page_shift;
215 view->flags = flags;
216 view->offset = offset;
217 view->mapping = mapping;
218 view->protect = vprot;
219 memset( view->prot, vprot, size );
221 /* Duplicate the mapping handle */
223 if ((view->mapping != -1) &&
224 !DuplicateHandle( GetCurrentProcess(), view->mapping,
225 GetCurrentProcess(), &view->mapping,
226 0, FALSE, DUPLICATE_SAME_ACCESS ))
228 free( view );
229 return NULL;
232 /* Insert it in the linked list */
234 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
236 view->next = VIRTUAL_FirstView;
237 view->prev = NULL;
238 if (view->next) view->next->prev = view;
239 VIRTUAL_FirstView = view;
241 else
243 prev = VIRTUAL_FirstView;
244 while (prev->next && (prev->next->base < base)) prev = prev->next;
245 view->next = prev->next;
246 view->prev = prev;
247 if (view->next) view->next->prev = view;
248 prev->next = view;
250 VIRTUAL_DEBUG_DUMP_VIEW( view );
251 return view;
255 /***********************************************************************
256 * VIRTUAL_DeleteView
257 * Deletes a view.
259 * RETURNS
260 * None
262 static void VIRTUAL_DeleteView(
263 FILE_VIEW *view /* [in] View */
265 FILE_munmap( (void *)view->base, 0, view->size );
266 if (view->next) view->next->prev = view->prev;
267 if (view->prev) view->prev->next = view->next;
268 else VIRTUAL_FirstView = view->next;
269 if (view->mapping) CloseHandle( view->mapping );
270 free( view );
274 /***********************************************************************
275 * VIRTUAL_GetUnixProt
277 * Convert page protections to protection for mmap/mprotect.
279 static int VIRTUAL_GetUnixProt( BYTE vprot )
281 int prot = 0;
282 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
284 if (vprot & VPROT_READ) prot |= PROT_READ;
285 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
286 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
288 return prot;
292 /***********************************************************************
293 * VIRTUAL_GetWin32Prot
295 * Convert page protections to Win32 flags.
297 * RETURNS
298 * None
300 static void VIRTUAL_GetWin32Prot(
301 BYTE vprot, /* [in] Page protection flags */
302 DWORD *protect, /* [out] Location to store Win32 protection flags */
303 DWORD *state /* [out] Location to store mem state flag */
305 if (protect) {
306 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
307 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
308 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
310 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
313 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
317 /***********************************************************************
318 * VIRTUAL_GetProt
320 * Build page protections from Win32 flags.
322 * RETURNS
323 * Value of page protection flags
325 static BYTE VIRTUAL_GetProt(
326 DWORD protect /* [in] Win32 protection flags */
328 BYTE vprot;
330 switch(protect & 0xff)
332 case PAGE_READONLY:
333 vprot = VPROT_READ;
334 break;
335 case PAGE_READWRITE:
336 vprot = VPROT_READ | VPROT_WRITE;
337 break;
338 case PAGE_WRITECOPY:
339 vprot = VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
340 break;
341 case PAGE_EXECUTE:
342 vprot = VPROT_EXEC;
343 break;
344 case PAGE_EXECUTE_READ:
345 vprot = VPROT_EXEC | VPROT_READ;
346 break;
347 case PAGE_EXECUTE_READWRITE:
348 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE | VPROT_WRITECOPY;
349 break;
350 case PAGE_EXECUTE_WRITECOPY:
351 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
352 break;
353 case PAGE_NOACCESS:
354 default:
355 vprot = 0;
356 break;
358 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
359 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
360 return vprot;
364 /***********************************************************************
365 * VIRTUAL_SetProt
367 * Change the protection of a range of pages.
369 * RETURNS
370 * TRUE: Success
371 * FALSE: Failure
373 static BOOL32 VIRTUAL_SetProt(
374 FILE_VIEW *view, /* [in] Pointer to view */
375 UINT32 base, /* [in] Starting address */
376 UINT32 size, /* [in] Size in bytes */
377 BYTE vprot /* [in] Protections to use */
379 TRACE(virtual, "%08x-%08x %s\n",
380 base, base + size - 1, VIRTUAL_GetProtStr( vprot ) );
382 if (mprotect( (void *)base, size, VIRTUAL_GetUnixProt(vprot) ))
383 return FALSE; /* FIXME: last error */
385 memset( view->prot + ((base - view->base) >> page_shift),
386 vprot, size >> page_shift );
387 VIRTUAL_DEBUG_DUMP_VIEW( view );
388 return TRUE;
392 /***********************************************************************
393 * VIRTUAL_CheckFlags
395 * Check that all pages in a range have the given flags.
397 * RETURNS
398 * TRUE: They do
399 * FALSE: They do not
401 static BOOL32 VIRTUAL_CheckFlags(
402 UINT32 base, /* [in] Starting address */
403 UINT32 size, /* [in] Size in bytes */
404 BYTE flags /* [in] Flags to check for */
406 FILE_VIEW *view;
407 UINT32 page;
409 if (!size) return TRUE;
410 if (!(view = VIRTUAL_FindView( base ))) return FALSE;
411 if (view->base + view->size < base + size) return FALSE;
412 page = (base - view->base) >> page_shift;
413 size = ROUND_SIZE( base, size ) >> page_shift;
414 while (size--) if ((view->prot[page++] & flags) != flags) return FALSE;
415 return TRUE;
419 /***********************************************************************
420 * VIRTUAL_Init
422 BOOL32 VIRTUAL_Init(void)
424 #ifndef __i386__
425 DWORD page_size;
427 # ifdef HAVE_GETPAGESIZE
428 page_size = getpagesize();
429 # else
430 # ifdef __svr4__
431 page_size = sysconf(_SC_PAGESIZE);
432 # else
433 # error Cannot get the page size on this platform
434 # endif
435 # endif
436 page_mask = page_size - 1;
437 granularity_mask = 0xffff; /* hard-coded for now */
438 /* Make sure we have a power of 2 */
439 assert( !(page_size & page_mask) );
440 page_shift = 0;
441 while ((1 << page_shift) != page_size) page_shift++;
442 #endif /* !__i386__ */
444 #ifdef linux
446 /* Do not use stdio here since it may temporarily change the size
447 * of some segments (ie libc6 adds 0x1000 per open FILE)
449 int fd = open ("/proc/self/maps", O_RDONLY);
450 if (fd >= 0)
452 char buffer[512]; /* line might be rather long in 2.1 */
454 for (;;)
456 int start, end, offset;
457 char r, w, x, p;
458 BYTE vprot = VPROT_COMMITTED;
460 char * ptr = buffer;
461 int count = sizeof(buffer);
462 while (1 == read(fd, ptr, 1) && *ptr != '\n' && --count > 0)
463 ptr++;
465 if (*ptr != '\n') break;
466 *ptr = '\0';
468 sscanf( buffer, "%x-%x %c%c%c%c %x",
469 &start, &end, &r, &w, &x, &p, &offset );
470 if (r == 'r') vprot |= VPROT_READ;
471 if (w == 'w') vprot |= VPROT_WRITE;
472 if (x == 'x') vprot |= VPROT_EXEC;
473 if (p == 'p') vprot |= VPROT_WRITECOPY;
474 VIRTUAL_CreateView( start, end - start, 0,
475 VFLAG_SYSTEM, vprot, -1 );
477 close (fd);
480 #endif /* linux */
481 return TRUE;
485 /***********************************************************************
486 * VIRTUAL_GetPageSize
488 DWORD VIRTUAL_GetPageSize(void)
490 return 1 << page_shift;
494 /***********************************************************************
495 * VIRTUAL_GetGranularity
497 DWORD VIRTUAL_GetGranularity(void)
499 return granularity_mask + 1;
503 /***********************************************************************
504 * VIRTUAL_SetFaultHandler
506 BOOL32 VIRTUAL_SetFaultHandler( LPVOID addr, HANDLERPROC proc, LPVOID arg )
508 FILE_VIEW *view;
510 if (!(view = VIRTUAL_FindView((UINT32)addr))) return FALSE;
511 view->handlerProc = proc;
512 view->handlerArg = arg;
513 return TRUE;
516 /***********************************************************************
517 * VIRTUAL_HandleFault
519 BOOL32 VIRTUAL_HandleFault(LPVOID addr)
521 FILE_VIEW *view = VIRTUAL_FindView((UINT32)addr);
523 if (view && view->handlerProc)
524 return view->handlerProc(view->handlerArg, addr);
525 return FALSE;
529 /***********************************************************************
530 * VirtualAlloc (KERNEL32.548)
531 * Reserves or commits a region of pages in virtual address space
533 * RETURNS
534 * Base address of allocated region of pages
535 * NULL: Failure
537 LPVOID WINAPI VirtualAlloc(
538 LPVOID addr, /* [in] Address of region to reserve or commit */
539 DWORD size, /* [in] Size of region */
540 DWORD type, /* [in] Type of allocation */
541 DWORD protect /* [in] Type of access protection */
543 FILE_VIEW *view;
544 UINT32 base, ptr, view_size;
545 BYTE vprot;
547 TRACE(virtual, "%08x %08lx %lx %08lx\n",
548 (UINT32)addr, size, type, protect );
550 /* Round parameters to a page boundary */
552 if (size > 0x7fc00000) /* 2Gb - 4Mb */
554 SetLastError( ERROR_OUTOFMEMORY );
555 return NULL;
557 if (addr)
559 if (type & MEM_RESERVE) /* Round down to 64k boundary */
560 base = ((UINT32)addr + granularity_mask) & ~granularity_mask;
561 else
562 base = ROUND_ADDR( addr );
563 size = (((UINT32)addr + size + page_mask) & ~page_mask) - base;
564 if (base + size < base) /* Disallow wrap-around */
566 SetLastError( ERROR_INVALID_PARAMETER );
567 return NULL;
570 else
572 base = 0;
573 size = (size + page_mask) & ~page_mask;
576 if (type & MEM_TOP_DOWN) {
577 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
578 * Is there _ANY_ way to do it with UNIX mmap()?
580 WARN(virtual,"MEM_TOP_DOWN ignored\n");
581 type &= ~MEM_TOP_DOWN;
583 /* Compute the protection flags */
585 if (!(type & (MEM_COMMIT | MEM_RESERVE)) ||
586 (type & ~(MEM_COMMIT | MEM_RESERVE)))
588 SetLastError( ERROR_INVALID_PARAMETER );
589 return NULL;
591 if (type & MEM_COMMIT)
592 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
593 else vprot = 0;
595 /* Reserve the memory */
597 if ((type & MEM_RESERVE) || !base)
599 view_size = size + (base ? 0 : granularity_mask + 1);
600 ptr = (UINT32)FILE_dommap( -1, (LPVOID)base, 0, view_size, 0, 0,
601 VIRTUAL_GetUnixProt( vprot ), MAP_PRIVATE );
602 if (ptr == (UINT32)-1)
604 SetLastError( ERROR_OUTOFMEMORY );
605 return NULL;
607 if (!base)
609 /* Release the extra memory while keeping the range */
610 /* starting on a 64k boundary. */
612 if (ptr & granularity_mask)
614 UINT32 extra = granularity_mask + 1 - (ptr & granularity_mask);
615 FILE_munmap( (void *)ptr, 0, extra );
616 ptr += extra;
617 view_size -= extra;
619 if (view_size > size)
620 FILE_munmap( (void *)(ptr + size), 0, view_size - size );
622 else if (ptr != base)
624 /* We couldn't get the address we wanted */
625 FILE_munmap( (void *)ptr, 0, view_size );
626 SetLastError( ERROR_INVALID_ADDRESS );
627 return NULL;
629 if (!(view = VIRTUAL_CreateView( ptr, size, 0, 0, vprot, -1 )))
631 FILE_munmap( (void *)ptr, 0, size );
632 SetLastError( ERROR_OUTOFMEMORY );
633 return NULL;
635 VIRTUAL_DEBUG_DUMP_VIEW( view );
636 return (LPVOID)ptr;
639 /* Commit the pages */
641 if (!(view = VIRTUAL_FindView( base )) ||
642 (base + size > view->base + view->size))
644 SetLastError( ERROR_INVALID_PARAMETER );
645 return NULL;
648 if (!VIRTUAL_SetProt( view, base, size, vprot )) return NULL;
649 return (LPVOID)base;
653 /***********************************************************************
654 * VirtualFree (KERNEL32.550)
655 * Release or decommits a region of pages in virtual address space.
657 * RETURNS
658 * TRUE: Success
659 * FALSE: Failure
661 BOOL32 WINAPI VirtualFree(
662 LPVOID addr, /* [in] Address of region of committed pages */
663 DWORD size, /* [in] Size of region */
664 DWORD type /* [in] Type of operation */
666 FILE_VIEW *view;
667 UINT32 base;
669 TRACE(virtual, "%08x %08lx %lx\n",
670 (UINT32)addr, size, type );
672 /* Fix the parameters */
674 size = ROUND_SIZE( addr, size );
675 base = ROUND_ADDR( addr );
677 if (!(view = VIRTUAL_FindView( base )) ||
678 (base + size > view->base + view->size))
680 SetLastError( ERROR_INVALID_PARAMETER );
681 return FALSE;
684 /* Compute the protection flags */
686 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
688 SetLastError( ERROR_INVALID_PARAMETER );
689 return FALSE;
692 /* Free the pages */
694 if (type == MEM_RELEASE)
696 if (size || (base != view->base))
698 SetLastError( ERROR_INVALID_PARAMETER );
699 return FALSE;
701 VIRTUAL_DeleteView( view );
702 return TRUE;
705 /* Decommit the pages */
706 return VIRTUAL_SetProt( view, base, size, 0 );
710 /***********************************************************************
711 * VirtualLock (KERNEL32.551)
712 * Locks the specified region of virtual address space
714 * NOTE
715 * Always returns TRUE
717 * RETURNS
718 * TRUE: Success
719 * FALSE: Failure
721 BOOL32 WINAPI VirtualLock(
722 LPVOID addr, /* [in] Address of first byte of range to lock */
723 DWORD size /* [in] Number of bytes in range to lock */
725 return TRUE;
729 /***********************************************************************
730 * VirtualUnlock (KERNEL32.556)
731 * Unlocks a range of pages in the virtual address space
733 * NOTE
734 * Always returns TRUE
736 * RETURNS
737 * TRUE: Success
738 * FALSE: Failure
740 BOOL32 WINAPI VirtualUnlock(
741 LPVOID addr, /* [in] Address of first byte of range */
742 DWORD size /* [in] Number of bytes in range */
744 return TRUE;
748 /***********************************************************************
749 * VirtualProtect (KERNEL32.552)
750 * Changes the access protection on a region of committed pages
752 * RETURNS
753 * TRUE: Success
754 * FALSE: Failure
756 BOOL32 WINAPI VirtualProtect(
757 LPVOID addr, /* [in] Address of region of committed pages */
758 DWORD size, /* [in] Size of region */
759 DWORD new_prot, /* [in] Desired access protection */
760 LPDWORD old_prot /* [out] Address of variable to get old protection */
762 FILE_VIEW *view;
763 UINT32 base, i;
764 BYTE vprot, *p;
766 TRACE(virtual, "%08x %08lx %08lx\n",
767 (UINT32)addr, size, new_prot );
769 /* Fix the parameters */
771 size = ROUND_SIZE( addr, size );
772 base = ROUND_ADDR( addr );
774 if (!(view = VIRTUAL_FindView( base )) ||
775 (base + size > view->base + view->size))
777 SetLastError( ERROR_INVALID_PARAMETER );
778 return FALSE;
781 /* Make sure all the pages are committed */
783 p = view->prot + ((base - view->base) >> page_shift);
784 for (i = size >> page_shift; i; i--, p++)
786 if (!(*p & VPROT_COMMITTED))
788 SetLastError( ERROR_INVALID_PARAMETER );
789 return FALSE;
793 VIRTUAL_GetWin32Prot( view->prot[0], old_prot, NULL );
794 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
795 return VIRTUAL_SetProt( view, base, size, vprot );
799 /***********************************************************************
800 * VirtualProtectEx (KERNEL32.553)
801 * Changes the access protection on a region of committed pages in the
802 * virtual address space of a specified process
804 * RETURNS
805 * TRUE: Success
806 * FALSE: Failure
808 BOOL32 WINAPI VirtualProtectEx(
809 HANDLE32 handle, /* [in] Handle of process */
810 LPVOID addr, /* [in] Address of region of committed pages */
811 DWORD size, /* [in] Size of region */
812 DWORD new_prot, /* [in] Desired access protection */
813 LPDWORD old_prot /* [out] Address of variable to get old protection */
815 BOOL32 ret = FALSE;
817 PDB32 *pdb = PROCESS_GetPtr( handle, PROCESS_VM_OPERATION, NULL );
818 if (pdb)
820 if (pdb == PROCESS_Current())
821 ret = VirtualProtect( addr, size, new_prot, old_prot );
822 else
823 ERR(virtual,"Unsupported on other process\n");
824 K32OBJ_DecCount( &pdb->header );
826 return ret;
830 /***********************************************************************
831 * VirtualQuery (KERNEL32.554)
832 * Provides info about a range of pages in virtual address space
834 * RETURNS
835 * Number of bytes returned in information buffer
837 DWORD WINAPI VirtualQuery(
838 LPCVOID addr, /* [in] Address of region */
839 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
840 DWORD len /* [in] Size of buffer */
842 FILE_VIEW *view = VIRTUAL_FirstView;
843 UINT32 base = ROUND_ADDR( addr );
844 UINT32 alloc_base = 0;
845 UINT32 size = 0;
847 /* Find the view containing the address */
849 for (;;)
851 if (!view)
853 size = 0xffff0000 - alloc_base;
854 break;
856 if (view->base > base)
858 size = view->base - alloc_base;
859 view = NULL;
860 break;
862 if (view->base + view->size > base)
864 alloc_base = view->base;
865 size = view->size;
866 break;
868 alloc_base = view->base + view->size;
869 view = view->next;
872 /* Fill the info structure */
874 if (!view)
876 info->State = MEM_FREE;
877 info->Protect = 0;
878 info->AllocationProtect = 0;
879 info->Type = 0;
881 else
883 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
884 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
885 for (size = base - alloc_base; size < view->size; size += page_mask+1)
886 if (view->prot[size >> page_shift] != vprot) break;
887 info->AllocationProtect = view->protect;
888 info->Type = MEM_PRIVATE; /* FIXME */
891 info->BaseAddress = (LPVOID)base;
892 info->AllocationBase = (LPVOID)alloc_base;
893 info->RegionSize = size - (base - alloc_base);
894 return sizeof(*info);
898 /***********************************************************************
899 * VirtualQueryEx (KERNEL32.555)
900 * Provides info about a range of pages in virtual address space of a
901 * specified process
903 * RETURNS
904 * Number of bytes returned in information buffer
906 DWORD WINAPI VirtualQueryEx(
907 HANDLE32 handle, /* [in] Handle of process */
908 LPCVOID addr, /* [in] Address of region */
909 LPMEMORY_BASIC_INFORMATION info, /* [out] Address of info buffer */
910 DWORD len /* [in] Size of buffer */
912 DWORD ret = len;
914 PDB32 *pdb = PROCESS_GetPtr( handle, PROCESS_QUERY_INFORMATION, NULL );
915 if (pdb)
917 if (pdb == PROCESS_Current())
918 ret = VirtualQuery( addr, info, len );
919 else
920 ERR(virtual,"Unsupported on other process\n");
921 K32OBJ_DecCount( &pdb->header );
923 return ret;
927 /***********************************************************************
928 * IsBadReadPtr32 (KERNEL32.354)
930 * RETURNS
931 * FALSE: Process has read access to entire block
932 * TRUE: Otherwise
934 BOOL32 WINAPI IsBadReadPtr32(
935 LPCVOID ptr, /* Address of memory block */
936 UINT32 size /* Size of block */
938 return !VIRTUAL_CheckFlags( (UINT32)ptr, size,
939 VPROT_READ | VPROT_COMMITTED );
943 /***********************************************************************
944 * IsBadWritePtr32 (KERNEL32.357)
946 * RETURNS
947 * FALSE: Process has write access to entire block
948 * TRUE: Otherwise
950 BOOL32 WINAPI IsBadWritePtr32(
951 LPVOID ptr, /* [in] Address of memory block */
952 UINT32 size /* [in] Size of block in bytes */
954 return !VIRTUAL_CheckFlags( (UINT32)ptr, size,
955 VPROT_WRITE | VPROT_COMMITTED );
959 /***********************************************************************
960 * IsBadHugeReadPtr32 (KERNEL32.352)
961 * RETURNS
962 * FALSE: Process has read access to entire block
963 * TRUE: Otherwise
965 BOOL32 WINAPI IsBadHugeReadPtr32(
966 LPCVOID ptr, /* [in] Address of memory block */
967 UINT32 size /* [in] Size of block */
969 return IsBadReadPtr32( ptr, size );
973 /***********************************************************************
974 * IsBadHugeWritePtr32 (KERNEL32.353)
975 * RETURNS
976 * FALSE: Process has write access to entire block
977 * TRUE: Otherwise
979 BOOL32 WINAPI IsBadHugeWritePtr32(
980 LPVOID ptr, /* [in] Address of memory block */
981 UINT32 size /* [in] Size of block */
983 return IsBadWritePtr32( ptr, size );
987 /***********************************************************************
988 * IsBadCodePtr32 (KERNEL32.351)
990 * RETURNS
991 * FALSE: Process has read access to specified memory
992 * TRUE: Otherwise
994 BOOL32 WINAPI IsBadCodePtr32(
995 FARPROC32 ptr /* [in] Address of function */
997 return !VIRTUAL_CheckFlags( (UINT32)ptr, 1, VPROT_EXEC | VPROT_COMMITTED );
1001 /***********************************************************************
1002 * IsBadStringPtr32A (KERNEL32.355)
1004 * RETURNS
1005 * FALSE: Read access to all bytes in string
1006 * TRUE: Else
1008 BOOL32 WINAPI IsBadStringPtr32A(
1009 LPCSTR str, /* [in] Address of string */
1010 UINT32 max /* [in] Maximum size of string */
1012 FILE_VIEW *view;
1013 UINT32 page, count;
1015 if (!max) return FALSE;
1016 if (!(view = VIRTUAL_FindView( (UINT32)str ))) return TRUE;
1017 page = ((UINT32)str - view->base) >> page_shift;
1018 count = page_mask + 1 - ((UINT32)str & page_mask);
1020 while (max)
1022 if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) !=
1023 (VPROT_READ | VPROT_COMMITTED))
1024 return TRUE;
1025 if (count > max) count = max;
1026 max -= count;
1027 while (count--) if (!*str++) return FALSE;
1028 if (++page >= view->size >> page_shift) return TRUE;
1029 count = page_mask + 1;
1031 return FALSE;
1035 /***********************************************************************
1036 * IsBadStringPtr32W (KERNEL32.356)
1037 * See IsBadStringPtr32A
1039 BOOL32 WINAPI IsBadStringPtr32W( LPCWSTR str, UINT32 max )
1041 FILE_VIEW *view;
1042 UINT32 page, count;
1044 if (!max) return FALSE;
1045 if (!(view = VIRTUAL_FindView( (UINT32)str ))) return TRUE;
1046 page = ((UINT32)str - view->base) >> page_shift;
1047 count = (page_mask + 1 - ((UINT32)str & page_mask)) / sizeof(WCHAR);
1049 while (max)
1051 if ((view->prot[page] & (VPROT_READ | VPROT_COMMITTED)) !=
1052 (VPROT_READ | VPROT_COMMITTED))
1053 return TRUE;
1054 if (count > max) count = max;
1055 max -= count;
1056 while (count--) if (!*str++) return FALSE;
1057 if (++page >= view->size >> page_shift) return TRUE;
1058 count = (page_mask + 1) / sizeof(WCHAR);
1060 return FALSE;
1064 /***********************************************************************
1065 * CreateFileMapping32A (KERNEL32.46)
1066 * Creates a named or unnamed file-mapping object for the specified file
1068 * RETURNS
1069 * Handle: Success
1070 * 0: Mapping object does not exist
1071 * NULL: Failure
1073 HANDLE32 WINAPI CreateFileMapping32A(
1074 HFILE32 hFile, /* [in] Handle of file to map */
1075 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
1076 DWORD protect, /* [in] Protection for mapping object */
1077 DWORD size_high, /* [in] High-order 32 bits of object size */
1078 DWORD size_low, /* [in] Low-order 32 bits of object size */
1079 LPCSTR name /* [in] Name of file-mapping object */ )
1081 FILE_MAPPING *mapping = NULL;
1082 FILE_OBJECT *file;
1083 struct create_mapping_request req;
1084 struct create_mapping_reply reply = { -1 };
1085 HANDLE32 handle;
1086 BYTE vprot;
1087 BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
1089 /* First search for an object with the same name */
1091 K32OBJ *obj = K32OBJ_FindName( name );
1093 if (obj)
1095 if (obj->type == K32OBJ_MEM_MAPPED_FILE)
1097 SetLastError( ERROR_ALREADY_EXISTS );
1098 handle = HANDLE_Alloc( PROCESS_Current(), obj,
1099 FILE_MAP_ALL_ACCESS /*FIXME*/, inherit, -1 );
1101 else
1103 SetLastError( ERROR_DUP_NAME );
1104 handle = 0;
1106 K32OBJ_DecCount( obj );
1107 return handle;
1110 /* Check parameters */
1112 TRACE(virtual,"(%x,%p,%08lx,%08lx%08lx,%s)\n",
1113 hFile, sa, protect, size_high, size_low, debugstr_a(name) );
1115 vprot = VIRTUAL_GetProt( protect );
1116 if (protect & SEC_RESERVE)
1118 if (hFile != INVALID_HANDLE_VALUE32)
1120 SetLastError( ERROR_INVALID_PARAMETER );
1121 return 0;
1124 else vprot |= VPROT_COMMITTED;
1125 if (protect & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1127 /* Compute the size and extend the file if necessary */
1129 if (hFile == INVALID_HANDLE_VALUE32)
1131 if (!size_high && !size_low)
1133 SetLastError( ERROR_INVALID_PARAMETER );
1134 return 0;
1136 file = NULL;
1137 if (name)
1139 CHAR buf[260];
1141 GetTempPath32A(260,buf);
1142 GetTempFileName32A(buf,"wine",0,buf);
1143 hFile = CreateFile32A(
1144 buf,
1145 GENERIC_READ|GENERIC_WRITE,
1146 FILE_SHARE_READ|FILE_SHARE_WRITE,/* so we can reuse the tmpfn */
1147 NULL,
1148 OPEN_ALWAYS,
1152 /* FIXME: bad hack to avoid lots of leftover tempfiles */
1153 DeleteFile32A(buf);
1154 if (hFile == INVALID_HANDLE_VALUE32)
1155 FIXME(virtual,"could not create temp. file for anon shared mapping: reason was 0x%08lx\n",GetLastError());
1158 if (hFile != INVALID_HANDLE_VALUE32) /* We have a file */
1160 BY_HANDLE_FILE_INFORMATION info;
1161 DWORD access = GENERIC_READ;
1163 if (((protect & 0xff) == PAGE_READWRITE) ||
1164 ((protect & 0xff) == PAGE_WRITECOPY) ||
1165 ((protect & 0xff) == PAGE_EXECUTE_READWRITE) ||
1166 ((protect & 0xff) == PAGE_EXECUTE_WRITECOPY))
1167 access |= GENERIC_WRITE;
1168 if (!(file = FILE_GetFile( hFile, access, &req.handle ))) goto error;
1169 if (!GetFileInformationByHandle( hFile, &info )) goto error;
1170 if (!size_high && !size_low)
1172 size_high = info.nFileSizeHigh;
1173 size_low = info.nFileSizeLow;
1175 else if ((size_high > info.nFileSizeHigh) ||
1176 ((size_high == info.nFileSizeHigh) &&
1177 (size_low > info.nFileSizeLow)))
1179 /* We have to grow the file */
1180 if (SetFilePointer( hFile, size_low, &size_high,
1181 FILE_BEGIN ) == 0xffffffff) goto error;
1182 if (!SetEndOfFile( hFile )) goto error;
1185 else req.handle = -1;
1187 /* Create the server object */
1189 req.size_high = size_high;
1190 req.size_low = ROUND_SIZE( 0, size_low );
1191 req.protect = vprot;
1192 CLIENT_SendRequest( REQ_CREATE_MAPPING, -1, 2,
1193 &req, sizeof(req),
1194 name, name ? strlen(name) + 1 : 0 );
1195 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
1196 goto error;
1198 /* Allocate the mapping object */
1200 if (!(mapping = HeapAlloc( SystemHeap, 0, sizeof(*mapping) ))) goto error;
1201 mapping->header.type = K32OBJ_MEM_MAPPED_FILE;
1202 mapping->header.refcount = 1;
1203 mapping->file = file;
1205 if (!K32OBJ_AddName( &mapping->header, name )) handle = 0;
1206 else handle = HANDLE_Alloc( PROCESS_Current(), &mapping->header,
1207 FILE_MAP_ALL_ACCESS /*FIXME*/, inherit, reply.handle );
1208 K32OBJ_DecCount( &mapping->header );
1209 SetLastError(0); /* Last error value is relevant. (see the start of fun) */
1210 return handle;
1212 error:
1213 if (reply.handle != -1) CLIENT_CloseHandle( reply.handle );
1214 if (file) K32OBJ_DecCount( &file->header );
1215 if (mapping) HeapFree( SystemHeap, 0, mapping );
1216 return 0;
1220 /***********************************************************************
1221 * CreateFileMapping32W (KERNEL32.47)
1222 * See CreateFileMapping32A
1224 HANDLE32 WINAPI CreateFileMapping32W( HFILE32 hFile, LPSECURITY_ATTRIBUTES attr,
1225 DWORD protect, DWORD size_high,
1226 DWORD size_low, LPCWSTR name )
1228 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1229 HANDLE32 ret = CreateFileMapping32A( hFile, attr, protect,
1230 size_high, size_low, nameA );
1231 HeapFree( GetProcessHeap(), 0, nameA );
1232 return ret;
1236 /***********************************************************************
1237 * OpenFileMapping32A (KERNEL32.397)
1238 * Opens a named file-mapping object.
1240 * RETURNS
1241 * Handle: Success
1242 * NULL: Failure
1244 HANDLE32 WINAPI OpenFileMapping32A(
1245 DWORD access, /* [in] Access mode */
1246 BOOL32 inherit, /* [in] Inherit flag */
1247 LPCSTR name ) /* [in] Name of file-mapping object */
1249 HANDLE32 handle = 0;
1250 K32OBJ *obj;
1251 struct open_named_obj_request req;
1252 struct open_named_obj_reply reply;
1253 int len = name ? strlen(name) + 1 : 0;
1255 req.type = OPEN_MAPPING;
1256 req.access = access;
1257 req.inherit = inherit;
1258 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ, -1, 2, &req, sizeof(req), name, len );
1259 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
1261 if (reply.handle != -1)
1263 SYSTEM_LOCK();
1264 if ((obj = K32OBJ_FindNameType( name, K32OBJ_MEM_MAPPED_FILE )))
1266 handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, reply.handle );
1267 K32OBJ_DecCount( obj );
1268 if (handle == INVALID_HANDLE_VALUE32)
1269 handle = 0; /* must return 0 on failure, not -1 */
1271 else CLIENT_CloseHandle( reply.handle );
1272 SYSTEM_UNLOCK();
1274 return handle;
1278 /***********************************************************************
1279 * OpenFileMapping32W (KERNEL32.398)
1280 * See OpenFileMapping32A
1282 HANDLE32 WINAPI OpenFileMapping32W( DWORD access, BOOL32 inherit, LPCWSTR name)
1284 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1285 HANDLE32 ret = OpenFileMapping32A( access, inherit, nameA );
1286 HeapFree( GetProcessHeap(), 0, nameA );
1287 return ret;
1291 /***********************************************************************
1292 * VIRTUAL_DestroyMapping
1294 * Destroy a FILE_MAPPING object.
1296 static void VIRTUAL_DestroyMapping( K32OBJ *ptr )
1298 FILE_MAPPING *mapping = (FILE_MAPPING *)ptr;
1299 assert( ptr->type == K32OBJ_MEM_MAPPED_FILE );
1301 if (mapping->file) K32OBJ_DecCount( &mapping->file->header );
1302 ptr->type = K32OBJ_UNKNOWN;
1303 HeapFree( SystemHeap, 0, mapping );
1307 /***********************************************************************
1308 * MapViewOfFile (KERNEL32.385)
1309 * Maps a view of a file into the address space
1311 * RETURNS
1312 * Starting address of mapped view
1313 * NULL: Failure
1315 LPVOID WINAPI MapViewOfFile(
1316 HANDLE32 mapping, /* [in] File-mapping object to map */
1317 DWORD access, /* [in] Access mode */
1318 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1319 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1320 DWORD count /* [in] Number of bytes to map */
1322 return MapViewOfFileEx( mapping, access, offset_high,
1323 offset_low, count, NULL );
1327 /***********************************************************************
1328 * MapViewOfFileEx (KERNEL32.386)
1329 * Maps a view of a file into the address space
1331 * RETURNS
1332 * Starting address of mapped view
1333 * NULL: Failure
1335 LPVOID WINAPI MapViewOfFileEx(
1336 HANDLE32 handle, /* [in] File-mapping object to map */
1337 DWORD access, /* [in] Access mode */
1338 DWORD offset_high, /* [in] High-order 32 bits of file offset */
1339 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
1340 DWORD count, /* [in] Number of bytes to map */
1341 LPVOID addr /* [in] Suggested starting address for mapped view */
1343 FILE_MAPPING *mapping;
1344 FILE_VIEW *view;
1345 UINT32 ptr = (UINT32)-1, size = 0;
1346 int flags = MAP_PRIVATE;
1347 int unix_handle = -1;
1348 struct get_mapping_info_request req;
1349 struct get_mapping_info_reply info;
1351 /* Check parameters */
1353 if ((offset_low & granularity_mask) ||
1354 (addr && ((UINT32)addr & granularity_mask)))
1356 SetLastError( ERROR_INVALID_PARAMETER );
1357 return NULL;
1360 if (!(mapping = (FILE_MAPPING *)HANDLE_GetObjPtr( PROCESS_Current(),
1361 handle,
1362 K32OBJ_MEM_MAPPED_FILE,
1363 0 /* FIXME */, &req.handle )))
1364 return NULL;
1366 CLIENT_SendRequest( REQ_GET_MAPPING_INFO, -1, 1, &req, sizeof(req) );
1367 if (CLIENT_WaitSimpleReply( &info, sizeof(info), &unix_handle ))
1368 goto error;
1370 if (info.size_high || offset_high)
1371 ERR(virtual, "Offsets larger than 4Gb not supported\n");
1373 if ((offset_low >= info.size_low) ||
1374 (count > info.size_low - offset_low))
1376 SetLastError( ERROR_INVALID_PARAMETER );
1377 goto error;
1379 if (count) size = ROUND_SIZE( offset_low, count );
1380 else size = info.size_low - offset_low;
1382 switch(access)
1384 case FILE_MAP_ALL_ACCESS:
1385 case FILE_MAP_WRITE:
1386 case FILE_MAP_WRITE | FILE_MAP_READ:
1387 if (!(info.protect & VPROT_WRITE))
1389 SetLastError( ERROR_INVALID_PARAMETER );
1390 goto error;
1392 flags = MAP_SHARED;
1393 /* fall through */
1394 case FILE_MAP_READ:
1395 case FILE_MAP_COPY:
1396 case FILE_MAP_COPY | FILE_MAP_READ:
1397 if (info.protect & VPROT_READ) break;
1398 /* fall through */
1399 default:
1400 SetLastError( ERROR_INVALID_PARAMETER );
1401 goto error;
1404 /* Map the file */
1406 TRACE(virtual, "handle=%x size=%x offset=%lx\n",
1407 handle, size, offset_low );
1409 ptr = (UINT32)FILE_dommap( unix_handle,
1410 addr, 0, size, 0, offset_low,
1411 VIRTUAL_GetUnixProt( info.protect ),
1412 flags );
1413 if (ptr == (UINT32)-1) {
1414 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1415 * Platform Differences":
1416 * Windows NT: ERROR_INVALID_PARAMETER
1417 * Windows 95: ERROR_INVALID_ADDRESS.
1418 * FIXME: So should we add a module dependend check here? -MM
1420 if (errno==ENOMEM)
1421 SetLastError( ERROR_OUTOFMEMORY );
1422 else
1423 SetLastError( ERROR_INVALID_PARAMETER );
1424 goto error;
1427 if (!(view = VIRTUAL_CreateView( ptr, size, offset_low, 0,
1428 info.protect, handle )))
1430 SetLastError( ERROR_OUTOFMEMORY );
1431 goto error;
1433 if (unix_handle != -1) close( unix_handle );
1434 K32OBJ_DecCount( &mapping->header );
1435 return (LPVOID)ptr;
1437 error:
1438 if (unix_handle != -1) close( unix_handle );
1439 if (ptr != (UINT32)-1) FILE_munmap( (void *)ptr, 0, size );
1440 K32OBJ_DecCount( &mapping->header );
1441 return NULL;
1445 /***********************************************************************
1446 * FlushViewOfFile (KERNEL32.262)
1447 * Writes to the disk a byte range within a mapped view of a file
1449 * RETURNS
1450 * TRUE: Success
1451 * FALSE: Failure
1453 BOOL32 WINAPI FlushViewOfFile(
1454 LPCVOID base, /* [in] Start address of byte range to flush */
1455 DWORD cbFlush /* [in] Number of bytes in range */
1457 FILE_VIEW *view;
1458 UINT32 addr = ROUND_ADDR( base );
1460 TRACE(virtual, "FlushViewOfFile at %p for %ld bytes\n",
1461 base, cbFlush );
1463 if (!(view = VIRTUAL_FindView( addr )))
1465 SetLastError( ERROR_INVALID_PARAMETER );
1466 return FALSE;
1468 if (!cbFlush) cbFlush = view->size;
1469 if (!msync( (void *)addr, cbFlush, MS_SYNC )) return TRUE;
1470 SetLastError( ERROR_INVALID_PARAMETER );
1471 return FALSE;
1475 /***********************************************************************
1476 * UnmapViewOfFile (KERNEL32.540)
1477 * Unmaps a mapped view of a file.
1479 * NOTES
1480 * Should addr be an LPCVOID?
1482 * RETURNS
1483 * TRUE: Success
1484 * FALSE: Failure
1486 BOOL32 WINAPI UnmapViewOfFile(
1487 LPVOID addr /* [in] Address where mapped view begins */
1489 FILE_VIEW *view;
1490 UINT32 base = ROUND_ADDR( addr );
1491 if (!(view = VIRTUAL_FindView( base )) || (base != view->base))
1493 SetLastError( ERROR_INVALID_PARAMETER );
1494 return FALSE;
1496 VIRTUAL_DeleteView( view );
1497 return TRUE;
1500 /***********************************************************************
1501 * VIRTUAL_MapFileW
1503 * Helper function to map a file to memory:
1504 * name - file name
1505 * [RETURN] ptr - pointer to mapped file
1507 LPVOID VIRTUAL_MapFileW( LPCWSTR name )
1509 HANDLE32 hFile, hMapping;
1510 LPVOID ptr = NULL;
1512 hFile = CreateFile32W( name, GENERIC_READ, FILE_SHARE_READ, NULL,
1513 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
1514 if (hFile != INVALID_HANDLE_VALUE32)
1516 hMapping = CreateFileMapping32A( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
1517 CloseHandle( hFile );
1518 if (hMapping)
1520 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
1521 CloseHandle( hMapping );
1524 return ptr;