This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=275726335d788f51e47a8c4911e04d038dfbd3f3 commit 275726335d788f51e47a8c4911e04d038dfbd3f3 Author: Guillem Jover <[email protected]> AuthorDate: Thu Jul 6 23:39:52 2023 +0200 libdpkg: Reorder varbuf functions Place the implementations in the order matching their usual life cycle. --- lib/dpkg/varbuf.c | 194 +++++++++++++++++++++++++++--------------------------- 1 file changed, 97 insertions(+), 97 deletions(-) diff --git a/lib/dpkg/varbuf.c b/lib/dpkg/varbuf.c index 4ef1f1e14..b605c1c4e 100644 --- a/lib/dpkg/varbuf.c +++ b/lib/dpkg/varbuf.c @@ -30,6 +30,75 @@ #include <dpkg/dpkg.h> #include <dpkg/dpkg-db.h> +struct varbuf * +varbuf_new(size_t size) +{ + struct varbuf *v; + + v = m_malloc(sizeof(*v)); + varbuf_init(v, size); + + return v; +} + +void +varbuf_init(struct varbuf *v, size_t size) +{ + v->used = 0; + v->size = size; + if (size) + v->buf = m_malloc(size); + else + v->buf = NULL; +} + +void +varbuf_grow(struct varbuf *v, size_t need_size) +{ + size_t new_size; + + /* Make sure the varbuf is in a sane state. */ + if (v->size < v->used) + internerr("varbuf used(%zu) > size(%zu)", v->used, v->size); + + /* Check if we already have enough room. */ + if ((v->size - v->used) >= need_size) + return; + + /* Check if we overflow. */ + new_size = (v->size + need_size) * 2; + if (new_size < v->size) + ohshit(_("cannot grow varbuf to size %zu; it would overflow"), + need_size); + + v->size = new_size; + v->buf = m_realloc(v->buf, v->size); +} + +void +varbuf_trunc(struct varbuf *v, size_t used_size) +{ + /* Make sure the caller does not claim more than available. */ + if (v->size < used_size) + internerr("varbuf new_used(%zu) > size(%zu)", used_size, v->size); + + v->used = used_size; +} + +void +varbuf_reset(struct varbuf *v) +{ + v->used = 0; +} + +const char * +varbuf_get_str(struct varbuf *v) +{ + varbuf_end_str(v); + + return v->buf; +} + void varbuf_add_char(struct varbuf *v, int c) { @@ -57,41 +126,12 @@ varbuf_map_char(struct varbuf *v, int c_src, int c_dst) v->buf[i] = c_dst; } -int -varbuf_printf(struct varbuf *v, const char *fmt, ...) -{ - int r; - va_list args; - - va_start(args, fmt); - r = varbuf_vprintf(v, fmt, args); - va_end(args); - - return r; -} - -int -varbuf_vprintf(struct varbuf *v, const char *fmt, va_list args) +void +varbuf_add_dir(struct varbuf *v, const char *dirname) { - va_list args_copy; - int needed, r; - - va_copy(args_copy, args); - needed = vsnprintf(NULL, 0, fmt, args_copy); - va_end(args_copy); - - if (needed < 0) - ohshite(_("error formatting string into varbuf variable")); - - varbuf_grow(v, needed + 1); - - r = vsnprintf(v->buf + v->used, needed + 1, fmt, args); - if (r < 0) - ohshite(_("error formatting string into varbuf variable")); - - v->used += r; - - return r; + varbuf_add_str(v, dirname); + if (v->used == 0 || v->buf[v->used - 1] != '/') + varbuf_add_char(v, '/'); } void @@ -104,14 +144,6 @@ varbuf_add_buf(struct varbuf *v, const void *s, size_t size) v->used += size; } -void -varbuf_add_dir(struct varbuf *v, const char *dirname) -{ - varbuf_add_str(v, dirname); - if (v->used == 0 || v->buf[v->used - 1] != '/') - varbuf_add_char(v, '/'); -} - void varbuf_end_str(struct varbuf *v) { @@ -119,73 +151,41 @@ varbuf_end_str(struct varbuf *v) v->buf[v->used] = '\0'; } -const char * -varbuf_get_str(struct varbuf *v) +int +varbuf_vprintf(struct varbuf *v, const char *fmt, va_list args) { - varbuf_end_str(v); + va_list args_copy; + int needed, r; - return v->buf; -} + va_copy(args_copy, args); + needed = vsnprintf(NULL, 0, fmt, args_copy); + va_end(args_copy); -struct varbuf * -varbuf_new(size_t size) -{ - struct varbuf *v; + if (needed < 0) + ohshite(_("error formatting string into varbuf variable")); - v = m_malloc(sizeof(*v)); - varbuf_init(v, size); + varbuf_grow(v, needed + 1); - return v; -} + r = vsnprintf(v->buf + v->used, needed + 1, fmt, args); + if (r < 0) + ohshite(_("error formatting string into varbuf variable")); -void -varbuf_init(struct varbuf *v, size_t size) -{ - v->used = 0; - v->size = size; - if (size) - v->buf = m_malloc(size); - else - v->buf = NULL; -} + v->used += r; -void -varbuf_reset(struct varbuf *v) -{ - v->used = 0; + return r; } -void -varbuf_grow(struct varbuf *v, size_t need_size) +int +varbuf_printf(struct varbuf *v, const char *fmt, ...) { - size_t new_size; - - /* Make sure the varbuf is in a sane state. */ - if (v->size < v->used) - internerr("varbuf used(%zu) > size(%zu)", v->used, v->size); - - /* Check if we already have enough room. */ - if ((v->size - v->used) >= need_size) - return; - - /* Check if we overflow. */ - new_size = (v->size + need_size) * 2; - if (new_size < v->size) - ohshit(_("cannot grow varbuf to size %zu; it would overflow"), - need_size); - - v->size = new_size; - v->buf = m_realloc(v->buf, v->size); -} + int r; + va_list args; -void -varbuf_trunc(struct varbuf *v, size_t used_size) -{ - /* Make sure the caller does not claim more than available. */ - if (v->size < used_size) - internerr("varbuf new_used(%zu) > size(%zu)", used_size, v->size); + va_start(args, fmt); + r = varbuf_vprintf(v, fmt, args); + va_end(args); - v->used = used_size; + return r; } void -- Dpkg.Org's dpkg

