compat: move strdup(3) replacement to its own file

Move our implementation of strdup(3) out of compat/nedmalloc/ and allow it to be used independently from USE_NED_ALLOCATOR. The original nedmalloc doesn't come with strdup() and doesn't need it. Only _users_ of nedmalloc need it, which was added when we imported it to our compat/ hierarchy. This reduces the difference of our copy of nedmalloc from the original, making it easier to update, and allows for easier testing and reusing of our version of strdup(). Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 3, 2016 at 17:59 UTC ca2baa3f7532dfe1816b623f39489ed10f3c9a49
4 files changed +33 -19
Makefile
+14 -3
@@ -296,6 +296,11 @@ all::
296 # Define USE_NED_ALLOCATOR if you want to replace the platforms default
297 # memory allocators with the nedmalloc allocator written by Niall Douglas.
298 #
299 +# Define OVERRIDE_STRDUP to override the libc version of strdup(3).
300 +# This is necessary when using a custom allocator in order to avoid
301 +# crashes due to allocation and free working on different 'heaps'.
302 +# It's defined automatically if USE_NED_ALLOCATOR is set.
303 +#
304 # Define NO_REGEX if you have no or inferior regex support in your C library.
305 #
306 # Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
@@ -1442,8 +1447,14 @@ ifdef NATIVE_CRLF
1447 endif
1448
1449 ifdef USE_NED_ALLOCATOR
1445 - COMPAT_CFLAGS += -Icompat/nedmalloc
1446 - COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
1450 + COMPAT_CFLAGS += -Icompat/nedmalloc
1451 + COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
1452 + OVERRIDE_STRDUP = YesPlease
1453 +endif
1454 +
1455 +ifdef OVERRIDE_STRDUP
1456 + COMPAT_CFLAGS += -DOVERRIDE_STRDUP
1457 + COMPAT_OBJS += compat/strdup.o
1458 endif
1459
1460 ifdef GIT_TEST_CMP_USE_COPIED_CONTEXT
@@ -1993,7 +2004,7 @@ endif
2004
2005 ifdef USE_NED_ALLOCATOR
2006 compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
1996 - -DNDEBUG -DOVERRIDE_STRDUP -DREPLACE_SYSTEM_ALLOCATOR
2007 + -DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR
2008 compat/nedmalloc/nedmalloc.sp: SPARSE_FLAGS += -Wno-non-pointer-null
2009 endif
2010
compat/nedmalloc/nedmalloc.c
-16
@@ -948,22 +948,6 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
948 return ret;
949 }
950
951 -#ifdef OVERRIDE_STRDUP
952 -/*
953 - * This implementation is purely there to override the libc version, to
954 - * avoid a crash due to allocation and free on different 'heaps'.
955 - */
956 -char *strdup(const char *s1)
957 -{
958 - size_t len = strlen(s1) + 1;
959 - char *s2 = malloc(len);
960 -
961 - if (s2)
962 - memcpy(s2, s1, len);
963 - return s2;
964 -}
965 -#endif
966 -
951 #if defined(__cplusplus)
952 }
953 #endif
compat/strdup.c new
+11
@@ -0,0 +1,11 @@
1 +#include "../git-compat-util.h"
2 +
3 +char *gitstrdup(const char *s1)
4 +{
5 + size_t len = strlen(s1) + 1;
6 + char *s2 = malloc(len);
7 +
8 + if (s2)
9 + memcpy(s2, s1, len);
10 + return s2;
11 +}
git-compat-util.h
+8
@@ -646,6 +646,14 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
646 const void *needle, size_t needlelen);
647 #endif
648
649 +#ifdef OVERRIDE_STRDUP
650 +#ifdef strdup
651 +#undef strdup
652 +#endif
653 +#define strdup gitstrdup
654 +char *gitstrdup(const char *s);
655 +#endif
656 +
657 #ifdef NO_GETPAGESIZE
658 #define getpagesize() sysconf(_SC_PAGESIZE)
659 #endif