update, move recovery information into docs
[nvi.git] / common / mem.h
blob91b7d28c0d14bb2ca95099e83cd3b73a0cfc0152
1 /*-
2 * Copyright (c) 1993
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 $
8 */
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)) \
15 goto binc_err; \
17 #define BINC_RET(sp, lp, llen, nlen) { \
18 if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \
19 return (1); \
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)) { \
30 bp = NULL; \
31 blen = 0; \
32 BINC_GOTO(sp, bp, blen, nlen); \
33 } else { \
34 BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
35 bp = __gp->tmp_bp; \
36 blen = __gp->tmp_blen; \
37 F_SET(__gp, G_TMP_INUSE); \
38 } \
40 #define GET_SPACE_RET(sp, bp, blen, nlen) { \
41 GS *__gp = (sp)->gp; \
42 if (F_ISSET(__gp, G_TMP_INUSE)) { \
43 bp = NULL; \
44 blen = 0; \
45 BINC_RET(sp, bp, blen, nlen); \
46 } else { \
47 BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
48 bp = __gp->tmp_bp; \
49 blen = __gp->tmp_blen; \
50 F_SET(__gp, G_TMP_INUSE); \
51 } \
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); \
63 bp = __gp->tmp_bp; \
64 blen = __gp->tmp_blen; \
65 F_SET(__gp, G_TMP_INUSE); \
66 } else \
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); \
74 bp = __gp->tmp_bp; \
75 blen = __gp->tmp_blen; \
76 F_SET(__gp, G_TMP_INUSE); \
77 } else \
78 BINC_RET(sp, bp, blen, nlen); \
81 /* Free memory, optionally making pointers unusable. */
82 #ifdef DEBUG
83 #define FREE(p, sz) { \
84 memset(p, 0xff, sz); \
85 free(p); \
87 #else
88 #define FREE(p, sz) free(p);
89 #endif
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); \
95 else \
96 FREE(bp, blen); \
100 * Malloc a buffer, casting the return pointer. Various versions.
102 * !!!
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); \
119 return (1); \
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); \
132 return (1); \
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));