blob: ba00946722c7962da445261b89cb2e82b2c7cd85 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/memory_mapped_file.h"
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/threading/thread_restrictions.h"
namespace base {
MemoryMappedFile::MemoryMappedFile()
: file_(kInvalidPlatformFileValue),
data_(NULL),
length_(0) {
}
bool MemoryMappedFile::MapFileToMemoryInternal() {
ThreadRestrictions::AssertIOAllowed();
struct stat file_stat;
if (fstat(file_, &file_stat) == kInvalidPlatformFileValue) {
DPLOG(ERROR) << "fstat " << file_;
return false;
}
length_ = file_stat.st_size;
data_ = static_cast<uint8*>(
mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0));
if (data_ == MAP_FAILED)
DPLOG(ERROR) << "mmap " << file_;
return data_ != MAP_FAILED;
}
void MemoryMappedFile::CloseHandles() {
ThreadRestrictions::AssertIOAllowed();
if (data_ != NULL)
munmap(data_, length_);
if (file_ != kInvalidPlatformFileValue)
ignore_result(HANDLE_EINTR(close(file_)));
data_ = NULL;
length_ = 0;
file_ = kInvalidPlatformFileValue;
}
} // namespace base
|