subr_sbuf.c (71724) | subr_sbuf.c (73891) |
---|---|
1/*- 2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 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. 27 * | 1/*- 2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 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. 27 * |
28 * $FreeBSD: head/sys/kern/subr_sbuf.c 71724 2001-01-28 00:33:58Z des $ | 28 * $FreeBSD: head/sys/kern/subr_sbuf.c 73891 2001-03-06 17:48:26Z des $ |
29 */ 30 31#include <sys/param.h> 32#include <sys/kernel.h> 33#include <sys/malloc.h> 34#include <sys/sbuf.h> 35#include <sys/systm.h> 36 --- 15 unchanged lines hidden (view full) --- 52#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) 53#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0) 54 55/* 56 * Debugging support 57 */ 58#ifdef INVARIANTS 59static void | 29 */ 30 31#include <sys/param.h> 32#include <sys/kernel.h> 33#include <sys/malloc.h> 34#include <sys/sbuf.h> 35#include <sys/systm.h> 36 --- 15 unchanged lines hidden (view full) --- 52#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) 53#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0) 54 55/* 56 * Debugging support 57 */ 58#ifdef INVARIANTS 59static void |
60assert_sbuf_integrity(struct sbuf *s) | 60_assert_sbuf_integrity(char *fun, struct sbuf *s) |
61{ 62 KASSERT(s != NULL, | 61{ 62 KASSERT(s != NULL, |
63 (__FUNCTION__ " called with a NULL sbuf pointer")); | 63 ("%s called with a NULL sbuf pointer", fun)); |
64 KASSERT(s->s_buf != NULL, | 64 KASSERT(s->s_buf != NULL, |
65 (__FUNCTION__ " called with unitialized or corrupt sbuf")); | 65 ("%s called with unitialized or corrupt sbuf", fun)); |
66 KASSERT(s->s_len < s->s_size, 67 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 68} 69 70static void | 66 KASSERT(s->s_len < s->s_size, 67 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 68} 69 70static void |
71assert_sbuf_state(struct sbuf *s, int state) | 71_assert_sbuf_state(char *fun, struct sbuf *s, int state) |
72{ 73 KASSERT((s->s_flags & SBUF_FINISHED) == state, | 72{ 73 KASSERT((s->s_flags & SBUF_FINISHED) == state, |
74 (__FUNCTION__ " called with %sfinished or corrupt sbuf", | 74 ("%s called with %sfinished or corrupt sbuf", fun, |
75 (state ? "un" : ""))); 76} | 75 (state ? "un" : ""))); 76} |
77#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__FUNCTION__, (s)) 78#define assert_sbuf_state(s, i) _assert_sbuf_state(__FUNCTION__, (s), (i)) |
|
77#else 78#define assert_sbuf_integrity(s) do { } while (0) 79#define assert_sbuf_state(s, i) do { } while (0) 80#endif 81 82/* 83 * Initialize an sbuf. 84 * If buf is non-NULL, it points to a static or already-allocated string --- 91 unchanged lines hidden (view full) --- 176} 177 178/* 179 * PCHAR function for sbuf_printf() 180 */ 181static void 182_sbuf_pchar(int c, void *v) 183{ | 79#else 80#define assert_sbuf_integrity(s) do { } while (0) 81#define assert_sbuf_state(s, i) do { } while (0) 82#endif 83 84/* 85 * Initialize an sbuf. 86 * If buf is non-NULL, it points to a static or already-allocated string --- 91 unchanged lines hidden (view full) --- 178} 179 180/* 181 * PCHAR function for sbuf_printf() 182 */ 183static void 184_sbuf_pchar(int c, void *v) 185{ |
184 struct sbuf *s = (struct sbuf *)v; 185 186 assert_sbuf_integrity(s); 187 assert_sbuf_state(s, 0); 188 189 if (SBUF_HASOVERFLOWED(s)) 190 return; 191 if (SBUF_HASROOM(s)) 192 s->s_buf[s->s_len++] = c; 193 else 194 SBUF_SETFLAG(s, SBUF_OVERFLOWED); | 186 sbuf_putc((struct sbuf *)v, c); |
195} 196 197/* 198 * Format the given arguments and append the resulting string to an sbuf. 199 */ 200int 201sbuf_printf(struct sbuf *s, char *fmt, ...) 202{ --- 32 unchanged lines hidden (view full) --- 235 236 if (SBUF_HASOVERFLOWED(s)) 237 return (-1); 238 239 if (!SBUF_HASROOM(s)) { 240 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 241 return (-1); 242 } | 187} 188 189/* 190 * Format the given arguments and append the resulting string to an sbuf. 191 */ 192int 193sbuf_printf(struct sbuf *s, char *fmt, ...) 194{ --- 32 unchanged lines hidden (view full) --- 227 228 if (SBUF_HASOVERFLOWED(s)) 229 return (-1); 230 231 if (!SBUF_HASROOM(s)) { 232 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 233 return (-1); 234 } |
243 s->s_buf[s->s_len++] = c; | 235 if (c != '\0') 236 s->s_buf[s->s_len++] = c; |
244 return (0); 245} 246 247/* 248 * Check if an sbuf overflowed 249 */ 250int 251sbuf_overflowed(struct sbuf *s) --- 5 unchanged lines hidden (view full) --- 257 * Finish off an sbuf. 258 */ 259void 260sbuf_finish(struct sbuf *s) 261{ 262 assert_sbuf_integrity(s); 263 assert_sbuf_state(s, 0); 264 | 237 return (0); 238} 239 240/* 241 * Check if an sbuf overflowed 242 */ 243int 244sbuf_overflowed(struct sbuf *s) --- 5 unchanged lines hidden (view full) --- 250 * Finish off an sbuf. 251 */ 252void 253sbuf_finish(struct sbuf *s) 254{ 255 assert_sbuf_integrity(s); 256 assert_sbuf_state(s, 0); 257 |
265 s->s_buf[s->s_len++] = '\0'; | 258 s->s_buf[s->s_len] = '\0'; |
266 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 267 SBUF_SETFLAG(s, SBUF_FINISHED); 268} 269 270/* 271 * Return a pointer to the sbuf data. 272 */ 273char * --- 35 unchanged lines hidden --- | 259 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 260 SBUF_SETFLAG(s, SBUF_FINISHED); 261} 262 263/* 264 * Return a pointer to the sbuf data. 265 */ 266char * --- 35 unchanged lines hidden --- |