summaryrefslogtreecommitdiff
path: root/common/mem.h
blob: 1e26a6ea6549ea4cc35950e40216a22d65103ce8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#ifdef DEBUG
#define CHECK_TYPE(type, var)						\
	type L__lp __attribute__((unused)) = var;
#else
#define CHECK_TYPE(type, var)
#endif

/* Increase the size of a malloc'd buffer.  Two versions, one that
 * returns, one that jumps to an error label.
 */
#define	BINC_GOTO(sp, type, lp, llen, nlen) {				\
	CHECK_TYPE(type *, lp)						\
	void *L__bincp;							\
	if ((nlen) > llen) {						\
		if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL)	\
			goto alloc_err;					\
		/*							\
		 * !!!							\
		 * Possible pointer conversion.				\
		 */							\
		lp = L__bincp;						\
	}								\
}
#define	BINC_GOTOC(sp, lp, llen, nlen)					\
	BINC_GOTO(sp, char, lp, llen, nlen)
#define	BINC_GOTOW(sp, lp, llen, nlen)					\
	BINC_GOTO(sp, CHAR_T, lp, llen, (nlen) * sizeof(CHAR_T))
#define	BINC_RET(sp, type, lp, llen, nlen) {				\
	CHECK_TYPE(type *, lp)						\
	void *L__bincp;							\
	if ((nlen) > llen) {						\
		if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL)	\
			return (1);					\
		/*							\
		 * !!!							\
		 * Possible pointer conversion.				\
		 */							\
		lp = L__bincp;						\
	}								\
}
#define	BINC_RETC(sp, lp, llen, nlen)					\
	BINC_RET(sp, char, lp, llen, nlen)
#define	BINC_RETW(sp, lp, llen, nlen)					\
	BINC_RET(sp, CHAR_T, lp, llen, (nlen) * sizeof(CHAR_T))

/*
 * Get some temporary space, preferably from the global temporary buffer,
 * from a malloc'd buffer otherwise.  Two versions, one that returns, one
 * that jumps to an error label.
 */
#define	GET_SPACE_GOTO(sp, type, bp, blen, nlen) {			\
	CHECK_TYPE(type *, bp)						\
	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
	if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) {		\
		bp = NULL;						\
		blen = 0;						\
		BINC_GOTO(sp, type, bp, blen, nlen); 			\
	} else {							\
		BINC_GOTOC(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen);	\
		bp = (type *) L__gp->tmp_bp;				\
		blen = L__gp->tmp_blen;					\
		F_SET(L__gp, G_TMP_INUSE);				\
	}								\
}
#define	GET_SPACE_GOTOC(sp, bp, blen, nlen)				\
	GET_SPACE_GOTO(sp, char, bp, blen, nlen)
#define	GET_SPACE_GOTOW(sp, bp, blen, nlen)				\
	GET_SPACE_GOTO(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
#define	GET_SPACE_RET(sp, type, bp, blen, nlen) {			\
	CHECK_TYPE(type *, bp)						\
	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
	if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) {		\
		bp = NULL;						\
		blen = 0;						\
		BINC_RET(sp, type, bp, blen, nlen);			\
	} else {							\
		BINC_RETC(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen);	\
		bp = (type *) L__gp->tmp_bp;				\
		blen = L__gp->tmp_blen;					\
		F_SET(L__gp, G_TMP_INUSE);				\
	}								\
}
#define	GET_SPACE_RETC(sp, bp, blen, nlen)				\
	GET_SPACE_RET(sp, char, bp, blen, nlen)
#define	GET_SPACE_RETW(sp, bp, blen, nlen)				\
	GET_SPACE_RET(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))

/*
 * Add space to a GET_SPACE returned buffer.  Two versions, one that
 * returns, one that jumps to an error label.
 */
#define	ADD_SPACE_GOTO(sp, type, bp, blen, nlen) {			\
	CHECK_TYPE(type *, bp)						\
	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
	if (L__gp == NULL || bp == (type *)L__gp->tmp_bp) {		\
		F_CLR(L__gp, G_TMP_INUSE);				\
		BINC_GOTOC(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen);	\
		bp = (type *) L__gp->tmp_bp;				\
		blen = L__gp->tmp_blen;					\
		F_SET(L__gp, G_TMP_INUSE);				\
	} else								\
		BINC_GOTO(sp, type, bp, blen, nlen);			\
}
#define	ADD_SPACE_GOTOC(sp, bp, blen, nlen)				\
	ADD_SPACE_GOTO(sp, char, bp, blen, nlen)
