/src/mozilla-central/devtools/shared/heapsnapshot/AutoMemMap.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include "mozilla/devtools/AutoMemMap.h" |
8 | | |
9 | | #include "mozilla/Unused.h" |
10 | | #include "nsDebug.h" |
11 | | |
12 | | namespace mozilla { |
13 | | namespace devtools { |
14 | | |
15 | | AutoMemMap::~AutoMemMap() |
16 | 0 | { |
17 | 0 | if (addr) { |
18 | 0 | Unused << NS_WARN_IF(PR_MemUnmap(addr, size()) != PR_SUCCESS); |
19 | 0 | addr = nullptr; |
20 | 0 | } |
21 | 0 |
|
22 | 0 | if (fileMap) { |
23 | 0 | Unused << NS_WARN_IF(PR_CloseFileMap(fileMap) != PR_SUCCESS); |
24 | 0 | fileMap = nullptr; |
25 | 0 | } |
26 | 0 |
|
27 | 0 | if (fd) { |
28 | 0 | Unused << NS_WARN_IF(PR_Close(fd) != PR_SUCCESS); |
29 | 0 | fd = nullptr; |
30 | 0 | } |
31 | 0 | } |
32 | | |
33 | | nsresult |
34 | | AutoMemMap::init(const char* filePath, int flags, int mode, PRFileMapProtect prot) |
35 | 0 | { |
36 | 0 | MOZ_ASSERT(!fd); |
37 | 0 | MOZ_ASSERT(!fileMap); |
38 | 0 | MOZ_ASSERT(!addr); |
39 | 0 |
|
40 | 0 | if (PR_GetFileInfo64(filePath, &fileInfo) != PR_SUCCESS) |
41 | 0 | return NS_ERROR_FILE_NOT_FOUND; |
42 | 0 | |
43 | 0 | // Check if the file is too big to memmap. |
44 | 0 | if (fileInfo.size > int64_t(UINT32_MAX)) |
45 | 0 | return NS_ERROR_INVALID_ARG; |
46 | 0 | auto length = uint32_t(fileInfo.size); |
47 | 0 |
|
48 | 0 | fd = PR_Open(filePath, flags, flags); |
49 | 0 | if (!fd) |
50 | 0 | return NS_ERROR_UNEXPECTED; |
51 | 0 | |
52 | 0 | fileMap = PR_CreateFileMap(fd, fileInfo.size, prot); |
53 | 0 | if (!fileMap) |
54 | 0 | return NS_ERROR_UNEXPECTED; |
55 | 0 | |
56 | 0 | addr = PR_MemMap(fileMap, 0, length); |
57 | 0 | if (!addr) |
58 | 0 | return NS_ERROR_UNEXPECTED; |
59 | 0 | |
60 | 0 | return NS_OK; |
61 | 0 | } |
62 | | |
63 | | } // namespace devtools |
64 | | } // namespace mozilla |