changeset 595:31a1556fc2d6

Make dump.cpp not use mmap() A couple more "handy" portability macros in audioDB-internals.h means the balance of this work adds lines of code; clearly suboptimal.
author mas01cr
date Tue, 11 Aug 2009 21:42:44 +0000
parents c850f3433454
children 6ad0a6e67d4c
files audioDB-internals.h create.cpp dump.cpp
diffstat 3 files changed, 56 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB-internals.h	Tue Aug 11 21:42:39 2009 +0000
+++ b/audioDB-internals.h	Tue Aug 11 21:42:44 2009 +0000
@@ -108,6 +108,20 @@
 #define ADB_HEADER_FLAG_TIMES		(0x20U)
 #define ADB_HEADER_FLAG_REFERENCES	(0x40U)
 
+/* macros to make file/directory creation non-painful */
+#if defined(WIN32)
+#define mkdir_or_goto_error(dirname) \
+  if(_mkdir(dirname) < 0) { \
+    goto error; \
+  }
+#define ADB_CREAT_PERMISSIONS (_S_IREAD|_S_IWRITE)
+#else
+#define mkdir_or_goto_error(dirname) \
+  if(mkdir(dirname, S_IRWXU|S_IRWXG|S_IRWXO) < 0) { \
+    goto error; \
+  }
+#define ADB_CREAT_PERMISSIONS (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
+#endif
 /* the transparent version of the opaque (forward-declared) adb_t. */
 struct adb {
   char *path;
@@ -171,6 +185,22 @@
     } \
   }
 
+#define malloc_and_fill_or_goto_error(type, var, start, length)   \
+  { void *tmp = malloc(length); \
+    if(tmp == NULL) { \
+      goto error; \
+    } \
+    var = (type) tmp; \
+    lseek_set_or_goto_error(adb->fd, (start)); \
+    read_or_goto_error(adb->fd, var, length); \
+  }
+
+#define maybe_free(var) \
+  { if(var) { \
+      free(var); \
+    } \
+  }
+
 #define maybe_delete_array(pointer) \
   { if(pointer) { \
       delete [] pointer; \
--- a/create.cpp	Tue Aug 11 21:42:39 2009 +0000
+++ b/create.cpp	Tue Aug 11 21:42:44 2009 +0000
@@ -46,13 +46,7 @@
     datadim = ADB_DEFAULT_DATADIM;
   }
 
-  if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, 
-#if defined(WIN32)
-                 _S_IREAD|_S_IWRITE
-#else
-                 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
-#endif
-                 )) < 0) {
+  if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
     goto error;
   }
 
--- a/dump.cpp	Tue Aug 11 21:42:39 2009 +0000
+++ b/dump.cpp	Tue Aug 11 21:42:44 2009 +0000
@@ -43,20 +43,18 @@
       powerTableLength = align_page_up(length/dim);
     }
 
-    mmap_or_goto_error(char *, fileTable, adb->header->fileTableOffset, fileTableLength);
+    malloc_and_fill_or_goto_error(char *, fileTable, adb->header->fileTableOffset, fileTableLength);
     if (adb->header->flags & ADB_HEADER_FLAG_REFERENCES) {
-      mmap_or_goto_error(char *, featureFileNameTable, adb->header->dataOffset, fileTableLength);
-      mmap_or_goto_error(char *, powerFileNameTable, adb->header->powerTableOffset, fileTableLength);
-      mmap_or_goto_error(char *, timesFileNameTable, adb->header->timesTableOffset, fileTableLength);
+      malloc_and_fill_or_goto_error(char *, featureFileNameTable, adb->header->dataOffset, fileTableLength);
+      malloc_and_fill_or_goto_error(char *, powerFileNameTable, adb->header->powerTableOffset, fileTableLength);
+      malloc_and_fill_or_goto_error(char *, timesFileNameTable, adb->header->timesTableOffset, fileTableLength);
     } else {
-      mmap_or_goto_error(double *, powerTable, adb->header->powerTableOffset, powerTableLength);
-      mmap_or_goto_error(double *, timesTable, adb->header->timesTableOffset, timesTableLength);
+      malloc_and_fill_or_goto_error(double *, powerTable, adb->header->powerTableOffset, powerTableLength);
+      malloc_and_fill_or_goto_error(double *, timesTable, adb->header->timesTableOffset, timesTableLength);
     }
   }
 
