slidingwindow.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: slidingwindow.h,v 1.2 1999/11/15 07:59:49 jgg Exp $
  4. /* ######################################################################
  5. Sliding Window - Implements a sliding buffer over a file.
  6. The buffer can be of arbitary size and where possible mmap is used
  7. to optimize IO.
  8. To use, init the class and then call Extend with a 0 input pointer
  9. to receive the first block and then call extend with Start <= End
  10. to get the next block. If Start != End then Start will be returned
  11. with a new value, but pointing at the same byte, that is the new
  12. region will contain the subregion Start -> End(o) but with a new
  13. length End-Start, End != End(o).
  14. After the file has been exhausted Start == End will be returned, but
  15. the old region Start -> End(o) will remain valid.
  16. ##################################################################### */
  17. /*}}}*/
  18. #ifndef SLIDING_WINDOW_H
  19. #define SLIDING_WINDOW_H
  20. #ifdef __GNUG__
  21. #pragma interface "dsync/slidingwindow.h"
  22. #endif
  23. #include <sys/types.h>
  24. #include <dsync/fileutl.h>
  25. class SlidingWindow
  26. {
  27. unsigned char *Buffer;
  28. unsigned long Size;
  29. unsigned long MinSize;
  30. FileFd &Fd;
  31. unsigned long PageSize;
  32. off_t Offset;
  33. off_t Left;
  34. inline unsigned long Align(off_t V) const {return ((V % PageSize) == 0)?V:V + PageSize - (V % PageSize);};
  35. inline unsigned long Align(unsigned long V) const {return ((V % PageSize) == 0)?V:V + PageSize - (V % PageSize);};
  36. inline unsigned long AlignDn(off_t V) const {return ((V % PageSize) == 0)?V:V - (V % PageSize);};
  37. inline unsigned long AlignDn(unsigned long V) const {return ((V % PageSize) == 0)?V:V - (V % PageSize);};
  38. public:
  39. // Make the distance Start - End longer if possible
  40. bool Extend(unsigned char *&Start,unsigned char *&End);
  41. SlidingWindow(FileFd &Fd,unsigned long MinSize = 0);
  42. ~SlidingWindow();
  43. };
  44. #endif