subr_sbuf.c (74840) | subr_sbuf.c (77989) |
---|---|
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 74840 2001-03-27 05:45:52Z ken $ | 28 * $FreeBSD: head/sys/kern/subr_sbuf.c 77989 2001-06-10 15:48:04Z des $ |
29 */ 30 31#include <sys/param.h> 32#include <sys/sbuf.h> 33 34#ifdef _KERNEL 35#include <sys/kernel.h> 36#include <sys/malloc.h> --- 13 unchanged lines hidden (view full) --- 50#define SBFREE(buf) free(buf) 51#define min(x,y) MIN(x,y) 52#endif /* _KERNEL */ 53 54/* 55 * Predicates 56 */ 57#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC) | 29 */ 30 31#include <sys/param.h> 32#include <sys/sbuf.h> 33 34#ifdef _KERNEL 35#include <sys/kernel.h> 36#include <sys/malloc.h> --- 13 unchanged lines hidden (view full) --- 50#define SBFREE(buf) free(buf) 51#define min(x,y) MIN(x,y) 52#endif /* _KERNEL */ 53 54/* 55 * Predicates 56 */ 57#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC) |
58#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT) |
|
58#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED) 59#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED) 60#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1) 61 62/* 63 * Set / clear flags 64 */ 65#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) --- 28 unchanged lines hidden (view full) --- 94#define assert_sbuf_state(s, i) do { } while (0) 95#endif /* _KERNEL && INVARIANTS */ 96 97/* 98 * Initialize an sbuf. 99 * If buf is non-NULL, it points to a static or already-allocated string 100 * big enough to hold at least length characters. 101 */ | 59#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED) 60#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED) 61#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1) 62 63/* 64 * Set / clear flags 65 */ 66#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) --- 28 unchanged lines hidden (view full) --- 95#define assert_sbuf_state(s, i) do { } while (0) 96#endif /* _KERNEL && INVARIANTS */ 97 98/* 99 * Initialize an sbuf. 100 * If buf is non-NULL, it points to a static or already-allocated string 101 * big enough to hold at least length characters. 102 */ |
102int | 103struct sbuf * |
103sbuf_new(struct sbuf *s, char *buf, int length, int flags) 104{ 105 KASSERT(length >= 0, 106 ("attempt to create an sbuf of negative length (%d)", length)); 107 KASSERT(flags == 0, 108 (__FUNCTION__ " called with non-zero flags")); | 104sbuf_new(struct sbuf *s, char *buf, int length, int flags) 105{ 106 KASSERT(length >= 0, 107 ("attempt to create an sbuf of negative length (%d)", length)); 108 KASSERT(flags == 0, 109 (__FUNCTION__ " called with non-zero flags")); |
109 KASSERT(s != NULL, 110 (__FUNCTION__ " called with a NULL sbuf pointer")); | |
111 | 110 |
112 bzero(s, sizeof *s); | 111 if (s == NULL) { 112 s = (struct sbuf *)SBMALLOC(sizeof *s); 113 if (s == NULL) 114 return (NULL); 115 bzero(s, sizeof *s); 116 SBUF_SETFLAG(s, SBUF_DYNSTRUCT); 117 } else { 118 bzero(s, sizeof *s); 119 } |
113 s->s_size = length; 114 if (buf) { 115 s->s_buf = buf; | 120 s->s_size = length; 121 if (buf) { 122 s->s_buf = buf; |
116 return (0); | 123 return (s); |
117 } 118 s->s_buf = (char *)SBMALLOC(s->s_size); | 124 } 125 s->s_buf = (char *)SBMALLOC(s->s_size); |
119 if (s->s_buf == NULL) 120 return (-1); | 126 if (s->s_buf == NULL) { 127 if (SBUF_ISDYNSTRUCT(s)) 128 SBFREE(s); 129 return (NULL); 130 } |
121 SBUF_SETFLAG(s, SBUF_DYNAMIC); | 131 SBUF_SETFLAG(s, SBUF_DYNAMIC); |
122 return (0); | 132 return (s); |
123} 124 125/* 126 * Clear an sbuf and reset its position 127 */ 128void 129sbuf_clear(struct sbuf *s) 130{ --- 179 unchanged lines hidden (view full) --- 310sbuf_delete(struct sbuf *s) 311{ 312 assert_sbuf_integrity(s); 313 /* don't care if it's finished or not */ 314 315 if (SBUF_ISDYNAMIC(s)) 316 SBFREE(s->s_buf); 317 bzero(s, sizeof *s); | 133} 134 135/* 136 * Clear an sbuf and reset its position 137 */ 138void 139sbuf_clear(struct sbuf *s) 140{ --- 179 unchanged lines hidden (view full) --- 320sbuf_delete(struct sbuf *s) 321{ 322 assert_sbuf_integrity(s); 323 /* don't care if it's finished or not */ 324 325 if (SBUF_ISDYNAMIC(s)) 326 SBFREE(s->s_buf); 327 bzero(s, sizeof *s); |
328 if (SBUF_ISDYNSTRUCT(s)) 329 SBFREE(s); |
|
318} | 330} |