1*6a5c9aabSmrg /* xmemdup.c -- Duplicate a memory buffer, using xmalloc.
2c3d31fe1Smrg    This trivial function is in the public domain.
3c3d31fe1Smrg    Jeff Garzik, September 1999.  */
4c3d31fe1Smrg 
5c3d31fe1Smrg /*
6c3d31fe1Smrg 
7af526226Smrg @deftypefn Replacement void* xmemdup (void *@var{input}, @
8af526226Smrg   size_t @var{copy_size}, size_t @var{alloc_size})
9c3d31fe1Smrg 
10c3d31fe1Smrg Duplicates a region of memory without fail.  First, @var{alloc_size} bytes
11c3d31fe1Smrg are allocated, then @var{copy_size} bytes from @var{input} are copied into
12c3d31fe1Smrg it, and the new memory is returned.  If fewer bytes are copied than were
13c3d31fe1Smrg allocated, the remaining memory is zeroed.
14c3d31fe1Smrg 
15c3d31fe1Smrg @end deftypefn
16c3d31fe1Smrg 
17c3d31fe1Smrg */
18c3d31fe1Smrg 
19c3d31fe1Smrg #ifdef HAVE_CONFIG_H
20c3d31fe1Smrg #include "config.h"
21c3d31fe1Smrg #endif
22c3d31fe1Smrg #include "ansidecl.h"
23c3d31fe1Smrg #include "libiberty.h"
24c3d31fe1Smrg 
25c3d31fe1Smrg #include <sys/types.h> /* For size_t. */
26c3d31fe1Smrg #ifdef HAVE_STRING_H
27c3d31fe1Smrg #include <string.h>
28c3d31fe1Smrg #else
29c3d31fe1Smrg # ifdef HAVE_STRINGS_H
30c3d31fe1Smrg #  include <strings.h>
31c3d31fe1Smrg # endif
32c3d31fe1Smrg #endif
33c3d31fe1Smrg 
34c3d31fe1Smrg PTR
xmemdup(const PTR input,size_t copy_size,size_t alloc_size)35c3d31fe1Smrg xmemdup (const PTR input, size_t copy_size, size_t alloc_size)
36c3d31fe1Smrg {
37*6a5c9aabSmrg   PTR output = xmalloc (alloc_size);
38*6a5c9aabSmrg   if (alloc_size > copy_size)
39*6a5c9aabSmrg     memset ((char *) output + copy_size, 0, alloc_size - copy_size);
40c3d31fe1Smrg   return (PTR) memcpy (output, input, copy_size);
41c3d31fe1Smrg }
42