Line data Source code
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 0 : AutoMemMap::~AutoMemMap()
16 : {
17 0 : if (addr) {
18 0 : Unused << NS_WARN_IF(PR_MemUnmap(addr, size()) != PR_SUCCESS);
19 0 : addr = nullptr;
20 : }
21 :
22 0 : if (fileMap) {
23 0 : Unused << NS_WARN_IF(PR_CloseFileMap(fileMap) != PR_SUCCESS);
24 0 : fileMap = nullptr;
25 : }
26 :
27 0 : if (fd) {
28 0 : Unused << NS_WARN_IF(PR_Close(fd) != PR_SUCCESS);
29 0 : fd = nullptr;
30 : }
31 0 : }
32 :
33 : nsresult
34 0 : AutoMemMap::init(const char* filePath, int flags, int mode, PRFileMapProtect prot)
35 : {
36 0 : MOZ_ASSERT(!fd);
37 0 : MOZ_ASSERT(!fileMap);
38 0 : MOZ_ASSERT(!addr);
39 :
40 0 : if (PR_GetFileInfo64(filePath, &fileInfo) != PR_SUCCESS)
41 0 : return NS_ERROR_FILE_NOT_FOUND;
42 :
43 : // 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 :
48 0 : fd = PR_Open(filePath, flags, flags);
49 0 : if (!fd)
50 0 : return NS_ERROR_UNEXPECTED;
51 :
52 0 : fileMap = PR_CreateFileMap(fd, fileInfo.size, prot);
53 0 : if (!fileMap)
54 0 : return NS_ERROR_UNEXPECTED;
55 :
56 0 : addr = PR_MemMap(fileMap, 0, length);
57 0 : if (!addr)
58 0 : return NS_ERROR_UNEXPECTED;
59 :
60 0 : return NS_OK;
61 : }
62 :
63 : } // namespace devtools
64 : } // namespace mozilla
|