160484Sobrien/* xmemdup.c -- Duplicate a memory buffer, using xcalloc.
260484Sobrien   This trivial function is in the public domain.
360484Sobrien   Jeff Garzik, September 1999.  */
460484Sobrien
589857Sobrien/*
689857Sobrien
789857Sobrien@deftypefn Replacement void* xmemdup (void *@var{input}, size_t @var{copy_size}, size_t @var{alloc_size})
889857Sobrien
989857SobrienDuplicates a region of memory without fail.  First, @var{alloc_size} bytes
1089857Sobrienare allocated, then @var{copy_size} bytes from @var{input} are copied into
1189857Sobrienit, and the new memory is returned.  If fewer bytes are copied than were
1289857Sobrienallocated, the remaining memory is zeroed.
1389857Sobrien
1489857Sobrien@end deftypefn
1589857Sobrien
1689857Sobrien*/
1789857Sobrien
1860484Sobrien#ifdef HAVE_CONFIG_H
1960484Sobrien#include "config.h"
2060484Sobrien#endif
2160484Sobrien#include "ansidecl.h"
2260484Sobrien#include "libiberty.h"
2360484Sobrien
2460484Sobrien#include <sys/types.h> /* For size_t. */
2577298Sobrien#ifdef HAVE_STRING_H
2677298Sobrien#include <string.h>
27218822Sdim#else
28218822Sdim# ifdef HAVE_STRINGS_H
29218822Sdim#  include <strings.h>
30218822Sdim# endif
3177298Sobrien#endif
3260484Sobrien
3360484SobrienPTR
34218822Sdimxmemdup (const PTR input, size_t copy_size, size_t alloc_size)
3560484Sobrien{
3660484Sobrien  PTR output = xcalloc (1, alloc_size);
37218822Sdim  return (PTR) memcpy (output, input, copy_size);
3860484Sobrien}
39