2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
9 * @(#)mem.h 10.7 (Berkeley) 3/30/96
12 /* Increase the size of a malloc'd buffer. Two versions, one that
13 * returns, one that jumps to an error label.
15 #define BINC_GOTO(sp, lp, llen, nlen) { \
17 if ((nlen) > llen) { \
18 if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
22 * Possible pointer conversion. \
27 #define BINC_RET(sp, lp, llen, nlen) { \
29 if ((nlen) > llen) { \
30 if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
34 * Possible pointer conversion. \
41 * Get some temporary space, preferably from the global temporary buffer,
42 * from a malloc'd buffer otherwise. Two versions, one that returns, one
43 * that jumps to an error label.
45 #define GET_SPACE_GOTO(sp, bp, blen, nlen) { \
46 GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
47 if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) { \
50 BINC_GOTO(sp, bp, blen, nlen); \
52 BINC_GOTO(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
54 blen = L__gp->tmp_blen; \
55 F_SET(L__gp, G_TMP_INUSE); \
58 #define GET_SPACE_RET(sp, bp, blen, nlen) { \
59 GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
60 if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) { \
63 BINC_RET(sp, bp, blen, nlen); \
65 BINC_RET(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
67 blen = L__gp->tmp_blen; \
68 F_SET(L__gp, G_TMP_INUSE); \
73 * Add space to a GET_SPACE returned buffer. Two versions, one that
74 * returns, one that jumps to an error label.
76 #define ADD_SPACE_GOTO(sp, bp, blen, nlen) { \
77 GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
78 if (L__gp == NULL || bp == L__gp->tmp_bp) { \
79 F_CLR(L__gp, G_TMP_INUSE); \
80 BINC_GOTO(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
82 blen = L__gp->tmp_blen; \
83 F_SET(L__gp, G_TMP_INUSE); \
85 BINC_GOTO(sp, bp, blen, nlen); \
87 #define ADD_SPACE_RET(sp, bp, blen, nlen) { \
88 GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
89 if (L__gp == NULL || bp == L__gp->tmp_bp) { \
90 F_CLR(L__gp, G_TMP_INUSE); \
91 BINC_RET(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
93 blen = L__gp->tmp_blen; \
94 F_SET(L__gp, G_TMP_INUSE); \
96 BINC_RET(sp, bp, blen, nlen); \
99 /* Free a GET_SPACE returned buffer. */
100 #define FREE_SPACE(sp, bp, blen) { \
101 GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
102 if (L__gp != NULL && bp == L__gp->tmp_bp) \
103 F_CLR(L__gp, G_TMP_INUSE); \
109 * Malloc a buffer, casting the return pointer. Various versions.
112 * The cast should be unnecessary, malloc(3) and friends return void *'s,
113 * which is all we need. However, some systems that nvi needs to run on
114 * don't do it right yet, resulting in the compiler printing out roughly
115 * a million warnings. After awhile, it seemed easier to put the casts
116 * in instead of explaining it all the time.
118 #define CALLOC(sp, p, cast, nmemb, size) { \
119 if ((p = (cast)calloc(nmemb, size)) == NULL) \
120 msgq(sp, M_SYSERR, NULL); \
122 #define CALLOC_GOTO(sp, p, cast, nmemb, size) { \
123 if ((p = (cast)calloc(nmemb, size)) == NULL) \
126 #define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \
127 p = (cast)calloc(nmemb, size); \
129 #define CALLOC_RET(sp, p, cast, nmemb, size) { \
130 if ((p = (cast)calloc(nmemb, size)) == NULL) { \
131 msgq(sp, M_SYSERR, NULL); \
136 #define MALLOC(sp, p, cast, size) { \
137 if ((p = (cast)malloc(size)) == NULL) \
138 msgq(sp, M_SYSERR, NULL); \
140 #define MALLOC_GOTO(sp, p, cast, size) { \
141 if ((p = (cast)malloc(size)) == NULL) \
144 #define MALLOC_NOMSG(sp, p, cast, size) { \
145 p = (cast)malloc(size); \
147 #define MALLOC_RET(sp, p, cast, size) { \
148 if ((p = (cast)malloc(size)) == NULL) { \
149 msgq(sp, M_SYSERR, NULL); \
155 * Don't depend on realloc(NULL, size) working.
157 #define REALLOC(sp, p, cast, size) { \
158 if ((p = (cast)(p == NULL ? \
159 malloc(size) : realloc(p, size))) == NULL) \
160 msgq(sp, M_SYSERR, NULL); \
164 * Versions of memmove(3) and memset(3) that use the size of the
165 * initial pointer to figure out how much memory to manipulate.
167 #define MEMMOVE(p, t, len) memmove(p, t, (len) * sizeof(*(p)))
168 #define MEMSET(p, value, len) memset(p, value, (len) * sizeof(*(p)))