the mmap patch

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3137 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Lars Gullik Bjønnes 2001-12-03 01:11:05 +00:00
parent ff9b02aaba
commit d196f7e852
2 changed files with 70 additions and 10 deletions

View File

@ -1,3 +1,8 @@
2001-12-03 Ben Stanley <bds02@uow.edu.au>
* lyxsum.C: Added mmap version of CRC and made it selected
by default where available. Used process_block for crc for speedup.
2001-12-01 John Levon <moz@compsoc.man.ac.uk>
* filetools.C: more robust failure for DirList()

View File

@ -10,14 +10,58 @@
#include <config.h>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <boost/crc.hpp>
#include "support/lyxlib.h"
// Various implementations of lyx::sum(), depending on what methods
// are available. Order is faster to slowest.
#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
#ifdef WITH_WARNINGS
#warning lyx::sum() using mmap (lightning fast)
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
unsigned long lyx::sum(string const & file)
{
int fd = open(file.c_str(), O_RDONLY);
if(!fd)
return 0;
struct stat info;
fstat(fd, &info);
void * mm = mmap(0, info.st_size, PROT_READ,
MAP_PRIVATE, fd, 0);
if (mm == MAP_FAILED) {
close(fd);
return 0;
}
char * beg = static_cast<char*>(mm);
char * end = beg + info.st_size;
boost::crc_32_type crc;
crc.process_block(beg, end);
unsigned long result = crc.checksum();
munmap(mm, info.st_size);
close(fd);
return result;
}
#else // No mmap
#include <fstream>
#include <iterator>
namespace {
template<typename InputIterator>
@ -31,23 +75,34 @@ unsigned long do_crc(InputIterator first, InputIterator last)
} // namespace
// And this would be the file interface.
#if HAVE_DECL_ISTREAMBUF_ITERATOR
#ifdef WITH_WARNINGS
#warning lyx::sum() using istreambuf_iterator (fast)
#endif
unsigned long lyx::sum(string const & file)
{
std::ifstream ifs(file.c_str());
if (!ifs) return 0;
#ifdef HAVE_DECL_ISTREAMBUF_ITERATOR
// This is a lot faster...
std::istreambuf_iterator<char> beg(ifs);
std::istreambuf_iterator<char> end;
return do_crc(beg,end);
}
#else
// than this.
#ifdef WITH_WARNINGS
#warning lyx::sum() using istream_iterator (slow as a snail)
#endif
unsigned long lyx::sum(string const & file)
{
std::ifstream ifs(file.c_str());
if (!ifs) return 0;
ifs.unsetf(std::ios::skipws);
std::istream_iterator<char> beg(ifs);
std::istream_iterator<char> end;
#endif
return do_crc(beg, end);
return do_crc(beg,end);
}
#endif
#endif // mmap