changeset 508:23c47e118bc6

Better soap memory correctness. Pass the struct soap down through audioDB::query into Reporter::report methods. We go through the audioDB constructor and do everything on the stack. We'll eventually also need to add a pointer member within the audioDB object, so that non-local transfers of control (particularly audioDB::error) can still allocate soap-specific memory. Then use soap_malloc() not new[] for memory allocation of adbQueryResponse data structures.
author mas01cr
date Tue, 13 Jan 2009 21:37:14 +0000
parents e7fd50483311
children cc2b97d020b1
files ReporterBase.h audioDB.cpp audioDB.h reporter.h soap.cpp
diffstat 5 files changed, 48 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/ReporterBase.h	Tue Jan 13 21:37:10 2009 +0000
+++ b/ReporterBase.h	Tue Jan 13 21:37:14 2009 +0000
@@ -6,7 +6,7 @@
 public:
   virtual ~ReporterBase(){};
   virtual void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) = 0;
-  virtual void report(adb_t *,void *) = 0;
+  virtual void report(adb_t *, struct soap *, void *) = 0;
 };
 
 #endif
--- a/audioDB.cpp	Tue Jan 13 21:37:10 2009 +0000
+++ b/audioDB.cpp	Tue Jan 13 21:37:14 2009 +0000
@@ -83,7 +83,7 @@
     error("Unrecognized command",command);
 }
 