#define	ADD_SPACE_GOTOW(sp, bp, blen, nlen)				\
	ADD_SPACE_GOTO(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
#define	ADD_SPACE_RET(sp, type, bp, blen, nlen) {			\
	CHECK_TYPE(type *, bp)						\
	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
	if (L__gp == NULL || bp == (type *)L__gp->tmp_bp) {		\
		F_CLR(L__gp, G_TMP_INUSE);				\
		BINC_RETC(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen);	\
		bp = (type *) L__gp->tmp_bp;				\
		blen = L__gp->tmp_blen;					\
		F_SET(L__gp, G_TMP_INUSE);				\
	} else								\
		BINC_RET(sp, type, bp, blen, nlen);			\
}
#define	ADD_SPACE_RETC(sp, bp, blen, nlen)				\
	ADD_SPACE_RET(sp, char, bp, blen, nlen)
#define	ADD_SPACE_RETW(sp, bp, blen, nlen)				\
	ADD_SPACE_RET(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))

/* Free a GET_SPACE returned buffer. */
#define	FREE_SPACE(sp, bp, blen) {					\
	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
	if (L__gp != NULL && bp == L__gp->tmp_bp)			\
		F_CLR(L__gp, G_TMP_INUSE);				\
	else								\
		free(bp);						\
}
#define	FREE_SPACEW(sp, bp, blen) {					\
	CHECK_TYPE(CHAR_T *, bp)					\
	FREE_SPACE(sp, (char *)bp, blen);				\
}

/*
 * Malloc a buffer, casting the return pointer.  Various versions.
 */
#define	CALLOC(sp, p, nmemb, size) {					\
	if ((p = calloc(nmemb, size)) == NULL)				\
		msgq(sp, M_SYSERR, NULL);				\
}
#define	CALLOC_GOTO(sp, p, nmemb, size) {				\
	if ((p = calloc(nmemb, size)) == NULL)				\
		goto alloc_err;						\
}
#define	CALLOC_RET(sp, p, nmemb, size) {				\
	if ((p = calloc(nmemb, size)) == NULL) {			\
		msgq(sp, M_SYSERR, NULL);				\
		return (1);						\
	}								\
}

#define	MALLOC(sp, p, size) {						\
	if ((p = malloc(size)) == NULL)					\
		msgq(sp, M_SYSERR, NULL);				\
}
#define	MALLOC_GOTO(sp, p, size) {					\
	if ((p = malloc(size)) == NULL)					\
		goto alloc_err;						\
}
#define	MALLOC_RET(sp, p, size) {					\
	if ((p = malloc(size)) == NULL) {				\
		msgq(sp, M_SYSERR, NULL);				\
		return (1);						\
	}								\
}

/*
 * Resize a buffer, free any already held memory if we can't get more.
 * FreeBSD's reallocf(3) does the same thing, but it's not portable yet.
 */
#define	REALLOC(sp, p, cast, size) {					\
	cast newp;							\
	if ((newp = realloc(p, size)) == NULL) {			\
		free(p);						\
		msgq(sp, M_SYSERR, NULL);				\
	}								\
	p = newp;							\
}

/* 
 * p2roundup --
 *	Get next power of 2; convenient for realloc.
 *
 * Reference: FreeBSD /usr/src/lib/libc/stdio/getdelim.c
 */
static __inline size_t
p2roundup(size_t n)
{
	n--;
	n |= n >> 1;
	n |= n >> 2;
	n |= n >> 4;
	n |= n >> 8;
	n |= n >> 16;
#if SIZE_T_MAX > 0xffffffffU
	n |= n >> 32;
#endif
	n++;
	return (n);
}

/* Additional TAILQ helper. */
#define TAILQ_ENTRY_ISVALID(elm, field)					\
	((elm)->field.tqe_prev != NULL)