This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: PATCH to add xmemdup to libiberty
- To: garzik at havoc dot gtf dot org
- Subject: Re: PATCH to add xmemdup to libiberty
- From: Marc Espie <espie at quatramaran dot ens dot fr>
- Date: Wed, 8 Sep 1999 11:27:04 +0200
- Cc: egcs-patches at egcs dot cygnus dot com
- Organization: Ecole Normale Superieure de Paris
In article <199909072040.QAA29321@havoc.gtf.org> you write:
>PTR
>xmemdup (input, copy_size, alloc_size)
> const PTR input;
> size_t copy_size;
> size_t alloc_size;
>{
> PTR output = xcalloc (1, alloc_size);
> memcpy (output, input, copy_size);
> return output;
>}
Looks wasteful.
BTW, I think libiberty functions traditionally check xmalloc result,
so that someone may use them with something else than libiberty's xmalloc.
How about
PTR output = xmalloc(alloc_size);
if (output) {
memcpy(output, input, copy_size);
if (alloc_size > copy_size)
memset(output + copy_size, 0, alloc_size - copy_size);
}
return output;