summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsam <sam@FreeBSD.org>2004-12-08 05:42:02 +0000
committersam <sam@FreeBSD.org>2004-12-08 05:42:02 +0000
commitbc6df700aa4860ce07ae94d1afda16fa44002a88 (patch)
tree803a4f23b459429421a13a67fef98793e4278908
parent051e994615fd7f1d1c92a0c735c4ae9684dec488 (diff)
downloadFreeBSD-src-bc6df700aa4860ce07ae94d1afda16fa44002a88.zip
FreeBSD-src-bc6df700aa4860ce07ae94d1afda16fa44002a88.tar.gz
add m_append utility function to be used in forthcoming changes
-rw-r--r--sys/kern/uipc_mbuf.c46
-rw-r--r--sys/sys/mbuf.h1
2 files changed, 47 insertions, 0 deletions
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index 1c2e410..15b0299 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -934,6 +934,52 @@ out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
}
/*
+ * Append the specified data to the indicated mbuf chain,
+ * Extend the mbuf chain if the new data does not fit in
+ * existing space.
+ *
+ * Return 1 if able to complete the job; otherwise 0.
+ */
+int
+m_append(struct mbuf *m0, int len, c_caddr_t cp)
+{
+ struct mbuf *m, *n;
+ int remainder, space;
+
+ for (m = m0; m->m_next != NULL; m = m->m_next)
+ ;
+ remainder = len;
+ space = M_TRAILINGSPACE(m);
+ if (space > 0) {
+ /*
+ * Copy into available space.
+ */
+ if (space > remainder)
+ space = remainder;
+ bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
+ m->m_len += space;
+ cp += space, remainder -= space;
+ }
+ while (remainder > 0) {
+ /*
+ * Allocate a new mbuf; could check space
+ * and allocate a cluster instead.
+ */
+ n = m_get(M_DONTWAIT, m->m_type);
+ if (n == NULL)
+ break;
+ n->m_len = min(MLEN, remainder);
+ bcopy(cp, mtod(m, caddr_t), m->m_len);
+ cp += m->m_len, remainder -= m->m_len;
+ m->m_next = n;
+ m = n;
+ }
+ if (m0->m_flags & M_PKTHDR)
+ m0->m_pkthdr.len += len - remainder;
+ return (remainder == 0);
+}
+
+/*
* Apply function f to the data in an mbuf chain starting "off" bytes from
* the beginning, continuing for "len" bytes.
*/
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index cd3d0b7..b7d8ce1 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -535,6 +535,7 @@ struct uio;
void m_adj(struct mbuf *, int);
int m_apply(struct mbuf *, int, int,
int (*)(void *, void *, u_int), void *);
+int m_append(struct mbuf *, int, c_caddr_t);
void m_cat(struct mbuf *, struct mbuf *);
void m_extadd(struct mbuf *, caddr_t, u_int,
void (*)(void *, void *), void *, int, int);
OpenPOWER on IntegriCloud