175f9f1baSchristos /* xmemdup.c -- Duplicate a memory buffer, using xmalloc.
22a6b7db3Sskrll    This trivial function is in the public domain.
32a6b7db3Sskrll    Jeff Garzik, September 1999.  */
42a6b7db3Sskrll 
52a6b7db3Sskrll /*
62a6b7db3Sskrll 
705caefcfSchristos @deftypefn Replacement void* xmemdup (void *@var{input}, @
805caefcfSchristos   size_t @var{copy_size}, size_t @var{alloc_size})
92a6b7db3Sskrll 
102a6b7db3Sskrll Duplicates a region of memory without fail.  First, @var{alloc_size} bytes
112a6b7db3Sskrll are allocated, then @var{copy_size} bytes from @var{input} are copied into
122a6b7db3Sskrll it, and the new memory is returned.  If fewer bytes are copied than were
132a6b7db3Sskrll allocated, the remaining memory is zeroed.
142a6b7db3Sskrll 
152a6b7db3Sskrll @end deftypefn
162a6b7db3Sskrll 
172a6b7db3Sskrll */
182a6b7db3Sskrll 
192a6b7db3Sskrll #ifdef HAVE_CONFIG_H
202a6b7db3Sskrll #include "config.h"
212a6b7db3Sskrll #endif
222a6b7db3Sskrll #include "ansidecl.h"
232a6b7db3Sskrll #include "libiberty.h"
242a6b7db3Sskrll 
252a6b7db3Sskrll #include <sys/types.h> /* For size_t. */
262a6b7db3Sskrll #ifdef HAVE_STRING_H
272a6b7db3Sskrll #include <string.h>
282a6b7db3Sskrll #else
292a6b7db3Sskrll # ifdef HAVE_STRINGS_H
302a6b7db3Sskrll #  include <strings.h>
312a6b7db3Sskrll # endif
322a6b7db3Sskrll #endif
332a6b7db3Sskrll 
34*f22f0ef4Schristos void *
xmemdup(const void * input,size_t copy_size,size_t alloc_size)35*f22f0ef4Schristos xmemdup (const void *input, size_t copy_size, size_t alloc_size)
362a6b7db3Sskrll {
37*f22f0ef4Schristos   void *output = xmalloc (alloc_size);
3875f9f1baSchristos   if (alloc_size > copy_size)
3975f9f1baSchristos     memset ((char *) output + copy_size, 0, alloc_size - copy_size);
40*f22f0ef4Schristos   return (void *) memcpy (output, input, copy_size);
412a6b7db3Sskrll }
42