Merge remote-tracking branches 'merge-requests/6' and 'merge-requests/7'
[geeqie.git] / src / filedata.c
index bc9685c..c99f3a9 100644 (file)
 
 #include <errno.h>
 
+#ifdef DEBUG_FILEDATA
+gint global_file_data_count = 0;
+#endif
+
 static GHashTable *file_data_pool = NULL;
 static GHashTable *file_data_planned_change_hash = NULL;
 
@@ -383,6 +387,10 @@ static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean
                }
 
        fd = g_new0(FileData, 1);
+#ifdef DEBUG_FILEDATA
+       global_file_data_count++;
+       DEBUG_2("file data count++: %d", global_file_data_count);
+#endif
 
        fd->size = st->st_size;
        fd->date = st->st_mtime;
@@ -532,6 +540,12 @@ static void file_data_free(FileData *fd)
 {
        g_assert(fd->magick == FD_MAGICK);
        g_assert(fd->ref == 0);
+       g_assert(!fd->locked);
+
+#ifdef DEBUG_FILEDATA
+       global_file_data_count--;
+       DEBUG_2("file data count--: %d", global_file_data_count);
+#endif
 
        metadata_cache_free(fd);
        g_hash_table_remove(file_data_pool, fd->original_path);
@@ -630,10 +644,12 @@ void file_data_unref(FileData *fd)
 /**
  * \brief Lock the FileData in memory.
  *
- * This allows the caller to prevent a FileData from being freed, even
- * after its refcount is zero.
+ * This allows the caller to prevent a FileData from being freed, even after its refcount is zero.
+ * This is intended to be used in cases where a FileData _should_ stay in memory as an optimization,
+ * even if the code would continue to function properly even if the FileData were freed.  Code that
+ * _requires_ the FileData to remain in memory should continue to use file_data_(un)ref.
  * <p />
- * This differs from file_data_ref in that the behavior is reentrant -- after N calls to
+ * Note: This differs from file_data_ref in that the behavior is reentrant -- after N calls to
  * file_data_lock, a single call to file_data_unlock will unlock the FileData.
  */
 void file_data_lock(FileData *fd)
@@ -666,6 +682,42 @@ void file_data_unlock(FileData *fd)
        file_data_consider_free(fd);
 }
 
+/**
+ * \brief Lock all of the FileDatas in the provided list
+ *
+ * \see file_data_lock(FileData)
+ */
+void file_data_lock_list(GList *list)
+{
+       GList *work;
+
+       work = list;
+       while (work)
+               {
+               FileData *fd = work->data;
+               work = work->next;
+               file_data_lock(fd);
+               }
+}
+
+/**
+ * \brief Unlock all of the FileDatas in the provided list
+ *
+ * \see file_data_unlock(FileData)
+ */
+void file_data_unlock_list(GList *list)
+{
+       GList *work;
+
+       work = list;
+       while (work)
+               {
+               FileData *fd = work->data;
+               work = work->next;
+               file_data_unlock(fd);
+               }
+}
+
 /*
  *-----------------------------------------------------------------------------
  * sidecar file info struct
@@ -1210,8 +1262,11 @@ FileData *file_data_new_group(const gchar *path_utf8)
        filelist_read_real(dir, &files, NULL, TRUE);
 
        fd = g_hash_table_lookup(file_data_pool, path_utf8);
-       g_assert(fd);
-       file_data_ref(fd);
+       if (!fd) fd = file_data_new(path_utf8, &st, TRUE);
+       if (fd)
+               {
+               file_data_ref(fd);
+               }
 
        filelist_free(files);
        g_free(dir);