1 // Copyright (c) 2012 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 "net/disk_cache/blockfile/mapped_file.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
12 namespace disk_cache
{
14 void* MappedFile::Init(const base::FilePath
& name
, size_t size
) {
16 if (init_
|| !File::Init(name
))
22 buffer_
= malloc(size
);
23 snapshot_
= malloc(size
);
24 if (buffer_
&& snapshot_
&& Read(buffer_
, size
, 0)) {
25 memcpy(snapshot_
, buffer_
, size
);
29 buffer_
= snapshot_
= 0;
37 void MappedFile::Flush() {
40 const char* buffer_ptr
= static_cast<const char*>(buffer_
);
41 char* snapshot_ptr
= static_cast<char*>(snapshot_
);
42 const size_t block_size
= 4096;
43 for (size_t offset
= 0; offset
< view_size_
; offset
+= block_size
) {
44 size_t size
= std::min(view_size_
- offset
, block_size
);
45 if (memcmp(snapshot_ptr
+ offset
, buffer_ptr
+ offset
, size
)) {
46 memcpy(snapshot_ptr
+ offset
, buffer_ptr
+ offset
, size
);
47 Write(snapshot_ptr
+ offset
, size
, offset
);
52 MappedFile::~MappedFile() {
56 if (buffer_
&& snapshot_
) {
63 } // namespace disk_cache