summaryrefslogtreecommitdiff
path: root/chromium/net/disk_cache/blockfile/mapped_file_win.cc
blob: 1490212c143953a5b2f1acf23c102f8c7df6fdee (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
55
56
57
58
// Copyright (c) 2012 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 "net/disk_cache/blockfile/mapped_file.h"

#include <memory>

#include "base/files/file_path.h"
#include "base/logging.h"
#include "net/disk_cache/disk_cache.h"

#include <windows.h>

namespace disk_cache {

void* MappedFile::Init(const base::FilePath& name, size_t size) {
  DCHECK(!init_);
  if (init_ || !File::Init(name))
    return nullptr;

  buffer_ = nullptr;
  init_ = true;
  section_ = CreateFileMapping(platform_file(), nullptr, PAGE_READWRITE, 0,
                               static_cast<DWORD>(size), nullptr);
  if (!section_)
    return nullptr;

  buffer_ = MapViewOfFile(section_, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size);
  DCHECK(buffer_);
  view_size_ = size;

  // Make sure we detect hardware failures reading the headers.
  size_t temp_len = size ? size : 4096;
  std::unique_ptr<char[]> temp(new char[temp_len]);
  if (!Read(temp.get(), temp_len, 0))
    return nullptr;

  return buffer_;
}

MappedFile::~MappedFile() {
  if (!init_)
    return;

  if (buffer_) {
    BOOL ret = UnmapViewOfFile(buffer_);
    DCHECK(ret);
  }

  if (section_)
    CloseHandle(section_);
}

void MappedFile::Flush() {
}

}  // namespace disk_cache