1
0
mirror of https://github.com/systemd/systemd.git synced 2025-08-31 09:49:54 +03:00

prioq: use GREEDY_REALLOC() and structured initializer

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2024-10-07 19:26:55 +09:00
parent bf34d642bc
commit 858eac353b

View File

@ -24,8 +24,7 @@ struct prioq_item {
struct Prioq { struct Prioq {
compare_func_t compare_func; compare_func_t compare_func;
unsigned n_items, n_allocated; unsigned n_items;
struct prioq_item *items; struct prioq_item *items;
}; };
@ -142,28 +141,18 @@ static unsigned shuffle_down(Prioq *q, unsigned idx) {
} }
int prioq_put(Prioq *q, void *data, unsigned *idx) { int prioq_put(Prioq *q, void *data, unsigned *idx) {
struct prioq_item *i;
unsigned k; unsigned k;
assert(q); assert(q);
if (q->n_items >= q->n_allocated) { if (!GREEDY_REALLOC(q->items, MAX(q->n_items + 1, 16u)))
unsigned n; return -ENOMEM;
struct prioq_item *j;
n = MAX((q->n_items+1) * 2, 16u);
j = reallocarray(q->items, n, sizeof(struct prioq_item));
if (!j)
return -ENOMEM;
q->items = j;
q->n_allocated = n;
}
k = q->n_items++; k = q->n_items++;
i = q->items + k; q->items[k] = (struct prioq_item) {
i->data = data; .data = data,
i->idx = idx; .idx = idx,
};
if (idx) if (idx)
*idx = k; *idx = k;