memalloc.h revision 193221
1139749Simp/*-
2113584Ssimokawa * Copyright (c) 1991, 1993
3103285Sikob *	The Regents of the University of California.  All rights reserved.
4272214Skan *
5103285Sikob * This code is derived from software contributed to Berkeley by
6103285Sikob * Kenneth Almquist.
7103285Sikob *
8103285Sikob * Redistribution and use in source and binary forms, with or without
9103285Sikob * modification, are permitted provided that the following conditions
10103285Sikob * are met:
11103285Sikob * 1. Redistributions of source code must retain the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer.
13103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
14103285Sikob *    notice, this list of conditions and the following disclaimer in the
15103285Sikob *    documentation and/or other materials provided with the distribution.
16103285Sikob * 4. Neither the name of the University nor the names of its contributors
17103285Sikob *    may be used to endorse or promote products derived from this software
18103285Sikob *    without specific prior written permission.
19103285Sikob *
20103285Sikob * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21272214Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22103285Sikob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23103285Sikob * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24103285Sikob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25103285Sikob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26103285Sikob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28103285Sikob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29103285Sikob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30103285Sikob * SUCH DAMAGE.
31103285Sikob *
32103285Sikob *	@(#)memalloc.h	8.2 (Berkeley) 5/4/95
33272214Skan * $FreeBSD: head/bin/sh/memalloc.h 193221 2009-06-01 10:50:17Z rse $
34103285Sikob */
35103285Sikob
36103285Sikob#include <string.h>
37150968Sglebius
38150968Sglebiusstruct stackmark {
39103285Sikob	struct stack_block *stackp;
40150968Sglebius	char *stacknxt;
41103285Sikob	int stacknleft;
42103285Sikob        struct stackmark *marknext;
43103285Sikob};
44103285Sikob
45103285Sikob
46103285Sikobextern char *stacknxt;
47103285Sikobextern int stacknleft;
48103285Sikobextern int sstrnleft;
49103285Sikobextern int herefd;
50103285Sikob
51103285Sikobpointer ckmalloc(size_t);
52113584Ssimokawapointer ckrealloc(pointer, int);
53103285Sikobvoid ckfree(pointer);
54103285Sikobchar *savestr(char *);
55103285Sikobpointer stalloc(int);
56103285Sikobvoid stunalloc(pointer);
57257176Sglebiusvoid setstackmark(struct stackmark *);
58103285Sikobvoid popstackmark(struct stackmark *);
59147256Sbrooksvoid growstackblock(void);
60103285Sikobvoid grabstackblock(int);
61103285Sikobchar *growstackstr(void);
62103285Sikobchar *makestrspace(void);
63103285Sikobvoid ungrabstackstr(char *, char *);
64103285Sikob
65103285Sikob
66122161Ssimokawa
67111942Ssimokawa#define stackblock() stacknxt
68103285Sikob#define stackblocksize() stacknleft
69103285Sikob#define STARTSTACKSTR(p)	p = stackblock(), sstrnleft = stackblocksize()
70124169Ssimokawa#define STPUTC(c, p)	(--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c)))
71124169Ssimokawa#define CHECKSTRSPACE(n, p)	{ if (sstrnleft < n) p = makestrspace(); }
72124169Ssimokawa#define USTPUTC(c, p)	(--sstrnleft, *p++ = (c))
73103285Sikob#define STACKSTRNUL(p)	(sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0'))
74124169Ssimokawa#define STUNPUTC(p)	(++sstrnleft, --p)
75124169Ssimokawa#define STTOPC(p)	p[-1]
76124169Ssimokawa#define STADJUST(amount, p)	(p += (amount), sstrnleft -= (amount))
77103285Sikob#define grabstackstr(p)	stalloc(stackblocksize() - sstrnleft)
78103285Sikob