summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorClaudio Jeker <claudio@cvs.openbsd.org>2018-11-12 07:45:53 +0000
committerClaudio Jeker <claudio@cvs.openbsd.org>2018-11-12 07:45:53 +0000
commit8870b8042d0ec1fbfa522f4cbdbff49a8e56105d (patch)
treefebee9dabaf503953a9a0be2b723853d1c060e39 /sys
parent850d36efb2ae248d4c7415508dac8f62dcc93a7b (diff)
Introduce m_align() a function that works like M_ALIGN() but works with
all types of mbufs. Also introduce some KASSERT in the m_*space() functions to ensure that no negative number is returned. This also introduces two internal macros M_SIZE() & M_DATABUF() which return the right size and start pointer of the mbuf data area. Use it in a few obvious places to simplify code. OK bluhm@
Diffstat (limited to 'sys')
-rw-r--r--sys/kern/uipc_mbuf.c40
-rw-r--r--sys/sys/mbuf.h9
2 files changed, 29 insertions, 20 deletions
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index 8661509b9d8..22e131c9711 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uipc_mbuf.c,v 1.260 2018/11/09 14:14:31 claudio Exp $ */
+/* $OpenBSD: uipc_mbuf.c,v 1.261 2018/11/12 07:45:52 claudio Exp $ */
/* $NetBSD: uipc_mbuf.c,v 1.15.4.1 1996/06/13 17:11:44 cgd Exp $ */
/*
@@ -150,6 +150,11 @@ struct pool_allocator m_pool_allocator = {
static void (*mextfree_fns[4])(caddr_t, u_int, void *);
static u_int num_extfree_fns;
+#define M_DATABUF(m) ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf : \
+ (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
+#define M_SIZE(m) ((m)->m_flags & M_EXT ? (m)->m_ext.ext_size : \
+ (m)->m_flags & M_PKTHDR ? MHLEN : MLEN)
+
/*
* Initialize the mbuf allocator.
*/
@@ -1262,14 +1267,7 @@ m_zero(struct mbuf *m)
panic("m_zero: M_READONLY");
#endif /* DIAGNOSTIC */
- if (m->m_flags & M_EXT)
- explicit_bzero(m->m_ext.ext_buf, m->m_ext.ext_size);
- else {
- if (m->m_flags & M_PKTHDR)
- explicit_bzero(m->m_pktdat, MHLEN);
- else
- explicit_bzero(m->m_dat, MLEN);
- }
+ explicit_bzero(M_DATABUF(m), M_SIZE(m));
}
/*
@@ -1321,9 +1319,8 @@ m_leadingspace(struct mbuf *m)
{
if (M_READONLY(m))
return 0;
- return (m->m_flags & M_EXT ? m->m_data - m->m_ext.ext_buf :
- m->m_flags & M_PKTHDR ? m->m_data - m->m_pktdat :
- m->m_data - m->m_dat);
+ KASSERT(m->m_data >= M_DATABUF(m));
+ return m->m_data - M_DATABUF(m);
}
/*
@@ -1335,9 +1332,22 @@ m_trailingspace(struct mbuf *m)
{
if (M_READONLY(m))
return 0;
- return (m->m_flags & M_EXT ? m->m_ext.ext_buf +
- m->m_ext.ext_size - (m->m_data + m->m_len) :
- &m->m_dat[MLEN] - (m->m_data + m->m_len));
+ KASSERT(M_DATABUF(m) + M_SIZE(m) >= (m->m_data + m->m_len));
+ return M_DATABUF(m) + M_SIZE(m) - (m->m_data + m->m_len);
+}
+
+/*
+ * Set the m_data pointer of a newly-allocated mbuf to place an object of
+ * the specified size at the end of the mbuf, longword aligned.
+ */
+void
+m_align(struct mbuf *m, int len)
+{
+ KASSERT(len >= 0 && !M_READONLY(m));
+ KASSERT(m->m_data == M_DATABUF(m)); /* newly-allocated check */
+ KASSERT(((len + sizeof(long) - 1) &~ (sizeof(long) - 1)) <= M_SIZE(m));
+
+ m->m_data = M_DATABUF(m) + ((M_SIZE(m) - (len)) &~ (sizeof(long) - 1));
}
/*
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index 64ef9fab147..5135942bbfe 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: mbuf.h,v 1.239 2018/11/09 14:14:32 claudio Exp $ */
+/* $OpenBSD: mbuf.h,v 1.240 2018/11/12 07:45:52 claudio Exp $ */
/* $NetBSD: mbuf.h,v 1.19 1996/02/09 18:25:14 christos Exp $ */
/*
@@ -348,14 +348,12 @@ u_int mextfree_register(void (*)(caddr_t, u_int, void *));
* Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
* an object of the specified size at the end of the mbuf, longword aligned.
*/
-#define M_ALIGN(m, len) \
- (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1)
+#define M_ALIGN(m, len) m_align((m), (len))
/*
* As above, for mbufs allocated with m_gethdr/MGETHDR
* or initialized by M_MOVE_PKTHDR.
*/
-#define MH_ALIGN(m, len) \
- (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1)
+#define MH_ALIGN(m, len) m_align((m), (len))
/*
* Determine if an mbuf's data area is read-only. This is true for
@@ -441,6 +439,7 @@ struct mbuf *m_makespace(struct mbuf *, int, int, int *);
struct mbuf *m_getptr(struct mbuf *, int, int *);
int m_leadingspace(struct mbuf *);
int m_trailingspace(struct mbuf *);
+void m_align(struct mbuf *, int);
struct mbuf *m_clget(struct mbuf *, int, u_int);
void m_extref(struct mbuf *, struct mbuf *);
void m_pool_init(struct pool *, u_int, u_int, const char *);