Author: mturk
Date: Mon Aug 10 08:35:24 2009
New Revision: 802694
URL: http://svn.apache.org/viewvc?rev=802694&view=rev
Log:
Add string buffer from FreeBSD kernel
Added:
commons/sandbox/runtime/trunk/src/main/native/include/acr_sbuf.h (with
props)
commons/sandbox/runtime/trunk/src/main/native/shared/sbuf.c (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.in
commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=802694&r1=802693&r2=802694&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Mon Aug 10
08:35:24 2009
@@ -90,6 +90,7 @@
$(SRCDIR)/shared/base64.$(OBJ) \
$(SRCDIR)/shared/md5.$(OBJ) \
$(SRCDIR)/shared/sha.$(OBJ) \
+ $(SRCDIR)/shared/sbuf.$(OBJ) \
$(SRCDIR)/shared/string.$(OBJ) \
$(SRCDIR)/shared/tables.$(OBJ) \
$(SRCDIR)/shared/xdr.$(OBJ) \
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=802694&r1=802693&r2=802694&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Mon Aug 10
08:35:24 2009
@@ -83,6 +83,7 @@
$(SRCDIR)/shared/base64.$(OBJ) \
$(SRCDIR)/shared/md5.$(OBJ) \
$(SRCDIR)/shared/sha.$(OBJ) \
+ $(SRCDIR)/shared/sbuf.$(OBJ) \
$(SRCDIR)/shared/string.$(OBJ) \
$(SRCDIR)/shared/tables.$(OBJ) \
$(SRCDIR)/shared/xdr.$(OBJ) \
Added: commons/sandbox/runtime/trunk/src/main/native/include/acr_sbuf.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_sbuf.h?rev=802694&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_sbuf.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_sbuf.h Mon Aug 10
08:35:24 2009
@@ -0,0 +1,96 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * 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.
+ *
+ */
+
+#ifndef _ACR_SBUF_H
+#define _ACR_SBUF_H
+
+#include "acr.h"
+
+/**
+ * sbuf flags
+ */
+#define ACR_SBUF_FIXEDLEN 0x00000000 /* fixed length buffer (default)
*/
+#define ACR_SBUF_AUTOEXTEND 0x00000001 /* automatically extend buffer
*/
+#define ACR_SBUF_USRFLAGMSK 0x0000ffff /* mask of flags the user may specify
*/
+#define ACR_SBUF_DYNAMIC 0x00010000 /* s_buf must be freed */
+#define ACR_SBUF_FINISHED 0x00020000 /* set by sbuf_finish() */
+#define ACR_SBUF_OVERFLOWED 0x00040000 /* sbuf overflowed */
+#define ACR_SBUF_DYNSTRUCT 0x00080000 /* sbuf must be freed */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct acr_sbuf_t acr_sbuf_t;
+
+/**
+ * Sbuf API functions
+ */
+acr_sbuf_t *acr_sbuf_new(acr_sbuf_t *, char *, size_t, int);
+#define acr_sbuf_new_auto() \
+ acr_sbuf_new(NULL, NULL, 0, ACR_SBUF_AUTOEXTEND)
+void acr_sbuf_clear(acr_sbuf_t *);
+int acr_sbuf_setpos(acr_sbuf_t *, size_t);
+int acr_sbuf_bcat(acr_sbuf_t *, const void *, size_t);
+int acr_sbuf_bcpy(acr_sbuf_t *, const void *, size_t);
+int acr_sbuf_cat(acr_sbuf_t *, const char *);
+int acr_sbuf_cpy(acr_sbuf_t *, const char *);
+int acr_sbuf_printf(acr_sbuf_t *, const char *, ...);
+int acr_sbuf_vprintf(acr_sbuf_t *, const char *, va_list);
+int acr_sbuf_putc(acr_sbuf_t *, int);
+int acr_acr_sbuf_trim(acr_sbuf_t *);
+int acr_sbuf_overflowed(acr_sbuf_t *);
+void acr_sbuf_finish(acr_sbuf_t *);
+char *acr_sbuf_data(acr_sbuf_t *);
+size_t acr_sbuf_len(acr_sbuf_t *);
+int acr_sbuf_done(acr_sbuf_t *);
+void acr_sbuf_delete(acr_sbuf_t *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_SBUF_H */
+
Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr_sbuf.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/native/shared/sbuf.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/sbuf.c?rev=802694&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sbuf.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sbuf.c Mon Aug 10
08:35:24 2009
@@ -0,0 +1,472 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * 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 "acr.h"
+#include "acr_private.h"
+#include "acr_sbuf.h"
+
+/**
+ * Structure definitions
+ */
+struct acr_sbuf_t {
+ char *s_buf; /* storage buffer */
+ size_t s_size; /* size of storage buffer */
+ size_t s_len; /* current length of string */
+ int s_flags; /* flags */
+};
+
+#define KASSERT(e, m)
+
+/*
+ * Predicates
+ */
+#define SBUF_ISDYNAMIC(s) ((s)->s_flags & ACR_SBUF_DYNAMIC)
+#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & ACR_SBUF_DYNSTRUCT)
+#define SBUF_ISFINISHED(s) ((s)->s_flags & ACR_SBUF_FINISHED)
+#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & ACR_SBUF_OVERFLOWED)
+#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 & ACR_SBUF_AUTOEXTEND)
+
+/*
+ * Set / clear flags
+ */
+#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
+#define SBUF_CLRFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
+
+#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
+#define SBUF_MAXEXTENDSIZE 65536 /* Original is PAGESIZE */
+#define SBUF_MAXEXTENDINCR 65536 /* Original is PAGESIZE */
+
+
+#define assert_sbuf_integrity(s) do { } while (0)
+#define assert_sbuf_state(s, i) do { } while (0)
+
+static size_t
+acr_sbuf_extendsize(size_t size)
+{
+ size_t newsize;
+
+ newsize = SBUF_MINEXTENDSIZE;
+ while (newsize < size) {
+ if (newsize < SBUF_MAXEXTENDSIZE)
+ newsize *= 2;
+ else
+ newsize += SBUF_MAXEXTENDINCR;
+ }
+ return newsize;
+}
+
+
+/*
+ * Extend an sbuf.
+ */
+static int
+acr_sbuf_extend(acr_sbuf_t *s, size_t addlen)
+{
+ char *newbuf;
+ size_t newsize;
+
+ if (!SBUF_CANEXTEND(s))
+ return -1;
+
+ newsize = acr_sbuf_extendsize(s->s_size + addlen);
+ newbuf = malloc(newsize);
+ if (newbuf == NULL)
+ return -1;
+ memcpy(newbuf, s->s_buf, s->s_size);
+ if (SBUF_ISDYNAMIC(s))
+ free(s->s_buf);
+ else
+ SBUF_SETFLAG(s, ACR_SBUF_DYNAMIC);
+ s->s_buf = newbuf;
+ s->s_size = newsize;
+
+ return 0;
+}
+
+/*
+ * 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.
+ */
+acr_sbuf_t *
+acr_sbuf_new(acr_sbuf_t *s, char *buf, size_t length, int flags)
+{
+
+ KASSERT(length >= 0,
+ ("attempt to create an sbuf of negative length (%d)", length));
+ KASSERT((flags & ~ACR_SBUF_USRFLAGMSK) == 0,
+ ("%s called with invalid flags", __func__));
+
+ flags &= ACR_SBUF_USRFLAGMSK;
+ if (s == NULL) {
+ s = calloc(1, sizeof(*s));
+ if (s == NULL)
+ return NULL;
+ s->s_flags = flags;
+ SBUF_SETFLAG(s, ACR_SBUF_DYNSTRUCT);
+ } else {
+ memset(s, 0, sizeof(*s));
+ s->s_flags = flags;
+ }
+ s->s_size = length;
+ if (buf) {
+ s->s_buf = buf;
+ return s;
+ }
+ if (flags & ACR_SBUF_AUTOEXTEND)
+ s->s_size = acr_sbuf_extendsize(s->s_size);
+ s->s_buf = malloc(s->s_size);
+ if (s->s_buf == NULL) {
+ if (SBUF_ISDYNSTRUCT(s))
+ free(s);
+ return NULL;
+ }
+ SBUF_SETFLAG(s, ACR_SBUF_DYNAMIC);
+ return s;
+}
+
+/*
+ * Clear an sbuf and reset its position.
+ */
+void
+acr_sbuf_clear(acr_sbuf_t *s)
+{
+
+ assert_sbuf_integrity(s);
+ /* don't care if it's finished or not */
+
+ SBUF_CLRFLAG(s, ACR_SBUF_FINISHED);
+ SBUF_CLRFLAG(s, ACR_SBUF_OVERFLOWED);
+ s->s_len = 0;
+}
+
+/*
+ * Set the sbuf's end position to an arbitrary value.
+ * Effectively truncates the sbuf at the new position.
+ */
+int
+acr_sbuf_setpos(acr_sbuf_t *s, size_t pos)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ KASSERT(pos >= 0,
+ ("attempt to seek to a negative position (%d)", pos));
+ KASSERT(pos < s->s_size,
+ ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
+
+ if (pos < 0 || pos > s->s_len)
+ return -1;
+ s->s_len = pos;
+ return 0;
+}
+
+/*
+ * Append a byte string to an sbuf.
+ */
+int
+acr_sbuf_bcat(acr_sbuf_t *s, const void *buf, size_t len)
+{
+ const char *str = (const char *)buf;
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ if (SBUF_HASOVERFLOWED(s))
+ return -1;
+ for (; len; len--) {
+ if (!SBUF_HASROOM(s) && acr_sbuf_extend(s, len) < 0)
+ break;
+ s->s_buf[s->s_len++] = *str++;
+ }
+ if (len) {
+ SBUF_SETFLAG(s, ACR_SBUF_OVERFLOWED);
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * Copy a byte string into an sbuf.
+ */
+int
+acr_sbuf_bcpy(acr_sbuf_t *s, const void *buf, size_t len)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ acr_sbuf_clear(s);
+ return acr_sbuf_bcat(s, buf, len);
+}
+
+/*
+ * Append a string to an sbuf.
+ */
+int
+acr_sbuf_cat(acr_sbuf_t *s, const char *str)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ if (SBUF_HASOVERFLOWED(s))
+ return -1;
+
+ while (*str) {
+ if (!SBUF_HASROOM(s) && acr_sbuf_extend(s, strlen(str)) < 0)
+ break;
+ s->s_buf[s->s_len++] = *str++;
+ }
+ if (*str) {
+ SBUF_SETFLAG(s, ACR_SBUF_OVERFLOWED);
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * Copy a string into an sbuf.
+ */
+int
+acr_sbuf_cpy(acr_sbuf_t *s, const char *str)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ acr_sbuf_clear(s);
+ return acr_sbuf_cat(s, str);
+}
+
+/*
+ * Format the given argument list and append the resulting string to an sbuf.
+ */
+int
+acr_sbuf_vprintf(acr_sbuf_t *s, const char *fmt, va_list ap)
+{
+ va_list ap_copy;
+ int len;
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ KASSERT(fmt != NULL,
+ ("%s called with a NULL format string", __func__));
+
+ if (SBUF_HASOVERFLOWED(s))
+ return -1;
+
+ do {
+ va_copy(ap_copy, ap);
+ len = vsnprintf((char *)&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
+ fmt, ap_copy);
+ va_end(ap_copy);
+ } while (len > SBUF_FREESPACE(s) &&
+ acr_sbuf_extend(s, (size_t)(len - SBUF_FREESPACE(s))) == 0);
+
+ /*
+ * s->s_len is the length of the string, without the terminating nul.
+ * When updating s->s_len, we must subtract 1 from the length that
+ * we passed into vsnprintf() because that length includes the
+ * terminating nul.
+ *
+ * vsnprintf() returns the amount that would have been copied,
+ * given sufficient space, hence the min() calculation below.
+ */
+ s->s_len += ACR_MIN(len, SBUF_FREESPACE(s));
+ if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
+ SBUF_SETFLAG(s, ACR_SBUF_OVERFLOWED);
+
+ KASSERT(s->s_len < s->s_size,
+ ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
+
+ if (SBUF_HASOVERFLOWED(s))
+ return -1;
+ return 0;
+}
+
+/*
+ * Format the given arguments and append the resulting string to an sbuf.
+ */
+int
+acr_sbuf_printf(acr_sbuf_t *s, const char *fmt, ...)
+{
+ va_list ap;
+ int result;
+
+ va_start(ap, fmt);
+ result = acr_sbuf_vprintf(s, fmt, ap);
+ va_end(ap);
+ return result;
+}
+
+/*
+ * Append a character to an sbuf.
+ */
+int
+acr_sbuf_putc(acr_sbuf_t *s, int c)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ if (SBUF_HASOVERFLOWED(s))
+ return -1;
+ if (!SBUF_HASROOM(s) && acr_sbuf_extend(s, 1) < 0) {
+ SBUF_SETFLAG(s, ACR_SBUF_OVERFLOWED);
+ return -1;
+ }
+ if (c != '\0')
+ s->s_buf[s->s_len++] = (char)(c & 0xFF);
+ return 0;
+}
+
+/*
+ * Trim whitespace characters from end of an sbuf.
+ */
+int
+acr_sbuf_trim(acr_sbuf_t *s)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ if (SBUF_HASOVERFLOWED(s))
+ return -1;
+
+ while (s->s_len && acr_isspace(s->s_buf[s->s_len-1]))
+ --s->s_len;
+
+ return 0;
+}
+
+/*
+ * Check if an sbuf overflowed
+ */
+int
+acr_sbuf_overflowed(acr_sbuf_t *s)
+{
+
+ return SBUF_HASOVERFLOWED(s);
+}
+
+/*
+ * Finish off an sbuf.
+ */
+void
+acr_sbuf_finish(acr_sbuf_t *s)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, 0);
+
+ s->s_buf[s->s_len] = '\0';
+
+ SBUF_CLRFLAG(s, ACR_SBUF_OVERFLOWED);
+ SBUF_SETFLAG(s, ACR_SBUF_FINISHED);
+}
+
+/*
+ * Return a pointer to the sbuf data.
+ */
+char *
+acr_sbuf_data(acr_sbuf_t *s)
+{
+
+ assert_sbuf_integrity(s);
+ assert_sbuf_state(s, ACR_SBUF_FINISHED);
+
+ return s->s_buf;
+}
+
+/*
+ * Return the length of the sbuf data.
+ */
+size_t
+acr_sbuf_len(acr_sbuf_t *s)
+{
+
+ assert_sbuf_integrity(s);
+ /* don't care if it's finished or not */
+
+ if (SBUF_HASOVERFLOWED(s))
+ return 0;
+ return s->s_len;
+}
+
+/*
+ * Clear an sbuf, free its buffer if necessary.
+ */
+void
+acr_sbuf_delete(acr_sbuf_t *s)
+{
+ int isdyn;
+
+ assert_sbuf_integrity(s);
+ /* don't care if it's finished or not */
+
+ if (SBUF_ISDYNAMIC(s))
+ free(s->s_buf);
+ isdyn = SBUF_ISDYNSTRUCT(s);
+ if (isdyn)
+ free(s);
+ else
+ memset(s, 0, sizeof(*s));
+}
+
+/*
+ * Check if an sbuf has been finished.
+ */
+int
+acr_sbuf_done(acr_sbuf_t *s)
+{
+
+ return SBUF_ISFINISHED(s);
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/sbuf.c
------------------------------------------------------------------------------
svn:eol-style = native