1169695Skan/* xmemdup.c -- Duplicate a memory buffer, using xcalloc. 2169695Skan This trivial function is in the public domain. 3169695Skan Jeff Garzik, September 1999. */ 4169695Skan 5169695Skan/* 6169695Skan 7169695Skan@deftypefn Replacement void* xmemdup (void *@var{input}, size_t @var{copy_size}, size_t @var{alloc_size}) 8169695Skan 9169695SkanDuplicates a region of memory without fail. First, @var{alloc_size} bytes 10169695Skanare allocated, then @var{copy_size} bytes from @var{input} are copied into 11169695Skanit, and the new memory is returned. If fewer bytes are copied than were 12169695Skanallocated, the remaining memory is zeroed. 13169695Skan 14169695Skan@end deftypefn 15169695Skan 16169695Skan*/ 17169695Skan 18169695Skan#ifdef HAVE_CONFIG_H 19169695Skan#include "config.h" 20169695Skan#endif 21169695Skan#include "ansidecl.h" 22169695Skan#include "libiberty.h" 23169695Skan 24169695Skan#include <sys/types.h> /* For size_t. */ 25169695Skan#ifdef HAVE_STRING_H 26169695Skan#include <string.h> 27169695Skan#else 28169695Skan# ifdef HAVE_STRINGS_H 29169695Skan# include <strings.h> 30169695Skan# endif 31169695Skan#endif 32169695Skan 33169695SkanPTR 34169695Skanxmemdup (const PTR input, size_t copy_size, size_t alloc_size) 35169695Skan{ 36169695Skan PTR output = xcalloc (1, alloc_size); 37169695Skan return (PTR) memcpy (output, input, copy_size); 38169695Skan} 39