From: Ben Pfaff Date: Wed, 30 Apr 2008 22:11:40 +0000 (-0700) Subject: New utility function xmemdup(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c411bcb409b32adb2ce00aacfcfd6ecadf09edb;p=openvswitch New utility function xmemdup(). --- diff --git a/include/util.h b/include/util.h index edcd8156..8b0bb402 100644 --- a/include/util.h +++ b/include/util.h @@ -80,6 +80,7 @@ void set_program_name(const char *); void *xmalloc(size_t); void *xcalloc(size_t, size_t); void *xrealloc(void *, size_t); +void *xmemdup(const void *, size_t); char *xstrdup(const char *); char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2); diff --git a/lib/util.c b/lib/util.c index 846ac880..04fcd90d 100644 --- a/lib/util.c +++ b/lib/util.c @@ -75,13 +75,18 @@ xrealloc(void *p, size_t size) return p; } +void * +xmemdup(const void *p_, size_t size) +{ + void *p = xmalloc(size); + memcpy(p, p_, size); + return p; +} + char * -xstrdup(const char *s_) +xstrdup(const char *s) { - size_t size = strlen(s_) + 1; - char *s = xmalloc(size); - memcpy(s, s_, size); - return s; + return xmemdup(s, strlen(s) + 1); } char *