Home | History | Annotate | Download | only in disk_cache
      1 // Copyright (c) 2006-2008 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 "net/disk_cache/mapped_file.h"
      6 
      7 #include "base/file_path.h"
      8 #include "base/logging.h"
      9 #include "net/disk_cache/disk_cache.h"
     10 
     11 namespace disk_cache {
     12 
     13 void* MappedFile::Init(const FilePath& name, size_t size) {
     14   DCHECK(!init_);
     15   if (init_ || !File::Init(name))
     16     return NULL;
     17 
     18   buffer_ = NULL;
     19   init_ = true;
     20   section_ = CreateFileMapping(platform_file(), NULL, PAGE_READWRITE, 0,
     21                                static_cast<DWORD>(size), NULL);
     22   if (!section_)
     23     return NULL;
     24 
     25   buffer_ = MapViewOfFile(section_, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size);
     26   DCHECK(buffer_);
     27   view_size_ = size;
     28 
     29   return buffer_;
     30 }
     31 
     32 MappedFile::~MappedFile() {
     33   if (!init_)
     34     return;
     35 
     36   if (buffer_) {
     37     BOOL ret = UnmapViewOfFile(buffer_);
     38     DCHECK(ret);
     39   }
     40 
     41   if (section_)
     42     CloseHandle(section_);
     43 }
     44 
     45 bool MappedFile::Load(const FileBlock* block) {
     46   size_t offset = block->offset() + view_size_;
     47   return Read(block->buffer(), block->size(), offset);
     48 }
     49 
     50 bool MappedFile::Store(const FileBlock* block) {
     51   size_t offset = block->offset() + view_size_;
     52   return Write(block->buffer(), block->size(), offset);
     53 }
     54 
     55 }  // namespace disk_cache
     56