Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef loader_AutoMemMap_h
7 : #define loader_AutoMemMap_h
8 :
9 : #include "mozilla/FileUtils.h"
10 : #include "mozilla/MemoryReporting.h"
11 : #include "mozilla/RangedPtr.h"
12 : #include "mozilla/Result.h"
13 : #include "mozilla/ipc/FileDescriptor.h"
14 : #include "nsIMemoryReporter.h"
15 :
16 : #include <prio.h>
17 :
18 : class nsIFile;
19 :
20 : namespace mozilla {
21 : namespace loader {
22 :
23 : using mozilla::ipc::FileDescriptor;
24 :
25 : class AutoMemMap
26 : {
27 : public:
28 105 : AutoMemMap() = default;
29 :
30 : ~AutoMemMap();
31 :
32 : Result<Ok, nsresult>
33 : init(nsIFile* file, int flags = PR_RDONLY, int mode = 0,
34 : PRFileMapProtect prot = PR_PROT_READONLY);
35 :
36 : Result<Ok, nsresult>
37 : init(const ipc::FileDescriptor& file);
38 :
39 : bool initialized() { return addr; }
40 :
41 206 : uint32_t size() const { MOZ_ASSERT(fd); return size_; }
42 :
43 : template<typename T = void>
44 105 : const RangedPtr<T> get()
45 : {
46 105 : MOZ_ASSERT(addr);
47 105 : return { static_cast<T*>(addr), size_ };
48 : }
49 :
50 : template<typename T = void>
51 : const RangedPtr<T> get() const
52 : {
53 : MOZ_ASSERT(addr);
54 : return { static_cast<T*>(addr), size_ };
55 : }
56 :
57 0 : size_t nonHeapSizeOfExcludingThis() { return size_; }
58 :
59 : FileDescriptor cloneFileDescriptor();
60 :
61 : private:
62 : Result<Ok, nsresult> initInternal(PRFileMapProtect prot = PR_PROT_READONLY);
63 :
64 : AutoFDClose fd;
65 : PRFileMap* fileMap = nullptr;
66 :
67 : uint32_t size_ = 0;
68 : void* addr = nullptr;
69 :
70 : AutoMemMap(const AutoMemMap&) = delete;
71 : void operator=(const AutoMemMap&) = delete;
72 : };
73 :
74 : } // namespace loader
75 : } // namespace mozilla
76 :
77 : #endif // loader_AutoMemMap_h
|