1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/profiler/AutoObjectMapper.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef AutoObjectMapper_h 1.11 +#define AutoObjectMapper_h 1.12 + 1.13 +#include <string> 1.14 + 1.15 +#include "mozilla/Attributes.h" 1.16 +#include "PlatformMacros.h" 1.17 + 1.18 +// A (nearly-) RAII class that maps an object in and then unmaps it on 1.19 +// destruction. This base class version uses the "normal" POSIX 1.20 +// functions: open, fstat, close, mmap, munmap. 1.21 + 1.22 +class MOZ_STACK_CLASS AutoObjectMapperPOSIX { 1.23 +public: 1.24 + // The constructor does not attempt to map the file, because that 1.25 + // might fail. Instead, once the object has been constructed, 1.26 + // call Map() to attempt the mapping. There is no corresponding 1.27 + // Unmap() since the unmapping is done in the destructor. Failure 1.28 + // messages are sent to |aLog|. 1.29 + AutoObjectMapperPOSIX(void(*aLog)(const char*)); 1.30 + 1.31 + // Unmap the file on destruction of this object. 1.32 + ~AutoObjectMapperPOSIX(); 1.33 + 1.34 + // Map |fileName| into the address space and return the mapping 1.35 + // extents. If the file is zero sized this will fail. The file is 1.36 + // mapped read-only and private. Returns true iff the mapping 1.37 + // succeeded, in which case *start and *length hold its extent. 1.38 + // Once a call to Map succeeds, all subsequent calls to it will 1.39 + // fail. 1.40 + bool Map(/*OUT*/void** start, /*OUT*/size_t* length, std::string fileName); 1.41 + 1.42 +protected: 1.43 + // If we are currently holding a mapped object, these record the 1.44 + // mapped address range. 1.45 + void* mImage; 1.46 + size_t mSize; 1.47 + 1.48 + // A logging sink, for complaining about mapping failures. 1.49 + void (*mLog)(const char*); 1.50 + 1.51 +private: 1.52 + // Are we currently holding a mapped object? This is private to 1.53 + // the base class. Derived classes need to have their own way to 1.54 + // track whether they are holding a mapped object. 1.55 + bool mIsMapped; 1.56 + 1.57 + // Disable copying and assignment. 1.58 + AutoObjectMapperPOSIX(const AutoObjectMapperPOSIX&); 1.59 + AutoObjectMapperPOSIX& operator=(const AutoObjectMapperPOSIX&); 1.60 + // Disable heap allocation of this class. 1.61 + void* operator new(size_t); 1.62 + void* operator new[](size_t); 1.63 + void operator delete(void*); 1.64 + void operator delete[](void*); 1.65 +}; 1.66 + 1.67 + 1.68 +#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) 1.69 +// This is a variant of AutoObjectMapperPOSIX suitable for use in 1.70 +// conjunction with faulty.lib on Android. How it behaves depends on 1.71 +// the name of the file to be mapped. There are three possible cases: 1.72 +// 1.73 +// (1) /foo/bar/xyzzy/blah.apk!/libwurble.so 1.74 +// We hand it as-is to faulty.lib and let it fish the relevant 1.75 +// bits out of the APK. 1.76 +// 1.77 +// (2) libmozglue.so 1.78 +// This is part of the Fennec installation, but is not in the 1.79 +// APK. Instead we have to figure out the installation path 1.80 +// and look for it there. Because of faulty.lib limitations, 1.81 +// we have to use regular open/mmap instead of faulty.lib. 1.82 +// 1.83 +// (3) libanythingelse.so 1.84 +// faulty.lib assumes this is a system library, and prepends 1.85 +// "/system/lib/" to the path. So as in (1), we can give it 1.86 +// as-is to faulty.lib. 1.87 +// 1.88 +// Hence (1) and (3) require special-casing here. Case (2) simply 1.89 +// hands the problem to the parent class. 1.90 + 1.91 +class MOZ_STACK_CLASS AutoObjectMapperFaultyLib : public AutoObjectMapperPOSIX { 1.92 +public: 1.93 + AutoObjectMapperFaultyLib(void(*aLog)(const char*)); 1.94 + 1.95 + ~AutoObjectMapperFaultyLib(); 1.96 + 1.97 + bool Map(/*OUT*/void** start, /*OUT*/size_t* length, std::string fileName); 1.98 + 1.99 +private: 1.100 + // faulty.lib requires us to maintain an abstract handle that can be 1.101 + // used later to unmap the area. If this is non-NULL, it is assumed 1.102 + // that unmapping is to be done by faulty.lib. Otherwise it goes 1.103 + // via the normal mechanism. 1.104 + void* mHdl; 1.105 + 1.106 + // Disable copying and assignment. 1.107 + AutoObjectMapperFaultyLib(const AutoObjectMapperFaultyLib&); 1.108 + AutoObjectMapperFaultyLib& operator=(const AutoObjectMapperFaultyLib&); 1.109 + // Disable heap allocation of this class. 1.110 + void* operator new(size_t); 1.111 + void* operator new[](size_t); 1.112 + void operator delete(void*); 1.113 + void operator delete[](void*); 1.114 +}; 1.115 + 1.116 +#endif // defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK) 1.117 + 1.118 +#endif // AutoObjectMapper_h