3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
7 * $Id: mem.h,v 8.2 1993/12/19 17:24:56 bostic Exp $ (Berkeley) $Date: 1993/12/19 17:24:56 $
10 /* Increase the size of a malloc'd buffer. Two versions, one that
11 * returns, one that jumps to an error label.
13 #define BINC_GOTO(sp, lp, llen, nlen) { \
14 if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \
17 #define BINC_RET(sp, lp, llen, nlen) { \
18 if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \
23 * Get some temporary space, preferably from the global temporary buffer,
24 * from a malloc'd buffer otherwise. Two versions, one that returns, one
25 * that jumps to an error label.
27 #define GET_SPACE_GOTO(sp, bp, blen, nlen) { \
28 GS *__gp = (sp)->gp; \
29 if (F_ISSET(__gp, G_TMP_INUSE)) { \
32 BINC_GOTO(sp, bp, blen, nlen); \
34 BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
36 blen = __gp->tmp_blen; \
37 F_SET(__gp, G_TMP_INUSE); \
40 #define GET_SPACE_RET(sp, bp, blen, nlen) { \
41 GS *__gp = (sp)->gp; \
42 if (F_ISSET(__gp, G_TMP_INUSE)) { \
45 BINC_RET(sp, bp, blen, nlen); \
47 BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
49 blen = __gp->tmp_blen; \
50 F_SET(__gp, G_TMP_INUSE); \
55 * Add space to a GET_SPACE returned buffer. Two versions, one that
56 * returns, one that jumps to an error label.
58 #define ADD_SPACE_GOTO(sp, bp, blen, nlen) { \
59 GS *__gp = (sp)->gp; \
60 if (bp == __gp->tmp_bp) { \
61 F_CLR(__gp, G_TMP_INUSE); \
62 BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
64 blen = __gp->tmp_blen; \
65 F_SET(__gp, G_TMP_INUSE); \
67 BINC_GOTO(sp, bp, blen, nlen); \
69 #define ADD_SPACE_RET(sp, bp, blen, nlen) { \
70 GS *__gp = (sp)->gp; \
71 if (bp == __gp->tmp_bp) { \
72 F_CLR(__gp, G_TMP_INUSE); \
73 BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
75 blen = __gp->tmp_blen; \
76 F_SET(__gp, G_TMP_INUSE); \
78 BINC_RET(sp, bp, blen, nlen); \
81 /* Free memory, optionally making pointers unusable. */
83 #define FREE(p, sz) { \
84 memset(p, 0xff, sz); \
88 #define FREE(p, sz) free(p);
91 /* Free a GET_SPACE returned buffer. */
92 #define FREE_SPACE(sp, bp, blen) { \
93 if (bp == sp->gp->tmp_bp) \
94 F_CLR(sp->gp, G_TMP_INUSE); \
100 * Malloc a buffer, casting the return pointer. Various versions.
103 * The cast should be unnecessary, malloc(3) and friends return void *'s,
104 * which is all we need. However, some systems that nvi needs to run on
105 * don't do it right yet, resulting in the compiler printing out roughly
106 * a million warnings. After awhile, it seemed easier to put the casts
107 * in instead of explaining it all the time.
109 #define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \
110 p = (cast)calloc(nmemb, size); \
112 #define CALLOC(sp, p, cast, nmemb, size) { \
113 if ((p = (cast)calloc(nmemb, size)) == NULL) \
114 msgq(sp, M_SYSERR, NULL); \
116 #define CALLOC_RET(sp, p, cast, nmemb, size) { \
117 if ((p = (cast)calloc(nmemb, size)) == NULL) { \
118 msgq(sp, M_SYSERR, NULL); \
122 #define MALLOC_NOMSG(sp, p, cast, size) { \
123 p = (cast)malloc(size); \
125 #define MALLOC(sp, p, cast, size) { \
126 if ((p = (cast)malloc(size)) == NULL) \
127 msgq(sp, M_SYSERR, NULL); \
129 #define MALLOC_RET(sp, p, cast, size) { \
130 if ((p = (cast)malloc(size)) == NULL) { \
131 msgq(sp, M_SYSERR, NULL); \
135 #define REALLOC(sp, p, cast, size) { \
136 if ((p = (cast)realloc(p, size)) == NULL) \
137 msgq(sp, M_SYSERR, NULL); \
140 int binc
__P((SCR
*, void *, size_t *, size_t));