#ifndef NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_
#define NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_
#include <stddef.h>
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "build/build_config.h"
#include "net/base/net_export.h"
#include "net/disk_cache/blockfile/file.h"
#include "net/disk_cache/blockfile/file_block.h"
#include "net/net_buildflags.h"
#if BUILDFLAG(POSIX_BYPASS_MMAP)
#include "base/containers/heap_array.h"
#endif
namespace base {
class FilePath;
}
namespace disk_cache {
class NET_EXPORT_PRIVATE MappedFile : public File {
public:
MappedFile();
MappedFile(const MappedFile&) = delete;
MappedFile& operator=(const MappedFile&) = delete;
void* Init(const base::FilePath& name, size_t size);
#if BUILDFLAG(POSIX_BYPASS_MMAP)
void* buffer() { return reinterpret_cast<void*>(buffer_.data()); }
base::span<uint8_t> as_span() { return buffer_.as_span(); }
#else
void* buffer() { return buffer_; }
base::span<uint8_t> as_span() {
return UNSAFE_BUFFERS(
base::span(reinterpret_cast<uint8_t*>(buffer_), view_size_));
}
#endif
bool Load(const FileBlock* block);
bool Store(const FileBlock* block);
void Flush();
#if BUILDFLAG(IS_WIN)
void EnableFlush();
#endif
bool Preload();
private:
~MappedFile() override;
bool init_ = false;
#if BUILDFLAG(IS_WIN)
bool enable_flush_ = false;
HANDLE section_;
#endif
size_t view_size_ = 0;
#if BUILDFLAG(POSIX_BYPASS_MMAP)
base::HeapArray<uint8_t> snapshot_;
base::HeapArray<uint8_t> buffer_;
#else
RAW_PTR_EXCLUSION void* buffer_ = nullptr;
#endif
};
class ScopedFlush {
public:
explicit ScopedFlush(MappedFile* file) : file_(file) {}
~ScopedFlush() {
file_->Flush();
}
private:
raw_ptr<MappedFile> file_;
};
}
#endif