diff options
author | bmilekic <bmilekic@FreeBSD.org> | 2001-02-14 05:13:04 +0000 |
---|---|---|
committer | bmilekic <bmilekic@FreeBSD.org> | 2001-02-14 05:13:04 +0000 |
commit | cc2f31e1a4c8a6d2678fbbb8b9705d49fc18de63 (patch) | |
tree | 639d9a4c07772cd49b2cf77c738e8aaf5c15f850 /sys/kern/uipc_mbuf.c | |
parent | 1c970ba5b314cd2286a5a889bb69df2bc58ed73c (diff) | |
download | FreeBSD-src-cc2f31e1a4c8a6d2678fbbb8b9705d49fc18de63.zip FreeBSD-src-cc2f31e1a4c8a6d2678fbbb8b9705d49fc18de63.tar.gz |
Implement m_getm() which will perform an "all or nothing" mbuf + cluster
allocation, as required.
If m_getm() receives NULL as a first argument, then it allocates `len'
(second argument) bytes worth of mbufs + clusters and returns the chain
only if it was able to allocate everything.
If the first argument is non-NULL, then it should be an existing mbuf
chain (e.g. pre-allocated mbuf sitting on a ring, on some list, etc.) and
so it will allocate `len' bytes worth of clusters and mbufs, as needed,
and append them to the tail of the passed in chain, only if it was able
to allocate everything requested.
If allocation fails, only what was allocated by the routine will be freed,
and NULL will be returned.
Also, get rid of existing m_getm() in netncp code and replace calls to it
to calls to this new generic code.
Heavily Reviewed by: bp
Diffstat (limited to 'sys/kern/uipc_mbuf.c')
-rw-r--r-- | sys/kern/uipc_mbuf.c | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c index ff174e3..c8cf25c 100644 --- a/sys/kern/uipc_mbuf.c +++ b/sys/kern/uipc_mbuf.c @@ -463,7 +463,7 @@ m_reclaim(void) /* * Space allocation routines. - * These are also available as macros + * Some of these are also available as macros * for critical paths. */ struct mbuf * @@ -505,6 +505,72 @@ m_free(struct mbuf *m) return (n); } +/* + * struct mbuf * + * m_getm(m, len, how, type) + * + * This will allocate len-worth of mbufs and/or mbuf clusters (whatever fits + * best) and return a pointer to the top of the allocated chain. If m is + * non-null, then we assume that it is a single mbuf or an mbuf chain to + * which we want len bytes worth of mbufs and/or clusters attached, and so + * if we succeed in allocating it, we will just return a pointer to m. + * + * If we happen to fail at any point during the allocation, we will free + * up everything we have already allocated and return NULL. + * + */ +struct mbuf * +m_getm(struct mbuf *m, int len, int how, int type) +{ + struct mbuf *top, *tail, *mp, *mtail = NULL; + + KASSERT(len >= 0, ("len is < 0 in m_getm")); + + MGET(mp, type, how); + if (mp == NULL) + return (NULL); + else if (len > MINCLSIZE) { + MCLGET(mp, how); + if ((mp->m_flags & M_EXT) == 0) { + m_free(mp); + return (NULL); + } + } + mp->m_len = 0; + len -= M_TRAILINGSPACE(mp); + + if (m != NULL) + for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); + else + m = mp; + + top = tail = mp; + while (len > 0) { + MGET(mp, type, how); + if (mp == NULL) + goto failed; + + tail->m_next = mp; + tail = mp; + if (len > MINCLSIZE) { + MCLGET(mp, how); + if ((mp->m_flags & M_EXT) == 0) + goto failed; + } + + mp->m_len = 0; + len -= M_TRAILINGSPACE(mp); + } + + if (mtail != NULL) + mtail->m_next = top; + return (m); + +failed: + m_freem(top); + return (NULL); +} + void m_freem(struct mbuf *m) { |