[libvirt PATCH 2/3] virbitmap: remove redundant mem_alloc

Peter Krempa pkrempa at redhat.com
Mon Aug 3 17:27:31 UTC 2020


On Mon, Aug 03, 2020 at 19:18:52 +0200, Ján Tomko wrote:
> We have two variables tracking the size of the map:
> map_len and mem_alloc.
> 
> Remove mem_alloc as well as code keeping them in sync.
> 
> Signed-off-by: Ján Tomko <jtomko at redhat.com>
> Fixes: 917426c8d7dd26f13142fc4c5c1a8a19137ac647
> ---
>  src/util/virbitmap.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)
> 
> diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
> index 60fd8491dd..4c205016ff 100644
> --- a/src/util/virbitmap.c
> +++ b/src/util/virbitmap.c

[...]

> @@ -197,13 +195,12 @@ virBitmapExpand(virBitmapPtr map,
>  
>      /* resize the memory if necessary */
>      if (map->map_len < new_len) {
> -        if (VIR_RESIZE_N(map->map, map->map_alloc, map->map_len,
> +        if (VIR_RESIZE_N(map->map, map->map_len, map->map_len,
>                           new_len - map->map_len) < 0)
>              return -1;

 * VIR_RESIZE_N:
 * @ptr: pointer to hold address of allocated memory
 * @alloc: variable tracking number of elements currently allocated
 * @count: number of elements currently in use
 * @add: minimum number of elements to additionally support
 *
 * Blindly using VIR_EXPAND_N(array, alloc, 1) in a loop scales
 * quadratically, because every iteration must copy contents from
 * all prior iterations.  But amortized linear scaling can be achieved
 * by tracking allocation size separately from the number of used
 * elements, and growing geometrically only as needed.
 *
 * If 'count' + 'add' is larger than 'alloc', then geometrically reallocate
 * the array of 'alloc' elements, each sizeof(*ptr) bytes long, and store
 * the address of allocated memory in 'ptr' and the new size in 'alloc'.
 * The new elements are filled with zero.




More information about the libvir-list mailing list