-  if((mkdir(output, S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
-    goto error;
-  }
+  mkdir_or_goto_error(output);
 
   if ((getcwd(cwd, PATH_MAX)) == 0) {
     goto error;
@@ -69,25 +67,25 @@
   }
   directory_changed = 1;
 
-  if ((fLfd = open("featureList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+  if ((fLfd = open("featureList.txt", O_CREAT|O_RDWR|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
     goto error;
   }
 
   times = adb->header->flags & ADB_HEADER_FLAG_TIMES;
   if (times) {
-    if ((tLfd = open("timesList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+    if ((tLfd = open("timesList.txt", O_CREAT|O_RDWR|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
       goto error;
     }
   }
 
   power = adb->header->flags & ADB_HEADER_FLAG_POWER;
   if (power) {
-    if ((pLfd = open("powerList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+    if ((pLfd = open("powerList.txt", O_CREAT|O_RDWR|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
       goto error;
     }
   }
 
-  if ((kLfd = open("keyList.txt", O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+  if ((kLfd = open("keyList.txt", O_CREAT|O_RDWR|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
     goto error;
   }
   
@@ -127,7 +125,7 @@
       }
     } else {
       snprintf(fName, 256, "%05d.features", k);
-      if ((ffd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+      if ((ffd = open(fName, O_CREAT|O_RDWR|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
         goto error;
       }
       write_or_goto_error(ffd, &(adb->header->dim), sizeof(uint32_t));
@@ -177,7 +175,7 @@
       if (power) {
 	uint32_t one = 1;
 	snprintf(fName, 256, "%05d.power", k);
-	if ((pfd = open(fName, O_CREAT|O_RDWR|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) {
+	if ((pfd = open(fName, O_CREAT|O_RDWR|O_EXCL, ADB_CREAT_PERMISSIONS)) < 0) {
           goto error;
 	}
         write_or_goto_error(pfd, &one, sizeof(uint32_t));
@@ -231,12 +229,12 @@
   }
   fclose(kLFile);
     
-  maybe_munmap(fileTable, fileTableLength);
-  maybe_munmap(timesTable, timesTableLength);
-  maybe_munmap(powerTable, powerTableLength);
-  maybe_munmap(featureFileNameTable, fileTableLength);
-  maybe_munmap(timesFileNameTable, fileTableLength);
-  maybe_munmap(powerFileNameTable, fileTableLength);
+  maybe_free(fileTable);
+  maybe_free(timesTable);
+  maybe_free(powerTable);
+  maybe_free(featureFileNameTable);
+  maybe_free(timesFileNameTable);
+  maybe_free(powerFileNameTable);
 
   if((chdir(cwd)) < 0) {
     /* don't goto error because the error handling will try to
@@ -271,12 +269,12 @@
     fclose(scriptFile);
   }
 
-  maybe_munmap(fileTable, fileTableLength);
-  maybe_munmap(timesTable, timesTableLength);
-  maybe_munmap(powerTable, powerTableLength);
-  maybe_munmap(featureFileNameTable, fileTableLength);
-  maybe_munmap(timesFileNameTable, fileTableLength);
-  maybe_munmap(powerFileNameTable, fileTableLength);
+  maybe_free(fileTable);
+  maybe_free(timesTable);
+  maybe_free(powerTable);
+  maybe_free(featureFileNameTable);
+  maybe_free(timesFileNameTable);
+  maybe_free(powerFileNameTable);
 
   if(directory_changed) {
     int gcc_warning_workaround = chdir(cwd);