InotifyQueue: use std::deque instead of GSList

This commit is contained in:
Max Kellermann 2013-01-03 09:40:37 +01:00
parent c4090b670d
commit 28f3e190c8
3 changed files with 19 additions and 37 deletions

View File

@ -21,6 +21,9 @@
#include "InotifyQueue.hxx" #include "InotifyQueue.hxx"
#include "UpdateGlue.hxx" #include "UpdateGlue.hxx"
#include <deque>
#include <string>
#include <glib.h> #include <glib.h>
#include <string.h> #include <string.h>
@ -37,7 +40,7 @@ enum {
INOTIFY_UPDATE_DELAY_S = 5, INOTIFY_UPDATE_DELAY_S = 5,
}; };
static GSList *inotify_queue; static std::deque<std::string> inotify_queue;
static guint queue_source_id; static guint queue_source_id;
void void
@ -45,20 +48,11 @@ mpd_inotify_queue_init(void)
{ {
} }
static void
free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
g_free(data);
}
void void
mpd_inotify_queue_finish(void) mpd_inotify_queue_finish(void)
{ {
if (queue_source_id != 0) if (queue_source_id != 0)
g_source_remove(queue_source_id); g_source_remove(queue_source_id);
g_slist_foreach(inotify_queue, free_callback, NULL);
g_slist_free(inotify_queue);
} }
static gboolean static gboolean
@ -66,8 +60,8 @@ mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
{ {
unsigned id; unsigned id;
while (inotify_queue != NULL) { while (!inotify_queue.empty()) {
char *uri_utf8 = (char *)inotify_queue->data; const char *uri_utf8 = inotify_queue.front().c_str();
id = update_enqueue(uri_utf8, false); id = update_enqueue(uri_utf8, false);
if (id == 0) if (id == 0)
@ -76,9 +70,7 @@ mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
g_debug("updating '%s' job=%u", uri_utf8, id); g_debug("updating '%s' job=%u", uri_utf8, id);
g_free(uri_utf8); inotify_queue.pop_front();
inotify_queue = g_slist_delete_link(inotify_queue,
inotify_queue);
} }
/* done, remove the timer event by returning false */ /* done, remove the timer event by returning false */
@ -97,39 +89,29 @@ path_in(const char *path, const char *possible_parent)
} }
void void
mpd_inotify_enqueue(char *uri_utf8) mpd_inotify_enqueue(const char *uri_utf8)
{ {
GSList *old_queue = inotify_queue;
if (queue_source_id != 0) if (queue_source_id != 0)
g_source_remove(queue_source_id); g_source_remove(queue_source_id);
queue_source_id = g_timeout_add_seconds(INOTIFY_UPDATE_DELAY_S, queue_source_id = g_timeout_add_seconds(INOTIFY_UPDATE_DELAY_S,
mpd_inotify_run_update, NULL); mpd_inotify_run_update, NULL);
inotify_queue = NULL; for (auto i = inotify_queue.begin(), end = inotify_queue.end();
while (old_queue != NULL) { i != end;) {
char *current_uri = (char *)old_queue->data; const char *current_uri = i->c_str();
if (path_in(uri_utf8, current_uri)) { if (path_in(uri_utf8, current_uri))
/* already enqueued */ /* already enqueued */
g_free(uri_utf8);
inotify_queue = g_slist_concat(inotify_queue,
old_queue);
return; return;
}
old_queue = g_slist_delete_link(old_queue, old_queue);
if (path_in(current_uri, uri_utf8)) if (path_in(current_uri, uri_utf8))
/* existing path is a sub-path of the new /* existing path is a sub-path of the new
path; we can dequeue the existing path and path; we can dequeue the existing path and
update the new path instead */ update the new path instead */
g_free(current_uri); i = inotify_queue.erase(i);
else else
/* move the existing path to the new queue */ ++i;
inotify_queue = g_slist_prepend(inotify_queue,
current_uri);
} }
inotify_queue = g_slist_prepend(inotify_queue, uri_utf8); inotify_queue.emplace_back(uri_utf8);
} }

View File

@ -27,6 +27,6 @@ void
mpd_inotify_queue_finish(void); mpd_inotify_queue_finish(void);
void void
mpd_inotify_enqueue(char *uri_utf8); mpd_inotify_enqueue(const char *uri_utf8);
#endif #endif

View File

@ -295,10 +295,10 @@ mpd_inotify_callback(int wd, unsigned mask,
? fs_charset_to_utf8(uri_fs) ? fs_charset_to_utf8(uri_fs)
: g_strdup(""); : g_strdup("");
if (uri_utf8 != NULL) if (uri_utf8 != NULL) {
/* this function will take care of freeing
uri_utf8 */
mpd_inotify_enqueue(uri_utf8); mpd_inotify_enqueue(uri_utf8);
g_free(uri_utf8);
}
} }
g_free(uri_fs); g_free(uri_fs);