-audioDB::audioDB(const unsigned argc, const char *argv[], adb__queryResponse *adbQueryResponse): O2_AUDIODB_INITIALIZERS
+audioDB::audioDB(const unsigned argc, const char *argv[], struct soap *soap, adb__queryResponse *adbQueryResponse): O2_AUDIODB_INITIALIZERS
 {
   try {
     isServer = 1; // Set to make errors report over SOAP
@@ -92,7 +92,7 @@
     if(dbName && adb_root)
       prefix_name((char** const)&dbName, adb_root);
     assert(O2_ACTION(COM_QUERY));
-    query(dbName, inFile, adbQueryResponse);
+    query(dbName, inFile, soap, adbQueryResponse);
   } catch(char *err) {
     cleanup();
     throw(err);
@@ -757,7 +757,7 @@
   status(dbName);
 }
 
-void audioDB::query(const char* dbName, const char* inFile, adb__queryResponse *adbQueryResponse) {
+void audioDB::query(const char* dbName, const char* inFile, struct soap *soap, adb__queryResponse *adbQueryResponse) {
 
   if(!adb) {
     if(!(adb = audiodb_open(dbName, O_RDWR))) {
@@ -951,7 +951,7 @@
   }
   audiodb_query_free_results(adb, &qspec, rs);
 
-  reporter->report(adb, adbQueryResponse);
+  reporter->report(adb, soap, adbQueryResponse);
 }
 
 // This entry point is visited once per instance
--- a/audioDB.h	Tue Jan 13 21:37:10 2009 +0000
+++ b/audioDB.h	Tue Jan 13 21:37:14 2009 +0000
@@ -310,7 +310,7 @@
 
  public:
   audioDB(const unsigned argc, const char *argv[]);
-  audioDB(const unsigned argc, const char *argv[], adb__queryResponse *adbQueryResponse);
+  audioDB(const unsigned argc, const char *argv[], struct soap *soap, adb__queryResponse *adbQueryResponse);
   audioDB(const unsigned argc, const char *argv[], adb__statusResponse *adbStatusResponse);
   audioDB(const unsigned argc, const char *argv[], adb__lisztResponse *adbLisztResponse);
 
@@ -322,7 +322,7 @@
   void create(const char* dbName);
   void insert(const char* dbName, const char* inFile);
   void batchinsert(const char* dbName, const char* inFile);
-  void query(const char* dbName, const char* inFile, adb__queryResponse *adbQueryResponse=0);
+  void query(const char* dbName, const char* inFile, struct soap *soap=0, adb__queryResponse *adbQueryResponse=0);
   void status(const char* dbName, adb__statusResponse *adbStatusResponse=0);
 
   unsigned random_track(unsigned *propTable, unsigned total);
--- a/reporter.h	Tue Jan 13 21:37:10 2009 +0000
+++ b/reporter.h	Tue Jan 13 21:37:14 2009 +0000
@@ -46,7 +46,7 @@
   // reporter classes for WS and for stdout, rather than passing this
   // adbQueryResponse thing everywhere; the adb argument is there
   // solely for converting trackIDs into names.  -- CSR, 2007-12-10.
-  virtual void report(adb_t *adb, void* adbQueryResponse) = 0;
+  virtual void report(adb_t *adb, struct soap *soap, void* adbQueryResponse) = 0;
 };
 
 template <class T> class pointQueryReporter : public Reporter {
@@ -54,7 +54,7 @@
   pointQueryReporter(unsigned int pointNN);
   ~pointQueryReporter();
   void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
-  void report(adb_t *adb, void* adbQueryResponse);
+  void report(adb_t *adb, struct soap *soap, void* adbQueryResponse);
 private:
   unsigned int pointNN;
   std::priority_queue< NNresult, std::vector< NNresult >, T> *queue;
@@ -83,7 +83,7 @@
   }
 }
 
-template <class T> void pointQueryReporter<T>::report(adb_t *adb, void *adbQueryResponse) {
+template <class T> void pointQueryReporter<T>::report(adb_t *adb, struct soap *soap, void *adbQueryResponse) {
   NNresult r;
   std::vector<NNresult> v;
   unsigned int size = queue->size();
@@ -109,14 +109,14 @@
     response->result.__sizeDist=size;
     response->result.__sizeQpos=size;
     response->result.__sizeSpos=size;
-    response->result.Rlist= new char*[size];
-    response->result.Dist = new double[size];
-    response->result.Qpos = new unsigned int[size];
-    response->result.Spos = new unsigned int[size];
+    response->result.Rlist= (char **) soap_malloc(soap, size * sizeof(char *));
+    response->result.Dist = (double *) soap_malloc(soap, size * sizeof(double));
+    response->result.Qpos = (unsigned int *) soap_malloc(soap, size * sizeof(unsigned int));
+    response->result.Spos = (unsigned int *) soap_malloc(soap, size * sizeof(unsigned int));
     unsigned int k = 0;
     for(rit = v.rbegin(); rit < v.rend(); rit++, k++) {
       r = *rit;
-      response->result.Rlist[k] = new char[O2_MAXFILESTR];
+      response->result.Rlist[k] = (char *) soap_malloc(soap, O2_MAXFILESTR);
       response->result.Dist[k] = r.dist;
       response->result.Qpos[k] = r.qpos;
       response->result.Spos[k] = r.spos;
@@ -133,7 +133,7 @@
   trackAveragingReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
   ~trackAveragingReporter();
   void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
-  void report(adb_t *adb, void *adbQueryResponse);
+  void report(adb_t *adb, struct soap *soap, void *adbQueryResponse);
  protected:
   unsigned int pointNN;
   unsigned int trackNN;
@@ -164,7 +164,7 @@
   }
 }
 
-template <class T> void trackAveragingReporter<T>::report(adb_t *adb, void *adbQueryResponse) {
+template <class T> void trackAveragingReporter<T>::report(adb_t *adb, struct soap *soap, void *adbQueryResponse) {
   std::priority_queue < NNresult, std::vector< NNresult>, T> result;
   for (int i = numFiles-1; i >= 0; i--) {
     unsigned int size = queues[i].size();
@@ -217,14 +217,14 @@
     response->result.__sizeDist=size;
     response->result.__sizeQpos=size;
     response->result.__sizeSpos=size;
-    response->result.Rlist= new char*[size];
-    response->result.Dist = new double[size];
-    response->result.Qpos = new unsigned int[size];
-    response->result.Spos = new unsigned int[size];
+    response->result.Rlist= (char **) soap_malloc(soap, size * sizeof(char *));
+    response->result.Dist = (double *) soap_malloc(soap, size * sizeof(double));
+    response->result.Qpos = (unsigned int *) soap_malloc(soap, size * sizeof(unsigned int));
+    response->result.Spos = (unsigned int *) soap_malloc(soap, size * sizeof(unsigned int));
     unsigned int k = 0;
     for(rit = v.rbegin(); rit < v.rend(); rit++, k++) {
       r = *rit;
-      response->result.Rlist[k] = new char[O2_MAXFILESTR];
+      response->result.Rlist[k] = (char *) soap_malloc(soap, O2_MAXFILESTR);
       response->result.Dist[k] = r.dist;
       response->result.Qpos[k] = r.qpos;
       response->result.Spos[k] = r.spos;
@@ -245,13 +245,13 @@
   using trackAveragingReporter<T>::pointNN;
  public:
   trackSequenceQueryNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
-  void report(adb_t *adb, void *adbQueryResponse);
+  void report(adb_t *adb, struct soap *soap, void *adbQueryResponse);
 };
 
 template <class T> trackSequenceQueryNNReporter<T>::trackSequenceQueryNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles)
 :trackAveragingReporter<T>(pointNN, trackNN, numFiles){}
 
-template <class T> void trackSequenceQueryNNReporter<T>::report(adb_t *adb, void *adbQueryResponse) {
+template <class T> void trackSequenceQueryNNReporter<T>::report(adb_t *adb, struct soap *soap, void *adbQueryResponse) {
   std::priority_queue < NNresult, std::vector< NNresult>, T> result;
   std::priority_queue< NNresult, std::vector< NNresult>, std::less<NNresult> > *point_queues 
     = new std::priority_queue< NNresult, std::vector< NNresult>, std::less<NNresult> >[numFiles];
@@ -322,10 +322,10 @@
     response->result.__sizeDist=size*pointNN;
     response->result.__sizeQpos=size*pointNN;
     response->result.__sizeSpos=size*pointNN;
-    response->result.Rlist= new char*[size*pointNN];
-    response->result.Dist = new double[size*pointNN];
-    response->result.Qpos = new unsigned int[size*pointNN];
-    response->result.Spos = new unsigned int[size*pointNN];
+    response->result.Rlist= (char **) soap_malloc(soap, size * pointNN * sizeof(char *));
+    response->result.Dist = (double *) soap_malloc(soap, size * pointNN * sizeof(double));
+    response->result.Qpos = (unsigned int *) soap_malloc(soap, size * pointNN * sizeof(unsigned int));
+    response->result.Spos = (unsigned int *) soap_malloc(soap, size * pointNN * sizeof(unsigned int));
     unsigned int k = 0;
     // Loop over returned tracks
     for(rit = v.rbegin(); rit < v.rend(); rit++) {
@@ -347,7 +347,7 @@
 	  rk.spos = 0xFFFFFFFF;
 	}
 	  
-	response->result.Rlist[k] = new char[O2_MAXFILESTR];
+	response->result.Rlist[k] = (char *) soap_malloc(soap, O2_MAXFILESTR);
 	response->result.Dist[k] = rk.dist;
 	response->result.Qpos[k] = rk.qpos;
 	response->result.Spos[k] = rk.spos;
@@ -415,7 +415,7 @@
   trackSequenceQueryRadReporter(unsigned int trackNN, unsigned int numFiles);
   ~trackSequenceQueryRadReporter();
   void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
-  void report(adb_t *adb, void *adbQueryResponse);
+  void report(adb_t *adb, struct soap *soap, void *adbQueryResponse);
  protected:
   unsigned int trackNN;
   unsigned int numFiles;
@@ -462,7 +462,7 @@
   }
 }
 
-void trackSequenceQueryRadReporter::report(adb_t *adb, void *adbQueryResponse) {
+void trackSequenceQueryRadReporter::report(adb_t *adb, struct soap *soap, void *adbQueryResponse) {
   std::priority_queue < Radresult, std::vector<Radresult>, std::greater<Radresult> > result;
   // KLUDGE: doing this backwards in an attempt to get the same
   // tiebreak behaviour as before.
@@ -503,14 +503,14 @@
     response->result.__sizeDist=size;
     response->result.__sizeQpos=size;
     response->result.__sizeSpos=size;
-    response->result.Rlist= new char*[size];
-    response->result.Dist = new double[size];
-    response->result.Qpos = new unsigned int[size];
-    response->result.Spos = new unsigned int[size];
+    response->result.Rlist= (char **) soap_malloc(soap, size * sizeof(char *));
+    response->result.Dist = (double *) soap_malloc(soap, size * sizeof(double));
+    response->result.Qpos = (unsigned int *) soap_malloc(soap, size * sizeof(unsigned int));
+    response->result.Spos = (unsigned int *) soap_malloc(soap, size * sizeof(unsigned int));
     unsigned int k = 0;
     for(rit = v.rbegin(); rit < v.rend(); rit++, k++) {
       r = *rit;
-      response->result.Rlist[k] = new char[O2_MAXFILESTR];
+      response->result.Rlist[k] = (char *) soap_malloc(soap, O2_MAXFILESTR);
       response->result.Dist[k] = 0;
       response->result.Qpos[k] = 0;
       response->result.Spos[k] = r.count;
@@ -531,7 +531,7 @@
   trackSequenceQueryRadNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
   ~trackSequenceQueryRadNNReporter();
   void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
-  void report(adb_t *adb, void *adbQueryResponse);
+  void report(adb_t *adb, struct soap *soap, void *adbQueryResponse);
  protected:
   unsigned int pointNN;
   unsigned int trackNN;
@@ -594,7 +594,7 @@
   }
 }
 
-void trackSequenceQueryRadNNReporter::report(adb_t *adb, void *adbQueryResponse) {
+void trackSequenceQueryRadNNReporter::report(adb_t *adb, struct soap *soap, void *adbQueryResponse) {
   std::priority_queue < Radresult, std::vector<Radresult>, std::greater<Radresult> > result;
   // KLUDGE: doing this backwards in an attempt to get the same
   // tiebreak behaviour as before.
@@ -636,7 +636,7 @@
       }	
     }
     // Report
-    rep->report(adb, adbQueryResponse);
+    rep->report(adb, soap, adbQueryResponse);
     // Exit
     delete[] point_queues;
     return;
@@ -674,10 +674,10 @@
     response->result.__sizeDist=size*pointNN;
     response->result.__sizeQpos=size*pointNN;
     response->result.__sizeSpos=size*pointNN;
-    response->result.Rlist= new char*[size*pointNN];
-    response->result.Dist = new double[size*pointNN];
-    response->result.Qpos = new unsigned int[size*pointNN];
-    response->result.Spos = new unsigned int[size*pointNN];
+    response->result.Rlist= (char **) soap_malloc(soap, size * pointNN * sizeof(char *));
+    response->result.Dist = (double *) soap_malloc(soap, size * pointNN * sizeof(double));
+    response->result.Qpos = (unsigned int *) soap_malloc(soap, size * pointNN * sizeof(unsigned int));
+    response->result.Spos = (unsigned int *) soap_malloc(soap, size * pointNN * sizeof(unsigned int));
     unsigned int k = 0;
     // Loop over returned tracks
     for(rit = v.rbegin(); rit < v.rend(); rit++) {
@@ -699,7 +699,7 @@
 	  rk.spos = 0xFFFFFFFF;
 	}
 	  
-	response->result.Rlist[k] = new char[O2_MAXFILESTR];
+	response->result.Rlist[k] = (char *) soap_malloc(soap, O2_MAXFILESTR);
 	response->result.Dist[k] = rk.dist;
 	response->result.Qpos[k] = rk.qpos;
 	response->result.Spos[k] = rk.spos;
@@ -730,7 +730,7 @@
   trackSequenceQueryRadNNReporterOneToOne(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
   ~trackSequenceQueryRadNNReporterOneToOne();
   void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
-  void report(adb_t *adb, void *adbQueryResponse);
+  void report(adb_t *adb, struct soap *soap, void *adbQueryResponse);
  protected:
   unsigned int pointNN;
   unsigned int trackNN;
@@ -778,7 +778,7 @@
 
 }
 
-void trackSequenceQueryRadNNReporterOneToOne::report(adb_t *adb, void *adbQueryResponse) {
+void trackSequenceQueryRadNNReporterOneToOne::report(adb_t *adb, struct soap *soap, void *adbQueryResponse) {
   if(adbQueryResponse==0) {
     std::vector< NNresult >::iterator vit;
     NNresult rk;
--- a/soap.cpp	Tue Jan 13 21:37:10 2009 +0000
+++ b/soap.cpp	Tue Jan 13 21:37:14 2009 +0000
@@ -294,7 +294,7 @@
   argv[argv_counter] = NULL;
 
   try {
-    audioDB(argc, argv, &adbQueryResponse);
+    audioDB(argc, argv, soap, &adbQueryResponse);
     delete [] argv;
     return SOAP_OK;
   } catch (char *err) {
@@ -361,7 +361,7 @@
   
  
   try {
-    audioDB(argc, argv, &adbQueryResponse);
+    audioDB(argc, argv, soap, &adbQueryResponse);
     return SOAP_OK;
   } catch (char *err) {
     soap_receiver_fault(soap, err, "");