]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/xmemdup.c
AArch64: Cleanup memset expansion
[thirdparty/gcc.git] / libiberty / xmemdup.c
CommitLineData
051154a1 1/* xmemdup.c -- Duplicate a memory buffer, using xmalloc.
b10647f1
JG
2 This trivial function is in the public domain.
3 Jeff Garzik, September 1999. */
4
aaa5f039
DD
5/*
6
996c0cb0
RW
7@deftypefn Replacement void* xmemdup (void *@var{input}, @
8 size_t @var{copy_size}, size_t @var{alloc_size})
aaa5f039
DD
9
10Duplicates a region of memory without fail. First, @var{alloc_size} bytes
11are allocated, then @var{copy_size} bytes from @var{input} are copied into
12it, and the new memory is returned. If fewer bytes are copied than were
13allocated, the remaining memory is zeroed.
14
15@end deftypefn
16
17*/
18
b10647f1
JG
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22#include "ansidecl.h"
23#include "libiberty.h"
24
33c5ddcd 25#include <sys/types.h> /* For size_t. */
d11ec6f0
ZW
26#ifdef HAVE_STRING_H
27#include <string.h>
bb99744f
KG
28#else
29# ifdef HAVE_STRINGS_H
30# include <strings.h>
31# endif
d11ec6f0 32#endif
33c5ddcd 33
50b009c5
ML
34void *
35xmemdup (const void *input, size_t copy_size, size_t alloc_size)
b10647f1 36{
50b009c5 37 void *output = xmalloc (alloc_size);
051154a1
AM
38 if (alloc_size > copy_size)
39 memset ((char *) output + copy_size, 0, alloc_size - copy_size);
50b009c5 40 return (void *) memcpy (output, input, copy_size);
b10647f1 41}