1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/files/memory_mapped_file.h" 6 7#include "base/files/file_path.h" 8#include "base/logging.h" 9 10namespace base { 11 12MemoryMappedFile::~MemoryMappedFile() { 13 CloseHandles(); 14} 15 16bool MemoryMappedFile::Initialize(const FilePath& file_name) { 17 if (IsValid()) 18 return false; 19 20 if (!MapFileToMemory(file_name)) { 21 CloseHandles(); 22 return false; 23 } 24 25 return true; 26} 27 28bool MemoryMappedFile::Initialize(PlatformFile file) { 29 if (IsValid()) 30 return false; 31 32 file_ = file; 33 34 if (!MapFileToMemoryInternal()) { 35 CloseHandles(); 36 return false; 37 } 38 39 return true; 40} 41 42bool MemoryMappedFile::IsValid() const { 43 return data_ != NULL; 44} 45 46bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { 47 file_ = CreatePlatformFile(file_name, PLATFORM_FILE_OPEN | PLATFORM_FILE_READ, 48 NULL, NULL); 49 50 if (file_ == kInvalidPlatformFileValue) { 51 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); 52 return false; 53 } 54 55 return MapFileToMemoryInternal(); 56} 57 58} // namespace base 59