This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: PATCH to add xmemdup to libiberty


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;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]