X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fshared%2Fprioq.c;h=b89888be0e8d5460313ffc3e9712856f214ce5d2;hb=b733fbe7a0214eb43e402db7179697bf9c0975c1;hp=b7d745990c4bcf78236ba44d6ad2495766d7a15d;hpb=e3017af97310da024ffb378ed155bc1676922ce7;p=elogind.git diff --git a/src/shared/prioq.c b/src/shared/prioq.c index b7d745990..b89888be0 100644 --- a/src/shared/prioq.c +++ b/src/shared/prioq.c @@ -45,12 +45,14 @@ Prioq *prioq_new(compare_func_t compare_func) { return q; } -void prioq_free(Prioq *q) { +Prioq* prioq_free(Prioq *q) { if (!q) - return; + return NULL; free(q->items); free(q); + + return NULL; } int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) { @@ -159,7 +161,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) { unsigned n; struct prioq_item *j; - n = MAX((q->n_items+1) * 2, 16); + n = MAX((q->n_items+1) * 2, 16u); j = realloc(q->items, sizeof(struct prioq_item) * n); if (!j) return -ENOMEM; @@ -211,13 +213,14 @@ static void remove_item(Prioq *q, struct prioq_item *i) { } } -static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { +_pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { struct prioq_item *i; assert(q); if (idx) { - if (*idx > q->n_items) + if (*idx == PRIOQ_IDX_NULL || + *idx > q->n_items) return NULL; i = q->items + *idx; @@ -236,7 +239,8 @@ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { int prioq_remove(Prioq *q, void *data, unsigned *idx) { struct prioq_item *i; - assert(q); + if (!q) + return 0; i = find_item(q, data, idx); if (!i) @@ -263,7 +267,9 @@ int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) { } void *prioq_peek(Prioq *q) { - assert(q); + + if (!q) + return NULL; if (q->n_items <= 0) return NULL; @@ -274,7 +280,8 @@ void *prioq_peek(Prioq *q) { void *prioq_pop(Prioq *q) { void *data; - assert(q); + if (!q) + return NULL; if (q->n_items <= 0) return NULL; @@ -285,13 +292,17 @@ void *prioq_pop(Prioq *q) { } unsigned prioq_size(Prioq *q) { - assert(q); - return q->n_items; + if (!q) + return 0; + return q->n_items; } + bool prioq_isempty(Prioq *q) { - assert(q); + + if (!q) + return true; return q->n_items <= 0; }