diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2013-02-22 09:00:13 -0800 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2013-02-22 09:00:13 -0800 |
commit | b8f4e42e1bd9f80690d61dc2ab86edb14b2ecb40 (patch) | |
tree | 23f8ccc7000a4fe55a4d3eaed644705fe2a34a01 /src | |
parent | 9dca7190f91189f5c0b8a7974fcc522e47d60e50 (diff) | |
parent | 1eb578796c98f457546059b66eb98fd0efc8e690 (diff) |
Merge pull request #2229 from sipa/prealloc
Native versions for AllocateFileRange()
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/util.cpp | 47 | ||||
-rw-r--r-- | src/util.h | 1 |
3 files changed, 54 insertions, 2 deletions
diff --git a/src/main.cpp b/src/main.cpp index 3151a806dc..9edd9a2990 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1533,7 +1533,7 @@ bool CBlock::DisconnectBlock(CValidationState &state, CBlockIndex *pindex, CCoin } } -void static FlushBlockFile() +void static FlushBlockFile(bool fFinalize = false) { LOCK(cs_LastBlockFile); @@ -1541,12 +1541,16 @@ void static FlushBlockFile() FILE *fileOld = OpenBlockFile(posOld); if (fileOld) { + if (fFinalize) + TruncateFile(fileOld, infoLastBlockFile.nSize); FileCommit(fileOld); fclose(fileOld); } fileOld = OpenUndoFile(posOld); if (fileOld) { + if (fFinalize) + TruncateFile(fileOld, infoLastBlockFile.nUndoSize); FileCommit(fileOld); fclose(fileOld); } @@ -1965,7 +1969,7 @@ bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAdd } else { while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) { printf("Leaving block file %i: %s\n", nLastBlockFile, infoLastBlockFile.ToString().c_str()); - FlushBlockFile(); + FlushBlockFile(true); nLastBlockFile++; infoLastBlockFile.SetNull(); pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine diff --git a/src/util.cpp b/src/util.cpp index 4eff6ce71b..fc3e846a6b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -3,6 +3,15 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#ifndef WIN32 +// for posix_fallocate +#ifdef __linux__ +#define _POSIX_C_SOURCE 200112L +#endif +#include <fcntl.h> +#include <sys/stat.h> +#endif + #include "util.h" #include "sync.h" #include "version.h" @@ -1152,9 +1161,46 @@ int GetFilesize(FILE* file) return nFilesize; } +bool TruncateFile(FILE *file, unsigned int length) { +#if defined(WIN32) + return _chsize(_fileno(file), length) == 0; +#else + return ftruncate(fileno(file), length) == 0; +#endif +} + // this function tries to make a particular range of a file allocated (corresponding to disk space) // it is advisory, and the range specified in the arguments will never contain live data void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) { +#if defined(WIN32) + // Windows-specific version + HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file)); + LARGE_INTEGER nFileSize; + int64 nEndPos = (int64)offset + length; + nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF; + nFileSize.u.HighPart = nEndPos >> 32; + SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN); + SetEndOfFile(hFile); +#elif defined(MAC_OSX) + // OSX specific version + fstore_t fst; + fst.fst_flags = F_ALLOCATECONTIG; + fst.fst_posmode = F_PEOFPOSMODE; + fst.fst_offset = 0; + fst.fst_length = (off_t)offset + length; + fst.fst_bytesalloc = 0; + if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) { + fst.fst_flags = F_ALLOCATEALL; + fcntl(fileno(file), F_PREALLOCATE, &fst); + } + ftruncate(fileno(file), fst.fst_length); +#elif defined(__linux__) + // Version using posix_fallocate + off_t nEndPos = (off_t)offset + length; + posix_fallocate(fileno(file), 0, nEndPos); +#else + // Fallback version + // TODO: just write one byte per block static const char buf[65536] = {}; fseek(file, offset, SEEK_SET); while (length > 0) { @@ -1164,6 +1210,7 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) { fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway length -= now; } +#endif } void ShrinkDebugFile() diff --git a/src/util.h b/src/util.h index a6b88206e9..d129d13692 100644 --- a/src/util.h +++ b/src/util.h @@ -193,6 +193,7 @@ bool WildcardMatch(const char* psz, const char* mask); bool WildcardMatch(const std::string& str, const std::string& mask); void FileCommit(FILE *fileout); int GetFilesize(FILE* file); +bool TruncateFile(FILE *file, unsigned int length); void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest); boost::filesystem::path GetDefaultDataDir(); |