Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/phil-wifi]: src/sys Add sbuf files from FreeBSD to support net80211.



details:   https://anonhg.NetBSD.org/src/rev/a6098c561cc6
branches:  phil-wifi
changeset: 320571:a6098c561cc6
user:      phil <phil%NetBSD.org@localhost>
date:      Thu Jul 12 16:02:50 2018 +0000

description:
Add sbuf files from FreeBSD to support net80211.
Files from same version as net80211 files.

diffstat:

 sys/kern/subr_sbuf.c |  884 +++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/sys/sbuf.h       |  116 ++++++
 2 files changed, 1000 insertions(+), 0 deletions(-)

diffs (truncated from 1008 to 300 lines):

diff -r 554e816542d1 -r a6098c561cc6 sys/kern/subr_sbuf.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/kern/subr_sbuf.c      Thu Jul 12 16:02:50 2018 +0000
@@ -0,0 +1,884 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2000-2008 Poul-Henning Kamp
+ * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#ifdef _KERNEL
+#include <sys/ctype.h>
+#include <sys/errno.h>
+#include <sys/kernel.h>
+#include <sys/limits.h>
+#include <sys/malloc.h>
+#include <sys/systm.h>
+#include <sys/uio.h>
+#include <machine/stdarg.h>
+#else /* _KERNEL */
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#endif /* _KERNEL */
+
+#include <sys/sbuf.h>
+
+#ifdef _KERNEL
+static MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
+#define        SBMALLOC(size)          malloc(size, M_SBUF, M_WAITOK|M_ZERO)
+#define        SBFREE(buf)             free(buf, M_SBUF)
+#else /* _KERNEL */
+#define        KASSERT(e, m)
+#define        SBMALLOC(size)          calloc(1, size)
+#define        SBFREE(buf)             free(buf)
+#endif /* _KERNEL */
+
+/*
+ * Predicates
+ */
+#define        SBUF_ISDYNAMIC(s)       ((s)->s_flags & SBUF_DYNAMIC)
+#define        SBUF_ISDYNSTRUCT(s)     ((s)->s_flags & SBUF_DYNSTRUCT)
+#define        SBUF_ISFINISHED(s)      ((s)->s_flags & SBUF_FINISHED)
+#define        SBUF_HASROOM(s)         ((s)->s_len < (s)->s_size - 1)
+#define        SBUF_FREESPACE(s)       ((s)->s_size - ((s)->s_len + 1))
+#define        SBUF_CANEXTEND(s)       ((s)->s_flags & SBUF_AUTOEXTEND)
+#define        SBUF_ISSECTION(s)       ((s)->s_flags & SBUF_INSECTION)
+#define        SBUF_NULINCLUDED(s)     ((s)->s_flags & SBUF_INCLUDENUL)
+#define        SBUF_ISDRAINTOEOR(s)    ((s)->s_flags & SBUF_DRAINTOEOR)
+#define        SBUF_DODRAINTOEOR(s)    (SBUF_ISSECTION(s) && SBUF_ISDRAINTOEOR(s))
+
+/*
+ * Set / clear flags
+ */
+#define        SBUF_SETFLAG(s, f)      do { (s)->s_flags |= (f); } while (0)
+#define        SBUF_CLEARFLAG(s, f)    do { (s)->s_flags &= ~(f); } while (0)
+
+#define        SBUF_MINSIZE             2              /* Min is 1 byte + nulterm. */
+#define        SBUF_MINEXTENDSIZE      16              /* Should be power of 2. */
+
+#ifdef PAGE_SIZE
+#define        SBUF_MAXEXTENDSIZE      PAGE_SIZE
+#define        SBUF_MAXEXTENDINCR      PAGE_SIZE
+#else
+#define        SBUF_MAXEXTENDSIZE      4096
+#define        SBUF_MAXEXTENDINCR      4096
+#endif
+
+/*
+ * Debugging support
+ */
+#if defined(_KERNEL) && defined(INVARIANTS)
+
+static void
+_assert_sbuf_integrity(const char *fun, struct sbuf *s)
+{
+
+       KASSERT(s != NULL,
+           ("%s called with a NULL sbuf pointer", fun));
+       KASSERT(s->s_buf != NULL,
+           ("%s called with uninitialized or corrupt sbuf", fun));
+       if (SBUF_ISFINISHED(s) && SBUF_NULINCLUDED(s)) {
+               KASSERT(s->s_len <= s->s_size,
+                   ("wrote past end of sbuf (%jd >= %jd)",
+                   (intmax_t)s->s_len, (intmax_t)s->s_size));
+       } else {
+               KASSERT(s->s_len < s->s_size,
+                   ("wrote past end of sbuf (%jd >= %jd)",
+                   (intmax_t)s->s_len, (intmax_t)s->s_size));
+       }
+}
+
+static void
+_assert_sbuf_state(const char *fun, struct sbuf *s, int state)
+{
+
+       KASSERT((s->s_flags & SBUF_FINISHED) == state,
+           ("%s called with %sfinished or corrupt sbuf", fun,
+           (state ? "un" : "")));
+}
+
+#define        assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
+#define        assert_sbuf_state(s, i)  _assert_sbuf_state(__func__, (s), (i))
+
+#else /* _KERNEL && INVARIANTS */
+
+#define        assert_sbuf_integrity(s) do { } while (0)
+#define        assert_sbuf_state(s, i)  do { } while (0)
+
+#endif /* _KERNEL && INVARIANTS */
+
+#ifdef CTASSERT
+CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
+CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
+#endif
+
+static int
+sbuf_extendsize(int size)
+{
+       int newsize;
+
+       if (size < (int)SBUF_MAXEXTENDSIZE) {
+               newsize = SBUF_MINEXTENDSIZE;
+               while (newsize < size)
+                       newsize *= 2;
+       } else {
+               newsize = roundup2(size, SBUF_MAXEXTENDINCR);
+       }
+       KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
+       return (newsize);
+}
+
+/*
+ * Extend an sbuf.
+ */
+static int
+sbuf_extend(struct sbuf *s, int addlen)
+{
+       char *newbuf;
+       int newsize;
+
+       if (!SBUF_CANEXTEND(s))
+               return (-1);
+       newsize = sbuf_extendsize(s->s_size + addlen);
+       newbuf = SBMALLOC(newsize);
+       if (newbuf == NULL)
+               return (-1);
+       memcpy(newbuf, s->s_buf, s->s_size);
+       if (SBUF_ISDYNAMIC(s))
+               SBFREE(s->s_buf);
+       else
+               SBUF_SETFLAG(s, SBUF_DYNAMIC);
+       s->s_buf = newbuf;
+       s->s_size = newsize;
+       return (0);
+}
+
+/*
+ * Initialize the internals of an sbuf.
+ * If buf is non-NULL, it points to a static or already-allocated string
+ * big enough to hold at least length characters.
+ */
+static struct sbuf *
+sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags)
+{
+
+       memset(s, 0, sizeof(*s));
+       s->s_flags = flags;
+       s->s_size = length;
+       s->s_buf = buf;
+
+       if ((s->s_flags & SBUF_AUTOEXTEND) == 0) {
+               KASSERT(s->s_size >= SBUF_MINSIZE,
+                   ("attempt to create an sbuf smaller than %d bytes",
+                   SBUF_MINSIZE));
+       }
+
+       if (s->s_buf != NULL)
+               return (s);
+
+       if ((flags & SBUF_AUTOEXTEND) != 0)
+               s->s_size = sbuf_extendsize(s->s_size);
+
+       s->s_buf = SBMALLOC(s->s_size);
+       if (s->s_buf == NULL)
+               return (NULL);
+       SBUF_SETFLAG(s, SBUF_DYNAMIC);
+       return (s);
+}
+
+/*
+ * Initialize an sbuf.
+ * If buf is non-NULL, it points to a static or already-allocated string
+ * big enough to hold at least length characters.
+ */
+struct sbuf *
+sbuf_new(struct sbuf *s, char *buf, int length, int flags)
+{
+
+       KASSERT(length >= 0,
+           ("attempt to create an sbuf of negative length (%d)", length));
+       KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
+           ("%s called with invalid flags", __func__));
+
+       flags &= SBUF_USRFLAGMSK;
+       if (s != NULL)
+               return (sbuf_newbuf(s, buf, length, flags));
+
+       s = SBMALLOC(sizeof(*s));
+       if (s == NULL)
+               return (NULL);
+       if (sbuf_newbuf(s, buf, length, flags) == NULL) {
+               SBFREE(s);
+               return (NULL);
+       }
+       SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
+       return (s);
+}
+
+#ifdef _KERNEL
+/*
+ * Create an sbuf with uio data
+ */
+struct sbuf *
+sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
+{
+
+       KASSERT(uio != NULL,
+           ("%s called with NULL uio pointer", __func__));
+       KASSERT(error != NULL,
+           ("%s called with NULL error pointer", __func__));
+
+       s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
+       if (s == NULL) {
+               *error = ENOMEM;
+               return (NULL);
+       }
+       *error = uiomove(s->s_buf, uio->uio_resid, uio);
+       if (*error != 0) {
+               sbuf_delete(s);
+               return (NULL);
+       }
+       s->s_len = s->s_size - 1;
+       if (SBUF_ISSECTION(s))
+               s->s_sect_len = s->s_size - 1;
+       *error = 0;
+       return (s);
+}
+#endif
+
+int
+sbuf_get_flags(struct sbuf *s)
+{
+
+       return (s->s_flags & SBUF_USRFLAGMSK);
+}
+
+void
+sbuf_clear_flags(struct sbuf *s, int flags)
+{
+
+       s->s_flags &= ~(flags & SBUF_USRFLAGMSK);
+}
+
+void
+sbuf_set_flags(struct sbuf *s, int flags)
+{



Home | Main Index | Thread Index | Old Index