Merge branch 'rs/compat-strdup' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 19 Sep 2016 20:51:42 +0000 (13:51 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 19 Sep 2016 20:51:42 +0000 (13:51 -0700)
Code cleanup.

* rs/compat-strdup:
compat: move strdup(3) replacement to its own file

Makefile
compat/nedmalloc/nedmalloc.c
compat/strdup.c [new file with mode: 0644]
git-compat-util.h
index d96ecb7141a12f1fe47246805c5d4e4e562a5c86..7f184923ddc6fa5089b64ced3cdca405758468e1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -296,6 +296,11 @@ all::
 # Define USE_NED_ALLOCATOR if you want to replace the platforms default
 # memory allocators with the nedmalloc allocator written by Niall Douglas.
 #
+# Define OVERRIDE_STRDUP to override the libc version of strdup(3).
+# This is necessary when using a custom allocator in order to avoid
+# crashes due to allocation and free working on different 'heaps'.
+# It's defined automatically if USE_NED_ALLOCATOR is set.
+#
 # Define NO_REGEX if you have no or inferior regex support in your C library.
 #
 # Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
@@ -1456,8 +1461,14 @@ ifdef NATIVE_CRLF
 endif
 
 ifdef USE_NED_ALLOCATOR
-       COMPAT_CFLAGS += -Icompat/nedmalloc
-       COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
+       COMPAT_CFLAGS += -Icompat/nedmalloc
+       COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
+       OVERRIDE_STRDUP = YesPlease
+endif
+
+ifdef OVERRIDE_STRDUP
+       COMPAT_CFLAGS += -DOVERRIDE_STRDUP
+       COMPAT_OBJS += compat/strdup.o
 endif
 
 ifdef GIT_TEST_CMP_USE_COPIED_CONTEXT
@@ -2029,7 +2040,7 @@ endif
 
 ifdef USE_NED_ALLOCATOR
 compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
-       -DNDEBUG -DOVERRIDE_STRDUP -DREPLACE_SYSTEM_ALLOCATOR
+       -DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR
 compat/nedmalloc/nedmalloc.sp: SPARSE_FLAGS += -Wno-non-pointer-null
 endif
 
index 2d4ef59013823d3c7caac8b705fbd4916cfe8fd6..1cc31c350223b84cd97939afccf1bcc1caa070b4 100644 (file)
@@ -948,22 +948,6 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
        return ret;
 }
 
-#ifdef OVERRIDE_STRDUP
-/*
- * This implementation is purely there to override the libc version, to
- * avoid a crash due to allocation and free on different 'heaps'.
- */
-char *strdup(const char *s1)
-{
-       size_t len = strlen(s1) + 1;
-       char *s2 = malloc(len);
-
-       if (s2)
-               memcpy(s2, s1, len);
-       return s2;
-}
-#endif
-
 #if defined(__cplusplus)
 }
 #endif
diff --git a/compat/strdup.c b/compat/strdup.c
new file mode 100644 (file)
index 0000000..f3fb978
--- /dev/null
@@ -0,0 +1,11 @@
+#include "../git-compat-util.h"
+
+char *gitstrdup(const char *s1)
+{
+       size_t len = strlen(s1) + 1;
+       char *s2 = malloc(len);
+
+       if (s2)
+               memcpy(s2, s1, len);
+       return s2;
+}
index 2ad45b3f759336acbca40499e737eb2d68a44669..68615b1a6a4db0f4ba7b4ce543e00e31e3b1705e 100644 (file)
@@ -664,6 +664,14 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
                 const void *needle, size_t needlelen);
 #endif
 
+#ifdef OVERRIDE_STRDUP
+#ifdef strdup
+#undef strdup
+#endif
+#define strdup gitstrdup
+char *gitstrdup(const char *s);
+#endif
+
 #ifdef NO_GETPAGESIZE
 #define getpagesize() sysconf(_SC_PAGESIZE)
 #endif