memalloc.h revision 217209
128263Spst/*-
228263Spst * Copyright (c) 1991, 1993
350472Speter *	The Regents of the University of California.  All rights reserved.
428263Spst *
561410Sbrian * This code is derived from software contributed to Berkeley by
661981Sbrian * Kenneth Almquist.
728263Spst *
828263Spst * Redistribution and use in source and binary forms, with or without
961410Sbrian * modification, are permitted provided that the following conditions
1061410Sbrian * are met:
1161981Sbrian * 1. Redistributions of source code must retain the above copyright
1261981Sbrian *    notice, this list of conditions and the following disclaimer.
1361981Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1461981Sbrian *    notice, this list of conditions and the following disclaimer in the
1528263Spst *    documentation and/or other materials provided with the distribution.
1628263Spst * 4. Neither the name of the University nor the names of its contributors
1761981Sbrian *    may be used to endorse or promote products derived from this software
1861410Sbrian *    without specific prior written permission.
1965843Sbrian *
2061458Sbrian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2165843Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2265843Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2365843Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2465843Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2561458Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2661458Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2761410Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2861458Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2961981Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30104574Sjoerg * SUCH DAMAGE.
31126342Sache *
32126342Sache *	@(#)memalloc.h	8.2 (Berkeley) 5/4/95
3361981Sbrian * $FreeBSD: head/bin/sh/memalloc.h 217209 2011-01-09 22:47:58Z jilles $
3461458Sbrian */
35126342Sache
36126342Sache#include <string.h>
37126342Sache
3861981Sbrianstruct stackmark {
3961458Sbrian	struct stack_block *stackp;
4061458Sbrian	char *stacknxt;
4161458Sbrian	int stacknleft;
4261458Sbrian        struct stackmark *marknext;
4361458Sbrian};
4461410Sbrian
4565843Sbrian
4665843Sbrianextern char *stacknxt;
4765843Sbrianextern int stacknleft;
48272430Sbdreweryextern char *sstrend;
49272430Sbdrewery
5065843Sbrianpointer ckmalloc(size_t);
5165843Sbrianpointer ckrealloc(pointer, int);
5265843Sbrianvoid ckfree(pointer);
5365843Sbrianchar *savestr(const char *);
5461458Sbrianpointer stalloc(int);
5561458Sbrianvoid stunalloc(pointer);
5665843Sbrianvoid setstackmark(struct stackmark *);
5765843Sbrianvoid popstackmark(struct stackmark *);
5861410Sbrianchar *growstackstr(void);
5965843Sbrianchar *makestrspace(int, char *);
6065843Sbrianchar *stputbin(const char *data, int len, char *p);
61char *stputs(const char *data, char *p);
62
63
64
65#define stackblock() stacknxt
66#define stackblocksize() stacknleft
67#define grabstackblock(n) stalloc(n)
68#define STARTSTACKSTR(p)	p = stackblock()
69#define STPUTC(c, p)	do { if (p == sstrend) p = growstackstr(); *p++ = (c); } while(0)
70#define CHECKSTRSPACE(n, p)	{ if (sstrend - p < n) p = makestrspace(n, p); }
71#define USTPUTC(c, p)	(*p++ = (c))
72/*
73 * STACKSTRNUL's use is where we want to be able to turn a stack
74 * (non-sentinel, character counting string) into a C string,
75 * and later pretend the NUL is not there.
76 * Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used
77 * on a stack that will grabstackstr()ed.
78 */
79#define STACKSTRNUL(p)	(p == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0'))
80#define STUNPUTC(p)	(--p)
81#define STTOPC(p)	p[-1]
82#define STADJUST(amount, p)	(p += (amount))
83#define grabstackstr(p)	stalloc((char *)p - stackblock())
84#define ungrabstackstr(s, p)	stunalloc((s))
85#define STPUTBIN(s, len, p)	p = stputbin((s), (len), p)
86#define STPUTS(s, p)	p = stputs((s), p)
87