From 08e5bd5b725de9a8437952b0a9673c7b969df405 Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Sat, 19 Apr 2008 15:50:18 +0000 Subject: [PATCH] Calculate string size within dm_pool_grow_object. --- WHATS_NEW_DM | 1 + libdm/libdevmapper.h | 3 ++- libdm/libdm-report.c | 12 ++++-------- libdm/mm/pool-debug.c | 6 +++--- libdm/mm/pool-fast.c | 15 +++++++++------ tools/dmsetup.c | 6 +++--- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index f81189935..852fbdb56 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,5 +1,6 @@ Version 1.02.26 - ================================= + Calculate string size within dm_pool_grow_object. Version 1.02.25 - 10th April 2008 ================================= diff --git a/libdm/libdevmapper.h b/libdm/libdevmapper.h index ef2b5301b..8d55d477f 100644 --- a/libdm/libdevmapper.h +++ b/libdm/libdevmapper.h @@ -500,7 +500,7 @@ void dm_pool_free(struct dm_pool *p, void *ptr); * * for (i = 0; i < 50; i++) { * snprintf(buffer, sizeof(buffer), "%d, ", i); - * if (!dm_pool_grow_object(mem, buffer, strlen(buffer))) + * if (!dm_pool_grow_object(mem, buffer, 0)) * goto bad; * } * @@ -524,6 +524,7 @@ void dm_pool_free(struct dm_pool *p, void *ptr); * dm_pool_grow_object. Finally get your object with * a call to dm_pool_end_object. * + * Setting delta to 0 means it will use strlen(extra). */ int dm_pool_begin_object(struct dm_pool *p, size_t hint); int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta); diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c index d8bd791f2..71004dd1f 100644 --- a/libdm/libdm-report.c +++ b/libdm/libdm-report.c @@ -668,15 +668,13 @@ static int _report_headings(struct dm_report *rh) log_error("dm_report: Failed to generate report headings for printing"); goto bad; } - } else if (!dm_pool_grow_object(rh->mem, heading, - strlen(heading))) { + } else if (!dm_pool_grow_object(rh->mem, heading, 0)) { log_error("dm_report: Failed to generate report headings for printing"); goto bad; } if (!list_end(&rh->field_props, &fp->list)) - if (!dm_pool_grow_object(rh->mem, rh->separator, - strlen(rh->separator))) { + if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) { log_error("dm_report: Failed to generate report headings for printing"); goto bad; } @@ -803,8 +801,7 @@ int dm_report_output(struct dm_report *rh) repstr = field->report_string; width = field->props->width; if (!(rh->flags & DM_REPORT_OUTPUT_ALIGNED)) { - if (!dm_pool_grow_object(rh->mem, repstr, - strlen(repstr))) { + if (!dm_pool_grow_object(rh->mem, repstr, 0)) { log_error("dm_report: Unable to extend output line"); goto bad; } @@ -836,8 +833,7 @@ int dm_report_output(struct dm_report *rh) } if (!list_end(&row->fields, fh)) - if (!dm_pool_grow_object(rh->mem, rh->separator, - strlen(rh->separator))) { + if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) { log_error("dm_report: Unable to extend output line"); goto bad; } diff --git a/libdm/mm/pool-debug.c b/libdm/mm/pool-debug.c index 1700c59f9..3f762efc2 100644 --- a/libdm/mm/pool-debug.c +++ b/libdm/mm/pool-debug.c @@ -216,10 +216,10 @@ int dm_pool_begin_object(struct dm_pool *p, size_t init_size) return 1; } -int dm_pool_grow_object(struct dm_pool *p, const void *buffer, size_t delta) +int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta) { struct block *new; - size_t size = delta; + size_t size = delta ? : strlen(extra); assert(p->begun); @@ -238,7 +238,7 @@ int dm_pool_grow_object(struct dm_pool *p, const void *buffer, size_t delta) } p->object = new; - memcpy(new->data + size - delta, buffer, delta); + memcpy(new->data + size - delta, extra, delta); return 1; } diff --git a/libdm/mm/pool-fast.c b/libdm/mm/pool-fast.c index 4071c72d4..0df8b01fb 100644 --- a/libdm/mm/pool-fast.c +++ b/libdm/mm/pool-fast.c @@ -162,14 +162,17 @@ int dm_pool_begin_object(struct dm_pool *p, size_t hint) return 1; } -int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t n) +int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta) { struct chunk *c = p->chunk, *nc; - if (c->end - (c->begin + p->object_len) < n) { + if (!delta) + delta = strlen(extra); + + if (c->end - (c->begin + p->object_len) < delta) { /* move into a new chunk */ - if (p->object_len + n > (p->chunk_size / 2)) - nc = _new_chunk(p, (p->object_len + n) * 2); + if (p->object_len + delta > (p->chunk_size / 2)) + nc = _new_chunk(p, (p->object_len + delta) * 2); else nc = _new_chunk(p, p->chunk_size); @@ -181,8 +184,8 @@ int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t n) c = p->chunk; } - memcpy(c->begin + p->object_len, extra, n); - p->object_len += n; + memcpy(c->begin + p->object_len, extra, delta); + p->object_len += delta; return 1; } diff --git a/tools/dmsetup.c b/tools/dmsetup.c index 3432402b9..d5a3ce9c7 100644 --- a/tools/dmsetup.c +++ b/tools/dmsetup.c @@ -1697,7 +1697,7 @@ static int _dm_tree_names(struct dm_report *rh, struct dm_pool *mem, log_error("dm_pool_grow_object failed"); goto out_abandon; } - if (!dm_pool_grow_object(mem, name, strlen(name))) { + if (!dm_pool_grow_object(mem, name, 0)) { log_error("dm_pool_grow_object failed"); goto out_abandon; } @@ -1763,7 +1763,7 @@ static int _dm_tree_parents_devs_disp(struct dm_report *rh, struct dm_pool *mem, log_error("dm_snprintf failed"); goto out_abandon; } - if (!dm_pool_grow_object(mem, buf, strlen(buf))) { + if (!dm_pool_grow_object(mem, buf, 0)) { log_error("dm_pool_grow_object failed"); goto out_abandon; } @@ -1816,7 +1816,7 @@ static int _dm_deps_disp(struct dm_report *rh, struct dm_pool *mem, log_error("dm_snprintf failed"); goto out_abandon; } - if (!dm_pool_grow_object(mem, buf, strlen(buf))) { + if (!dm_pool_grow_object(mem, buf, 0)) { log_error("dm_pool_grow_object failed"); goto out_abandon; }