summaryrefslogtreecommitdiff
path: root/usr.bin/vi/common/mem.h
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/vi/common/mem.h')
-rw-r--r--usr.bin/vi/common/mem.h138
1 files changed, 56 insertions, 82 deletions
diff --git a/usr.bin/vi/common/mem.h b/usr.bin/vi/common/mem.h
index 31612b5ae38..9eb99dd4c94 100644
--- a/usr.bin/vi/common/mem.h
+++ b/usr.bin/vi/common/mem.h
@@ -1,63 +1,39 @@
/*-
* 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.
*
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
+ * See the LICENSE file for redistribution information.
*
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * @(#)mem.h 8.8 (Berkeley) 8/16/94
+ * @(#)mem.h 10.7 (Berkeley) 3/30/96
*/
/* Increase the size of a malloc'd buffer. Two versions, one that
* returns, one that jumps to an error label.
*/
#define BINC_GOTO(sp, lp, llen, nlen) { \
- void *__bincp; \
+ void *L__bincp; \
if ((nlen) > llen) { \
- if ((__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
- goto binc_err; \
+ if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
+ goto alloc_err; \
/* \
* !!! \
* Possible pointer conversion. \
*/ \
- lp = __bincp; \
+ lp = L__bincp; \
} \
}
#define BINC_RET(sp, lp, llen, nlen) { \
- void *__bincp; \
+ void *L__bincp; \
if ((nlen) > llen) { \
- if ((__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
+ if ((L__bincp = binc(sp, lp, &(llen), nlen)) == NULL) \
return (1); \
/* \
* !!! \
* Possible pointer conversion. \
*/ \
- lp = __bincp; \
+ lp = L__bincp; \
} \
}
@@ -67,29 +43,29 @@
* that jumps to an error label.
*/
#define GET_SPACE_GOTO(sp, bp, blen, nlen) { \
- GS *__gp = (sp)->gp; \
- if (F_ISSET(__gp, G_TMP_INUSE)) { \
+ 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, bp, blen, nlen); \
} else { \
- BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
- bp = __gp->tmp_bp; \
- blen = __gp->tmp_blen; \
- F_SET(__gp, G_TMP_INUSE); \
+ BINC_GOTO(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
+ bp = L__gp->tmp_bp; \
+ blen = L__gp->tmp_blen; \
+ F_SET(L__gp, G_TMP_INUSE); \
} \
}
#define GET_SPACE_RET(sp, bp, blen, nlen) { \
- GS *__gp = (sp)->gp; \
- if (F_ISSET(__gp, G_TMP_INUSE)) { \
+ 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, bp, blen, nlen); \
} else { \
- BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
- bp = __gp->tmp_bp; \
- blen = __gp->tmp_blen; \
- F_SET(__gp, G_TMP_INUSE); \
+ BINC_RET(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
+ bp = L__gp->tmp_bp; \
+ blen = L__gp->tmp_blen; \
+ F_SET(L__gp, G_TMP_INUSE); \
} \
}
@@ -98,44 +74,35 @@
* returns, one that jumps to an error label.
*/
#define ADD_SPACE_GOTO(sp, bp, blen, nlen) { \
- GS *__gp = (sp)->gp; \
- if (bp == __gp->tmp_bp) { \
- F_CLR(__gp, G_TMP_INUSE); \
- BINC_GOTO(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
- bp = __gp->tmp_bp; \
- blen = __gp->tmp_blen; \
- F_SET(__gp, G_TMP_INUSE); \
+ GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
+ if (L__gp == NULL || bp == L__gp->tmp_bp) { \
+ F_CLR(L__gp, G_TMP_INUSE); \
+ BINC_GOTO(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
+ bp = L__gp->tmp_bp; \
+ blen = L__gp->tmp_blen; \
+ F_SET(L__gp, G_TMP_INUSE); \
} else \
BINC_GOTO(sp, bp, blen, nlen); \
}
#define ADD_SPACE_RET(sp, bp, blen, nlen) { \
- GS *__gp = (sp)->gp; \
- if (bp == __gp->tmp_bp) { \
- F_CLR(__gp, G_TMP_INUSE); \
- BINC_RET(sp, __gp->tmp_bp, __gp->tmp_blen, nlen); \
- bp = __gp->tmp_bp; \
- blen = __gp->tmp_blen; \
- F_SET(__gp, G_TMP_INUSE); \
+ GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
+ if (L__gp == NULL || bp == L__gp->tmp_bp) { \
+ F_CLR(L__gp, G_TMP_INUSE); \
+ BINC_RET(sp, L__gp->tmp_bp, L__gp->tmp_blen, nlen); \
+ bp = L__gp->tmp_bp; \
+ blen = L__gp->tmp_blen; \
+ F_SET(L__gp, G_TMP_INUSE); \
} else \
BINC_RET(sp, bp, blen, nlen); \
}
-/* Free memory, optionally making pointers unusable. */
-#ifdef DEBUG
-#define FREE(p, sz) { \
- memset(p, 0xff, sz); \
- free(p); \
-}
-#else
-#define FREE(p, sz) free(p);
-#endif
-
/* Free a GET_SPACE returned buffer. */
#define FREE_SPACE(sp, bp, blen) { \
- if (bp == sp->gp->tmp_bp) \
- F_CLR(sp->gp, G_TMP_INUSE); \
+ GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
+ if (L__gp != NULL && (CHAR_T *)bp == (CHAR_T *)L__gp->tmp_bp) \
+ F_CLR(L__gp, G_TMP_INUSE); \
else \
- FREE(bp, blen); \
+ free(bp); \
}
/*
@@ -148,26 +115,35 @@
* a million warnings. After awhile, it seemed easier to put the casts
* in instead of explaining it all the time.
*/
-#define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \
- p = (cast)calloc(nmemb, size); \
-}
#define CALLOC(sp, p, cast, nmemb, size) { \
if ((p = (cast)calloc(nmemb, size)) == NULL) \
msgq(sp, M_SYSERR, NULL); \
}
+#define CALLOC_GOTO(sp, p, cast, nmemb, size) { \
+ if ((p = (cast)calloc(nmemb, size)) == NULL) \
+ goto alloc_err; \
+}
+#define CALLOC_NOMSG(sp, p, cast, nmemb, size) { \
+ p = (cast)calloc(nmemb, size); \
+}
#define CALLOC_RET(sp, p, cast, nmemb, size) { \
if ((p = (cast)calloc(nmemb, size)) == NULL) { \
msgq(sp, M_SYSERR, NULL); \
return (1); \
} \
}
-#define MALLOC_NOMSG(sp, p, cast, size) { \
- p = (cast)malloc(size); \
-}
+
#define MALLOC(sp, p, cast, size) { \
if ((p = (cast)malloc(size)) == NULL) \
msgq(sp, M_SYSERR, NULL); \
}
+#define MALLOC_GOTO(sp, p, cast, size) { \
+ if ((p = (cast)malloc(size)) == NULL) \
+ goto alloc_err; \
+}
+#define MALLOC_NOMSG(sp, p, cast, size) { \
+ p = (cast)malloc(size); \
+}
#define MALLOC_RET(sp, p, cast, size) { \
if ((p = (cast)malloc(size)) == NULL) { \
msgq(sp, M_SYSERR, NULL); \
@@ -190,5 +166,3 @@
*/
#define MEMMOVE(p, t, len) memmove(p, t, (len) * sizeof(*(p)))
#define MEMSET(p, value, len) memset(p, value, (len) * sizeof(*(p)))
-
-void *binc __P((SCR *, void *, size_t *, size_t));