2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/kern/subr_sbuf.c,v 1.11.2.2 2002/03/12 01:01:07 archie Exp $
29 * $DragonFly: src/sys/kern/subr_sbuf.c,v 1.6 2005/02/17 13:59:36 joerg Exp $
32 #include <sys/param.h>
35 #include <sys/ctype.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
40 #include <machine/stdarg.h>
52 MALLOC_DEFINE(M_SBUF
, "sbuf", "string buffers");
53 #define SBMALLOC(size) malloc(size, M_SBUF, M_WAITOK)
54 #define SBFREE(buf) free(buf, M_SBUF)
57 #define SBMALLOC(size) malloc(size)
58 #define SBFREE(buf) free(buf)
64 #define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC)
65 #define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT)
66 #define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED)
67 #define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED)
68 #define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
69 #define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
70 #define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
75 #define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
76 #define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
78 #define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
79 #define SBUF_MAXEXTENDSIZE PAGE_SIZE
80 #define SBUF_MAXEXTENDINCR PAGE_SIZE
85 #if defined(_KERNEL) && defined(INVARIANTS)
87 _assert_sbuf_integrity(const char *fun
, struct sbuf
*s
)
90 ("%s called with a NULL sbuf pointer", fun
));
91 KASSERT(s
->s_buf
!= NULL
,
92 ("%s called with uninitialized or corrupt sbuf", fun
));
93 KASSERT(s
->s_len
< s
->s_size
,
94 ("wrote past end of sbuf (%d >= %d)", s
->s_len
, s
->s_size
));
98 _assert_sbuf_state(const char *fun
, struct sbuf
*s
, int state
)
100 KASSERT((s
->s_flags
& SBUF_FINISHED
) == state
,
101 ("%s called with %sfinished or corrupt sbuf", fun
,
102 (state
? "un" : "")));
104 #define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
105 #define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
106 #else /* _KERNEL && INVARIANTS */
107 #define assert_sbuf_integrity(s) do { } while (0)
108 #define assert_sbuf_state(s, i) do { } while (0)
109 #endif /* _KERNEL && INVARIANTS */
112 sbuf_extendsize(int size
)
116 newsize
= SBUF_MINEXTENDSIZE
;
117 while (newsize
< size
) {
118 if (newsize
< SBUF_MAXEXTENDSIZE
)
121 newsize
+= SBUF_MAXEXTENDINCR
;
132 sbuf_extend(struct sbuf
*s
, int addlen
)
137 if (!SBUF_CANEXTEND(s
))
140 newsize
= sbuf_extendsize(s
->s_size
+ addlen
);
141 newbuf
= (char *)SBMALLOC(newsize
);
144 bcopy(s
->s_buf
, newbuf
, s
->s_size
);
145 if (SBUF_ISDYNAMIC(s
))
148 SBUF_SETFLAG(s
, SBUF_DYNAMIC
);
155 * Initialize an sbuf.
156 * If buf is non-NULL, it points to a static or already-allocated string
157 * big enough to hold at least length characters.
160 sbuf_new(struct sbuf
*s
, char *buf
, int length
, int flags
)
163 ("attempt to create an sbuf of negative length (%d)", length
));
164 KASSERT((flags
& ~SBUF_USRFLAGMSK
) == 0,
165 ("%s called with invalid flags", __func__
));
167 flags
&= SBUF_USRFLAGMSK
;
169 s
= (struct sbuf
*)SBMALLOC(sizeof *s
);
174 SBUF_SETFLAG(s
, SBUF_DYNSTRUCT
);
184 if (flags
& SBUF_AUTOEXTEND
)
185 s
->s_size
= sbuf_extendsize(s
->s_size
);
186 s
->s_buf
= (char *)SBMALLOC(s
->s_size
);
187 if (s
->s_buf
== NULL
) {
188 if (SBUF_ISDYNSTRUCT(s
))
192 SBUF_SETFLAG(s
, SBUF_DYNAMIC
);
198 * Create an sbuf with uio data
201 sbuf_uionew(struct sbuf
*s
, struct uio
*uio
, int *error
)
204 ("%s called with NULL uio pointer", __func__
));
205 KASSERT(error
!= NULL
,
206 ("%s called with NULL error pointer", __func__
));
208 s
= sbuf_new(s
, NULL
, uio
->uio_resid
+ 1, 0);
213 *error
= uiomove(s
->s_buf
, uio
->uio_resid
, uio
);
218 s
->s_len
= s
->s_size
- 1;
225 * Clear an sbuf and reset its position.
228 sbuf_clear(struct sbuf
*s
)
230 assert_sbuf_integrity(s
);
231 /* don't care if it's finished or not */
233 SBUF_CLEARFLAG(s
, SBUF_FINISHED
);
234 SBUF_CLEARFLAG(s
, SBUF_OVERFLOWED
);
239 * Set the sbuf's end position to an arbitrary value.
240 * Effectively truncates the sbuf at the new position.
243 sbuf_setpos(struct sbuf
*s
, int pos
)
245 assert_sbuf_integrity(s
);
246 assert_sbuf_state(s
, 0);
249 ("attempt to seek to a negative position (%d)", pos
));
250 KASSERT(pos
< s
->s_size
,
251 ("attempt to seek past end of sbuf (%d >= %d)", pos
, s
->s_size
));
253 if (pos
< 0 || pos
> s
->s_len
)
260 * Append a byte string to an sbuf.
263 sbuf_bcat(struct sbuf
*s
, const char *str
, size_t len
)
265 assert_sbuf_integrity(s
);
266 assert_sbuf_state(s
, 0);
268 if (SBUF_HASOVERFLOWED(s
))
272 if (!SBUF_HASROOM(s
) && sbuf_extend(s
, len
) < 0)
274 s
->s_buf
[s
->s_len
++] = *str
++;
277 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
285 * Copy a byte string from userland into an sbuf.
288 sbuf_bcopyin(struct sbuf
*s
, const void *uaddr
, size_t len
)
290 assert_sbuf_integrity(s
);
291 assert_sbuf_state(s
, 0);
293 if (SBUF_HASOVERFLOWED(s
))
298 if (len
> SBUF_FREESPACE(s
)) {
299 sbuf_extend(s
, len
- SBUF_FREESPACE(s
));
300 len
= MIN(len
, SBUF_FREESPACE(s
));
302 if (copyin(uaddr
, s
->s_buf
+ s
->s_len
, len
) != 0)
311 * Copy a byte string into an sbuf.
314 sbuf_bcpy(struct sbuf
*s
, const char *str
, size_t len
)
316 assert_sbuf_integrity(s
);
317 assert_sbuf_state(s
, 0);
320 return (sbuf_bcat(s
, str
, len
));
324 * Append a string to an sbuf.
327 sbuf_cat(struct sbuf
*s
, const char *str
)
329 assert_sbuf_integrity(s
);
330 assert_sbuf_state(s
, 0);
332 if (SBUF_HASOVERFLOWED(s
))
336 if (!SBUF_HASROOM(s
) && sbuf_extend(s
, strlen(str
)) < 0)
338 s
->s_buf
[s
->s_len
++] = *str
++;
341 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
349 * Append a string from userland to an sbuf.
352 sbuf_copyin(struct sbuf
*s
, const void *uaddr
, size_t len
)
356 assert_sbuf_integrity(s
);
357 assert_sbuf_state(s
, 0);
359 if (SBUF_HASOVERFLOWED(s
))
363 len
= SBUF_FREESPACE(s
); /* XXX return 0? */
364 if (len
> SBUF_FREESPACE(s
)) {
366 len
= MIN(len
, SBUF_FREESPACE(s
));
368 switch (copyinstr(uaddr
, s
->s_buf
+ s
->s_len
, len
+ 1, &done
)) {
370 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
373 s
->s_len
+= done
- 1;
376 return (-1); /* XXX */
384 * Copy a string into an sbuf.
387 sbuf_cpy(struct sbuf
*s
, const char *str
)
389 assert_sbuf_integrity(s
);
390 assert_sbuf_state(s
, 0);
393 return (sbuf_cat(s
, str
));
397 * Format the given argument list and append the resulting string to an sbuf.
400 sbuf_vprintf(struct sbuf
*s
, const char *fmt
, __va_list ap
)
404 assert_sbuf_integrity(s
);
405 assert_sbuf_state(s
, 0);
408 ("%s called with a NULL format string", __func__
));
410 if (SBUF_HASOVERFLOWED(s
))
414 len
= vsnprintf(&s
->s_buf
[s
->s_len
], SBUF_FREESPACE(s
) + 1,
416 } while (len
> SBUF_FREESPACE(s
) &&
417 sbuf_extend(s
, len
- SBUF_FREESPACE(s
)) == 0);
420 * s->s_len is the length of the string, without the terminating nul.
421 * When updating s->s_len, we must subtract 1 from the length that
422 * we passed into vsnprintf() because that length includes the
425 * vsnprintf() returns the amount that would have been copied,
426 * given sufficient space, hence the min() calculation below.
428 s
->s_len
+= MIN(len
, SBUF_FREESPACE(s
));
429 if (!SBUF_HASROOM(s
) && !SBUF_CANEXTEND(s
))
430 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
432 KASSERT(s
->s_len
< s
->s_size
,
433 ("wrote past end of sbuf (%d >= %d)", s
->s_len
, s
->s_size
));
435 if (SBUF_HASOVERFLOWED(s
))
441 * Format the given arguments and append the resulting string to an sbuf.
444 sbuf_printf(struct sbuf
*s
, const char *fmt
, ...)
450 result
= sbuf_vprintf(s
, fmt
, ap
);
456 * Append a character to an sbuf.
459 sbuf_putc(struct sbuf
*s
, int c
)
461 assert_sbuf_integrity(s
);
462 assert_sbuf_state(s
, 0);
464 if (SBUF_HASOVERFLOWED(s
))
467 if (!SBUF_HASROOM(s
) && sbuf_extend(s
, 1) < 0) {
468 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
472 s
->s_buf
[s
->s_len
++] = c
;
477 * Trim whitespace characters from end of an sbuf.
480 sbuf_trim(struct sbuf
*s
)
482 assert_sbuf_integrity(s
);
483 assert_sbuf_state(s
, 0);
485 if (SBUF_HASOVERFLOWED(s
))
488 while (s
->s_len
&& isspace(s
->s_buf
[s
->s_len
-1]))
495 * Check if an sbuf overflowed
498 sbuf_overflowed(struct sbuf
*s
)
500 return SBUF_HASOVERFLOWED(s
);
504 * Finish off an sbuf.
507 sbuf_finish(struct sbuf
*s
)
509 assert_sbuf_integrity(s
);
510 assert_sbuf_state(s
, 0);
512 s
->s_buf
[s
->s_len
] = '\0';
513 SBUF_CLEARFLAG(s
, SBUF_OVERFLOWED
);
514 SBUF_SETFLAG(s
, SBUF_FINISHED
);
518 * Return a pointer to the sbuf data.
521 sbuf_data(struct sbuf
*s
)
523 assert_sbuf_integrity(s
);
524 assert_sbuf_state(s
, SBUF_FINISHED
);
530 * Return the length of the sbuf data.
533 sbuf_len(struct sbuf
*s
)
535 assert_sbuf_integrity(s
);
536 /* don't care if it's finished or not */
538 if (SBUF_HASOVERFLOWED(s
))
544 * Clear an sbuf, free its buffer if necessary.
547 sbuf_delete(struct sbuf
*s
)
551 assert_sbuf_integrity(s
);
552 /* don't care if it's finished or not */
554 if (SBUF_ISDYNAMIC(s
))
556 isdyn
= SBUF_ISDYNSTRUCT(s
);