reset screen offset of top line if it exceeds the number of screens
[nvi.git] / common / mem.h
blob0904281bb0026d318636d71f548a1569152960b8
1 /*-
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.13 2002/01/05 23:13:37 skimo Exp $ (Berkeley) $Date: 2002/01/05 23:13:37 $
12 #ifdef HAVE_GCC
13 #define CHECK_TYPE(type, var) \
14 type L__lp __attribute__((unused)) = var;
15 #else
16 #define CHECK_TYPE(type, var)
17 #endif
19 /* Increase the size of a malloc'd buffer. Two versions, one that
20 * returns, one that jumps to an error label.
22 #define BINC_GOTO(sp, type, lp, llen, nlen) { \
23 CHECK_TYPE(type *, lp) \
24 void *L__bincp; \
25 if ((nlen) > llen) { \
26 if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
27 goto alloc_err; \
28 /* \
29 * !!! \
30 * Possible pointer conversion. \
31 */ \
32 lp = L__bincp; \
33 } \
35 #define BINC_GOTOC(sp, lp, llen, nlen) \
36 BINC_GOTO(sp, char, lp, llen, nlen)
37 #define BINC_GOTOW(sp, lp, llen, nlen) \
38 BINC_GOTO(sp, CHAR_T, lp, llen, (nlen) * sizeof(CHAR_T))
39 #define BINC_RET(sp, type, lp, llen, nlen) { \
40 CHECK_TYPE(type *, lp) \
41 void *L__bincp; \
42 if ((nlen) > llen) { \
43 if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
44 return (1); \
45 /* \
46 * !!! \
47 * Possible pointer conversion. \
48 */ \
49 lp = L__bincp; \
50 } \
52 #define BINC_RETC(sp, lp, llen, nlen) \
53 BINC_RET(sp, char, lp, llen, nlen)
54 #define BINC_RETW(sp, lp, llen, nlen) \
55 BINC_RET(sp, CHAR_T, lp, llen, (nlen) * sizeof(CHAR_T))
58 * Get some temporary space, preferably from the global temporary buffer,
59 * from a malloc'd buffer otherwise. Two versions, one that returns, one
60 * that jumps to an error label.
62 #define GET_SPACE_GOTO(sp, type, bp, blen, nlen) { \
63 CHECK_TYPE(type *, bp) \
64 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
65 if (L__wp == NULL || F_ISSET(L__wp, W_TMP_INUSE)) { \
66 bp = NULL; \
67 blen = 0; \
68 BINC_GOTO(sp, type, bp, blen, nlen); \
69 } else { \
70 BINC_GOTOC(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
71 bp = (type *) L__wp->tmp_bp; \
72 blen = L__wp->tmp_blen; \
73 F_SET(L__wp, W_TMP_INUSE); \
74 } \
76 #define GET_SPACE_GOTOC(sp, bp, blen, nlen) \
77 GET_SPACE_GOTO(sp, char, bp, blen, nlen)
78 #define GET_SPACE_GOTOW(sp, bp, blen, nlen) \
79 GET_SPACE_GOTO(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
80 #define GET_SPACE_RET(sp, type, bp, blen, nlen) { \
81 CHECK_TYPE(type *, bp) \
82 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
83 if (L__wp == NULL || F_ISSET(L__wp, W_TMP_INUSE)) { \
84 bp = NULL; \
85 blen = 0; \
86 BINC_RET(sp, type, bp, blen, nlen); \
87 } else { \
88 BINC_RETC(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
89 bp = (type *) L__wp->tmp_bp; \
90 blen = L__wp->tmp_blen; \
91 F_SET(L__wp, W_TMP_INUSE); \
92 } \
94 #define GET_SPACE_RETC(sp, bp, blen, nlen) \
95 GET_SPACE_RET(sp, char, bp, blen, nlen)
96 #define GET_SPACE_RETW(sp, bp, blen, nlen) \
97 GET_SPACE_RET(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
100 * Add space to a GET_SPACE returned buffer. Two versions, one that
101 * returns, one that jumps to an error label.
103 #define ADD_SPACE_GOTO(sp, type, bp, blen, nlen) { \
104 CHECK_TYPE(type *, bp) \
105 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
106 if (L__wp == NULL || bp == (type *)L__wp->tmp_bp) { \
107 F_CLR(L__wp, W_TMP_INUSE); \
108 BINC_GOTOC(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
109 bp = (type *) L__wp->tmp_bp; \
110 blen = L__wp->tmp_blen; \
111 F_SET(L__wp, W_TMP_INUSE); \
112 } else \
113 BINC_GOTO(sp, type, bp, blen, nlen); \
115 #define ADD_SPACE_GOTOW(sp, bp, blen, nlen) \
116 ADD_SPACE_GOTO(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
117 #define ADD_SPACE_RET(sp, type, bp, blen, nlen) { \
118 CHECK_TYPE(type *, bp) \
119 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
120 if (L__wp == NULL || bp == (type *)L__wp->tmp_bp) { \
121 F_CLR(L__wp, W_TMP_INUSE); \
122 BINC_RETC(sp, L__wp->tmp_bp, L__wp->tmp_blen, nlen); \
123 bp = (type *) L__wp->tmp_bp; \
124 blen = L__wp->tmp_blen; \
125 F_SET(L__wp, W_TMP_INUSE); \
126 } else \
127 BINC_RET(sp, type, bp, blen, nlen); \
129 #define ADD_SPACE_RETW(sp, bp, blen, nlen) \
130 ADD_SPACE_RET(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
132 /* Free a GET_SPACE returned buffer. */
133 #define FREE_SPACE(sp, bp, blen) { \
134 WIN *L__wp = (sp) == NULL ? NULL : (sp)->wp; \
135 if (L__wp != NULL && bp == L__wp->tmp_bp) \
136 F_CLR(L__wp, W_TMP_INUSE); \
137 else \
138 free(bp); \
140 #define FREE_SPACEW(sp, bp, blen) { \
141 CHECK_TYPE(CHAR_T *, bp) \
142 FREE_SPACE(sp, (char *)bp, blen); \
146 * Malloc a buffer, casting the return pointer. Various versions.
148 * !!!
149 * The cast should be unnecessary, malloc(3) and friends return void *'s,
150 * which is all we need. However, some systems that nvi needs to run on
151 * don't do it right yet, resulting in the compiler printing out roughly
152 * a million warnings. After awhile, it seemed easier to put the casts
153 * in instead of explaining it all the time.
155 #define CALLOC(sp, p, cast, nmemb, size) { \
156 if ((p = (cast)calloc(nmemb, size)) == NULL) \
157 msgq(sp, M_SYSERR, NULL); \
159 #define CALLOC_GOTO(sp, p, cast, nmemb, size) { \
160 if ((p = (cast)calloc(nmemb, size)) == NULL) \
161 goto alloc_err; \
163 #define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \
164 p = (cast)calloc(nmemb, size); \
166 #define CALLOC_RET(sp, p, cast, nmemb, size) { \
167 if ((p = (cast)calloc(nmemb, size)) == NULL) { \
168 msgq(sp, M_SYSERR, NULL); \
169 return (1); \
173 #define MALLOC(sp, p, cast, size) { \
174 if ((p = (cast)malloc(size)) == NULL) \
175 msgq(sp, M_SYSERR, NULL); \
177 #define MALLOC_GOTO(sp, p, cast, size) { \
178 if ((p = (cast)malloc(size)) == NULL) \
179 goto alloc_err; \
181 #define MALLOC_NOMSG(sp, p, cast, size) { \
182 p = (cast)malloc(size); \
184 #define MALLOC_RET(sp, p, cast, size) { \
185 if ((p = (cast)malloc(size)) == NULL) { \
186 msgq(sp, M_SYSERR, NULL); \
187 return (1); \
191 * XXX
192 * Don't depend on realloc(NULL, size) working.
194 #define REALLOC(sp, p, cast, size) { \
195 if ((p = (cast)(p == NULL ? \
196 malloc(size) : realloc(p, size))) == NULL) \
197 msgq(sp, M_SYSERR, NULL); \
201 * Versions of memmove(3) and memset(3) that use the size of the
202 * initial pointer to figure out how much memory to manipulate.
204 #define MEMMOVE(p, t, len) memmove(p, t, (len) * sizeof(*(p)))
205 #define MEMSET(p, value, len) memset(p, value, (len) * sizeof(*(p)))