Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/net Replace malloc/free with kmem_* in if_bridge
details: https://anonhg.NetBSD.org/src/rev/02a902e2f161
branches: trunk
changeset: 335078:02a902e2f161
user: ozaki-r <ozaki-r%NetBSD.org@localhost>
date: Wed Dec 24 08:55:09 2014 +0000
description:
Replace malloc/free with kmem_* in if_bridge
Additionally M_NOWAIT is replaced with KM_SLEEP.
diffstat:
sys/net/if_bridge.c | 59 +++++++++++++++++++++++++++++++++-------------------
1 files changed, 37 insertions(+), 22 deletions(-)
diffs (172 lines):
diff -r 2db69b1696f3 -r 02a902e2f161 sys/net/if_bridge.c
--- a/sys/net/if_bridge.c Wed Dec 24 04:03:02 2014 +0000
+++ b/sys/net/if_bridge.c Wed Dec 24 08:55:09 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: if_bridge.c,v 1.92 2014/12/22 09:42:45 ozaki-r Exp $ */
+/* $NetBSD: if_bridge.c,v 1.93 2014/12/24 08:55:09 ozaki-r Exp $ */
/*
* Copyright 2001 Wasabi Systems, Inc.
@@ -80,7 +80,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.92 2014/12/22 09:42:45 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.93 2014/12/24 08:55:09 ozaki-r Exp $");
#ifdef _KERNEL_OPT
#include "opt_bridge_ipf.h"
@@ -101,6 +101,7 @@
#include <sys/cpu.h>
#include <sys/cprng.h>
#include <sys/mutex.h>
+#include <sys/kmem.h>
#include <net/bpf.h>
#include <net/if.h>
@@ -205,7 +206,7 @@
static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
-static int bridge_rtable_init(struct bridge_softc *);
+static void bridge_rtable_init(struct bridge_softc *);
static void bridge_rtable_fini(struct bridge_softc *);
static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
@@ -347,7 +348,7 @@
struct bridge_softc *sc;
struct ifnet *ifp;
- sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
+ sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
ifp = &sc->sc_if;
sc->sc_brtmax = BRIDGE_RTABLE_MAX;
@@ -445,7 +446,7 @@
if (sc->sc_iflist_lock)
mutex_obj_free(sc->sc_iflist_lock);
- free(sc, M_DEVBUF);
+ kmem_free(sc, sizeof(*sc));
return (0);
}
@@ -773,7 +774,7 @@
}
#endif
- free(bif, M_DEVBUF);
+ kmem_free(bif, sizeof(*bif));
}
static int
@@ -804,9 +805,7 @@
if ((ifs->if_flags & IFF_SIMPLEX) == 0)
return EINVAL;
- bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT);
- if (bif == NULL)
- return (ENOMEM);
+ bif = kmem_alloc(sizeof(*bif), KM_SLEEP);
switch (ifs->if_type) {
case IFT_ETHER:
@@ -846,7 +845,7 @@
out:
if (error) {
if (bif != NULL)
- free(bif, M_DEVBUF);
+ kmem_free(bif, sizeof(*bif));
}
return (error);
}
@@ -986,20 +985,40 @@
struct ifbreq *breqs;
int i, count, error = 0;
+retry:
BRIDGE_LOCK(sc);
-
count = 0;
LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
count++;
+ BRIDGE_UNLOCK(sc);
+
+ if (count == 0) {
+ bifc->ifbic_len = 0;
+ return 0;
+ }
if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) {
- BRIDGE_UNLOCK(sc);
/* Tell that a larger buffer is needed */
bifc->ifbic_len = sizeof(*breqs) * count;
return 0;
}
- breqs = malloc(sizeof(*breqs) * count, M_DEVBUF, M_NOWAIT);
+ breqs = kmem_alloc(sizeof(*breqs) * count, KM_SLEEP);
+
+ BRIDGE_LOCK(sc);
+
+ i = 0;
+ LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
+ i++;
+ if (i > count) {
+ /*
+ * The number of members has been increased.
+ * We need more memory!
+ */
+ BRIDGE_UNLOCK(sc);
+ kmem_free(breqs, sizeof(*breqs) * count);
+ goto retry;
+ }
i = 0;
LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
@@ -1025,7 +1044,7 @@
}
bifc->ifbic_len = sizeof(*breqs) * i;
- free(breqs, M_DEVBUF);
+ kmem_free(breqs, sizeof(*breqs) * count);
return error;
}
@@ -2154,15 +2173,13 @@
*
* Initialize the route table for this bridge.
*/
-static int
+static void
bridge_rtable_init(struct bridge_softc *sc)
{
int i;
- sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
- M_DEVBUF, M_NOWAIT);
- if (sc->sc_rthash == NULL)
- return (ENOMEM);
+ sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
+ KM_SLEEP);
for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
LIST_INIT(&sc->sc_rthash[i]);
@@ -2172,8 +2189,6 @@
LIST_INIT(&sc->sc_rtlist);
sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
-
- return (0);
}
/*
@@ -2185,7 +2200,7 @@
bridge_rtable_fini(struct bridge_softc *sc)
{
- free(sc->sc_rthash, M_DEVBUF);
+ kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE);
if (sc->sc_rtlist_lock)
mutex_obj_free(sc->sc_rtlist_lock);
}
Home |
Main Index |
Thread Index |
Old Index