AutoMemMap.cpp (1609B)
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 "nsDebug.h" 10 11 namespace mozilla { 12 namespace devtools { 13 14 AutoMemMap::~AutoMemMap() { 15 if (addr) { 16 (void)NS_WARN_IF(PR_MemUnmap(addr, size()) != PR_SUCCESS); 17 addr = nullptr; 18 } 19 20 if (fileMap) { 21 (void)NS_WARN_IF(PR_CloseFileMap(fileMap) != PR_SUCCESS); 22 fileMap = nullptr; 23 } 24 25 if (fd) { 26 (void)NS_WARN_IF(PR_Close(fd) != PR_SUCCESS); 27 fd = nullptr; 28 } 29 } 30 31 nsresult AutoMemMap::init(nsIFile* file, int flags, int mode, 32 PRFileMapProtect prot) { 33 MOZ_ASSERT(!fd); 34 MOZ_ASSERT(!fileMap); 35 MOZ_ASSERT(!addr); 36 37 nsresult rv; 38 int64_t inputFileSize; 39 rv = file->GetFileSize(&inputFileSize); 40 if (NS_FAILED(rv)) { 41 return rv; 42 } 43 44 // Check if the file is too big to memmap. 45 if (inputFileSize > int64_t(UINT32_MAX)) { 46 return NS_ERROR_INVALID_ARG; 47 } 48 fileSize = uint32_t(inputFileSize); 49 50 rv = file->OpenNSPRFileDesc(flags, mode, &fd); 51 if (NS_FAILED(rv)) { 52 return rv; 53 } 54 if (!fd) return NS_ERROR_UNEXPECTED; 55 56 fileMap = PR_CreateFileMap(fd, inputFileSize, prot); 57 if (!fileMap) return NS_ERROR_UNEXPECTED; 58 59 addr = PR_MemMap(fileMap, 0, fileSize); 60 if (!addr) return NS_ERROR_UNEXPECTED; 61 62 return NS_OK; 63 } 64 65 } // namespace devtools 66 } // namespace mozilla