Remove unnecessary buffer allocation and memcpy.

Partially taken from nvi2.
This commit is contained in:
rin 2018-08-07 11:25:45 +00:00
parent d40ccf42c1
commit 5006b3769f
1 changed files with 12 additions and 18 deletions

View File

@ -15,7 +15,7 @@
static const char sccsid[] = "Id: db1.c,v 10.1 2002/03/09 12:53:57 skimo Exp (Berkeley) Date: 2002/03/09 12:53:57 "; static const char sccsid[] = "Id: db1.c,v 10.1 2002/03/09 12:53:57 skimo Exp (Berkeley) Date: 2002/03/09 12:53:57 ";
#endif /* not lint */ #endif /* not lint */
#else #else
__RCSID("$NetBSD: vi_db1.c,v 1.9 2017/11/10 14:35:25 rin Exp $"); __RCSID("$NetBSD: vi_db1.c,v 1.10 2018/08/07 11:25:45 rin Exp $");
#endif #endif
#include <sys/types.h> #include <sys/types.h>
@ -98,7 +98,6 @@ db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
db_recno_t l1, l2; db_recno_t l1, l2;
const CHAR_T *wp; const CHAR_T *wp;
size_t wlen; size_t wlen;
size_t nlen;
/* /*
* The underlying recno stuff handles zero by returning NULL, but * The underlying recno stuff handles zero by returning NULL, but
@ -159,11 +158,6 @@ db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
sp->c_lno = OOBLNO; sp->c_lno = OOBLNO;
nocache: nocache:
nlen = 1024;
retry:
/* data.size contains length in bytes */
BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
/* Get the line from the underlying database. */ /* Get the line from the underlying database. */
key.data = &lno; key.data = &lno;
key.size = sizeof(lno); key.size = sizeof(lno);
@ -179,12 +173,8 @@ err3: if (lenp != NULL)
if (pp != NULL) if (pp != NULL)
*pp = NULL; *pp = NULL;
return (1); return (1);
case 0: default:
if (data.size > nlen) { break;
nlen = data.size;
goto retry;
} else
memcpy(sp->c_lp, data.data, data.size);
} }
if (FILE2INT(sp, data.data, data.size, wp, wlen)) { if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
@ -199,7 +189,8 @@ err3: if (lenp != NULL)
if (wp != data.data) { if (wp != data.data) {
BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen); BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
MEMCPYW(sp->c_lp, wp, wlen); MEMCPYW(sp->c_lp, wp, wlen);
} } else
sp->c_lp = data.data;
sp->c_lno = lno; sp->c_lno = lno;
sp->c_len = wlen; sp->c_len = wlen;
@ -560,8 +551,8 @@ alloc_err:
msgq(sp, M_DBERR, "007|unable to get last line"); msgq(sp, M_DBERR, "007|unable to get last line");
*lnop = 0; *lnop = 0;
return (1); return (1);
case 0: default:
; break;
} }
memcpy(&lno, key.data, sizeof(lno)); memcpy(&lno, key.data, sizeof(lno));
@ -570,8 +561,11 @@ alloc_err:
FILE2INT(sp, data.data, data.size, wp, wlen); FILE2INT(sp, data.data, data.size, wp, wlen);
/* Fill the cache. */ /* Fill the cache. */
BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen); if (wp != data.data) {
MEMCPYW(sp->c_lp, wp, wlen); BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
MEMCPYW(sp->c_lp, wp, wlen);
} else
sp->c_lp = data.data;
sp->c_lno = lno; sp->c_lno = lno;
sp->c_len = wlen; sp->c_len = wlen;
} }