changeset 176:8193dbd66e34 no-big-mmap

map only the database header page as db, not the entire database. Change some naughty accesses through db + offset into the right thing Move some memcpy()s (updating the database header) further down, so as to be a little bit more transactional
author mas01cr
date Wed, 14 Nov 2007 17:58:57 +0000
parents 38bdbab60972
children c32bf13c3978
files audioDB.cpp
diffstat 1 files changed, 13 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.cpp	Wed Nov 14 16:40:07 2007 +0000
+++ b/audioDB.cpp	Wed Nov 14 17:58:57 2007 +0000
@@ -110,7 +110,7 @@
   if(indata)
     munmap(indata,statbuf.st_size);
   if(db)
-    munmap(db,dbH->dbSize);
+    munmap(db,getpagesize());
   if(fileTable)
     munmap(fileTable, dbH->trackTableOffset - dbH->fileTableOffset);
   if(trackTable)
@@ -466,11 +466,6 @@
     dbH->dbSize = O2_DEFAULTDBSIZE;
   }
 
-  // mmap the database file
-  if ((db = (char*) mmap(0, dbH->dbSize, PROT_READ | (forWrite ? PROT_WRITE : 0),
-			 MAP_SHARED, dbfid, 0)) == (caddr_t) -1)
-    error("mmap error for initting tables of database", "", "mmap");
-
   // Make some handy tables with correct types
 #define CHECKED_MMAP(type, var, start, end) \
   { void *tmp = mmap(0, ((end) - (start)), (PROT_READ | (forWrite ? PROT_WRITE : 0)), MAP_SHARED, dbfid, (start)); \
@@ -480,6 +475,8 @@
     var = (type) tmp; \
   }
 
+  CHECKED_MMAP(char *, db, 0, getpagesize());
+
   CHECKED_MMAP(char *, fileTable, dbH->fileTableOffset, dbH->trackTableOffset);
   CHECKED_MMAP(unsigned *, trackTable, dbH->trackTableOffset, dbH->dataOffset);
   CHECKED_MMAP(double *, dataBuf, dbH->dataOffset, dbH->timesTableOffset);
@@ -588,19 +585,15 @@
   // Update Header information
   dbH->length+=(statbuf.st_size-sizeof(int));
 
-  // Copy the header back to the database
-  memcpy (db, dbH, sizeof(dbTableHeaderT));  
-
   // Update track to file index map
-  //memcpy (db+trackTableOffset+(dbH->numFiles-1)*sizeof(unsigned), &numVectors, sizeof(unsigned));  
   memcpy (trackTable+dbH->numFiles-1, &numVectors, sizeof(unsigned));  
 
   // Update the feature database
-  memcpy (db+dbH->dataOffset+insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int));
+  memcpy (((char *) dataBuf) + insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int));
   
   // Norm the vectors on input if the database is already L2 normed
   if(dbH->flags & O2_FLAG_L2NORM)
-    unitNormAndInsertL2((double*)(db+dbH->dataOffset+insertoffset), dbH->dim, numVectors, 1); // append
+    unitNormAndInsertL2((double*)(((char *) dataBuf) + insertoffset), dbH->dim, numVectors, 1); // append
 
   // Report status
   status(dbName);
@@ -609,6 +602,9 @@
 	 << (statbuf.st_size-sizeof(int)) << " bytes." << endl;
   }
 
+  // Copy the header back to the database
+  memcpy (db, dbH, sizeof(dbTableHeaderT));  
+
   // CLEAN UP
   munmap(indata,statbuf.st_size);
   close(infid);
@@ -757,21 +753,21 @@
   
 	// Update Header information
 	dbH->length+=(statbuf.st_size-sizeof(int));
-	// Copy the header back to the database
-	memcpy (db, dbH, sizeof(dbTableHeaderT));  
   
 	// Update track to file index map
-	//memcpy (db+trackTableOffset+(dbH->numFiles-1)*sizeof(unsigned), &numVectors, sizeof(unsigned));  
 	memcpy (trackTable+dbH->numFiles-1, &numVectors, sizeof(unsigned));  
 	
 	// Update the feature database
-	memcpy (db+dbH->dataOffset+insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int));
+	memcpy (((char *) dataBuf) + insertoffset, indata+sizeof(int), statbuf.st_size-sizeof(int));
 	
 	// Norm the vectors on input if the database is already L2 normed
 	if(dbH->flags & O2_FLAG_L2NORM)
-	  unitNormAndInsertL2((double*)(db+dbH->dataOffset+insertoffset), dbH->dim, numVectors, 1); // append
+	  unitNormAndInsertL2((double*)(((char *) dataBuf) + insertoffset), dbH->dim, numVectors, 1); // append
 	
 	totalVectors+=numVectors;
+
+	// Copy the header back to the database
+	memcpy (db, dbH, sizeof(dbTableHeaderT));  
       }
     }
     // CLEAN UP