1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef AutoObjectMapper_h
8 #define AutoObjectMapper_h
12 #include "mozilla/Attributes.h"
13 #include "PlatformMacros.h"
15 // A (nearly-) RAII class that maps an object in and then unmaps it on
16 // destruction. This base class version uses the "normal" POSIX
17 // functions: open, fstat, close, mmap, munmap.
19 class MOZ_STACK_CLASS AutoObjectMapperPOSIX
{
21 // The constructor does not attempt to map the file, because that
22 // might fail. Instead, once the object has been constructed,
23 // call Map() to attempt the mapping. There is no corresponding
24 // Unmap() since the unmapping is done in the destructor. Failure
25 // messages are sent to |aLog|.
26 explicit AutoObjectMapperPOSIX(void (*aLog
)(const char*));
28 // Unmap the file on destruction of this object.
29 ~AutoObjectMapperPOSIX();
31 // Map |fileName| into the address space and return the mapping
32 // extents. If the file is zero sized this will fail. The file is
33 // mapped read-only and private. Returns true iff the mapping
34 // succeeded, in which case *start and *length hold its extent.
35 // Once a call to Map succeeds, all subsequent calls to it will
37 bool Map(/*OUT*/ void** start
, /*OUT*/ size_t* length
, std::string fileName
);
40 // If we are currently holding a mapped object, these record the
41 // mapped address range.
45 // A logging sink, for complaining about mapping failures.
46 void (*mLog
)(const char*);
49 // Are we currently holding a mapped object? This is private to
50 // the base class. Derived classes need to have their own way to
51 // track whether they are holding a mapped object.
54 // Disable copying and assignment.
55 AutoObjectMapperPOSIX(const AutoObjectMapperPOSIX
&);
56 AutoObjectMapperPOSIX
& operator=(const AutoObjectMapperPOSIX
&);
57 // Disable heap allocation of this class.
58 void* operator new(size_t);
59 void* operator new[](size_t);
60 void operator delete(void*);
61 void operator delete[](void*);
64 #endif // AutoObjectMapper_h