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.
5 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/threading/thread_restrictions.h"
16 MemoryMappedFile::MemoryMappedFile() : data_(NULL
), length_(0) {
19 bool MemoryMappedFile::MapFileToMemory() {
20 ThreadRestrictions::AssertIOAllowed();
22 struct stat file_stat
;
23 if (fstat(file_
.GetPlatformFile(), &file_stat
) == -1 ) {
24 DPLOG(ERROR
) << "fstat " << file_
.GetPlatformFile();
27 length_
= file_stat
.st_size
;
29 data_
= static_cast<uint8
*>(
30 mmap(NULL
, length_
, PROT_READ
, MAP_SHARED
, file_
.GetPlatformFile(), 0));
31 if (data_
== MAP_FAILED
)
32 DPLOG(ERROR
) << "mmap " << file_
.GetPlatformFile();
34 return data_
!= MAP_FAILED
;
37 void MemoryMappedFile::CloseHandles() {
38 ThreadRestrictions::AssertIOAllowed();
41 munmap(data_
, length_
);