2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
13 #include <sys/types.h>
41 struct _FV
*next
; /* Next view */
42 struct _FV
*prev
; /* Prev view */
43 UINT32 base
; /* Base address */
44 UINT32 size
; /* Size in bytes */
45 UINT32 flags
; /* Allocation flags */
46 UINT32 offset
; /* Offset from start of mapped file */
47 FILE_MAPPING
*mapping
; /* File mapping */
48 BYTE protect
; /* Protection for all pages at allocation time */
49 BYTE prot
[1]; /* Protection byte for each page */
52 /* Per-page protection byte values */
53 #define VPROT_READ 0x01
54 #define VPROT_WRITE 0x02
55 #define VPROT_EXEC 0x04
56 #define VPROT_WRITECOPY 0x08
57 #define VPROT_GUARD 0x10
58 #define VPROT_NOCACHE 0x20
59 #define VPROT_COMMITTED 0x40
62 #define VFLAG_SYSTEM 0x01
64 /* Conversion from VPROT_* to Win32 flags */
65 static const BYTE VIRTUAL_Win32Flags
[16] =
67 PAGE_NOACCESS
, /* 0 */
68 PAGE_READONLY
, /* READ */
69 PAGE_READWRITE
, /* WRITE */
70 PAGE_READWRITE
, /* READ | WRITE */
71 PAGE_EXECUTE
, /* EXEC */
72 PAGE_EXECUTE_READ
, /* READ | EXEC */
73 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
74 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
75 PAGE_WRITECOPY
, /* WRITECOPY */
76 PAGE_WRITECOPY
, /* READ | WRITECOPY */
77 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
78 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
79 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
80 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
81 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
82 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
86 static FILE_VIEW
*VIRTUAL_FirstView
;
88 static UINT32 page_shift
;
89 static UINT32 page_mask
;
90 static UINT32 granularity_mask
; /* Allocation granularity (usually 64k) */
92 #define ROUND_ADDR(addr) \
93 ((UINT32)(addr) & ~page_mask)
95 #define ROUND_SIZE(addr,size) \
96 (((UINT32)(size) + ((UINT32)(addr) & page_mask) + page_mask) & ~page_mask)
98 static void VIRTUAL_DestroyMapping( K32OBJ
*obj
);
100 const K32OBJ_OPS MEM_MAPPED_FILE_Ops
=
102 /* Object cannot be waited upon, so we don't need these (except destroy) */
104 NULL
, /* satisfied */
106 NULL
, /* remove_wait */
107 VIRTUAL_DestroyMapping
/* destroy */
110 /***********************************************************************
113 static const char *VIRTUAL_GetProtStr( BYTE prot
)
115 static char buffer
[6];
116 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
117 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
118 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
119 buffer
[3] = (prot
& VPROT_WRITE
) ?
120 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
121 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
127 /***********************************************************************
130 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
133 UINT32 addr
= view
->base
;
134 BYTE prot
= view
->prot
[0];
136 dprintf_virtual( stddeb
, "View: %08x - %08x%s",
137 view
->base
, view
->base
+ view
->size
- 1,
138 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
139 if (view
->mapping
&& view
->mapping
->file
)
140 dprintf_virtual( stddeb
, " %s @ %08x\n",
141 view
->mapping
->file
->unix_name
, view
->offset
);
143 dprintf_virtual( stddeb
, " (anonymous)\n");
145 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
147 if (view
->prot
[i
] == prot
) continue;
148 dprintf_virtual( stddeb
, " %08x - %08x %s\n",
149 addr
, addr
+ (count
<< page_shift
) - 1,
150 VIRTUAL_GetProtStr(prot
) );
151 addr
+= (count
<< page_shift
);
152 prot
= view
->prot
[i
];
156 dprintf_virtual( stddeb
, " %08x - %08x %s\n",
157 addr
, addr
+ (count
<< page_shift
) - 1,
158 VIRTUAL_GetProtStr(prot
) );
162 /***********************************************************************
165 void VIRTUAL_Dump(void)
167 FILE_VIEW
*view
= VIRTUAL_FirstView
;
168 dprintf_virtual( stddeb
, "\nDump of all virtual memory views:\n\n" );
171 VIRTUAL_DumpView( view
);
177 /***********************************************************************
180 * Find the view containing a given address.
182 static FILE_VIEW
*VIRTUAL_FindView( UINT32 addr
)
184 FILE_VIEW
*view
= VIRTUAL_FirstView
;
187 if (view
->base
> addr
) return NULL
;
188 if (view
->base
+ view
->size
> addr
) return view
;
195 /***********************************************************************
198 * Create a new view and add it in the linked list.
200 static FILE_VIEW
*VIRTUAL_CreateView( UINT32 base
, UINT32 size
, UINT32 offset
,
201 UINT32 flags
, BYTE vprot
,
202 FILE_MAPPING
*mapping
)
204 FILE_VIEW
*view
, *prev
;
206 /* Create the view structure */
208 assert( !(base
& page_mask
) );
209 assert( !(size
& page_mask
) );
211 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
213 view
->size
= size
<< page_shift
;
215 view
->offset
= offset
;
216 view
->mapping
= mapping
;
217 view
->protect
= vprot
;
218 memset( view
->prot
, vprot
, size
);
220 /* Insert it in the linked list */
222 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
224 view
->next
= VIRTUAL_FirstView
;
226 if (view
->next
) view
->next
->prev
= view
;
227 VIRTUAL_FirstView
= view
;
231 prev
= VIRTUAL_FirstView
;
232 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
233 view
->next
= prev
->next
;
235 if (view
->next
) view
->next
->prev
= view
;
238 if (debugging_virtual
) VIRTUAL_DumpView( view
);
243 /***********************************************************************
248 static void VIRTUAL_DeleteView( FILE_VIEW
*view
)
250 FILE_munmap( (void *)view
->base
, 0, view
->size
);
251 if (view
->next
) view
->next
->prev
= view
->prev
;
252 if (view
->prev
) view
->prev
->next
= view
->next
;
253 else VIRTUAL_FirstView
= view
->next
;
254 if (view
->mapping
) K32OBJ_DecCount( &view
->mapping
->header
);
259 /***********************************************************************
260 * VIRTUAL_GetUnixProt
262 * Convert page protections to protection for mmap/mprotect.
264 static int VIRTUAL_GetUnixProt( BYTE vprot
)
267 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
269 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
270 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
271 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
277 /***********************************************************************
278 * VIRTUAL_GetWin32Prot
280 * Convert page protections to Win32 flags.
282 static void VIRTUAL_GetWin32Prot( BYTE vprot
, DWORD
*protect
, DWORD
*state
)
284 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
285 if (vprot
& VPROT_GUARD
) *protect
|= PAGE_GUARD
;
286 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
288 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
292 /***********************************************************************
295 * Build page protections from Win32 flags.
297 static BYTE
VIRTUAL_GetProt( DWORD protect
)
301 switch(protect
& 0xff)
307 vprot
= VPROT_READ
| VPROT_WRITE
;
310 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
315 case PAGE_EXECUTE_READ
:
316 vprot
= VPROT_EXEC
| VPROT_READ
;
318 case PAGE_EXECUTE_READWRITE
:
319 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
321 case PAGE_EXECUTE_WRITECOPY
:
322 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
329 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
330 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
335 /***********************************************************************
338 * Change the protection of a range of pages.
340 static BOOL32
VIRTUAL_SetProt( FILE_VIEW
*view
, UINT32 base
,
341 UINT32 size
, BYTE vprot
)
343 dprintf_virtual( stddeb
, "VIRTUAL_SetProt: %08x-%08x %s\n",
344 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
346 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
347 return FALSE
; /* FIXME: last error */
349 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
350 vprot
, size
>> page_shift
);
351 if (debugging_virtual
) VIRTUAL_DumpView( view
);
356 /***********************************************************************
359 * Check that all pages in a range have the given flags.
361 static BOOL32
VIRTUAL_CheckFlags( UINT32 base
, UINT32 size
, BYTE flags
)
366 if (!size
) return TRUE
;
367 if (!(view
= VIRTUAL_FindView( base
))) return FALSE
;
368 if (view
->base
+ view
->size
< base
+ size
) return FALSE
;
369 page
= (base
- view
->base
) >> page_shift
;
370 size
= ROUND_SIZE( base
, size
) >> page_shift
;
371 while (size
--) if ((view
->prot
[page
++] & flags
) != flags
) return FALSE
;
376 /***********************************************************************
379 BOOL32
VIRTUAL_Init(void)
382 GetSystemInfo( &sysinfo
);
384 page_mask
= sysinfo
.dwPageSize
- 1;
385 granularity_mask
= sysinfo
.dwAllocationGranularity
- 1;
386 /* Make sure we have a power of 2 */
387 assert( !(sysinfo
.dwPageSize
& page_mask
) );
388 assert( !(sysinfo
.dwAllocationGranularity
& granularity_mask
) );
390 while ((1 << page_shift
) != sysinfo
.dwPageSize
) page_shift
++;
394 /* Do not use stdio here since it may temporarily change the size
395 * of some segments (ie libc6 adds 0x1000 per open FILE)
397 int fd
= open ("/proc/self/maps", O_RDONLY
);
404 int start
, end
, offset
;
406 BYTE vprot
= VPROT_COMMITTED
;
409 int count
= sizeof(buffer
);
410 while (1 == read(fd
, ptr
, 1) && *ptr
!= '\n' && --count
> 0)
413 if (*ptr
!= '\n') break;
416 sscanf( buffer
, "%x-%x %c%c%c%c %x",
417 &start
, &end
, &r
, &w
, &x
, &p
, &offset
);
418 if (r
== 'r') vprot
|= VPROT_READ
;
419 if (w
== 'w') vprot
|= VPROT_WRITE
;
420 if (x
== 'x') vprot
|= VPROT_EXEC
;
421 if (p
== 'p') vprot
|= VPROT_WRITECOPY
;
422 VIRTUAL_CreateView( start
, end
- start
, 0,
423 VFLAG_SYSTEM
, vprot
, NULL
);
433 /***********************************************************************
434 * VirtualAlloc (KERNEL32.548)
436 LPVOID WINAPI
VirtualAlloc( LPVOID addr
, DWORD size
, DWORD type
, DWORD protect
)
439 UINT32 base
, ptr
, view_size
;
442 dprintf_virtual( stddeb
, "VirtualAlloc: %08x %08lx %lx %08lx\n",
443 (UINT32
)addr
, size
, type
, protect
);
445 /* Round parameters to a page boundary */
447 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
449 SetLastError( ERROR_OUTOFMEMORY
);
454 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
455 base
= ((UINT32
)addr
+ granularity_mask
) & ~granularity_mask
;
457 base
= ROUND_ADDR( addr
);
458 size
= (((UINT32
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
459 if (base
+ size
< base
) /* Disallow wrap-around */
461 SetLastError( ERROR_INVALID_PARAMETER
);
468 size
= (size
+ page_mask
) & ~page_mask
;
471 /* Compute the protection flags */
473 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
474 (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
476 SetLastError( ERROR_INVALID_PARAMETER
);
479 if (type
& MEM_COMMIT
)
480 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
483 /* Reserve the memory */
485 if ((type
& MEM_RESERVE
) || !base
)
487 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
488 ptr
= (UINT32
)FILE_dommap( NULL
, (LPVOID
)base
, 0, view_size
, 0, 0,
489 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
490 if (ptr
== (UINT32
)-1)
492 SetLastError( ERROR_OUTOFMEMORY
);
497 /* Release the extra memory while keeping the range */
498 /* starting on a 64k boundary. */
500 if (ptr
& granularity_mask
)
502 UINT32 extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
503 FILE_munmap( (void *)ptr
, 0, extra
);
507 if (view_size
> size
)
508 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
510 else if (ptr
!= base
)
512 /* We couldn't get the address we wanted */
513 FILE_munmap( (void *)ptr
, 0, view_size
);
514 SetLastError( ERROR_INVALID_ADDRESS
);
517 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, 0, vprot
, NULL
)))
519 FILE_munmap( (void *)ptr
, 0, size
);
520 SetLastError( ERROR_OUTOFMEMORY
);
523 if (debugging_virtual
) VIRTUAL_DumpView( view
);
527 /* Commit the pages */
529 if (!(view
= VIRTUAL_FindView( base
)) ||
530 (base
+ size
> view
->base
+ view
->size
))
532 SetLastError( ERROR_INVALID_PARAMETER
);
536 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
541 /***********************************************************************
542 * VirtualFree (KERNEL32.550)
544 BOOL32 WINAPI
VirtualFree( LPVOID addr
, DWORD size
, DWORD type
)
549 dprintf_virtual( stddeb
, "VirtualFree: %08x %08lx %lx\n",
550 (UINT32
)addr
, size
, type
);
552 /* Fix the parameters */
554 size
= ROUND_SIZE( addr
, size
);
555 base
= ROUND_ADDR( addr
);
557 if (!(view
= VIRTUAL_FindView( base
)) ||
558 (base
+ size
> view
->base
+ view
->size
))
560 SetLastError( ERROR_INVALID_PARAMETER
);
564 /* Compute the protection flags */
566 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
568 SetLastError( ERROR_INVALID_PARAMETER
);
574 if (type
== MEM_RELEASE
)
576 if (size
|| (base
!= view
->base
))
578 SetLastError( ERROR_INVALID_PARAMETER
);
581 VIRTUAL_DeleteView( view
);
585 /* Decommit the pages */
587 return VIRTUAL_SetProt( view
, base
, size
, 0 );
591 /***********************************************************************
592 * VirtualLock (KERNEL32.551)
594 BOOL32 WINAPI
VirtualLock( LPVOID addr
, DWORD size
)
600 /***********************************************************************
601 * VirtualUnlock (KERNEL32.556)
603 BOOL32 WINAPI
VirtualUnlock( LPVOID addr
, DWORD size
)
609 /***********************************************************************
610 * VirtualProtect (KERNEL32.552)
612 BOOL32 WINAPI
VirtualProtect( LPVOID addr
, DWORD size
, DWORD new_prot
,
619 dprintf_virtual( stddeb
, "VirtualProtect: %08x %08lx %08lx\n",
620 (UINT32
)addr
, size
, new_prot
);
622 /* Fix the parameters */
624 size
= ROUND_SIZE( addr
, size
);
625 base
= ROUND_ADDR( addr
);
627 if (!(view
= VIRTUAL_FindView( base
)) ||
628 (base
+ size
> view
->base
+ view
->size
))
630 SetLastError( ERROR_INVALID_PARAMETER
);
634 /* Make sure all the pages are committed */
636 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
637 for (i
= size
>> page_shift
; i
; i
--, p
++)
639 if (!(*p
& VPROT_COMMITTED
))
641 SetLastError( ERROR_INVALID_PARAMETER
);
646 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
647 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
648 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
652 /***********************************************************************
653 * VirtualProtectEx (KERNEL32.553)
655 BOOL32 WINAPI
VirtualProtectEx( HANDLE32 handle
, LPVOID addr
, DWORD size
,
656 DWORD new_prot
, LPDWORD old_prot
)
660 PDB32
*pdb
= (PDB32
*)PROCESS_GetObjPtr( handle
, K32OBJ_PROCESS
);
663 if (pdb
== PROCESS_Current())
664 ret
= VirtualProtect( addr
, size
, new_prot
, old_prot
);
666 fprintf(stderr
,"Unsupported: VirtualProtectEx on other process\n");
667 K32OBJ_DecCount( &pdb
->header
);
673 /***********************************************************************
674 * VirtualQuery (KERNEL32.554)
676 BOOL32 WINAPI
VirtualQuery( LPCVOID addr
, LPMEMORY_BASIC_INFORMATION info
,
679 FILE_VIEW
*view
= VIRTUAL_FirstView
;
680 UINT32 base
= ROUND_ADDR( addr
);
681 UINT32 alloc_base
= 0;
684 /* Find the view containing the address */
690 size
= 0xffff0000 - alloc_base
;
693 if (view
->base
> base
)
695 size
= view
->base
- alloc_base
;
699 if (view
->base
+ view
->size
> base
)
701 alloc_base
= view
->base
;
705 alloc_base
= view
->base
+ view
->size
;
709 /* Fill the info structure */
713 info
->State
= MEM_FREE
;
715 info
->AllocationProtect
= 0;
720 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
721 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
722 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
723 if (view
->prot
[size
>> page_shift
] != vprot
) break;
724 info
->AllocationProtect
= view
->protect
;
725 info
->Type
= MEM_PRIVATE
; /* FIXME */
728 info
->BaseAddress
= (LPVOID
)base
;
729 info
->AllocationBase
= (LPVOID
)alloc_base
;
730 info
->RegionSize
= size
- (base
- alloc_base
);
735 /***********************************************************************
736 * VirtualQueryEx (KERNEL32.555)
738 BOOL32 WINAPI
VirtualQueryEx( HANDLE32 handle
, LPCVOID addr
,
739 LPMEMORY_BASIC_INFORMATION info
, DWORD len
)
743 PDB32
*pdb
= (PDB32
*)PROCESS_GetObjPtr( handle
, K32OBJ_PROCESS
);
746 if (pdb
== PROCESS_Current())
747 ret
= VirtualQuery( addr
, info
, len
);
749 fprintf(stderr
,"Unsupported: VirtualQueryEx on other process\n");
750 K32OBJ_DecCount( &pdb
->header
);
756 /***********************************************************************
757 * IsBadReadPtr32 (KERNEL32.354)
759 BOOL32 WINAPI
IsBadReadPtr32( LPCVOID ptr
, UINT32 size
)
761 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
762 VPROT_READ
| VPROT_COMMITTED
);
766 /***********************************************************************
767 * IsBadWritePtr32 (KERNEL32.357)
769 BOOL32 WINAPI
IsBadWritePtr32( LPVOID ptr
, UINT32 size
)
771 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, size
,
772 VPROT_WRITE
| VPROT_COMMITTED
);
776 /***********************************************************************
777 * IsBadHugeReadPtr32 (KERNEL32.352)
779 BOOL32 WINAPI
IsBadHugeReadPtr32( LPCVOID ptr
, UINT32 size
)
781 return IsBadReadPtr32( ptr
, size
);
785 /***********************************************************************
786 * IsBadHugeWritePtr32 (KERNEL32.353)
788 BOOL32 WINAPI
IsBadHugeWritePtr32( LPVOID ptr
, UINT32 size
)
790 return IsBadWritePtr32( ptr
, size
);
794 /***********************************************************************
795 * IsBadCodePtr32 (KERNEL32.351)
797 BOOL32 WINAPI
IsBadCodePtr32( FARPROC32 ptr
)
799 return !VIRTUAL_CheckFlags( (UINT32
)ptr
, 1, VPROT_EXEC
| VPROT_COMMITTED
);
803 /***********************************************************************
804 * IsBadStringPtr32A (KERNEL32.355)
806 BOOL32 WINAPI
IsBadStringPtr32A( LPCSTR str
, UINT32 max
)
811 if (!max
) return FALSE
;
812 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
813 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
814 count
= page_mask
+ 1 - ((UINT32
)str
& page_mask
);
818 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
819 (VPROT_READ
| VPROT_COMMITTED
))
821 if (count
> max
) count
= max
;
823 while (count
--) if (!*str
++) return FALSE
;
824 if (++page
>= view
->size
>> page_shift
) return TRUE
;
825 count
= page_mask
+ 1;
831 /***********************************************************************
832 * IsBadStringPtr32W (KERNEL32.356)
834 BOOL32 WINAPI
IsBadStringPtr32W( LPCWSTR str
, UINT32 max
)
839 if (!max
) return FALSE
;
840 if (!(view
= VIRTUAL_FindView( (UINT32
)str
))) return TRUE
;
841 page
= ((UINT32
)str
- view
->base
) >> page_shift
;
842 count
= (page_mask
+ 1 - ((UINT32
)str
& page_mask
)) / sizeof(WCHAR
);
846 if ((view
->prot
[page
] & (VPROT_READ
| VPROT_COMMITTED
)) !=
847 (VPROT_READ
| VPROT_COMMITTED
))
849 if (count
> max
) count
= max
;
851 while (count
--) if (!*str
++) return FALSE
;
852 if (++page
>= view
->size
>> page_shift
) return TRUE
;
853 count
= (page_mask
+ 1) / sizeof(WCHAR
);
859 /***********************************************************************
860 * CreateFileMapping32A (KERNEL32.46)
862 HANDLE32 WINAPI
CreateFileMapping32A(HFILE32 hFile
, LPSECURITY_ATTRIBUTES attr
,
863 DWORD protect
, DWORD size_high
,
864 DWORD size_low
, LPCSTR name
)
866 FILE_MAPPING
*mapping
= NULL
;
870 /* First search for an object with the same name */
872 K32OBJ
*obj
= K32OBJ_FindName( name
);
875 if (obj
->type
== K32OBJ_MEM_MAPPED_FILE
)
877 SetLastError( ERROR_ALREADY_EXISTS
);
878 handle
= PROCESS_AllocHandle( obj
, 0 );
882 SetLastError( ERROR_DUP_NAME
);
885 K32OBJ_DecCount( obj
);
889 /* Check parameters */
891 dprintf_virtual(stddeb
,"CreateFileMapping32A(%x,%p,%08lx,%08lx%08lx,%s)\n",
892 hFile
, attr
, protect
, size_high
, size_low
, name
);
894 vprot
= VIRTUAL_GetProt( protect
);
895 if (protect
& SEC_RESERVE
)
897 if (hFile
!= INVALID_HANDLE_VALUE32
)
899 SetLastError( ERROR_INVALID_PARAMETER
);
903 else vprot
|= VPROT_COMMITTED
;
904 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
906 /* Compute the size and extend the file if necessary */
908 if (hFile
== INVALID_HANDLE_VALUE32
)
910 if (!size_high
&& !size_low
)
912 SetLastError( ERROR_INVALID_PARAMETER
);
917 else /* We have a file */
919 BY_HANDLE_FILE_INFORMATION info
;
920 if (!(obj
= PROCESS_GetObjPtr( hFile
, K32OBJ_FILE
))) goto error
;
921 /* FIXME: should check if the file permissions agree
922 * with the required protection flags */
923 if (!GetFileInformationByHandle( hFile
, &info
)) goto error
;
924 if (!size_high
&& !size_low
)
926 size_high
= info
.nFileSizeHigh
;
927 size_low
= info
.nFileSizeLow
;
929 else if ((size_high
> info
.nFileSizeHigh
) ||
930 ((size_high
== info
.nFileSizeHigh
) &&
931 (size_low
> info
.nFileSizeLow
)))
933 /* We have to grow the file */
934 if (SetFilePointer( hFile
, size_low
, &size_high
,
935 FILE_BEGIN
) == 0xffffffff) goto error
;
936 if (!SetEndOfFile( hFile
)) goto error
;
940 /* Allocate the mapping object */
942 if (!(mapping
= HeapAlloc( SystemHeap
, 0, sizeof(*mapping
) ))) goto error
;
943 mapping
->header
.type
= K32OBJ_MEM_MAPPED_FILE
;
944 mapping
->header
.refcount
= 1;
945 mapping
->protect
= vprot
;
946 mapping
->size_high
= size_high
;
947 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
948 mapping
->file
= (FILE_OBJECT
*)obj
;
950 if (!K32OBJ_AddName( &mapping
->header
, name
)) handle
= 0;
951 else handle
= PROCESS_AllocHandle( &mapping
->header
, 0 );
952 K32OBJ_DecCount( &mapping
->header
);
956 if (obj
) K32OBJ_DecCount( obj
);
957 if (mapping
) HeapFree( SystemHeap
, 0, mapping
);
962 /***********************************************************************
963 * CreateFileMapping32W (KERNEL32.47)
965 HANDLE32 WINAPI
CreateFileMapping32W(HFILE32 hFile
, LPSECURITY_ATTRIBUTES attr
,
966 DWORD protect
, DWORD size_high
,
967 DWORD size_low
, LPCWSTR name
)
969 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
970 HANDLE32 ret
= CreateFileMapping32A( hFile
, attr
, protect
,
971 size_high
, size_low
, nameA
);
972 HeapFree( GetProcessHeap(), 0, nameA
);
977 /***********************************************************************
978 * OpenFileMapping32A (KERNEL32.397)
980 HANDLE32 WINAPI
OpenFileMapping32A( DWORD access
, BOOL32 inherit
, LPCSTR name
)
985 if ((obj
= K32OBJ_FindNameType( name
, K32OBJ_MEM_MAPPED_FILE
)))
987 handle
= PROCESS_AllocHandle( obj
, 0 );
988 K32OBJ_DecCount( obj
);
995 /***********************************************************************
996 * OpenFileMapping32W (KERNEL32.398)
998 HANDLE32 WINAPI
OpenFileMapping32W( DWORD access
, BOOL32 inherit
, LPCWSTR name
)
1000 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1001 HANDLE32 ret
= OpenFileMapping32A( access
, inherit
, nameA
);
1002 HeapFree( GetProcessHeap(), 0, nameA
);
1007 /***********************************************************************
1008 * VIRTUAL_DestroyMapping
1010 * Destroy a FILE_MAPPING object.
1012 static void VIRTUAL_DestroyMapping( K32OBJ
*ptr
)
1014 FILE_MAPPING
*mapping
= (FILE_MAPPING
*)ptr
;
1015 assert( ptr
->type
== K32OBJ_MEM_MAPPED_FILE
);
1017 if (mapping
->file
) K32OBJ_DecCount( &mapping
->file
->header
);
1018 ptr
->type
= K32OBJ_UNKNOWN
;
1019 HeapFree( SystemHeap
, 0, mapping
);
1023 /***********************************************************************
1024 * MapViewOfFile (KERNEL32.385)
1026 LPVOID WINAPI
MapViewOfFile( HANDLE32 mapping
, DWORD access
, DWORD offset_high
,
1027 DWORD offset_low
, DWORD count
)
1029 return MapViewOfFileEx( mapping
, access
, offset_high
,
1030 offset_low
, count
, NULL
);
1034 /***********************************************************************
1035 * MapViewOfFileEx (KERNEL32.386)
1037 LPVOID WINAPI
MapViewOfFileEx(HANDLE32 handle
, DWORD access
, DWORD offset_high
,
1038 DWORD offset_low
, DWORD count
, LPVOID addr
)
1040 FILE_MAPPING
*mapping
;
1042 UINT32 ptr
= (UINT32
)-1, size
= 0;
1043 int flags
= MAP_PRIVATE
;
1045 /* Check parameters */
1047 if ((offset_low
& granularity_mask
) ||
1048 (addr
&& ((UINT32
)addr
& granularity_mask
)))
1050 SetLastError( ERROR_INVALID_PARAMETER
);
1054 if (!(mapping
= (FILE_MAPPING
*)PROCESS_GetObjPtr( handle
,
1055 K32OBJ_MEM_MAPPED_FILE
)))
1058 if (mapping
->size_high
|| offset_high
)
1059 fprintf( stderr
, "MapViewOfFileEx: offsets larger than 4Gb not supported\n");
1061 if ((offset_low
>= mapping
->size_low
) ||
1062 (count
> mapping
->size_low
- offset_low
))
1064 SetLastError( ERROR_INVALID_PARAMETER
);
1067 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1068 else size
= mapping
->size_low
- offset_low
;
1072 case FILE_MAP_ALL_ACCESS
:
1073 case FILE_MAP_WRITE
:
1074 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1075 if (!(mapping
->protect
& VPROT_WRITE
))
1077 SetLastError( ERROR_INVALID_PARAMETER
);
1084 case FILE_MAP_COPY
| FILE_MAP_READ
:
1085 if (mapping
->protect
& VPROT_READ
) break;
1088 SetLastError( ERROR_INVALID_PARAMETER
);
1094 dprintf_virtual( stddeb
, "MapViewOfFile: handle=%x size=%x offset=%lx\n",
1095 handle
, size
, offset_low
);
1097 ptr
= (UINT32
)FILE_dommap( mapping
->file
, addr
, 0, size
, 0, offset_low
,
1098 VIRTUAL_GetUnixProt( mapping
->protect
),
1100 if (ptr
== (UINT32
)-1)
1102 SetLastError( ERROR_OUTOFMEMORY
);
1106 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0,
1107 mapping
->protect
, mapping
)))
1109 SetLastError( ERROR_OUTOFMEMORY
);
1115 if (ptr
!= (UINT32
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1116 K32OBJ_DecCount( &mapping
->header
);
1121 /***********************************************************************
1122 * FlushViewOfFile (KERNEL32.262)
1124 BOOL32 WINAPI
FlushViewOfFile( LPCVOID base
, DWORD cbFlush
)
1127 UINT32 addr
= ROUND_ADDR( base
);
1129 dprintf_virtual( stddeb
, "FlushViewOfFile at %p for %ld bytes\n",
1132 if (!(view
= VIRTUAL_FindView( addr
)))
1134 SetLastError( ERROR_INVALID_PARAMETER
);
1137 if (!cbFlush
) cbFlush
= view
->size
;
1138 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1139 SetLastError( ERROR_INVALID_PARAMETER
);
1143 /***********************************************************************
1144 * UnmapViewOfFile (KERNEL32.540)
1146 BOOL32 WINAPI
UnmapViewOfFile( LPVOID addr
)
1149 UINT32 base
= ROUND_ADDR( addr
);
1150 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1152 SetLastError( ERROR_INVALID_PARAMETER
);
1155 VIRTUAL_DeleteView( view
);