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