Skip to content

Commit

Permalink
compat: move strdup(3) replacement to its own file
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
rscharfe authored and gitster committed Sep 7, 2016
1 parent e0c1cea commit ca2baa3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1442,8 +1447,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
Expand Down Expand Up @@ -1993,7 +2004,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

Expand Down
16 changes: 0 additions & 16 deletions compat/nedmalloc/nedmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions compat/strdup.c
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,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
Expand Down

0 comments on commit ca2baa3

Please sign in to comment.