1 // -*- mode: cpp; mode: fold -*-
3 // $Id: slidingwindow.cc,v 1.1 1999/11/05 05:47:06 jgg Exp $
4 /* ######################################################################
6 Sliding Window - Implements a sliding buffer over a file.
8 It would be possible to implement an alternate version if
9 _POSIX_MAPPED_FILES is not defined..
11 ##################################################################### */
13 // Include files /*{{{*/
15 #pragma implementation "dsync/slidingwindow.h"
18 #include <dsync/slidingwindow.h>
19 #include <dsync/error.h>
25 // SlidingWindow::SlidingWindow - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
28 SlidingWindow::SlidingWindow(FileFd &Fd,unsigned long MnSize) : Buffer(0),
29 MinSize(MnSize), Fd(Fd)
33 PageSize = sysconf(_SC_PAGESIZE);
35 if (MinSize < 1024*1024)
37 MinSize = Align(MinSize);
40 // SlidingWindow::~SlidingWindow - Destructor /*{{{*/
41 // ---------------------------------------------------------------------
42 /* Just unmap the mapping */
43 SlidingWindow::~SlidingWindow()
47 if (munmap((char *)Buffer,Size) != 0)
48 _error->Warning("Unable to munmap");
52 // SlidingWindow::Extend - Make Start - End longer /*{{{*/
53 // ---------------------------------------------------------------------
54 /* Start == End when the file is exhausted, false is an IO error. */
55 bool SlidingWindow::Extend(unsigned char *&Start,unsigned char *&End)
57 unsigned long Remainder = 0;
60 if (Start == 0 || Buffer == 0)
67 if (AlignDn((unsigned long)(Start - Buffer)) == 0)
68 return _error->Error("SlidingWindow::Extend called with too small a 'Start'");
70 // Scanning is finished.
71 if (Left < (off_t)Size)
77 Offset += AlignDn((unsigned long)(Start - Buffer));
78 Left -= AlignDn((unsigned long)(Start - Buffer));
79 Remainder = (Start - Buffer) % PageSize;
82 // Release the old region
85 if (munmap((char *)Buffer,Size) != 0)
86 return _error->Errno("munmap","Unable to munmap");
90 // Maximize the amount that can be mapped
91 if (Left < (off_t)MinSize)
97 Buffer = (unsigned char *)mmap(0,Size,PROT_READ,MAP_PRIVATE,Fd.Fd(),Offset);
98 if (Buffer == (unsigned char *)-1)
99 return _error->Errno("mmap","Couldn't make mmap %lu->%lu bytes",(unsigned long)Offset,
103 if (Left < (off_t)Size)
107 Start = Buffer + Remainder;