michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef AutoObjectMapper_h michael@0: #define AutoObjectMapper_h michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "PlatformMacros.h" michael@0: michael@0: // A (nearly-) RAII class that maps an object in and then unmaps it on michael@0: // destruction. This base class version uses the "normal" POSIX michael@0: // functions: open, fstat, close, mmap, munmap. michael@0: michael@0: class MOZ_STACK_CLASS AutoObjectMapperPOSIX { michael@0: public: michael@0: // The constructor does not attempt to map the file, because that michael@0: // might fail. Instead, once the object has been constructed, michael@0: // call Map() to attempt the mapping. There is no corresponding michael@0: // Unmap() since the unmapping is done in the destructor. Failure michael@0: // messages are sent to |aLog|. michael@0: AutoObjectMapperPOSIX(void(*aLog)(const char*)); michael@0: michael@0: // Unmap the file on destruction of this object. michael@0: ~AutoObjectMapperPOSIX(); michael@0: michael@0: // Map |fileName| into the address space and return the mapping michael@0: // extents. If the file is zero sized this will fail. The file is michael@0: // mapped read-only and private. Returns true iff the mapping michael@0: // succeeded, in which case *start and *length hold its extent. michael@0: // Once a call to Map succeeds, all subsequent calls to it will michael@0: // fail. michael@0: bool Map(/*OUT*/void** start, /*OUT*/size_t* length, std::string fileName); michael@0: michael@0: protected: michael@0: // If we are currently holding a mapped object, these record the michael@0: // mapped address range. michael@0: void* mImage; michael@0: size_t mSize; michael@0: michael@0: // A logging sink, for complaining about mapping failures. michael@0: void (*mLog)(const char*); michael@0: michael@0: private: michael@0: // Are we currently holding a mapped object? This is private to michael@0: // the base class. Derived classes need to have their own way to michael@0: // track whether they are holding a mapped object. michael@0: bool mIsMapped; michael@0: michael@0: // Disable copying and assignment. michael@0: AutoObjectMapperPOSIX(const AutoObjectMapperPOSIX&); michael@0: AutoObjectMapperPOSIX& operator=(const AutoObjectMapperPOSIX&); michael@0: // Disable heap allocation of this class. michael@0: void* operator new(size_t); michael@0: void* operator new[](size_t); michael@0: void operator delete(void*); michael@0: void operator delete[](void*); michael@0: }; michael@0: michael@0: michael@0: #if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) michael@0: // This is a variant of AutoObjectMapperPOSIX suitable for use in michael@0: // conjunction with faulty.lib on Android. How it behaves depends on michael@0: // the name of the file to be mapped. There are three possible cases: michael@0: // michael@0: // (1) /foo/bar/xyzzy/blah.apk!/libwurble.so michael@0: // We hand it as-is to faulty.lib and let it fish the relevant michael@0: // bits out of the APK. michael@0: // michael@0: // (2) libmozglue.so michael@0: // This is part of the Fennec installation, but is not in the michael@0: // APK. Instead we have to figure out the installation path michael@0: // and look for it there. Because of faulty.lib limitations, michael@0: // we have to use regular open/mmap instead of faulty.lib. michael@0: // michael@0: // (3) libanythingelse.so michael@0: // faulty.lib assumes this is a system library, and prepends michael@0: // "/system/lib/" to the path. So as in (1), we can give it michael@0: // as-is to faulty.lib. michael@0: // michael@0: // Hence (1) and (3) require special-casing here. Case (2) simply michael@0: // hands the problem to the parent class. michael@0: michael@0: class MOZ_STACK_CLASS AutoObjectMapperFaultyLib : public AutoObjectMapperPOSIX { michael@0: public: michael@0: AutoObjectMapperFaultyLib(void(*aLog)(const char*)); michael@0: michael@0: ~AutoObjectMapperFaultyLib(); michael@0: michael@0: bool Map(/*OUT*/void** start, /*OUT*/size_t* length, std::string fileName); michael@0: michael@0: private: michael@0: // faulty.lib requires us to maintain an abstract handle that can be michael@0: // used later to unmap the area. If this is non-NULL, it is assumed michael@0: // that unmapping is to be done by faulty.lib. Otherwise it goes michael@0: // via the normal mechanism. michael@0: void* mHdl; michael@0: michael@0: // Disable copying and assignment. michael@0: AutoObjectMapperFaultyLib(const AutoObjectMapperFaultyLib&); michael@0: AutoObjectMapperFaultyLib& operator=(const AutoObjectMapperFaultyLib&); michael@0: // Disable heap allocation of this class. michael@0: void* operator new(size_t); michael@0: void* operator new[](size_t); michael@0: void operator delete(void*); michael@0: void operator delete[](void*); michael@0: }; michael@0: michael@0: #endif // defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) michael@0: michael@0: #endif // AutoObjectMapper_h