aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Cerutti <gahr@FreeBSD.org>2014-07-10 13:08:51 +0000
committerPietro Cerutti <gahr@FreeBSD.org>2014-07-10 13:08:51 +0000
commit7150b86bfe2474cebd10c863c1c57f089684f8bc (patch)
tree0c4dc8a9d1f44507f1648cc9f8160425257f7eeb
parent4472d6e1df6bdf98c5bf3f692296e6b2de5cbd55 (diff)
downloadsrc-7150b86bfe2474cebd10c863c1c57f089684f8bc.tar.gz
src-7150b86bfe2474cebd10c863c1c57f089684f8bc.zip
Implement Short/Small String Optimization in SBUF(9) and change lengths and
positions in the API from ssize_t and int to size_t. CR: D388 Approved by: des, bapt
Notes
Notes: svn path=/head/; revision=268494
-rw-r--r--lib/libsbuf/Makefile2
-rw-r--r--sys/kern/subr_sbuf.c23
-rw-r--r--sys/sys/sbuf.h9
3 files changed, 23 insertions, 11 deletions
diff --git a/lib/libsbuf/Makefile b/lib/libsbuf/Makefile
index 79d3fe973e05..42d4a4e4674c 100644
--- a/lib/libsbuf/Makefile
+++ b/lib/libsbuf/Makefile
@@ -4,7 +4,7 @@ LIB= sbuf
SHLIBDIR?= /lib
SRCS= subr_sbuf.c
-SHLIB_MAJOR = 6
+SHLIB_MAJOR = 7
SYMBOL_MAPS= ${.CURDIR}/Symbol.map
VERSION_DEF= ${.CURDIR}/Version.def
diff --git a/sys/kern/subr_sbuf.c b/sys/kern/subr_sbuf.c
index 1490bc6ebd44..a0f3b7e71f9d 100644
--- a/sys/kern/subr_sbuf.c
+++ b/sys/kern/subr_sbuf.c
@@ -152,11 +152,16 @@ static int
sbuf_extend(struct sbuf *s, int addlen)
{
char *newbuf;
- int newsize;
+ size_t newsize;
if (!SBUF_CANEXTEND(s))
return (-1);
newsize = sbuf_extendsize(s->s_size + addlen);
+ if (s->s_buf == s->s_static_buf && newsize <= sizeof(s->s_static_buf)) {
+ s->s_size = sizeof(s->s_static_buf);
+ return (0);
+ }
+
newbuf = SBMALLOC(newsize);
if (newbuf == NULL)
return (-1);
@@ -176,7 +181,7 @@ sbuf_extend(struct sbuf *s, int addlen)
* big enough to hold at least length characters.
*/
static struct sbuf *
-sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags)
+sbuf_newbuf(struct sbuf *s, char *buf, size_t length, int flags)
{
memset(s, 0, sizeof(*s));
@@ -195,6 +200,11 @@ sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags)
if ((flags & SBUF_AUTOEXTEND) != 0)
s->s_size = sbuf_extendsize(s->s_size);
+ if (s->s_size <= sizeof(s->s_static_buf)) {
+ s->s_buf = s->s_static_buf;
+ return (s);
+ }
+
s->s_buf = SBMALLOC(s->s_size);
if (s->s_buf == NULL)
return (NULL);
@@ -283,21 +293,19 @@ sbuf_clear(struct sbuf *s)
* Effectively truncates the sbuf at the new position.
*/
int
-sbuf_setpos(struct sbuf *s, ssize_t pos)
+sbuf_setpos(struct sbuf *s, size_t pos)
{
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- KASSERT(pos >= 0,
- ("attempt to seek to a negative position (%jd)", (intmax_t)pos));
KASSERT(pos < s->s_size,
("attempt to seek past end of sbuf (%jd >= %jd)",
(intmax_t)pos, (intmax_t)s->s_size));
KASSERT(!SBUF_ISSECTION(s),
("attempt to seek when in a section"));
- if (pos < 0 || pos > s->s_len)
+ if (pos > s->s_len)
return (-1);
s->s_len = pos;
return (0);
@@ -561,7 +569,8 @@ int
sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
{
va_list ap_copy;
- int error, len;
+ size_t len;
+ int error;
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
diff --git a/sys/sys/sbuf.h b/sys/sys/sbuf.h
index 9816a4cd6c54..9c794c830543 100644
--- a/sys/sys/sbuf.h
+++ b/sys/sys/sbuf.h
@@ -44,8 +44,8 @@ struct sbuf {
sbuf_drain_func *s_drain_func; /* drain function */
void *s_drain_arg; /* user-supplied drain argument */
int s_error; /* current error code */
- ssize_t s_size; /* size of storage buffer */
- ssize_t s_len; /* current length of string */
+ size_t s_size; /* size of storage buffer */
+ size_t s_len; /* current length of string */
#define SBUF_FIXEDLEN 0x00000000 /* fixed length buffer (default) */
#define SBUF_AUTOEXTEND 0x00000001 /* automatically extend buffer */
#define SBUF_USRFLAGMSK 0x0000ffff /* mask of flags the user may specify */
@@ -55,6 +55,9 @@ struct sbuf {
#define SBUF_INSECTION 0x00100000 /* set by sbuf_start_section() */
int s_flags; /* flags */
ssize_t s_sect_len; /* current length of section */
+#define SBUF_STATIC_LEN 64 /* static storage buffer length */
+ char s_static_buf[SBUF_STATIC_LEN];
+ /* static storage buffer */
};
__BEGIN_DECLS
@@ -65,7 +68,7 @@ struct sbuf *sbuf_new(struct sbuf *, char *, int, int);
#define sbuf_new_auto() \
sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND)
void sbuf_clear(struct sbuf *);
-int sbuf_setpos(struct sbuf *, ssize_t);
+int sbuf_setpos(struct sbuf *, size_t);
int sbuf_bcat(struct sbuf *, const void *, size_t);
int sbuf_bcpy(struct sbuf *, const void *, size_t);
int sbuf_cat(struct sbuf *, const char *);