1 / 9

Memory Mapped I/O

Memory Mapped I/O. Gregory Mortensen CSIS 4330, Advanced Windows Programming – UVSC. Implementing Memory Mapped I/O. Create or Open the File Object (see lecture 5). Create the Memory Mapped Object Create a view to map the object into your address space.

kyoko
Download Presentation

Memory Mapped I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Mapped I/O Gregory Mortensen CSIS 4330, Advanced Windows Programming – UVSC

  2. Implementing Memory Mapped I/O • Create or Open the File Object (see lecture 5). • Create the Memory Mapped Object • Create a view to map the object into your address space

  3. Memory Mapped I/O2. Create the Memory Mapped Object • HANDLE CreateFileMapping (HANDLE hFile, psa, DWORD protect, DWORD maxSizeHigh, DWORD maxSizeLow PCTSTR optionalName) • Protect is PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY, SEC_RESERVE or SEC_COMMIT, for 2000 also SEC_NOCACHE, or PAGE_NOCACHE, SEC_IMAGE. • If maxSizeLow and msHi are both 0, it opens the file at its current size, but appends fail. • optionalName is used if the Memory Mapped object is shared between processes.

  4. Memory Mapped I/O3. Create a view • PVOID MapViewOfFileObject (HANDLE hFMObject, DWORD access, DWORD highOffset, DWORD lowOffset,SIZE_T numBytes) • Access= FILE_MAP_WRITE, FILE_MAP_READ, or FILE_MAP_COPY(F_M_C the hFMObject must be created with PAGE_WRITE_COPY – WIN98) • All zeros for hOffset, lOffset and numBytes maps the whole file

  5. Coherency • If the view is created with access FILE_MAP_COPY, then only the original file is shared between process boundries. Any changes are made to your copy not to the original • If the view is create with access FILE_MAP_WRITE then changes made in your process are also made in other processes transparently, by the operating system.

  6. Using Memory Mapped IO without a file • Passing INVALID_HANDLE_VALUE (the value returned by a failed CreateFile) creates a memory mapping of the page file and not of a disk file. • A simple clipboard could use a memory mapped file of a process’es memory space and not of an actual file.

  7. Preserving Memory • By default, when you create a Map Object, the default pageProtection is SEC_COMMIT. • To use memory only as you need it: 1. Create with SEC_RESERVE 2. Use PVOID VirtualAlloc(PVOID pvAddress, SIZE_T dwSize, DWORD allocType, DWORD protect) 3. All processes mapped to the object can then access that location successfully!

  8. Relocatability • You can use CreateFileMappingEx & CreateMapViewEx to force a Map Object or view to always be forced to an address. • This allows you to use pointers freely, (every process would have the Map Object to same the memory address) however if that address is in use, the above creates would fail. • To use pointers and guarantee relocatablity you should use relative addressing.

  9. Clean up • Close the View: UnmapViewOfFile (PVOID returnedByCreateMapView) • Close the File Mapping Object: CloseHandle(MappingObject) 3. Close the File Handle, if using files: CloseHandle(FileObject)

More Related