1 /* xmemdup.c -- Duplicate a memory buffer, using xmalloc.
2 This trivial function is in the public domain.
3 Jeff Garzik, September 1999. */
7 @deftypefn Replacement void* xmemdup (void *@var{input}, @
8 size_t @var{copy_size}, size_t @var{alloc_size})
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.
23 #include "libiberty.h"
25 #include <sys/types.h> /* For size_t. */
29 # ifdef HAVE_STRINGS_H
35 xmemdup (const PTR input
, size_t copy_size
, size_t alloc_size
)
37 PTR output
= xmalloc (alloc_size
);
38 if (alloc_size
> copy_size
)
39 memset ((char *) output
+ copy_size
, 0, alloc_size
- copy_size
);
40 return (PTR
) memcpy (output
, input
, copy_size
);