bfd: use less memory in string merging

Jan Beulich jbeulich@suse.com
Wed Nov 8 07:41:20 GMT 2023


On 07.11.2023 17:51, Michael Matz wrote:
> --- a/bfd/merge.c
> +++ b/bfd/merge.c
> @@ -767,6 +767,18 @@ record_section (struct sec_merge_info *sinfo,
>  
>    free (contents);
>    contents = NULL;
> +
> +  /* We allocate the ofsmap arrays in blocks of 2048 elements.
> +     In case we have very many small input files/sections,
> +     this might waste large amounts of memory, so reallocate these
> +     arrays here to their true size.  */
> +  amt = secinfo->noffsetmap + 1;
> +  secinfo->map_ofs = bfd_realloc (secinfo->map_ofs,
> +				  amt * sizeof(secinfo->map_ofs[0]));
> +  BFD_ASSERT (secinfo->map_ofs);
> +  secinfo->map = bfd_realloc (secinfo->map, amt * sizeof(secinfo->map[0]));
> +  BFD_ASSERT (secinfo->map);

Re-use of the same block when shrinking isn't guaranteed, so depending
on the underlying allocator this may actually add memory pressure (and
the allocations may therefore also fail). I think it would be nice to
be independent of such an implementation detail of the underlying
library. (It may also be worthwhile then to shrink the larger of the
two arrays first. Otoh the comment ahead of mapofs_type already
indicates that this type may need widening at some point.)

Jan


More information about the Binutils mailing list