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 * $Id: mem.h,v 10.11 2000/08/20 09:12:59 skimo Exp $ (Berkeley) $Date: 2000/08/20 09:12:59 $
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) { \
18 if ((nlen) > llen) { \
19 if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
23 * Possible pointer conversion. \
28 #define BINC_GOTOW(sp, lp, llen, nlen) { \
30 BINC_GOTO(sp, (char *)lp, llen, (nlen) * sizeof(CHAR_T)) \
32 #define BINC_RET(sp, lp, llen, nlen) { \
35 if ((nlen) > llen) { \
36 if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
40 * Possible pointer conversion. \
45 #define BINC_RETW(sp, lp, llen, nlen) { \
47 BINC_RET(sp, (char *)lp, llen, (nlen) * sizeof(CHAR_T)) \
51 * Get some temporary space, preferably from the global temporary buffer,
52 * from a malloc'd buffer otherwise. Two versions, one that returns, one
53 * that jumps to an error label.
55 #define GET_SPACE_GOTO(sp, bp, blen, nlen) { \
56 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
57 if (L__wp == NULL || F_ISSET(L__wp, W_TMP_INUSE)) { \
60 BINC_GOTO(sp, bp, blen, nlen); \
62 BINC_GOTO(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
64 blen = L__wp->tmp_blen; \
65 F_SET(L__wp, W_TMP_INUSE); \
68 #define GET_SPACE_GOTOW(sp, bp, blen, nlen) { \
70 GET_SPACE_GOTO(sp, (char *)bp, blen, (nlen) * sizeof(CHAR_T)) \
72 #define GET_SPACE_RET(sp, bp, blen, nlen) { \
73 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
74 if (L__wp == NULL || F_ISSET(L__wp, W_TMP_INUSE)) { \
77 BINC_RET(sp, bp, blen, nlen); \
79 BINC_RET(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
81 blen = L__wp->tmp_blen; \
82 F_SET(L__wp, W_TMP_INUSE); \
85 #define GET_SPACE_RETW(sp, bp, blen, nlen) { \
87 GET_SPACE_RET(sp, (char *)bp, blen, (nlen) * sizeof(CHAR_T)) \
91 * Add space to a GET_SPACE returned buffer. Two versions, one that
92 * returns, one that jumps to an error label.
94 #define ADD_SPACE_GOTO(sp, bp, blen, nlen) { \
95 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
96 if (L__wp == NULL || bp == L__wp->tmp_bp) { \
97 F_CLR(L__wp, W_TMP_INUSE); \
98 BINC_GOTO(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
100 blen = L__wp->tmp_blen; \
101 F_SET(L__wp, W_TMP_INUSE); \
103 BINC_GOTO(sp, bp, blen, nlen); \
105 #define ADD_SPACE_GOTOW(sp, bp, blen, nlen) { \
106 CHAR_T *L__bp = bp; \
107 ADD_SPACE_GOTO(sp, (char *)bp, blen, (nlen) * sizeof(CHAR_T)) \
109 #define ADD_SPACE_RET(sp, bp, blen, nlen) { \
110 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
111 if (L__wp == NULL || bp == L__wp->tmp_bp) { \
112 F_CLR(L__wp, W_TMP_INUSE); \
113 BINC_RET(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
114 bp = L__wp->tmp_bp; \
115 blen = L__wp->tmp_blen; \
116 F_SET(L__wp, W_TMP_INUSE); \
118 BINC_RET(sp, bp, blen, nlen); \
120 #define ADD_SPACE_RETW(sp, bp, blen, nlen) { \
121 CHAR_T *L__bp = bp; \
122 ADD_SPACE_RET(sp, (char *)bp, blen, (nlen) * sizeof(CHAR_T)) \
125 /* Free a GET_SPACE returned buffer. */
126 #define FREE_SPACE(sp, bp, blen) { \
127 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
128 if (L__wp != NULL && bp == L__wp->tmp_bp) \
129 F_CLR(L__wp, W_TMP_INUSE); \
133 #define FREE_SPACEW(sp, bp, blen) { \
134 CHAR_T *L__bp = bp; \
135 FREE_SPACE(sp, (char *)bp, blen); \
139 * Malloc a buffer, casting the return pointer. Various versions.
142 * The cast should be unnecessary, malloc(3) and friends return void *'s,
143 * which is all we need. However, some systems that nvi needs to run on
144 * don't do it right yet, resulting in the compiler printing out roughly
145 * a million warnings. After awhile, it seemed easier to put the casts
146 * in instead of explaining it all the time.
148 #define CALLOC(sp, p, cast, nmemb, size) { \
149 if ((p = (cast)calloc(nmemb, size)) == NULL) \
150 msgq(sp, M_SYSERR, NULL); \
152 #define CALLOC_GOTO(sp, p, cast, nmemb, size) { \
153 if ((p = (cast)calloc(nmemb, size)) == NULL) \
156 #define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \
157 p = (cast)calloc(nmemb, size); \
159 #define CALLOC_RET(sp, p, cast, nmemb, size) { \
160 if ((p = (cast)calloc(nmemb, size)) == NULL) { \
161 msgq(sp, M_SYSERR, NULL); \
166 #define MALLOC(sp, p, cast, size) { \
167 if ((p = (cast)malloc(size)) == NULL) \
168 msgq(sp, M_SYSERR, NULL); \
170 #define MALLOC_GOTO(sp, p, cast, size) { \
171 if ((p = (cast)malloc(size)) == NULL) \
174 #define MALLOC_NOMSG(sp, p, cast, size) { \
175 p = (cast)malloc(size); \
177 #define MALLOC_RET(sp, p, cast, size) { \
178 if ((p = (cast)malloc(size)) == NULL) { \
179 msgq(sp, M_SYSERR, NULL); \
185 * Don't depend on realloc(NULL, size) working.
187 #define REALLOC(sp, p, cast, size) { \
188 if ((p = (cast)(p == NULL ? \
189 malloc(size) : realloc(p, size))) == NULL) \
190 msgq(sp, M_SYSERR, NULL); \
194 * Versions of memmove(3) and memset(3) that use the size of the
195 * initial pointer to figure out how much memory to manipulate.
197 #define MEMMOVE(p, t, len) memmove(p, t, (len) * sizeof(*(p)))
198 #define MEMSET(p, value, len) memset(p, value, (len) * sizeof(*(p)))