Back to index

cell-binutils  2.17cvs20070401
xmemdup.c
Go to the documentation of this file.
00001 /* xmemdup.c -- Duplicate a memory buffer, using xcalloc.
00002    This trivial function is in the public domain.
00003    Jeff Garzik, September 1999.  */
00004 
00005 /*
00006 
00007 @deftypefn Replacement void* xmemdup (void *@var{input}, size_t @var{copy_size}, size_t @var{alloc_size})
00008 
00009 Duplicates a region of memory without fail.  First, @var{alloc_size} bytes
00010 are allocated, then @var{copy_size} bytes from @var{input} are copied into
00011 it, and the new memory is returned.  If fewer bytes are copied than were
00012 allocated, the remaining memory is zeroed.
00013 
00014 @end deftypefn
00015 
00016 */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include "config.h"
00020 #endif
00021 #include "ansidecl.h"
00022 #include "libiberty.h"
00023 
00024 #include <sys/types.h> /* For size_t. */
00025 #ifdef HAVE_STRING_H
00026 #include <string.h>
00027 #else
00028 # ifdef HAVE_STRINGS_H
00029 #  include <strings.h>
00030 # endif
00031 #endif
00032 
00033 PTR
00034 xmemdup (const PTR input, size_t copy_size, size_t alloc_size)
00035 {
00036   PTR output = xcalloc (1, alloc_size);
00037   return (PTR) memcpy (output, input, copy_size);
00038 }