diff options
Diffstat (limited to 'courgette')
-rw-r--r-- | courgette/memory_allocator.cc | 62 | ||||
-rw-r--r-- | courgette/memory_allocator.h | 28 |
2 files changed, 20 insertions, 70 deletions
diff --git a/courgette/memory_allocator.cc b/courgette/memory_allocator.cc index f089163..07daf43 100644 --- a/courgette/memory_allocator.cc +++ b/courgette/memory_allocator.cc @@ -11,54 +11,25 @@ #if defined(OS_WIN) -namespace courgette { - -// TempFile - -TempFile::TempFile() : file_(base::kInvalidPlatformFileValue) { -} +namespace { -TempFile::~TempFile() { - Close(); -} - -void TempFile::Close() { - if (valid()) { - base::ClosePlatformFile(file_); - file_ = base::kInvalidPlatformFileValue; - } -} - -bool TempFile::Create() { - DCHECK(file_ == base::kInvalidPlatformFileValue); +// The file is created in the %TEMP% folder. +// NOTE: Since the file will be used as backing for a memory allocation, +// it will never be so big that size_t cannot represent its size. +base::File CreateTempFile() { base::FilePath path; if (!base::CreateTemporaryFile(&path)) - return false; + return base::File(); - bool created = false; - base::PlatformFileError error_code = base::PLATFORM_FILE_OK; - int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ | - base::PLATFORM_FILE_WRITE | - base::PLATFORM_FILE_DELETE_ON_CLOSE | - base::PLATFORM_FILE_TEMPORARY; - file_ = base::CreatePlatformFile(path, flags, &created, &error_code); - if (file_ == base::kInvalidPlatformFileValue) - return false; - - return true; -} - -bool TempFile::valid() const { - return file_ != base::kInvalidPlatformFileValue; + int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | + base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE | + base::File::FLAG_TEMPORARY; + return base::File(path, flags); } -base::PlatformFile TempFile::handle() const { - return file_; -} +} // namespace -bool TempFile::SetSize(size_t size) { - return base::TruncatePlatformFile(file_, size); -} +namespace courgette { // FileMapping @@ -116,13 +87,16 @@ TempMapping::~TempMapping() { } bool TempMapping::Initialize(size_t size) { + file_ = CreateTempFile(); + if (!file_.IsValid()) + return false; + // TODO(tommi): The assumption here is that the alignment of pointers (this) // is as strict or stricter than the alignment of the element type. This is // not always true, e.g. __m128 has 16-byte alignment. size += sizeof(this); - if (!file_.Create() || - !file_.SetSize(size) || - !mapping_.Create(file_.handle(), size)) { + if (!file_.SetLength(size) || + !mapping_.Create(file_.GetPlatformFile(), size)) { file_.Close(); return false; } diff --git a/courgette/memory_allocator.h b/courgette/memory_allocator.h index d848915..5386dad 100644 --- a/courgette/memory_allocator.h +++ b/courgette/memory_allocator.h @@ -8,9 +8,9 @@ #include <memory> #include "base/basictypes.h" +#include "base/files/file.h" #include "base/files/file_path.h" #include "base/logging.h" -#include "base/platform_file.h" #ifndef NDEBUG @@ -57,30 +57,6 @@ namespace courgette { #if defined(OS_WIN) -// Manages a temporary file. The file is created in the %TEMP% folder and -// is deleted when the file handle is closed. -// NOTE: Since the file will be used as backing for a memory allocation, -// it will never be so big that size_t cannot represent its size. -class TempFile { - public: - TempFile(); - ~TempFile(); - - bool Create(); - void Close(); - bool SetSize(size_t size); - - // Returns true iff the temp file is currently open. - bool valid() const; - - // Returns the handle of the temporary file or INVALID_HANDLE_VALUE if - // a temp file has not been created. - base::PlatformFile handle() const; - - protected: - base::PlatformFile file_; -}; - // Manages a read/write virtual mapping of a physical file. class FileMapping { public: @@ -130,7 +106,7 @@ class TempMapping { static TempMapping* GetMappingFromPtr(void* mem); protected: - TempFile file_; + base::File file_; FileMapping mapping_; }; |