diff --git a/auxstorage.h b/auxstorage.h index 8d860df..c8d668c 100644 --- a/auxstorage.h +++ b/auxstorage.h @@ -11,16 +11,20 @@ #include #include +//CAUTION: +//it will not work if T is itself a class with dynamically allocated components +//it cannot be implemented for SparseMat, which lacks fixed record length + template class AuxStorage { - char filename[16]; + char filename[32]; int fd; - off64_t recl; + size_t recl; public: AuxStorage(void); - ~AuxStorage(void) {close(fd);}; + ~AuxStorage(void) {close(fd); unlink(filename);}; void get(NRVec &x, const int pos) const; void put(const NRVec &x, const int pos); void get(NRMat &x, const int pos) const; @@ -32,10 +36,10 @@ public: template AuxStorage::AuxStorage(void) { -strcpy(filename,"AuxStorageXXXXXX"); +strcpy(filename,"AUX_XXXXXX"); mktemp(filename); unlink(filename); -fd=open(filename,O_CREAT|O_LARGEFILE); +fd=open(filename,O_CREAT|O_LARGEFILE|O_RDWR,0777); if(fd<0) {perror(""); laerror("open failed in AuxStorage");} recl=0; } @@ -44,16 +48,16 @@ template void AuxStorage::get(NRVec &x, const int pos) const { if(recl==0) laerror("get from an empty file in AuxStorage"); -if((off64_t)-1 == lseek64(fd,pos*recl,SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");} -if(recl!=read(fd,&x[0],recl)) {perror(""); laerror("read failed in AuxStorage");} +if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");} +if((ssize_t)recl!=read(fd,&x[0],recl)) {perror(""); laerror("read failed in AuxStorage");} } template -void AuxStorage::put(const NRVec &x, const int pos) const +void AuxStorage::put(const NRVec &x, const int pos) { if(recl) {if(recl!=x.size()*sizeof(T)) laerror("attempt to write objects of different size to one AuxStorage");} else recl=x.size()*sizeof(T); -if((off64_t)-1 == lseek64(fd,pos*recl,SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");} +if((off64_t)-1 == lseek64(fd,pos*((off64_t)recl),SEEK_SET)) {perror(""); laerror("seek failed in AuxStorage");} if(0>write(fd,&x[0],recl)) {perror(""); laerror("write failed in AuxStorage");} }