Performance improvement and safety check.
authorGustavo Sverzut Barbieri <barbieri@gmail.com>
Thu, 22 Nov 2007 02:49:07 +0000 (23:49 -0300)
committerGustavo Sverzut Barbieri <barbieri@gmail.com>
Thu, 22 Nov 2007 23:41:31 +0000 (00:41 +0100)
 * Just process lms_db_*_start() once.
 * Check if _references would drop below 0 and log.

src/lib/lightmediascanner_db_audio.c
src/lib/lightmediascanner_db_image.c

index d69e568..556e398 100644 (file)
@@ -12,7 +12,8 @@ struct lms_db_audio {
     sqlite3_stmt *get_artist;
     sqlite3_stmt *get_album;
     sqlite3_stmt *get_genre;
-    int _references;
+    unsigned int _references;
+    unsigned int _is_started:1;
 };
 
 static lms_db_audio_t *_singleton = NULL;
@@ -185,6 +186,8 @@ lms_db_audio_start(lms_db_audio_t *lda)
 {
     if (!lda)
         return -1;
+    if (lda->_is_started)
+        return 0;
 
     lda->insert_audio = lms_db_compile_stmt(lda->db,
         "INSERT OR REPLACE INTO audios "
@@ -223,6 +226,7 @@ lms_db_audio_start(lms_db_audio_t *lda)
     if (!lda->get_genre)
         return -8;
 
+    lda->_is_started = 1;
     return 0;
 }
 
@@ -231,6 +235,10 @@ lms_db_audio_free(lms_db_audio_t *lda)
 {
     if (!lda)
         return -1;
+    if (lda->_references == 0) {
+        fprintf(stderr, "ERROR: over-called lms_db_audio_free(%p)\n", lda);
+        return -1;
+    }
 
     lda->_references--;
     if (lda->_references > 0)
index 080f958..c4c1ce3 100644 (file)
@@ -6,7 +6,8 @@
 struct lms_db_image {
     sqlite3 *db;
     sqlite3_stmt *insert;
-    int _references;
+    unsigned int _references;
+    unsigned int _is_started:1;
 };
 
 static lms_db_image_t *_singleton = NULL;
@@ -98,6 +99,8 @@ lms_db_image_start(lms_db_image_t *ldi)
 {
     if (!ldi)
         return -1;
+    if (ldi->_is_started)
+        return 0;
 
     ldi->insert = lms_db_compile_stmt(ldi->db,
         "INSERT OR REPLACE INTO images ("
@@ -107,6 +110,7 @@ lms_db_image_start(lms_db_image_t *ldi)
     if (!ldi->insert)
         return -2;
 
+    ldi->_is_started = 1;
     return 0;
 }
 
@@ -115,6 +119,10 @@ lms_db_image_free(lms_db_image_t *ldi)
 {
     if (!ldi)
         return -1;
+    if (ldi->_references == 0) {
+        fprintf(stderr, "ERROR: over-called lms_db_image_free(%p)\n", ldi);
+        return -1;
+    }
 
     ldi->_references--;
     if (ldi->_references > 0)