git: 45356a1864c7 - main - netlink: simplify temporary address allocation in rtnl_handle_getlink().
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 17 Feb 2023 18:16:46 UTC
The branch main has been updated by melifaro:
URL: https://cgit.FreeBSD.org/src/commit/?id=45356a1864c79680c6911b48a18b14a88a7d07fa
commit 45356a1864c79680c6911b48a18b14a88a7d07fa
Author: Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-02-17 17:57:44 +0000
Commit: Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-02-17 18:16:07 +0000
netlink: simplify temporary address allocation in rtnl_handle_getlink().
MFC after: 3 days
---
sys/netlink/route/iface.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/sys/netlink/route/iface.c b/sys/netlink/route/iface.c
index 0eafacff4775..96f21a79a369 100644
--- a/sys/netlink/route/iface.c
+++ b/sys/netlink/route/iface.c
@@ -439,9 +439,8 @@ rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *n
NL_LOG(LOG_DEBUG2, "Start dump");
- struct ifnet **match_array;
- int offset = 0, base_count = 16; /* start with 128 bytes */
- match_array = malloc(base_count * sizeof(void *), M_TEMP, M_NOWAIT);
+ struct ifnet **match_array = NULL;
+ int offset = 0, base_count = 0;
NLP_LOG(LOG_DEBUG3, nlp, "MATCHING: index=%u type=%d name=%s",
attrs.ifi_index, attrs.ifi_type, attrs.ifla_ifname);
@@ -452,14 +451,14 @@ rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *n
if (offset >= base_count) {
/* Too many matches, need to reallocate */
struct ifnet **new_array;
- int sz = base_count * sizeof(void *);
- base_count *= 2;
- new_array = malloc(sz * 2, M_TEMP, M_NOWAIT);
+ /* Start with 128 bytes, do 2x increase on each realloc */
+ base_count = (base_count != 0) ? base_count * 2 : 16;
+ new_array = malloc(base_count * sizeof(void *), M_TEMP, M_NOWAIT);
if (new_array == NULL) {
error = ENOMEM;
break;
}
- memcpy(new_array, match_array, sz);
+ memcpy(new_array, match_array, offset * sizeof(void *));
free(match_array, M_TEMP);
match_array = new_array;
}