11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes * 3217987Speter * @(#)memalloc.h 8.2 (Berkeley) 5/4/95 3350471Speter * $FreeBSD$ 341556Srgrimes */ 351556Srgrimes 36193221Srse#include <string.h> 37193221Srse 381556Srgrimesstruct stackmark { 391556Srgrimes struct stack_block *stackp; 401556Srgrimes char *stacknxt; 411556Srgrimes int stacknleft; 421556Srgrimes}; 431556Srgrimes 441556Srgrimes 451556Srgrimesextern char *stacknxt; 461556Srgrimesextern int stacknleft; 47216743Sjillesextern char *sstrend; 481556Srgrimes 49193221Srsepointer ckmalloc(size_t); 5090111Simppointer ckrealloc(pointer, int); 51151795Sstefanfvoid ckfree(pointer); 52200956Sjilleschar *savestr(const char *); 5390111Simppointer stalloc(int); 5490111Simpvoid stunalloc(pointer); 5590111Simpvoid setstackmark(struct stackmark *); 5690111Simpvoid popstackmark(struct stackmark *); 5790111Simpchar *growstackstr(void); 58216743Sjilleschar *makestrspace(int, char *); 59248980Sjilleschar *stputbin(const char *data, size_t len, char *p); 60215783Sjilleschar *stputs(const char *data, char *p); 611556Srgrimes 621556Srgrimes 631556Srgrimes 641556Srgrimes#define stackblock() stacknxt 651556Srgrimes#define stackblocksize() stacknleft 66217209Sjilles#define grabstackblock(n) stalloc(n) 67216743Sjilles#define STARTSTACKSTR(p) p = stackblock() 68216743Sjilles#define STPUTC(c, p) do { if (p == sstrend) p = growstackstr(); *p++ = (c); } while(0) 69248980Sjilles#define CHECKSTRSPACE(n, p) { if ((size_t)(sstrend - p) < n) p = makestrspace(n, p); } 70216743Sjilles#define USTPUTC(c, p) (*p++ = (c)) 71213814Sobrien/* 72213814Sobrien * STACKSTRNUL's use is where we want to be able to turn a stack 73213814Sobrien * (non-sentinel, character counting string) into a C string, 74213814Sobrien * and later pretend the NUL is not there. 75213814Sobrien * Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used 76213814Sobrien * on a stack that will grabstackstr()ed. 77213814Sobrien */ 78216743Sjilles#define STACKSTRNUL(p) (p == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0')) 79216743Sjilles#define STUNPUTC(p) (--p) 801556Srgrimes#define STTOPC(p) p[-1] 81216743Sjilles#define STADJUST(amount, p) (p += (amount)) 82216743Sjilles#define grabstackstr(p) stalloc((char *)p - stackblock()) 83216743Sjilles#define ungrabstackstr(s, p) stunalloc((s)) 84215783Sjilles#define STPUTBIN(s, len, p) p = stputbin((s), (len), p) 85215783Sjilles#define STPUTS(s, p) p = stputs((s), p) 86