mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-27 23:20:12 +01:00
This is a direct dump from systemd git.
======
SYSTEMD_DIR=../systemd
COMMIT=8eab766804ef4fa21d26c00fd0baab3f1a47bb5c
(
cd "$SYSTEMD_DIR"
git checkout "$COMMIT"
git reset --hard
git clean -fdx
)
git ls-files -z :/src/systemd/src/ \
:/shared/systemd/src/ \
:/shared/nm-utils/unaligned.h | \
xargs -0 rm -f
nm_copy_sd_shared() {
mkdir -p "./shared/systemd/$(dirname "$1")"
cp "$SYSTEMD_DIR/$1" "./shared/systemd/$1"
}
nm_copy_sd_core() {
mkdir -p "./src/systemd/$(dirname "$1")"
cp "$SYSTEMD_DIR/$1" "./src/systemd/$1"
}
nm_copy_sd_nmutils() {
mkdir -p "./shared/nm-utils/"
cp "$SYSTEMD_DIR/$1" "./shared/nm-utils/${1##*/}"
}
nm_copy_sd_core "src/libsystemd-network/arp-util.c"
nm_copy_sd_core "src/libsystemd-network/arp-util.h"
nm_copy_sd_core "src/libsystemd-network/dhcp-identifier.c"
nm_copy_sd_core "src/libsystemd-network/dhcp-identifier.h"
nm_copy_sd_core "src/libsystemd-network/dhcp-internal.h"
nm_copy_sd_core "src/libsystemd-network/dhcp-lease-internal.h"
nm_copy_sd_core "src/libsystemd-network/dhcp-network.c"
nm_copy_sd_core "src/libsystemd-network/dhcp-option.c"
nm_copy_sd_core "src/libsystemd-network/dhcp-packet.c"
nm_copy_sd_core "src/libsystemd-network/dhcp-protocol.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-internal.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-lease-internal.h"
nm_copy_sd_core "src/libsystemd-network/dhcp6-network.c"
nm_copy_sd_core "src/libsystemd-network/dhcp6-option.c"
nm_copy_sd_core "src/libsystemd-network/dhcp6-protocol.h"
nm_copy_sd_core "src/libsystemd-network/lldp-internal.h"
nm_copy_sd_core "src/libsystemd-network/lldp-neighbor.c"
nm_copy_sd_core "src/libsystemd-network/lldp-neighbor.h"
nm_copy_sd_core "src/libsystemd-network/lldp-network.c"
nm_copy_sd_core "src/libsystemd-network/lldp-network.h"
nm_copy_sd_core "src/libsystemd-network/network-internal.c"
nm_copy_sd_core "src/libsystemd-network/network-internal.h"
nm_copy_sd_core "src/libsystemd-network/sd-dhcp-client.c"
nm_copy_sd_core "src/libsystemd-network/sd-dhcp-lease.c"
nm_copy_sd_core "src/libsystemd-network/sd-dhcp6-client.c"
nm_copy_sd_core "src/libsystemd-network/sd-dhcp6-lease.c"
nm_copy_sd_core "src/libsystemd-network/sd-ipv4acd.c"
nm_copy_sd_core "src/libsystemd-network/sd-ipv4ll.c"
nm_copy_sd_core "src/libsystemd-network/sd-lldp.c"
nm_copy_sd_core "src/libsystemd/sd-event/event-source.h"
nm_copy_sd_core "src/libsystemd/sd-event/event-util.c"
nm_copy_sd_core "src/libsystemd/sd-event/event-util.h"
nm_copy_sd_core "src/libsystemd/sd-event/sd-event.c"
nm_copy_sd_core "src/libsystemd/sd-id128/id128-util.c"
nm_copy_sd_core "src/libsystemd/sd-id128/id128-util.h"
nm_copy_sd_core "src/libsystemd/sd-id128/sd-id128.c"
nm_copy_sd_core "src/shared/dns-domain.c"
nm_copy_sd_core "src/shared/dns-domain.h"
nm_copy_sd_core "src/systemd/_sd-common.h"
nm_copy_sd_core "src/systemd/sd-dhcp-client.h"
nm_copy_sd_core "src/systemd/sd-dhcp-lease.h"
nm_copy_sd_core "src/systemd/sd-dhcp6-client.h"
nm_copy_sd_core "src/systemd/sd-dhcp6-lease.h"
nm_copy_sd_core "src/systemd/sd-event.h"
nm_copy_sd_core "src/systemd/sd-id128.h"
nm_copy_sd_core "src/systemd/sd-ipv4acd.h"
nm_copy_sd_core "src/systemd/sd-ipv4ll.h"
nm_copy_sd_core "src/systemd/sd-lldp.h"
nm_copy_sd_core "src/systemd/sd-ndisc.h"
nm_copy_sd_nmutils "src/basic/unaligned.h"
nm_copy_sd_shared "src/basic/alloc-util.c"
nm_copy_sd_shared "src/basic/alloc-util.h"
nm_copy_sd_shared "src/basic/async.h"
nm_copy_sd_shared "src/basic/env-file.c"
nm_copy_sd_shared "src/basic/env-file.h"
nm_copy_sd_shared "src/basic/env-util.c"
nm_copy_sd_shared "src/basic/env-util.h"
nm_copy_sd_shared "src/basic/escape.c"
nm_copy_sd_shared "src/basic/escape.h"
nm_copy_sd_shared "src/basic/ether-addr-util.c"
nm_copy_sd_shared "src/basic/ether-addr-util.h"
nm_copy_sd_shared "src/basic/extract-word.c"
nm_copy_sd_shared "src/basic/extract-word.h"
nm_copy_sd_shared "src/basic/fd-util.c"
nm_copy_sd_shared "src/basic/fd-util.h"
nm_copy_sd_shared "src/basic/fileio.c"
nm_copy_sd_shared "src/basic/fileio.h"
nm_copy_sd_shared "src/basic/fs-util.c"
nm_copy_sd_shared "src/basic/fs-util.h"
nm_copy_sd_shared "src/basic/hash-funcs.c"
nm_copy_sd_shared "src/basic/hash-funcs.h"
nm_copy_sd_shared "src/basic/hashmap.c"
nm_copy_sd_shared "src/basic/hashmap.h"
nm_copy_sd_shared "src/basic/hexdecoct.c"
nm_copy_sd_shared "src/basic/hexdecoct.h"
nm_copy_sd_shared "src/basic/hostname-util.c"
nm_copy_sd_shared "src/basic/hostname-util.h"
nm_copy_sd_shared "src/basic/in-addr-util.c"
nm_copy_sd_shared "src/basic/in-addr-util.h"
nm_copy_sd_shared "src/basic/io-util.c"
nm_copy_sd_shared "src/basic/io-util.h"
nm_copy_sd_shared "src/basic/list.h"
nm_copy_sd_shared "src/basic/log.h"
nm_copy_sd_shared "src/basic/macro.h"
nm_copy_sd_shared "src/basic/mempool.c"
nm_copy_sd_shared "src/basic/mempool.h"
nm_copy_sd_shared "src/basic/missing_type.h"
nm_copy_sd_shared "src/basic/parse-util.c"
nm_copy_sd_shared "src/basic/parse-util.h"
nm_copy_sd_shared "src/basic/path-util.c"
nm_copy_sd_shared "src/basic/path-util.h"
nm_copy_sd_shared "src/basic/prioq.c"
nm_copy_sd_shared "src/basic/prioq.h"
nm_copy_sd_shared "src/basic/process-util.c"
nm_copy_sd_shared "src/basic/process-util.h"
nm_copy_sd_shared "src/basic/random-util.c"
nm_copy_sd_shared "src/basic/random-util.h"
nm_copy_sd_shared "src/basic/refcnt.h"
nm_copy_sd_shared "src/basic/set.h"
nm_copy_sd_shared "src/basic/signal-util.h"
nm_copy_sd_shared "src/basic/siphash24.h"
nm_copy_sd_shared "src/basic/socket-util.c"
nm_copy_sd_shared "src/basic/socket-util.h"
nm_copy_sd_shared "src/basic/sparse-endian.h"
nm_copy_sd_shared "src/basic/stat-util.c"
nm_copy_sd_shared "src/basic/stat-util.h"
nm_copy_sd_shared "src/basic/stdio-util.h"
nm_copy_sd_shared "src/basic/string-table.c"
nm_copy_sd_shared "src/basic/string-table.h"
nm_copy_sd_shared "src/basic/string-util.c"
nm_copy_sd_shared "src/basic/string-util.h"
nm_copy_sd_shared "src/basic/strv.c"
nm_copy_sd_shared "src/basic/strv.h"
nm_copy_sd_shared "src/basic/time-util.c"
nm_copy_sd_shared "src/basic/time-util.h"
nm_copy_sd_shared "src/basic/tmpfile-util.c"
nm_copy_sd_shared "src/basic/tmpfile-util.h"
nm_copy_sd_shared "src/basic/umask-util.h"
nm_copy_sd_shared "src/basic/utf8.c"
nm_copy_sd_shared "src/basic/utf8.h"
nm_copy_sd_shared "src/basic/util.c"
nm_copy_sd_shared "src/basic/util.h"
303 lines
6.6 KiB
C
303 lines
6.6 KiB
C
/* SPDX-License-Identifier: LGPL-2.1+ */
|
|
|
|
/*
|
|
* Priority Queue
|
|
* The prioq object implements a priority queue. That is, it orders objects by
|
|
* their priority and allows O(1) access to the object with the highest
|
|
* priority. Insertion and removal are Θ(log n). Optionally, the caller can
|
|
* provide a pointer to an index which will be kept up-to-date by the prioq.
|
|
*
|
|
* The underlying algorithm used in this implementation is a Heap.
|
|
*/
|
|
|
|
#include "nm-sd-adapt-shared.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "alloc-util.h"
|
|
#include "hashmap.h"
|
|
#include "prioq.h"
|
|
|
|
struct prioq_item {
|
|
void *data;
|
|
unsigned *idx;
|
|
};
|
|
|
|
struct Prioq {
|
|
compare_func_t compare_func;
|
|
unsigned n_items, n_allocated;
|
|
|
|
struct prioq_item *items;
|
|
};
|
|
|
|
Prioq *prioq_new(compare_func_t compare_func) {
|
|
Prioq *q;
|
|
|
|
q = new(Prioq, 1);
|
|
if (!q)
|
|
return q;
|
|
|
|
*q = (Prioq) {
|
|
.compare_func = compare_func,
|
|
};
|
|
|
|
return q;
|
|
}
|
|
|
|
Prioq* prioq_free(Prioq *q) {
|
|
if (!q)
|
|
return NULL;
|
|
|
|
free(q->items);
|
|
return mfree(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) {
|
|
assert(q);
|
|
assert(j < q->n_items);
|
|
assert(k < q->n_items);
|
|
|
|
assert(!q->items[j].idx || *(q->items[j].idx) == j);
|
|
assert(!q->items[k].idx || *(q->items[k].idx) == k);
|
|
|
|
SWAP_TWO(q->items[j].data, q->items[k].data);
|
|
SWAP_TWO(q->items[j].idx, q->items[k].idx);
|
|
|
|
if (q->items[j].idx)
|
|
*q->items[j].idx = j;
|
|
|
|
if (q->items[k].idx)
|
|
*q->items[k].idx = k;
|
|
}
|
|
|
|
static unsigned shuffle_up(Prioq *q, unsigned idx) {
|
|
assert(q);
|
|
assert(idx < q->n_items);
|
|
|
|
while (idx > 0) {
|
|
unsigned k;
|
|
|
|
k = (idx-1)/2;
|
|
|
|
if (q->compare_func(q->items[k].data, q->items[idx].data) <= 0)
|
|
break;
|
|
|
|
swap(q, idx, k);
|
|
idx = k;
|
|
}
|
|
|
|
return idx;
|
|
}
|
|
|
|
static unsigned shuffle_down(Prioq *q, unsigned idx) {
|
|
assert(q);
|
|
|
|
for (;;) {
|
|
unsigned j, k, s;
|
|
|
|
k = (idx+1)*2; /* right child */
|
|
j = k-1; /* left child */
|
|
|
|
if (j >= q->n_items)
|
|
break;
|
|
|
|
if (q->compare_func(q->items[j].data, q->items[idx].data) < 0)
|
|
|
|
/* So our left child is smaller than we are, let's
|
|
* remember this fact */
|
|
s = j;
|
|
else
|
|
s = idx;
|
|
|
|
if (k < q->n_items &&
|
|
q->compare_func(q->items[k].data, q->items[s].data) < 0)
|
|
|
|
/* So our right child is smaller than we are, let's
|
|
* remember this fact */
|
|
s = k;
|
|
|
|
/* s now points to the smallest of the three items */
|
|
|
|
if (s == idx)
|
|
/* No swap necessary, we're done */
|
|
break;
|
|
|
|
swap(q, idx, s);
|
|
idx = s;
|
|
}
|
|
|
|
return idx;
|
|
}
|
|
|
|
int prioq_put(Prioq *q, void *data, unsigned *idx) {
|
|
struct prioq_item *i;
|
|
unsigned k;
|
|
|
|
assert(q);
|
|
|
|
if (q->n_items >= q->n_allocated) {
|
|
unsigned n;
|
|
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++;
|
|
i = q->items + k;
|
|
i->data = data;
|
|
i->idx = idx;
|
|
|
|
if (idx)
|
|
*idx = k;
|
|
|
|
shuffle_up(q, k);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void remove_item(Prioq *q, struct prioq_item *i) {
|
|
struct prioq_item *l;
|
|
|
|
assert(q);
|
|
assert(i);
|
|
|
|
l = q->items + q->n_items - 1;
|
|
|
|
if (i == l)
|
|
/* Last entry, let's just remove it */
|
|
q->n_items--;
|
|
else {
|
|
unsigned k;
|
|
|
|
/* Not last entry, let's replace the last entry with
|
|
* this one, and reshuffle */
|
|
|
|
k = i - q->items;
|
|
|
|
i->data = l->data;
|
|
i->idx = l->idx;
|
|
if (i->idx)
|
|
*i->idx = k;
|
|
q->n_items--;
|
|
|
|
k = shuffle_down(q, k);
|
|
shuffle_up(q, k);
|
|
}
|
|
}
|
|
|
|
_pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) {
|
|
struct prioq_item *i;
|
|
|
|
assert(q);
|
|
|
|
if (q->n_items <= 0)
|
|
return NULL;
|
|
|
|
if (idx) {
|
|
if (*idx == PRIOQ_IDX_NULL ||
|
|
*idx >= q->n_items)
|
|
return NULL;
|
|
|
|
i = q->items + *idx;
|
|
if (i->data != data)
|
|
return NULL;
|
|
|
|
return i;
|
|
} else {
|
|
for (i = q->items; i < q->items + q->n_items; i++)
|
|
if (i->data == data)
|
|
return i;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
int prioq_remove(Prioq *q, void *data, unsigned *idx) {
|
|
struct prioq_item *i;
|
|
|
|
if (!q)
|
|
return 0;
|
|
|
|
i = find_item(q, data, idx);
|
|
if (!i)
|
|
return 0;
|
|
|
|
remove_item(q, i);
|
|
return 1;
|
|
}
|
|
|
|
int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
|
|
struct prioq_item *i;
|
|
unsigned k;
|
|
|
|
assert(q);
|
|
|
|
i = find_item(q, data, idx);
|
|
if (!i)
|
|
return 0;
|
|
|
|
k = i - q->items;
|
|
k = shuffle_down(q, k);
|
|
shuffle_up(q, k);
|
|
return 1;
|
|
}
|
|
|
|
void *prioq_peek(Prioq *q) {
|
|
|
|
if (!q)
|
|
return NULL;
|
|
|
|
if (q->n_items <= 0)
|
|
return NULL;
|
|
|
|
return q->items[0].data;
|
|
}
|
|
|
|
void *prioq_pop(Prioq *q) {
|
|
void *data;
|
|
|
|
if (!q)
|
|
return NULL;
|
|
|
|
if (q->n_items <= 0)
|
|
return NULL;
|
|
|
|
data = q->items[0].data;
|
|
remove_item(q, q->items);
|
|
return data;
|
|
}
|
|
|
|
unsigned prioq_size(Prioq *q) {
|
|
|
|
if (!q)
|
|
return 0;
|
|
|
|
return q->n_items;
|
|
}
|
|
|
|
bool prioq_isempty(Prioq *q) {
|
|
|
|
if (!q)
|
|
return true;
|
|
|
|
return q->n_items <= 0;
|
|
}
|