diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/prioq.c | 20 | ||||
-rw-r--r-- | src/shared/prioq.h | 1 |
2 files changed, 19 insertions, 2 deletions
diff --git a/src/shared/prioq.c b/src/shared/prioq.c index fe91324d9b..b7d745990c 100644 --- a/src/shared/prioq.c +++ b/src/shared/prioq.c @@ -53,6 +53,19 @@ void prioq_free(Prioq *q) { free(q); } +int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) { + assert(q); + + if (*q) + return 0; + + *q = prioq_new(compare_func); + if (!*q) + return -ENOMEM; + + return 0; +} + static void swap(Prioq *q, unsigned j, unsigned k) { void *saved_data; unsigned *saved_idx; @@ -204,9 +217,12 @@ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { assert(q); if (idx) { - assert(*idx < q->n_items); + if (*idx > q->n_items) + return NULL; + i = q->items + *idx; - assert(i->data == data); + if (i->data != data) + return NULL; return i; } else { diff --git a/src/shared/prioq.h b/src/shared/prioq.h index 1b5b05e7c7..17679b2aaf 100644 --- a/src/shared/prioq.h +++ b/src/shared/prioq.h @@ -27,6 +27,7 @@ typedef struct Prioq Prioq; Prioq *prioq_new(compare_func_t compare); void prioq_free(Prioq *q); +int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func); int prioq_put(Prioq *q, void *data, unsigned *idx); int prioq_remove(Prioq *q, void *data, unsigned *idx); |