From 7fba30e97d12d2c9fb9cfeea0df82f5b6c0e7d52 Mon Sep 17 00:00:00 2001 From: pixel Date: Tue, 25 Nov 2003 10:18:04 +0000 Subject: Fixed mmap --- lib/Handle.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/Handle.cc b/lib/Handle.cc index 4b2b4f1..8ec6219 100644 --- a/lib/Handle.cc +++ b/lib/Handle.cc @@ -37,7 +37,7 @@ enum { INFLATE }; -Handle::Handle(const Handle & nh) : itell(0), hFile(0), h(nh.h >= 0 ? dup(nh.h) : nh.h), closed(nh.closed), nonblock(nh.closed), zfile(0), z(0) +Handle::Handle(const Handle & nh) : itell(0), hFile(0), h(nh.h >= 0 ? dup(nh.h) : nh.h), closed(nh.closed), nonblock(nh.closed), zfile(0), z(0), hMapObject(0), mapped(0) { #ifdef DEBUG printm(M_INFO, String(_("Duplication of handle ")) + nh.h + _(" to ") + h + "\n"); @@ -54,7 +54,7 @@ Handle::~Handle() { close(); } -Handle::Handle(int nh) : h(nh), closed(false), nonblock(false), zfile(0), z(0) +Handle::Handle(int nh) : h(nh), closed(false), nonblock(false), zfile(0), z(0), hMapObject(0), mapped(0) { #ifdef DEBUG printm(M_INFO, String(_("Initialising handle ")) + h + "\n"); @@ -255,6 +255,10 @@ void Handle::close() throw (GeneralException) { hFile = 0; } #endif + + if (mapped) { + munmap(); + } h = -1; closed = 1; @@ -522,16 +526,25 @@ void Handle::Flush() { } void * Handle::mmap(off_t offset, size_t length) throw (GeneralException) { + void * r; + if (h == -1) { throw GeneralException("Can't mmap() a virtual handle"); } + if (mapped) { + throw GeneralException("Handle already mmap()ped"); + } + mapped = true; + maplength = length; if (length == -1) { length = GetSize(); } -#ifndef _WIN32 - return ::mmap(0, length, (CanRead() ? PROT_READ : 0) | (CanWrite() ? PROT_WRITE : 0), MAP_SHARED, h, offset); +#ifndef _WIN32 + r = ::mmap(0, length, (CanRead() ? PROT_READ : 0) | (CanWrite() ? PROT_WRITE : 0), MAP_SHARED, h, offset); + if (!r) { + throw GeneralException(String("Was not able to mmap(): ") + strerror(errno)); + } #else - void * lpvMem = 0; hMapObject = CreateFileMapping( hFile, 0, @@ -540,16 +553,41 @@ void * Handle::mmap(off_t offset, size_t length) throw (GeneralException) { length, GetName().to_charp()); if (hMapObject != NULL) { - lpvMem = MapViewOfFile( + r = MapViewOfFile( hMapObject, CanWrite() ? FILE_MAP_WRITE : FILE_MAP_READ, 0, offset, length); - if (lpvMem == NULL) { + if (!r) { CloseHandle(hMapObject); + throw GeneralException("Was not able to MapViewOfFile()"); } + } else { + throw GeneralException("Was not able to CreateFileMapping()"); } - return lpvMem; #endif + + mappedarea = r; + + return r; +} + +void Handle::munmap() throw (GeneralException) { + if (!mapped) { + throw GeneralException("Can't munmap, was not mapped"); + } +#ifndef _WIN32 + if (::munmap(mappedarea, maplength) { + throw GeneralException(String("Was not able to munmap(): ") + strerror(errno)); + } +#else + if (!UnmapViewOfFile(mappedarea)) { + throw GeneralException("Was not able to UnmapViewOfFile()"); + } + CloseHandle(hMapObject); +#endif + mapped = false; + mappedarea = 0; + maplength = 0; } -- cgit v1.2.3