core/tempq: Store queue operations passed to tempq_init()

We don't need different operations for each queue.  If we store this
then a higher layer doesn't need to keep passing the same structure over
and over again.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-01-21 10:42:20 -05:00
parent 87c59ce888
commit 8d2b0caf6c
5 changed files with 18 additions and 14 deletions

View File

@ -7,15 +7,17 @@
#include <core/tempq.h>
#include <glib.h>
static struct queue_ops *tempq_ops = NULL;
static GSList *tempq_list;
static struct file tempq_file;
static struct queue *__tempq_alloc(struct queue_ops *ops, unsigned int flags)
static struct queue *__tempq_alloc(unsigned int flags)
{
struct queue *queue = g_malloc(sizeof(struct queue));
flags = flags | Q_SAVE_FLAGS | Q_SAVE_SORT;
tempq_list = g_slist_append(tempq_list, queue);
queue_init(queue, flags | Q_SAVE_FLAGS | Q_SAVE_SORT, ops);
queue_init(queue, flags, tempq_ops);
return queue;
}
@ -26,13 +28,13 @@ static void __tempq_free(struct queue *queue)
g_free(queue);
}
static void __tempq_read_queue(struct queue_ops *ops)
static void __tempq_read_queue()
{
unsigned int flags, count, i, track;
struct queue *queue;
file_readf(&tempq_file, "%u %u", &flags, &count);
queue = __tempq_alloc(ops, flags);
queue = __tempq_alloc(flags);
for (i = 0; i < count; i++) {
file_readf(&tempq_file, "%u", &track);
@ -58,6 +60,8 @@ void tempq_init(struct queue_ops *ops)
{
unsigned int num, i;
tempq_ops = ops;
file_init(&tempq_file, "deck", 1);
if (!file_open(&tempq_file, OPEN_READ))
return;
@ -66,7 +70,7 @@ void tempq_init(struct queue_ops *ops)
file_readf(&tempq_file, "%u", &num);
for (i = 0; i < num; i++)
__tempq_read_queue(ops);
__tempq_read_queue();
out:
file_close(&tempq_file);
}
@ -91,9 +95,9 @@ void tempq_save(struct queue *queue, enum queue_flags flag)
file_close(&tempq_file);
}
struct queue *tempq_alloc(struct queue_ops *ops, unsigned int flags)
struct queue *tempq_alloc(unsigned int flags)
{
struct queue *queue = __tempq_alloc(ops, Q_ENABLED | flags);
struct queue *queue = __tempq_alloc(Q_ENABLED | flags);
tempq_save(queue, Q_ENABLED);
return queue;
}

View File

@ -178,7 +178,7 @@ bool Tab :: tab_queue_selected(bool random)
if (tempq_count() >= 10)
return true;
queue *pq = tempq_alloc(&tempq_ops, flags);
queue *pq = tempq_alloc(flags);
on_pq_created(pq, tempq_count() - 1);
gui_sidebar_add(gui_queue(pq));
tab_queue_add(pq);

View File

@ -25,7 +25,7 @@ void tempq_save(struct queue *, enum queue_flags);
/* Called to allocate a new temporary queue. */
struct queue *tempq_alloc(struct queue_ops *, unsigned int);
struct queue *tempq_alloc(unsigned int);
/* Called to free a temporary queue. */
void tempq_free(struct queue *);

View File

@ -128,7 +128,7 @@ static void test_playback()
static void test_next()
{
struct queue *history_q = history_get_queue();
struct queue *temp_q = tempq_alloc(NULL, 0);
struct queue *temp_q = tempq_alloc(0);
int i;
state_count = 0;

View File

@ -32,7 +32,7 @@ static void test_alloc()
{
struct queue *q0, *q1;
q0 = tempq_alloc(NULL, 0);
q0 = tempq_alloc(0);
test_not_equal((void *)q0, NULL);
test_equal(queue_has_flag(q0, Q_ENABLED), (bool)true);
test_equal(queue_has_flag(q0, Q_RANDOM), (bool)false);
@ -42,7 +42,7 @@ static void test_alloc()
test_equal(tempq_count(), 1);
test_equal(tempq_index(q0), 0);
q1 = tempq_alloc(NULL, Q_RANDOM);
q1 = tempq_alloc(Q_RANDOM);
test_not_equal((void *)q1, NULL);
test_equal(queue_has_flag(q1, Q_ENABLED), (bool)true);
test_equal(queue_has_flag(q1, Q_RANDOM), (bool)true);
@ -116,8 +116,8 @@ static void test_next()
collection_add("tests/Music/Hyrule Symphony");
while (idle_run_task()) {};
q0 = tempq_alloc(NULL, 0);
q1 = tempq_alloc(NULL, 0);
q0 = tempq_alloc(0);
q1 = tempq_alloc(0);
for (i = 0; i < track_db->db_size; i++) {
queue_add(q0, track_get(i));
queue_add(q1, track_get(i));