2 * Copyright (c) 1994, David Greenman
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 unmodified, this list of conditions, and the following
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/kern/tty_subr.c,v 1.32 1999/08/28 00:46:21 peter Exp $
31 * clist support routines
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/clist.h>
41 static void clist_init __P((void *));
42 SYSINIT(clist, SI_SUB_CLIST, SI_ORDER_FIRST, clist_init, NULL)
44 static struct cblock *cfreelist = 0;
46 static int cslushcount;
49 #ifndef INITIAL_CBLOCKS
50 #define INITIAL_CBLOCKS 50
53 static struct cblock *cblock_alloc __P((void));
54 static void cblock_alloc_cblocks __P((int number));
55 static void cblock_free __P((struct cblock *cblockp));
56 static void cblock_free_cblocks __P((int number));
62 DB_SHOW_COMMAND(cbstat, cbstat)
67 "tot = %d (active = %d, free = %d (reserved = %d, slush = %d))\n",
68 ctotcount * cbsize, ctotcount * cbsize - cfreecount, cfreecount,
69 cfreecount - cslushcount * cbsize, cslushcount * cbsize);
74 * Called from init_main.c
82 * Allocate an initial base set of cblocks as a 'slush'.
83 * We allocate non-slush cblocks with each initial ttyopen() and
84 * deallocate them with each ttyclose().
85 * We should adjust the slush allocation. This can't be done in
86 * the i/o routines because they are sometimes called from
87 * interrupt handlers when it may be unsafe to call malloc().
89 cblock_alloc_cblocks(cslushcount = INITIAL_CBLOCKS);
93 * Remove a cblock from the cfreelist queue and return a pointer
96 static __inline struct cblock *
99 struct cblock *cblockp;
103 panic("clist reservation botch");
104 cfreelist = cblockp->c_next;
105 cblockp->c_next = NULL;
106 cfreecount -= CBSIZE;
111 * Add a cblock to the cfreelist queue.
115 struct cblock *cblockp;
117 if (isset(cblockp->c_quote, CBQSIZE * NBBY - 1))
118 bzero(cblockp->c_quote, sizeof cblockp->c_quote);
119 cblockp->c_next = cfreelist;
121 cfreecount += CBSIZE;
125 * Allocate some cblocks for the cfreelist queue.
128 cblock_alloc_cblocks(number)
134 for (i = 0; i < number; ++i) {
135 cbp = malloc(sizeof *cbp, M_TTYS, M_NOWAIT);
138 "clist_alloc_cblocks: M_NOWAIT malloc failed, trying M_WAITOK\n");
139 cbp = malloc(sizeof *cbp, M_TTYS, M_WAITOK);
142 * Freed cblocks have zero quotes and garbage elsewhere.
143 * Set the may-have-quote bit to force zeroing the quotes.
145 setbit(cbp->c_quote, CBQSIZE * NBBY - 1);
152 * Set the cblock allocation policy for a a clist.
153 * Must be called in process context at spltty().
156 clist_alloc_cblocks(clistp, ccmax, ccreserved)
157 struct clist *clistp;
164 * Allow for wasted space at the head.
169 ccreserved += CBSIZE - 1;
171 clistp->c_cbmax = roundup(ccmax, CBSIZE) / CBSIZE;
172 dcbr = roundup(ccreserved, CBSIZE) / CBSIZE - clistp->c_cbreserved;
174 cblock_alloc_cblocks(dcbr);
176 if (clistp->c_cbreserved + dcbr < clistp->c_cbcount)
177 dcbr = clistp->c_cbcount - clistp->c_cbreserved;
178 cblock_free_cblocks(-dcbr);
180 clistp->c_cbreserved += dcbr;
184 * Free some cblocks from the cfreelist queue back to the
185 * system malloc pool.
188 cblock_free_cblocks(number)
193 for (i = 0; i < number; ++i)
194 free(cblock_alloc(), M_TTYS);
199 * Free the cblocks reserved for a clist.
200 * Must be called at spltty().
203 clist_free_cblocks(clistp)
204 struct clist *clistp;
206 if (clistp->c_cbcount != 0)
207 panic("freeing active clist cblocks");
208 cblock_free_cblocks(clistp->c_cbreserved);
210 clistp->c_cbreserved = 0;
214 * Get a character from the head of a clist.
218 struct clist *clistp;
222 struct cblock *cblockp;
226 /* If there are characters in the list, get one */
228 cblockp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
229 chr = (u_char)*clistp->c_cf;
232 * If this char is quoted, set the flag.
234 if (isset(cblockp->c_quote, clistp->c_cf - (char *)cblockp->c_info))
238 * Advance to next character.
243 * If we have advanced the 'first' character pointer
244 * past the end of this cblock, advance to the next one.
245 * If there are no more characters, set the first and
246 * last pointers to NULL. In either case, free the
249 if ((clistp->c_cf >= (char *)(cblockp+1)) || (clistp->c_cc == 0)) {
250 if (clistp->c_cc > 0) {
251 clistp->c_cf = cblockp->c_next->c_info;
253 clistp->c_cf = clistp->c_cl = NULL;
255 cblock_free(cblockp);
256 if (--clistp->c_cbcount >= clistp->c_cbreserved)
266 * Copy 'amount' of chars, beginning at head of clist 'clistp' to
267 * destination linear buffer 'dest'. Return number of characters
271 q_to_b(clistp, dest, amount)
272 struct clist *clistp;
276 struct cblock *cblockp;
277 struct cblock *cblockn;
278 char *dest_orig = dest;
284 while (clistp && amount && (clistp->c_cc > 0)) {
285 cblockp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
286 cblockn = cblockp + 1; /* pointer arithmetic! */
287 numc = min(amount, (char *)cblockn - clistp->c_cf);
288 numc = min(numc, clistp->c_cc);
289 bcopy(clistp->c_cf, dest, numc);
291 clistp->c_cf += numc;
292 clistp->c_cc -= numc;
295 * If this cblock has been emptied, advance to the next
296 * one. If there are no more characters, set the first
297 * and last pointer to NULL. In either case, free the
300 if ((clistp->c_cf >= (char *)cblockn) || (clistp->c_cc == 0)) {
301 if (clistp->c_cc > 0) {
302 clistp->c_cf = cblockp->c_next->c_info;
304 clistp->c_cf = clistp->c_cl = NULL;
306 cblock_free(cblockp);
307 if (--clistp->c_cbcount >= clistp->c_cbreserved)
313 return (dest - dest_orig);
317 * Flush 'amount' of chars, beginning at head of clist 'clistp'.
320 ndflush(clistp, amount)
321 struct clist *clistp;
324 struct cblock *cblockp;
325 struct cblock *cblockn;
331 while (amount && (clistp->c_cc > 0)) {
332 cblockp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
333 cblockn = cblockp + 1; /* pointer arithmetic! */
334 numc = min(amount, (char *)cblockn - clistp->c_cf);
335 numc = min(numc, clistp->c_cc);
337 clistp->c_cf += numc;
338 clistp->c_cc -= numc;
340 * If this cblock has been emptied, advance to the next
341 * one. If there are no more characters, set the first
342 * and last pointer to NULL. In either case, free the
345 if ((clistp->c_cf >= (char *)cblockn) || (clistp->c_cc == 0)) {
346 if (clistp->c_cc > 0) {
347 clistp->c_cf = cblockp->c_next->c_info;
349 clistp->c_cf = clistp->c_cl = NULL;
351 cblock_free(cblockp);
352 if (--clistp->c_cbcount >= clistp->c_cbreserved)
361 * Add a character to the end of a clist. Return -1 is no
362 * more clists, or 0 for success.
367 struct clist *clistp;
369 struct cblock *cblockp;
374 if (clistp->c_cl == NULL) {
375 if (clistp->c_cbreserved < 1) {
377 printf("putc to a clist with no reserved cblocks\n");
378 return (-1); /* nothing done */
380 cblockp = cblock_alloc();
381 clistp->c_cbcount = 1;
382 clistp->c_cf = clistp->c_cl = cblockp->c_info;
385 cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
386 if (((intptr_t)clistp->c_cl & CROUND) == 0) {
387 struct cblock *prev = (cblockp - 1);
389 if (clistp->c_cbcount >= clistp->c_cbreserved) {
390 if (clistp->c_cbcount >= clistp->c_cbmax
391 || cslushcount <= 0) {
397 cblockp = cblock_alloc();
399 prev->c_next = cblockp;
400 clistp->c_cl = cblockp->c_info;
405 * If this character is quoted, set the quote bit, if not, clear it.
407 if (chr & TTY_QUOTE) {
408 setbit(cblockp->c_quote, clistp->c_cl - (char *)cblockp->c_info);
410 * Use one of the spare quote bits to record that something
413 setbit(cblockp->c_quote, CBQSIZE * NBBY - 1);
415 clrbit(cblockp->c_quote, clistp->c_cl - (char *)cblockp->c_info);
417 *clistp->c_cl++ = chr;
425 * Copy data from linear buffer to clist chain. Return the
426 * number of characters not copied.
429 b_to_q(src, amount, clistp)
432 struct clist *clistp;
434 struct cblock *cblockp;
435 char *firstbyte, *lastbyte;
436 u_char startmask, endmask;
437 int startbit, endbit, num_between, numc;
441 * Avoid allocating an initial cblock and then not using it.
442 * c_cc == 0 must imply c_cbount == 0.
450 * If there are no cblocks assigned to this clist yet,
453 if (clistp->c_cl == NULL) {
454 if (clistp->c_cbreserved < 1) {
456 printf("b_to_q to a clist with no reserved cblocks.\n");
457 return (amount); /* nothing done */
459 cblockp = cblock_alloc();
460 clistp->c_cbcount = 1;
461 clistp->c_cf = clistp->c_cl = cblockp->c_info;
464 cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
469 * Get another cblock if needed.
471 if (((intptr_t)clistp->c_cl & CROUND) == 0) {
472 struct cblock *prev = cblockp - 1;
474 if (clistp->c_cbcount >= clistp->c_cbreserved) {
475 if (clistp->c_cbcount >= clistp->c_cbmax
476 || cslushcount <= 0) {
482 cblockp = cblock_alloc();
484 prev->c_next = cblockp;
485 clistp->c_cl = cblockp->c_info;
489 * Copy a chunk of the linear buffer up to the end
492 numc = min(amount, (char *)(cblockp + 1) - clistp->c_cl);
493 bcopy(src, clistp->c_cl, numc);
496 * Clear quote bits if they aren't known to be clear.
497 * The following could probably be made into a seperate
498 * "bitzero()" routine, but why bother?
500 if (isset(cblockp->c_quote, CBQSIZE * NBBY - 1)) {
501 startbit = clistp->c_cl - (char *)cblockp->c_info;
502 endbit = startbit + numc - 1;
504 firstbyte = (u_char *)cblockp->c_quote + (startbit / NBBY);
505 lastbyte = (u_char *)cblockp->c_quote + (endbit / NBBY);
508 * Calculate mask of bits to preserve in first and
511 startmask = NBBY - (startbit % NBBY);
512 startmask = 0xff >> startmask;
513 endmask = (endbit % NBBY);
514 endmask = 0xff << (endmask + 1);
516 if (firstbyte != lastbyte) {
517 *firstbyte &= startmask;
518 *lastbyte &= endmask;
520 num_between = lastbyte - firstbyte - 1;
522 bzero(firstbyte + 1, num_between);
524 *firstbyte &= (startmask | endmask);
529 * ...and update pointer for the next chunk.
532 clistp->c_cl += numc;
533 clistp->c_cc += numc;
536 * If we go through the loop again, it's always
537 * for data in the next cblock, so by adding one (cblock),
538 * (which makes the pointer 1 beyond the end of this
539 * cblock) we prepare for the assignment of 'prev'
551 * Get the next character in the clist. Store it at dst. Don't
552 * advance any clist pointers, but return a pointer to the next
553 * character position.
556 nextc(clistp, cp, dst)
557 struct clist *clistp;
561 struct cblock *cblockp;
565 * See if the next character is beyond the end of
568 if (clistp->c_cc && (cp != clistp->c_cl)) {
570 * If the next character is beyond the end of this
571 * cblock, advance to the next cblock.
573 if (((intptr_t)cp & CROUND) == 0)
574 cp = ((struct cblock *)cp - 1)->c_next->c_info;
575 cblockp = (struct cblock *)((intptr_t)cp & ~CROUND);
578 * Get the character. Set the quote flag if this character
581 *dst = (u_char)*cp | (isset(cblockp->c_quote, cp - (char *)cblockp->c_info) ? TTY_QUOTE : 0);
590 * "Unput" a character from a clist.
594 struct clist *clistp;
596 struct cblock *cblockp = 0, *cbp = 0;
607 chr = (u_char)*clistp->c_cl;
609 cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
612 * Set quote flag if this character was quoted.
614 if (isset(cblockp->c_quote, (u_char *)clistp->c_cl - cblockp->c_info))
618 * If all of the characters have been unput in this
619 * cblock, then find the previous one and free this
622 if (clistp->c_cc && (clistp->c_cl <= (char *)cblockp->c_info)) {
623 cbp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
625 while (cbp->c_next != cblockp)
629 * When the previous cblock is at the end, the 'last'
630 * pointer always points (invalidly) one past.
632 clistp->c_cl = (char *)(cbp+1);
633 cblock_free(cblockp);
634 if (--clistp->c_cbcount >= clistp->c_cbreserved)
641 * If there are no more characters on the list, then
642 * free the last cblock.
644 if ((clistp->c_cc == 0) && clistp->c_cl) {
645 cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
646 cblock_free(cblockp);
647 if (--clistp->c_cbcount >= clistp->c_cbreserved)
649 clistp->c_cf = clistp->c_cl = NULL;
657 * Move characters in source clist to destination clist,
658 * preserving quote bits.
661 catq(src_clistp, dest_clistp)
662 struct clist *src_clistp, *dest_clistp;
668 * If the destination clist is empty (has no cblocks atttached),
669 * and there are no possible complications with the resource counters,
670 * then we simply assign the current clist to the destination.
672 if (!dest_clistp->c_cf
673 && src_clistp->c_cbcount <= src_clistp->c_cbmax
674 && src_clistp->c_cbcount <= dest_clistp->c_cbmax) {
675 dest_clistp->c_cf = src_clistp->c_cf;
676 dest_clistp->c_cl = src_clistp->c_cl;
677 src_clistp->c_cf = src_clistp->c_cl = NULL;
679 dest_clistp->c_cc = src_clistp->c_cc;
680 src_clistp->c_cc = 0;
681 dest_clistp->c_cbcount = src_clistp->c_cbcount;
682 src_clistp->c_cbcount = 0;
691 * XXX This should probably be optimized to more than one
692 * character at a time.
694 while ((chr = getc(src_clistp)) != -1)
695 putc(chr, dest_clistp);