Add memdup() helper
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Fri, 2 Dec 2011 19:21:18 +0000 (17:21 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Sat, 3 Dec 2011 06:07:15 +0000 (04:07 -0200)
libkmod/libkmod-private.h
libkmod/libkmod-util.c

index c589a22..f2797b4 100644 (file)
@@ -80,5 +80,6 @@ char *getline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1)
 char *underscores(struct kmod_ctx *ctx, char *s) __attribute__((nonnull(1, 2)));
 #define streq(a, b) (strcmp((a), (b)) == 0)
 bool startswith(const char *s, const char *prefix) __attribute__((nonnull(1, 2)));
+void *memdup(const void *p, size_t n) __attribute__((nonnull(1));
 
 #endif
index 556dfcd..b6e3cfc 100644 (file)
@@ -133,3 +133,13 @@ bool startswith(const char *s, const char *prefix) {
 
         return memcmp(s, prefix, pl) == 0;
 }
+
+inline void *memdup(const void *p, size_t n)
+{
+       void *r = malloc(n);
+
+       if (r == NULL)
+               return NULL;
+
+       return memcpy(r, p, n);
+}