diff options
author | Max Kellermann <max@duempel.org> | 2013-10-17 21:13:40 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-10-17 21:13:40 +0200 |
commit | 7ef40de98bb3cbf2fd1fe693871a30064d7ef41b (patch) | |
tree | df7b0904b1629f64a746053c4d38868d7dfe92b9 | |
parent | 196ec25682e303e90f3e0b2c93691203c19ec339 (diff) | |
download | mpd-7ef40de98bb3cbf2fd1fe693871a30064d7ef41b.tar.gz mpd-7ef40de98bb3cbf2fd1fe693871a30064d7ef41b.tar.xz mpd-7ef40de98bb3cbf2fd1fe693871a30064d7ef41b.zip |
UpdateQueue: use std::string and std::queue
-rw-r--r-- | src/UpdateGlue.cxx | 10 | ||||
-rw-r--r-- | src/UpdateQueue.cxx | 46 | ||||
-rw-r--r-- | src/UpdateQueue.hxx | 19 |
3 files changed, 35 insertions, 40 deletions
diff --git a/src/UpdateGlue.cxx b/src/UpdateGlue.cxx index 86b80271f..663b996b5 100644 --- a/src/UpdateGlue.cxx +++ b/src/UpdateGlue.cxx @@ -139,8 +139,6 @@ update_enqueue(const char *path, bool _discard) */ static void update_finished_event(void) { - char *path; - assert(progress == UPDATE_PROGRESS_DONE); update_thread.Join(); @@ -151,11 +149,11 @@ static void update_finished_event(void) /* send "idle" events */ instance->DatabaseModified(); - path = update_queue_shift(&discard); - if (path != NULL) { + auto i = update_queue_shift(); + if (i.IsDefined()) { /* schedule the next path */ - spawn_update_task(path); - g_free(path); + discard = i.discard; + spawn_update_task(i.path_utf8.c_str()); } else { progress = UPDATE_PROGRESS_IDLE; diff --git a/src/UpdateQueue.cxx b/src/UpdateQueue.cxx index 9479e76f5..aa6074c7f 100644 --- a/src/UpdateQueue.cxx +++ b/src/UpdateQueue.cxx @@ -19,49 +19,31 @@ #include "config.h" #include "UpdateQueue.hxx" -#include "util/Macros.hxx" -#include <glib.h> +#include <queue> +#include <list> -#include <assert.h> -#include <string.h> +static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32; -/* make this dynamic?, or maybe this is big enough... */ -static struct { - char *path; - bool discard; -} update_queue[32]; - -static size_t update_queue_length; +static std::queue<UpdateQueueItem, std::list<UpdateQueueItem>> update_queue; unsigned update_queue_push(const char *path, bool discard, unsigned base) { - assert(update_queue_length <= ARRAY_SIZE(update_queue)); - - if (update_queue_length == ARRAY_SIZE(update_queue)) + if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE) return 0; - update_queue[update_queue_length].path = g_strdup(path); - update_queue[update_queue_length].discard = discard; - - ++update_queue_length; - - return base + update_queue_length; + update_queue.emplace(path, discard); + return base + update_queue.size(); } -char * -update_queue_shift(bool *discard_r) +UpdateQueueItem +update_queue_shift() { - char *path; - - if (update_queue_length == 0) - return NULL; - - path = update_queue[0].path; - *discard_r = update_queue[0].discard; + if (update_queue.empty()) + return UpdateQueueItem(); - memmove(&update_queue[0], &update_queue[1], - --update_queue_length * sizeof(update_queue[0])); - return path; + auto i = std::move(update_queue.front()); + update_queue.pop(); + return i; } diff --git a/src/UpdateQueue.hxx b/src/UpdateQueue.hxx index 7de06964f..80c15f600 100644 --- a/src/UpdateQueue.hxx +++ b/src/UpdateQueue.hxx @@ -22,10 +22,25 @@ #include "check.h" +#include <string> + +struct UpdateQueueItem { + std::string path_utf8; + bool discard; + + UpdateQueueItem() = default; + UpdateQueueItem(const char *_path, bool _discard) + :path_utf8(_path), discard(_discard) {} + + bool IsDefined() const { + return !path_utf8.empty(); + } +}; + unsigned update_queue_push(const char *path, bool discard, unsigned base); -char * -update_queue_shift(bool *discard_r); +UpdateQueueItem +update_queue_shift(); #endif |