| 1 | /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | |
| 16 | #include "mysys_priv.h" |
| 17 | |
| 18 | #ifdef HAVE_SYS_MMAN_H |
| 19 | |
| 20 | /* |
| 21 | system msync() only syncs mmap'ed area to fs cache. |
| 22 | fsync() is required to really sync to disc |
| 23 | */ |
| 24 | int my_msync(int fd, void *addr, size_t len, int flags) |
| 25 | { |
| 26 | msync(addr, len, flags); |
| 27 | return my_sync(fd, MYF(0)); |
| 28 | } |
| 29 | |
| 30 | #elif defined(_WIN32) |
| 31 | |
| 32 | static SECURITY_ATTRIBUTES mmap_security_attributes= |
| 33 | {sizeof(SECURITY_ATTRIBUTES), 0, TRUE}; |
| 34 | |
| 35 | void *my_mmap(void *addr, size_t len, int prot, |
| 36 | int flags, File fd, my_off_t offset) |
| 37 | { |
| 38 | HANDLE hFileMap; |
| 39 | LPVOID ptr; |
| 40 | HANDLE hFile= (HANDLE)my_get_osfhandle(fd); |
| 41 | DBUG_ENTER("my_mmap" ); |
| 42 | DBUG_PRINT("mysys" , ("map fd: %d" , fd)); |
| 43 | |
| 44 | if (hFile == INVALID_HANDLE_VALUE) |
| 45 | DBUG_RETURN(MAP_FAILED); |
| 46 | |
| 47 | hFileMap=CreateFileMapping(hFile, &mmap_security_attributes, |
| 48 | PAGE_READWRITE, 0, (DWORD) len, NULL); |
| 49 | if (hFileMap == 0) |
| 50 | DBUG_RETURN(MAP_FAILED); |
| 51 | |
| 52 | ptr=MapViewOfFile(hFileMap, |
| 53 | prot & PROT_WRITE ? FILE_MAP_WRITE : FILE_MAP_READ, |
| 54 | (DWORD)(offset >> 32), (DWORD)offset, len); |
| 55 | |
| 56 | /* |
| 57 | MSDN explicitly states that it's possible to close File Mapping Object |
| 58 | even when a view is not unmapped - then the object will be held open |
| 59 | implicitly until unmap, as every view stores internally a handler of |
| 60 | a corresponding File Mapping Object |
| 61 | */ |
| 62 | CloseHandle(hFileMap); |
| 63 | |
| 64 | if (ptr) |
| 65 | { |
| 66 | DBUG_PRINT("mysys" , ("mapped addr: %p" , ptr)); |
| 67 | DBUG_RETURN(ptr); |
| 68 | } |
| 69 | |
| 70 | DBUG_RETURN(MAP_FAILED); |
| 71 | } |
| 72 | |
| 73 | int my_munmap(void *addr, size_t len) |
| 74 | { |
| 75 | DBUG_ENTER("my_munmap" ); |
| 76 | DBUG_PRINT("mysys" , ("unmap addr: %p" , addr)); |
| 77 | DBUG_RETURN(UnmapViewOfFile(addr) ? 0 : -1); |
| 78 | } |
| 79 | |
| 80 | int my_msync(int fd, void *addr, size_t len, int flags) |
| 81 | { |
| 82 | return FlushViewOfFile(addr, len) ? 0 : -1; |
| 83 | } |
| 84 | |
| 85 | #else |
| 86 | #warning "no mmap!" |
| 87 | #endif |
| 88 | |
| 89 | |