memalloc.h revision 217209
123353Sdfr/*-
223353Sdfr * Copyright (c) 1991, 1993
323353Sdfr *	The Regents of the University of California.  All rights reserved.
423353Sdfr *
523353Sdfr * This code is derived from software contributed to Berkeley by
623353Sdfr * Kenneth Almquist.
723353Sdfr *
823353Sdfr * Redistribution and use in source and binary forms, with or without
923353Sdfr * modification, are permitted provided that the following conditions
1023353Sdfr * are met:
1123353Sdfr * 1. Redistributions of source code must retain the above copyright
1223353Sdfr *    notice, this list of conditions and the following disclaimer.
1323353Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1423353Sdfr *    notice, this list of conditions and the following disclaimer in the
1523353Sdfr *    documentation and/or other materials provided with the distribution.
1623353Sdfr * 4. Neither the name of the University nor the names of its contributors
1723353Sdfr *    may be used to endorse or promote products derived from this software
1823353Sdfr *    without specific prior written permission.
1923353Sdfr *
2023353Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2123353Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2223353Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2323353Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2423353Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2523353Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2623353Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2723353Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2823353Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2950476Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3023353Sdfr * SUCH DAMAGE.
31192691Stmclaugh *
32206622Suqs *	@(#)memalloc.h	8.2 (Berkeley) 5/4/95
3323353Sdfr * $FreeBSD: head/bin/sh/memalloc.h 217209 2011-01-09 22:47:58Z jilles $
3423353Sdfr */
3523353Sdfr
3623353Sdfr#include <string.h>
3723353Sdfr
3884306Srustruct stackmark {
3984306Sru	struct stack_block *stackp;
4084306Sru	char *stacknxt;
4123353Sdfr	int stacknleft;
42192691Stmclaugh        struct stackmark *marknext;
4323353Sdfr};
44139858Skeramida
45139858Skeramida
46139858Skeramidaextern char *stacknxt;
47139858Skeramidaextern int stacknleft;
4823353Sdfrextern char *sstrend;
49139858Skeramida
50139858Skeramidapointer ckmalloc(size_t);
51115440Shmppointer ckrealloc(pointer, int);
52107788Sruvoid ckfree(pointer);
53115440Shmpchar *savestr(const char *);
5423353Sdfrpointer stalloc(int);
5561579Salexvoid stunalloc(pointer);
5661579Salexvoid setstackmark(struct stackmark *);
5761579Salexvoid popstackmark(struct stackmark *);
5861579Salexchar *growstackstr(void);
5961579Salexchar *makestrspace(int, char *);
6061579Salexchar *stputbin(const char *data, int len, char *p);
6161579Salexchar *stputs(const char *data, char *p);
62107788Sru
6361579Salex
6423353Sdfr
6523353Sdfr#define stackblock() stacknxt
66139858Skeramida#define stackblocksize() stacknleft
67139858Skeramida#define grabstackblock(n) stalloc(n)
68139858Skeramida#define STARTSTACKSTR(p)	p = stackblock()
69139858Skeramida#define STPUTC(c, p)	do { if (p == sstrend) p = growstackstr(); *p++ = (c); } while(0)
70147398Sru#define CHECKSTRSPACE(n, p)	{ if (sstrend - p < n) p = makestrspace(n, p); }
7123353Sdfr#define USTPUTC(c, p)	(*p++ = (c))
72107788Sru/*
7323353Sdfr * STACKSTRNUL's use is where we want to be able to turn a stack
7429966Swosch * (non-sentinel, character counting string) into a C string,
7529966Swosch * and later pretend the NUL is not there.
7629966Swosch * Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used
7723353Sdfr * on a stack that will grabstackstr()ed.
7829966Swosch */
7923353Sdfr#define STACKSTRNUL(p)	(p == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0'))
80121414Shmp#define STUNPUTC(p)	(--p)
8134504Scharnier#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