From 4d3d73ce7ab9adb17e75f190e387e19f21a1e9e0 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Wed, 4 Jun 2008 07:32:33 +0300 Subject: [PATCH] libdpkg: Add a size argument to varbufinit Change modstatdb_init to use varbufinit instead of its own initialization. --- ChangeLog | 9 +++++++++ lib/dbmodify.c | 4 +--- lib/dpkg-db.h | 6 +++--- lib/varbuf.c | 13 +++++++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index b823e598..4ebbb805 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-06-04 Guillem Jover + + * lib/dpkg-db.h (varbufinit): Add a size argument, fix all callers. + (varbuf::init): Add an optional size argument. + (varbuf::varbuf): Likewise. + * lib/varbuf.c (varbufinit): Allocate a buffer if size is not zero. + * lib/dbmodify.c (modstatdb_init): Use varbufinit instead of its + own initialization. + 2008-06-04 Guillem Jover * lib/dpkg-db.h (VARBUF_INIT): New macro. diff --git a/lib/dbmodify.c b/lib/dbmodify.c index 5c5a4311..1eacd5a6 100644 --- a/lib/dbmodify.c +++ b/lib/dbmodify.c @@ -191,9 +191,7 @@ enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritere if (cstatus >= msdbrw_write) { createimptmp(); - uvb.used= 0; - uvb.size= 10240; - uvb.buf= m_malloc(uvb.size); + varbufinit(&uvb, 10240); } trig_incorporate(cstatus, admindir); diff --git a/lib/dpkg-db.h b/lib/dpkg-db.h index fedaa3c6..d92ab5d8 100644 --- a/lib/dpkg-db.h +++ b/lib/dpkg-db.h @@ -372,7 +372,7 @@ extern void varbufaddc(struct varbuf *v, int c); extern void varbufdupc(struct varbuf *v, int c, ssize_t s); int varbufprintf(struct varbuf *v, const char *fmt, ...) PRINTFFORMAT(2,3); int varbufvprintf(struct varbuf *v, const char *fmt, va_list va); -void varbufinit(struct varbuf *v); +void varbufinit(struct varbuf *v, size_t size); void varbufreset(struct varbuf *v); void varbufextend(struct varbuf *v); void varbuffree(struct varbuf *v); @@ -399,9 +399,9 @@ struct varbuf { char *buf; #ifdef __cplusplus - void init() { varbufinit(this); } + void init(size_t _size = 0) { varbufinit(this, _size); } void free() { varbuffree(this); } - varbuf() { varbufinit(this); } + varbuf(size_t _size = 0) { varbufinit(this, _size); } ~varbuf() { varbuffree(this); } void operator()(int c) { varbufaddc(this,c); } void operator()(const char *s) { varbufaddstr(this,s); } diff --git a/lib/varbuf.c b/lib/varbuf.c index 3336f030..70f45117 100644 --- a/lib/varbuf.c +++ b/lib/varbuf.c @@ -87,10 +87,15 @@ void varbufaddbuf(struct varbuf *v, const void *s, const int l) { memcpy(v->buf + ou, s, l); } -void varbufinit(struct varbuf *v) { - /* NB: dbmodify.c does its own init to get a big buffer */ - v->size= v->used= 0; - v->buf= NULL; +void +varbufinit(struct varbuf *v, size_t size) +{ + v->used = 0; + v->size = size; + if (size) + v->buf = m_malloc(size); + else + v->buf = NULL; } void varbufreset(struct varbuf *v) { -- 2.39.5