xref: /titanic_50/usr/src/lib/libast/common/string/fmtbuf.c (revision 02b4e56ca3a4e4a4fe9e52fca9c2972101f0e57f)
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
23 
24 #include <ast.h>
25 
26 /*
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)
31  */
32 
33 static char		buf[16 * 1024];
34 static char*		nxt = buf;
35 static int		lck = -1;
36 
37 static char*		big;
38 static size_t		bigsiz;
39 
40 char*
41 fmtbuf(size_t n)
42 {
43 	register char*	cur;
44 
45 	while (++lck)
46 		lck--;
47 	if (n > (&buf[elementsof(buf)] - nxt))
48 	{
49 		if (n > elementsof(buf))
50 		{
51 			if (n > bigsiz)
52 			{
53 				bigsiz = roundof(n, 8 * 1024);
54 				if (!(big = newof(big, char, bigsiz, 0)))
55 				{
56 					lck--;
57 					return 0;
58 				}
59 			}
60 			lck--;
61 			return big;
62 		}
63 		nxt = buf;
64 	}
65 	cur = nxt;
66 	nxt += n;
67 	lck--;
68 	return cur;
69 }
70