import less(1)
[unleashed/tickless.git] / usr / src / lib / libast / common / string / fmtbuf.c
blob7b7e21e27c7ae60e115096b061bc0d040f7486d1
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 #include <ast.h>
27 * return small format buffer chunk of size n
28 * spin lock for thread access
29 * format buffers are short lived
30 * only one concurrent buffer with size > sizeof(buf)
33 static char buf[16 * 1024];
34 static char* nxt = buf;
35 static int lck = -1;
37 static char* big;
38 static size_t bigsiz;
40 char*
41 fmtbuf(size_t n)
43 register char* cur;
45 while (++lck)
46 lck--;
47 if (n > (&buf[elementsof(buf)] - nxt))
49 if (n > elementsof(buf))
51 if (n > bigsiz)
53 bigsiz = roundof(n, 8 * 1024);
54 if (!(big = newof(big, char, bigsiz, 0)))
56 lck--;
57 return 0;
60 lck--;
61 return big;
63 nxt = buf;
65 cur = nxt;
66 nxt += n;
67 lck--;
68 return cur;