00001 00007 #include<time.h> 00008 #include<stdint.h> 00009 #include<sys/types.h> 00010 #include<sys/stat.h> 00011 #include<sys/uio.h> 00012 #include<fcntl.h> 00013 #include<unistd.h> 00014 #include<string.h> 00015 #include"pmseg.h" 00016 00017 typedef unsigned char octet_t; 00018 00022 struct VerwData_t 00023 { 00025 time_t stamp; 00027 uint32_t checksum; 00028 }; 00029 00030 struct TVerwData_t 00031 { 00032 bool dirty; 00033 VerwData_t pverw; 00034 }; 00035 const int maxblocks=8; 00042 const size_t BLOCKSIZE=4096+sizeof(VerwData_t); 00043 const size_t RELPAGESZ=4096; 00044 00050 class ReliableSegStrategy 00051 { 00052 public: 00053 00060 virtual int open(char *filename)=0; 00062 virtual void close()=0; 00064 virtual int nplex()=0; 00066 virtual int fd(int i)=0; 00068 virtual off_t offset(int i,int pageno)=0; 00069 }; 00070 00076 class TwinblockStrategy : public ReliableSegStrategy 00077 { 00078 int f; 00079 public: 00080 TwinblockStrategy() {} 00081 int open(char *filename); 00082 void close(); 00083 int nplex(); 00084 int fd(int i); 00085 off_t offset(int i,int pageno); 00086 }; 00087 00093 class ReliableSegment: public PMSeg 00094 { 00095 protected: 00096 int fd0; 00097 TVerwData_t vdata[maxblocks]; 00098 ReliableSegStrategy *strat; 00099 public: 00100 ReliableSegment(ReliableSegStrategy *s); 00101 static uint32_t calc_checksum(octet_t *page); 00102 octet_t *page(int pageno) { return (octet_t *)begin()+RELPAGESZ*pageno; } 00109 void write_page(int pageno); 00116 void read_page_optimistic(int pageno); 00117 00123 void read_page_reliable(int pageno); 00130 int open(char *filename) { return strat->open(filename); } 00131 00135 void handle(void *page,void *addr); 00136 00140 void save(); 00144 void close() { strat->close(); } 00145 protected: 00146 void protect(int pageno); 00147 void unprotect(int pageno); 00148 }; 00149 00150 00151 00152