From 0c344bc22906f0e166f01c1b2075ecfba7dac40f Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 23 Jul 2007 22:11:54 +0200 Subject: [PATCH 001/253] Skip some git diff tests if the file system does not support funny names. Signed-off-by: Johannes Sixt --- t/t3902-quoted.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/t/t3902-quoted.sh b/t/t3902-quoted.sh index 63f950b179..e8ad85afe4 100755 --- a/t/t3902-quoted.sh +++ b/t/t3902-quoted.sh @@ -14,6 +14,13 @@ LF=' ' DQ='"' +echo foo > "Name and an${HT}HT" +test -f "Name and an${HT}HT" || { + # since FAT/NTFS does not allow tabs in filenames, skip this test + say 'Your filesystem does not allow tabs in filenames, test skipped.' + test_done +} + for_each_name () { for name in \ Name "Name and a${LF}LF" "Name and an${HT}HT" "Name${DQ}" \ From 5c52dc4bf0d53c75b473fa60ffafa4e2e961dc55 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 23 Jul 2007 22:18:36 +0200 Subject: [PATCH 002/253] update-server-info is not supported on MinGW. Skip tests. --- t/t1301-shared-repo.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/t1301-shared-repo.sh b/t/t1301-shared-repo.sh index bb5f30220a..3bf78ae619 100755 --- a/t/t1301-shared-repo.sh +++ b/t/t1301-shared-repo.sh @@ -14,6 +14,10 @@ test_expect_success 'shared=all' ' test 2 = $(git config core.sharedrepository) ' +say "update-server-info not supported - skipping tests" +test_done +exit 0 + test_expect_success 'update-server-info honors core.sharedRepository' ' : > a1 && git add a1 && From f0ec63a99a4925804eecd69c792e0ee600871b73 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sun, 10 Jun 2007 20:37:42 +0200 Subject: [PATCH 003/253] Install 'core.symlinks false' under MinGW. --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 760b5448be..47b1fdc326 100644 --- a/Makefile +++ b/Makefile @@ -722,6 +722,7 @@ bindir_SQ = $(subst ','\'',$(bindir)) gitexecdir_SQ = $(subst ','\'',$(gitexecdir)) template_dir_SQ = $(subst ','\'',$(template_dir)) prefix_SQ = $(subst ','\'',$(prefix)) +sysconfdir_SQ = $(subst ','\'',$(sysconfdir)) SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH)) PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH)) @@ -1001,8 +1002,11 @@ remove-dashes: install: all $(INSTALL) -d -m755 '$(DESTDIR_SQ)$(bindir_SQ)' $(INSTALL) -d -m755 '$(DESTDIR_SQ)$(gitexecdir_SQ)' + $(INSTALL) -d -m755 '$(DESTDIR_SQ)$(sysconfdir_SQ)' $(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(INSTALL) git$X '$(DESTDIR_SQ)$(bindir_SQ)' + GIT_CONFIG='$(DESTDIR_SQ)$(sysconfdir_SQ)/gitconfig' \ + $(DESTDIR_SQ)$(bindir_SQ)/git-config$X core.symlinks false $(MAKE) -C templates DESTDIR='$(DESTDIR_SQ)' install $(MAKE) -C perl prefix='$(prefix_SQ)' install ifndef NO_TCLTK From 631f6e62498bb0439be2718c04a4f5e76baaeec0 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 12 Jan 2007 16:41:22 +0100 Subject: [PATCH 004/253] Fake reencoding success under NO_ICONV instead of returning NULL. git-am when invoked from git-rebase seems to rely on successful conversion. --- utf8.c | 7 +++++++ utf8.h | 4 ---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/utf8.c b/utf8.c index 4efef6faf7..5949b4d2d4 100644 --- a/utf8.c +++ b/utf8.c @@ -353,4 +353,11 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e iconv_close(conv); return out; } +#else +char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding) +{ + if (!in_encoding) + return NULL; + return xstrdup(in); +} #endif diff --git a/utf8.h b/utf8.h index 15db6f1f27..a8b8c2f4da 100644 --- a/utf8.h +++ b/utf8.h @@ -7,10 +7,6 @@ int is_encoding_utf8(const char *name); int print_wrapped_text(const char *text, int indent, int indent2, int len); -#ifndef NO_ICONV char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding); -#else -#define reencode_string(a,b,c) NULL -#endif #endif From f5f46bfe24411ce8b4955df4cc5b2e0d78f0a0c5 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 21 Jul 2007 23:21:54 +0200 Subject: [PATCH 005/253] Work around a CR being written by git-tag if it is redirected to a file. Where the heck does this come from? A useless use of cat helps. --- t/t7004-tag.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 17de2a90e6..99b417ada5 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -108,9 +108,9 @@ mytag EOF test_expect_success \ 'trying to delete tags without params should succeed and do nothing' ' - git tag -l > actual && git diff expect actual && + git tag -l | cat > actual && git diff expect actual && git-tag -d && - git tag -l > actual && git diff expect actual + git tag -l | cat > actual && git diff expect actual ' test_expect_success \ @@ -164,7 +164,7 @@ test_expect_success 'listing all tags should print them ordered' ' git tag a1 && git tag v1.0 && git tag t210 && - git tag -l > actual && + git tag -l | cat > actual && git diff expect actual ' From 64e9bd1483cea1fee0dd1b76ee988e20a0b997fb Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 23 Jul 2007 22:19:06 +0200 Subject: [PATCH 006/253] Skip some tests that depend on technology that we have not dealt with. --- t/t3900-i18n-commit.sh | 4 ++++ t/t3901-i18n-patch.sh | 4 ++++ t/t4121-apply-diffs.sh | 2 ++ t/t5100-mailinfo.sh | 4 ++++ t/t5302-pack-index.sh | 2 ++ t/t5502-quickfetch.sh | 3 +++ t/t6023-merge-file.sh | 3 +++ t/t7003-filter-branch.sh | 3 +++ t/t9001-send-email.sh | 4 ++++ t/t9200-git-cvsexportcommit.sh | 4 ++++ t/t9300-fast-import.sh | 4 ++++ 11 files changed, 37 insertions(+) diff --git a/t/t3900-i18n-commit.sh b/t/t3900-i18n-commit.sh index fcbabe8ec3..4886d9f37b 100755 --- a/t/t3900-i18n-commit.sh +++ b/t/t3900-i18n-commit.sh @@ -7,6 +7,10 @@ test_description='commit and log output encodings' . ./test-lib.sh +say "iconv not supported, skipping tests." +test_done +exit 0 + compare_with () { git show -s $1 | sed -e '1,/^$/d' -e 's/^ //' -e '$d' >current && git diff current "$2" diff --git a/t/t3901-i18n-patch.sh b/t/t3901-i18n-patch.sh index 28e9e372f3..41e0d42c62 100755 --- a/t/t3901-i18n-patch.sh +++ b/t/t3901-i18n-patch.sh @@ -7,6 +7,10 @@ test_description='i18n settings and format-patch | am pipe' . ./test-lib.sh +say "iconv not supported, skipping tests." +test_done +exit 0 + check_encoding () { # Make sure characters are not corrupted cnt="$1" header="$2" i=1 j=0 bad=0 diff --git a/t/t4121-apply-diffs.sh b/t/t4121-apply-diffs.sh index aff551a1d7..d93b61d8c4 100755 --- a/t/t4121-apply-diffs.sh +++ b/t/t4121-apply-diffs.sh @@ -3,6 +3,8 @@ test_description='git apply for contextually independent diffs' . ./test-lib.sh +test_done + echo '1 2 3 diff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh index 9b1a74542a..0dcab1fbfe 100755 --- a/t/t5100-mailinfo.sh +++ b/t/t5100-mailinfo.sh @@ -7,6 +7,10 @@ test_description='git mailinfo and git mailsplit test' . ./test-lib.sh +say "git-mailinfo does not work yet; skipping tests." +test_done +exit 0 + test_expect_success 'split sample box' \ 'git mailsplit -o. ../t5100/sample.mbox >last && last=`cat last` && diff --git a/t/t5302-pack-index.sh b/t/t5302-pack-index.sh index 4f58c4c3f9..06b2f38d75 100755 --- a/t/t5302-pack-index.sh +++ b/t/t5302-pack-index.sh @@ -59,6 +59,7 @@ test_expect_success \ 'cmp "test-1-${pack1}.idx" "1.idx" && cmp "test-2-${pack2}.idx" "2.idx"' +false && { test_expect_success \ 'index v2: force some 64-bit offsets with pack-objects' \ 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 merge.err && grep "Cannot merge binary files" merge.err diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh index 4ddd656e84..7ec74172ae 100755 --- a/t/t7003-filter-branch.sh +++ b/t/t7003-filter-branch.sh @@ -3,6 +3,9 @@ test_description='git-filter-branch' . ./test-lib.sh +say "filter-branch has not been taken care of - skipping tests" +test_done + make_commit () { lower=$(echo $1 | tr A-Z a-z) echo $lower > $lower diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh index e9ea33c18d..d58fccb522 100755 --- a/t/t9001-send-email.sh +++ b/t/t9001-send-email.sh @@ -3,6 +3,10 @@ test_description='git-send-email' . ./test-lib.sh +say "cannot invoke fake.sendmail; skipping test" +test_done +exit 0 + PROG='git send-email' test_expect_success \ 'prepare reference tree' \ diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh index 8e5722245d..f7936e87da 100755 --- a/t/t9200-git-cvsexportcommit.sh +++ b/t/t9200-git-cvsexportcommit.sh @@ -6,6 +6,10 @@ test_description='CVS export comit. ' . ./test-lib.sh +say "CVS does not work on MinGW, skipping tests." +test_done +exit 0 + cvs >/dev/null 2>&1 if test $? -ne 1 then diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh index 4b920be331..902b5f5f7f 100755 --- a/t/t9300-fast-import.sh +++ b/t/t9300-fast-import.sh @@ -7,6 +7,10 @@ test_description='test git-fast-import utility' . ./test-lib.sh . ../diff-lib.sh ;# test-lib chdir's into trash +say "git-fast-import has not been taken care of, skipping test." +test_done +exit 0 + file2_data='file2 second line of EOF' From c10900175c37827e1c53a30e07df843b4ac94a8c Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 23 Jul 2007 22:17:16 +0200 Subject: [PATCH 007/253] Work around failures due to bogus quoting of ^ and {}. --- t/t3404-rebase-interactive.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index 43a6675caa..48dc6d6b41 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -150,7 +150,7 @@ test_expect_success 'retain authorship' ' test_tick && GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" && git tag twerp && - git rebase -i --onto master HEAD^ && + git rebase -i --onto master HEAD~1 && git show HEAD | grep "^Author: Twerp Snog" ' @@ -194,7 +194,7 @@ test_expect_success 'preserve merges with -p' ' test_expect_success '--continue tries to commit' ' test_tick && - ! git rebase -i --onto new-branch1 HEAD^ && + ! git rebase -i --onto new-branch1 HEAD~1 && echo resolved > file1 && git add file1 && FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue && @@ -203,9 +203,9 @@ test_expect_success '--continue tries to commit' ' ' test_expect_success 'verbose flag is heeded, even after --continue' ' - git reset --hard HEAD@{1} && + git reset --hard $(git rev-parse HEAD@{1}) && test_tick && - ! git rebase -v -i --onto new-branch1 HEAD^ && + ! git rebase -v -i --onto new-branch1 HEAD~1 && echo resolved > file1 && git add file1 && git rebase --continue > output && From 382210f715c0e1ef7a19eb10977dae4265a2c88f Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 14 Feb 2007 15:53:54 +0100 Subject: [PATCH 008/253] Remove the symbolic link to simplify bootstrapping this version. The MinGW port does have partial support for symbolic links now. But in order to allow others who still do not have this version to use their old version on Windows to pull and checkout this version we better drop the symbolic link. --- RelNotes | 1 - 1 file changed, 1 deletion(-) delete mode 120000 RelNotes diff --git a/RelNotes b/RelNotes deleted file mode 120000 index 1f6c16e1dc..0000000000 --- a/RelNotes +++ /dev/null @@ -1 +0,0 @@ -Documentation/RelNotes-1.5.2.4.txt \ No newline at end of file From 2d5d2c84f18762a50ddc843b560b2b0b53ee1426 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 6 Aug 2007 22:54:41 +0100 Subject: [PATCH 009/253] Fix inifinite loop In setup_git_directory_gently(), there is a hack to allow for stopping the search at the drive letter. Only that the patch was incomplete; we really have to stop there, instead of looping infinitely. Signed-off-by: Johannes Schindelin --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.c b/setup.c index a1aa5a3076..e921013926 100644 --- a/setup.c +++ b/setup.c @@ -260,7 +260,7 @@ const char *setup_git_directory_gently(int *nongit_ok) for (;;) { if (is_git_directory(".git")) break; - if (offset == 0) { + if (offset == minoffset) { offset = -1; break; } From bf788d7e65eeaebb65ddb330c32aac0d7bd5e325 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 6 Aug 2007 22:31:42 +0100 Subject: [PATCH 010/253] Makefile: be nice when running in a path containing spaces Without this patch, GIT-VERSION-GEN is not able to run. Signed-off-by: Johannes Schindelin --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 47b1fdc326..d42734aeef 100644 --- a/Makefile +++ b/Makefile @@ -124,7 +124,7 @@ all: # GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE - @$(SHELL_PATH) ./GIT-VERSION-GEN + @"$(SHELL_PATH)" ./GIT-VERSION-GEN -include GIT-VERSION-FILE uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') From 70cff4144391c83bdac5c98c99642d29181aa67a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 6 Aug 2007 23:01:57 +0100 Subject: [PATCH 011/253] Fix some compiler settings/paths for MinGW It seems that you have to jump through hoops to make trivial things such as "install.exe" do the right thing on Windows... Also, it seems that Windows Vista deliberately broke "access()". Apparently, they used that name in their runtime, but that function does something completely different than POSIX access(). So define __USE_MINGW_ACCESS to work around that. Signed-off-by: Johannes Schindelin --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index d42734aeef..85df9a5c67 100644 --- a/Makefile +++ b/Makefile @@ -176,7 +176,7 @@ CC = gcc AR = ar RM = rm -f TAR = tar -INSTALL = install +INSTALL = /bin/install RPMBUILD = rpmbuild TCL_PATH = tclsh TCLTK_PATH = wish @@ -373,7 +373,7 @@ BUILTIN_OBJS = \ builtin-pack-refs.o GITLIBS = $(LIB_FILE) $(XDIFF_LIB) -EXTLIBS = -lz +EXTLIBS = /mingw/lib/libz.a # # Platform specific tweaks @@ -488,7 +488,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_SYMLINKS=YesPlease NO_SVN_TESTS=YesPlease NO_PERL_MAKEMAKER=YesPlease - COMPAT_CFLAGS += -DNO_ETC_PASSWD -DNO_ST_BLOCKS -DSTRIP_EXTENSION=\".exe\" -I compat + COMPAT_CFLAGS += -DNO_ETC_PASSWD -DNO_ST_BLOCKS -DSTRIP_EXTENSION=\".exe\" -D__USE_MINGW_ACCESS -I compat COMPAT_OBJS += compat/mingw.o compat/fnmatch.o compat/regex.o EXTLIBS += -lws2_32 X = .exe From 7999f434d70dd1c4849c874d1767b24fb77a33df Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 6 Aug 2007 23:12:03 +0100 Subject: [PATCH 012/253] For the time being, set prefix= On MinGW, we want to install git in /bin. Signed-off-by: Johannes Schindelin --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 85df9a5c67..24bf460a1d 100644 --- a/Makefile +++ b/Makefile @@ -493,8 +493,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) EXTLIBS += -lws2_32 X = .exe NOEXECTEMPL = .noexec - template_dir = ../share/git-core/templates/ - ETC_GITCONFIG = ../etc/gitconfig + prefix = SCRIPT_SH += cpio.sh endif ifneq (,$(findstring arm,$(uname_M))) From 0e2bdc35afa484b521b7af2e74ad661b732d92eb Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Fri, 3 Aug 2007 19:27:53 -0400 Subject: [PATCH 013/253] Add and use expat and curl to enable http://. The expat build was added and the pieces of curl that seemed missing. The default NO_CURL was removed so that curl is used by default. Signed-off-by: Mike Pape --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 24bf460a1d..4103eca01b 100644 --- a/Makefile +++ b/Makefile @@ -474,7 +474,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_MMAP=YesPlease NO_PREAD=YesPlease NO_OPENSSL=YesPlease - NO_CURL=YesPlease NO_SYMLINK_HEAD=YesPlease NO_IPV6=YesPlease NO_ETC_PASSWD=YesPlease @@ -488,6 +487,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_SYMLINKS=YesPlease NO_SVN_TESTS=YesPlease NO_PERL_MAKEMAKER=YesPlease + NO_R_TO_GCC_LINKER = YesPlease COMPAT_CFLAGS += -DNO_ETC_PASSWD -DNO_ST_BLOCKS -DSTRIP_EXTENSION=\".exe\" -D__USE_MINGW_ACCESS -I compat COMPAT_OBJS += compat/mingw.o compat/fnmatch.o compat/regex.o EXTLIBS += -lws2_32 From ef5af72062ef0e0c0b5d8a7a3dda89b0609050bc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 4 Aug 2007 15:28:24 +0100 Subject: [PATCH 014/253] Minimal changes to allow for GIT_TRACE=/trace.txt (is_absolute_path()) This imitates what is currently in git.git's master: a function to determine if a path is absolute. msysgit.git is not really the proper place to fix this, but Hannes is still on holiday. Signed-off-by: Johannes Schindelin --- cache.h | 4 ++++ trace.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cache.h b/cache.h index e0a5f6decd..91e9f7109f 100644 --- a/cache.h +++ b/cache.h @@ -359,6 +359,10 @@ int git_config_perm(const char *var, const char *value); int adjust_shared_perm(const char *path); int safe_create_leading_directories(char *path); char *enter_repo(char *path, int strict); +static inline int is_absolute_path(const char *path) +{ + return path[0] == '/' || (isalpha(path[0]) && path[1] == ':'); +} /* Read and unpack a sha1 file into memory, write memory to a sha1 file */ extern int sha1_object_info(const unsigned char *, unsigned long *); diff --git a/trace.c b/trace.c index 7961a27a2e..344066e88b 100644 --- a/trace.c +++ b/trace.c @@ -64,7 +64,7 @@ static int get_trace_fd(int *need_close) return STDERR_FILENO; if (strlen(trace) == 1 && isdigit(*trace)) return atoi(trace); - if (*trace == '/') { + if (is_absolute_path(trace)) { int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666); if (fd == -1) { fprintf(stderr, From 4a66a6c4c236827c3ea89c75f908bf8019b3758c Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Sat, 4 Aug 2007 04:38:13 -0700 Subject: [PATCH 015/253] Correctly test for absolute path This fix (while correct) actually avoids another nasty bug that must be fixed later: environment.c caches results of many getenv calls. Under MinGW setenv(X) invalidates all previous values returned by getenv(X) so cached values become dangling pointers. Signed-off-by: Dmitry Kakurin Signed-off-by: Johannes Schindelin --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.c b/setup.c index e921013926..4d14541bcc 100644 --- a/setup.c +++ b/setup.c @@ -332,7 +332,7 @@ const char *setup_git_directory_gently(int *nongit_ok) * In case there is a work tree we may change the directory, * therefore make GIT_DIR an absolute path. */ - if (gitdirenv[0] != '/') { + if (!is_absolute_path(gitdirenv)) { setenv(GIT_DIR_ENVIRONMENT, gitdir, 1); gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (!gitdirenv) From 13e33743145fced9306fc7594bc84514fbd5883c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 5 Aug 2007 05:49:59 +0100 Subject: [PATCH 016/253] Trace ssh connections when GIT_TRACE is set This is really a patch for mingw.git. Signed-off-by: Johannes Schindelin --- rsh.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rsh.c b/rsh.c index 5754a230e2..9bb03d12c6 100644 --- a/rsh.c +++ b/rsh.c @@ -71,6 +71,8 @@ int setup_connection(int *fd_in, int *fd_out, const char *remote_prog, ssh_basename = ssh; else ssh_basename++; + trace_printf("Calling '%s' '%s' '%s' '%s'\n", + ssh, ssh_basename, host, command); close(sv[1]); dup2(sv[0], 0); dup2(sv[0], 1); From 60096146d67d408837b80c6b720bee722f35cf72 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 6 Aug 2007 16:49:51 +0100 Subject: [PATCH 017/253] Work around MinGW mangling of "host:/path" The common way to specify an ssh remote is to say "host:/path", but MinGW decides for us that this is probably a path list, and path lists are separated by a semicolon on Windows. So before passing this to git-peek-remote.exe, it transforms that to "host;C:/msysGit/path". Avoid that by expanding it to "ssh://host/path", but take extra care not to convert absolute paths to that syntax! Signed-off-by: Johannes Schindelin --- git-parse-remote.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100755 => 100644 git-parse-remote.sh diff --git a/git-parse-remote.sh b/git-parse-remote.sh old mode 100755 new mode 100644 index 695a4094bb..7c0d242eec --- a/git-parse-remote.sh +++ b/git-parse-remote.sh @@ -51,7 +51,8 @@ get_remote_url () { ;; *) die "internal error: get-remote-url $1" ;; - esac + esac | sed "s|^\(.[^:][^:]*\):\(/[^/]\)|ssh://\1\2|" + # work around MinGW path mangling } get_default_remote () { From 3d9012418c93b5e5f0ada041e1fc071cac48cbfa Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Mon, 6 Aug 2007 16:11:19 -0400 Subject: [PATCH 018/253] Added is_dev_null check because Windows /dev/null is nul. This function should be called to test for /dev/null when nul on Windows can also be accepted. The is_dev_null function in builtin-apply.c was renamed. Signed-off-by: Mike Pape --- builtin-apply.c | 8 ++++---- cache.h | 9 +++++++++ diff-lib.c | 6 +++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index 0a0b4a9e3f..f8cde7f3f5 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -222,7 +222,7 @@ static unsigned long linelen(const char *buffer, unsigned long size) return len; } -static int is_dev_null(const char *str) +static int is_dev_null_line(const char *str) { return !memcmp("/dev/null", str, 9) && isspace(str[9]); } @@ -333,7 +333,7 @@ static int guess_p_value(const char *nameline) char *name, *cp; int val = -1; - if (is_dev_null(nameline)) + if (is_dev_null_line(nameline)) return -1; name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB); if (!name) @@ -381,12 +381,12 @@ static void parse_traditional_patch(const char *first, const char *second, struc p_value_known = 1; } } - if (is_dev_null(first)) { + if (is_dev_null_line(first)) { patch->is_new = 1; patch->is_delete = 0; name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB); patch->new_name = name; - } else if (is_dev_null(second)) { + } else if (is_dev_null_line(second)) { patch->is_new = 0; patch->is_delete = 1; name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); diff --git a/cache.h b/cache.h index 91e9f7109f..f13e1b74a1 100644 --- a/cache.h +++ b/cache.h @@ -364,6 +364,15 @@ static inline int is_absolute_path(const char *path) return path[0] == '/' || (isalpha(path[0]) && path[1] == ':'); } +static inline int is_dev_null(const char *str) +{ +#ifdef __MINGW32__ + if (!strcmp(str, "nul")) + return 1; +#endif + return !strcmp(str, "/dev/null"); +} + /* Read and unpack a sha1 file into memory, write memory to a sha1 file */ extern int sha1_object_info(const unsigned char *, unsigned long *); extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size); diff --git a/diff-lib.c b/diff-lib.c index 92c0e39ad6..9a424621ed 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -34,7 +34,7 @@ static int get_mode(const char *path, int *mode) { struct stat st; - if (!path || !strcmp(path, "/dev/null")) + if (!path || is_dev_null(path)) *mode = 0; else if (!strcmp(path, "-")) *mode = ntohl(create_ce_mode(0666)); @@ -229,6 +229,10 @@ static int is_outside_repo(const char *path, int nongit, const char *prefix) int i; if (nongit || !strcmp(path, "-") || path[0] == '/') return 1; +#ifdef __MINGW32__ + if (!strcmp(path, "nul")) + return 1; +#endif if (prefixcmp(path, "../")) return 0; if (!prefix) From f1e1dc5119bd9862bb4d2a975c8ca6362ea43af5 Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Tue, 7 Aug 2007 07:58:08 +0200 Subject: [PATCH 019/253] Use builtin config function when installing. As the symlink for git-config is not set up yet at this point, we use the builtin instead. Signed-off-by: Marius Storm-Olsen --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4103eca01b..2f83c432bc 100644 --- a/Makefile +++ b/Makefile @@ -1005,7 +1005,7 @@ install: all $(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(INSTALL) git$X '$(DESTDIR_SQ)$(bindir_SQ)' GIT_CONFIG='$(DESTDIR_SQ)$(sysconfdir_SQ)/gitconfig' \ - $(DESTDIR_SQ)$(bindir_SQ)/git-config$X core.symlinks false + $(DESTDIR_SQ)$(bindir_SQ)/git$X config core.symlinks false $(MAKE) -C templates DESTDIR='$(DESTDIR_SQ)' install $(MAKE) -C perl prefix='$(prefix_SQ)' install ifndef NO_TCLTK From 8ca1f6af8c343172ca885076b8e581137ac05faf Mon Sep 17 00:00:00 2001 From: Julian Phillips Date: Tue, 17 Jul 2007 22:14:06 +0100 Subject: [PATCH 020/253] git-gui: Handle git versions of the form n.n.n.GIT The git-gui version check doesn't handle versions of the form n.n.n.GIT which you can get by installing from an tarball produced by git-archive. Without this change you get an error of the form: 'Error in startup script: expected version number but got "1.5.3.GIT"' Signed-off-by: Julian Phillips Signed-off-by: Shawn O. Pearce --- git-gui/git-gui.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 2077261e64..f8b1f10187 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -546,6 +546,7 @@ if {![regsub {^git version } $_git_version {} _git_version]} { } regsub {\.[0-9]+\.g[0-9a-f]+$} $_git_version {} _git_version regsub {\.rc[0-9]+$} $_git_version {} _git_version +regsub {\.GIT$} $_git_version {} _git_version proc git-version {args} { global _git_version From 6bb4b679d6fcee5104c46c3a5750cbe1d1dc06ce Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 9 Aug 2007 01:54:12 +0100 Subject: [PATCH 021/253] Fix new tests which ignored --no-symlinks Signed-off-by: Johannes Schindelin --- t/t0000-basic.sh | 2 ++ t/t1300-repo-config.sh | 2 ++ 2 files changed, 4 insertions(+) diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index 2c3b6b797c..b42b1e6948 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -305,6 +305,7 @@ test_expect_success 'update-index D/F conflict' ' test $numpath0 = 1 ' +test "$no_symlinks" || { test_expect_success 'absolute path works as expected' ' mkdir first && ln -s ../.git first/.git && @@ -320,5 +321,6 @@ test_expect_success 'absolute path works as expected' ' sym="$(cd first; pwd -P)"/file && test "$sym" = "$(test-absolute-path $dir2/syml)" ' +} test_done diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh index 1d2bf2c060..a59f1f3aed 100755 --- a/t/t1300-repo-config.sh +++ b/t/t1300-repo-config.sh @@ -601,6 +601,7 @@ echo >>result test_expect_success '--null --get-regexp' 'cmp result expect' +test "$no_symlinks" || { test_expect_success 'symlinked configuration' ' ln -s notyet myconfig && @@ -615,5 +616,6 @@ test_expect_success 'symlinked configuration' ' test "z$(GIT_CONFIG=notyet git config test.xyzzy)" = zrezrov ' +} test_done From 327005a9ef9c3b784e4742cece0710de6a549313 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Wed, 8 Aug 2007 22:19:49 -0400 Subject: [PATCH 022/253] Returning -1 does not fail like it should in MSys. We need to return 1 rather than -1 on failure since -1 does not seem to signal an error in MSys. Signed-off-by: Mike Pape --- config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.c b/config.c index bbc5d34e5a..977115a6e9 100644 --- a/config.c +++ b/config.c @@ -436,7 +436,7 @@ int git_config_from_file(config_fn_t fn, const char *filename) int ret; FILE *f = fopen(filename, "r"); - ret = -1; + ret = 1; if (f) { config_file = f; config_file_name = filename; From cfbeb65f6e9a2a990a4feaaf0475b226acda44c0 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Thu, 9 Aug 2007 13:29:48 -0400 Subject: [PATCH 023/253] Added a sleep to the test for MSys so commits happen. When committing, git thought there were no changes. Signed-off-by: Mike Pape --- t/t6200-fmt-merge-msg.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t6200-fmt-merge-msg.sh b/t/t6200-fmt-merge-msg.sh index 526d7d1c44..878f51a8d9 100755 --- a/t/t6200-fmt-merge-msg.sh +++ b/t/t6200-fmt-merge-msg.sh @@ -9,6 +9,7 @@ test_description='fmt-merge-msg test' datestamp=1151939923 setdate () { + sleep 1 GIT_COMMITTER_DATE="$datestamp +0200" GIT_AUTHOR_DATE="$datestamp +0200" datestamp=`expr "$datestamp" + 1` From e11db787dc8e680b379303393949da6ee2c45f0b Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Thu, 9 Aug 2007 14:30:18 -0400 Subject: [PATCH 024/253] We need to check for msys as well as Windows in add--interactive. Signed-off-by: Mike Pape --- git-add--interactive.perl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 7921cde8cb..717960037c 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -3,7 +3,7 @@ use strict; sub run_cmd_pipe { - if ($^O eq 'MSWin32') { + if ($^O eq 'MSWin32' || $^O eq 'msys') { my @invalid = grep {m/[":*]/} @_; die "$^O does not support: @invalid\n" if @invalid; my @args = map { m/ /o ? "\"$_\"": $_ } @_; From a917ddbacf11b1f2280134bd8bc5c248fbf77a5d Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Thu, 9 Aug 2007 14:35:43 -0400 Subject: [PATCH 025/253] sed does not accept -i on msys. We use a temporary .new file instead. Signed-off-by: Mike Pape --- t/t7501-commit.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh index 6bd3c9e3e0..cfa67227e5 100644 --- a/t/t7501-commit.sh +++ b/t/t7501-commit.sh @@ -69,7 +69,8 @@ test_expect_success \ cat >editor <<\EOF #!/bin/sh -sed -i -e "s/a file/an amend commit/g" $1 +sed -e "s/a file/an amend commit/g" $1 > $1.new +mv $1.new $1 EOF chmod 755 editor @@ -88,7 +89,8 @@ test_expect_success \ cat >editor <<\EOF #!/bin/sh -sed -i -e "s/amend/older/g" $1 +sed -e "s/amend/older/g" $1 > $1.new +mv $1.new $1 EOF chmod 755 editor From 96f438b98a7b8b3fec062ed6064d74074e5e1ff0 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Thu, 9 Aug 2007 16:18:50 -0400 Subject: [PATCH 026/253] In msys, chmod does not work. Using the shebang makes it "executable". Thanks to Dscho for figuring out the work around. Signed-off-by: Mike Pape --- t/t7005-editor.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh index 28643b0da4..b21ced1bc2 100755 --- a/t/t7005-editor.sh +++ b/t/t7005-editor.sh @@ -7,6 +7,7 @@ test_description='GIT_EDITOR, core.editor, and stuff' for i in GIT_EDITOR core_editor EDITOR VISUAL vi do cat >e-$i.sh <<-EOF + #!/bin/sh echo "Edited by $i" >"\$1" EOF chmod +x e-$i.sh From dfcdb3d9fc34a6a6f50e00934af014e0fc62f0c6 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Thu, 9 Aug 2007 20:05:38 -0400 Subject: [PATCH 027/253] Fixed INSTALL set in Makefile from the merge with git.git. INSTALL was set to /bin/install but then set to install right after that from the merge. MSys needs to have the explicit path so it doesn't use Windows install. Signed-off-by: Mike Pape --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index 36d7bebbad..f3f08dbcc4 100644 --- a/Makefile +++ b/Makefile @@ -179,7 +179,6 @@ RM = rm -f TAR = tar FIND = find INSTALL = /bin/install -INSTALL = install RPMBUILD = rpmbuild TCL_PATH = tclsh TCLTK_PATH = wish From 855f254b2b5b083a63fc8d7709a42e2cbdc5a136 Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Thu, 9 Aug 2007 13:41:02 -0700 Subject: [PATCH 028/253] git clone was failing with 'invalid object name HEAD' if ran from cmd.exe directly environment.c caches results of many getenv calls. Under MinGW setenv(X) invalidates all previous values returned by getenv(X) so cached values become dangling pointers. Replaced all setenv(GIT_DIR, ...) with set_git_dir Signed-off-by: Dmitry Kakurin --- builtin-init-db.c | 4 +--- git.c | 6 +++--- path.c | 2 +- setup.c | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/builtin-init-db.c b/builtin-init-db.c index 2a20737249..31427c571d 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -318,9 +318,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) /* * Set up the default .git directory contents */ - git_dir = getenv(GIT_DIR_ENVIRONMENT); - if (!git_dir) - git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; + git_dir = get_git_dir(); safe_create_dir(git_dir, 0); /* Check to see if the repository version is right. diff --git a/git.c b/git.c index 1f7f595653..5fc277c895 100644 --- a/git.c +++ b/git.c @@ -67,14 +67,14 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) fprintf(stderr, "No directory given for --git-dir.\n" ); usage(git_usage_string); } - setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); + set_git_dir( (*argv)[1] ); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; handled++; } else if (!prefixcmp(cmd, "--git-dir=")) { - setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1); + set_git_dir(cmd + 10); if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--work-tree")) { @@ -93,7 +93,7 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--bare")) { static char git_dir[PATH_MAX+1]; - setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1); + set_git_dir(getcwd(git_dir, sizeof(git_dir))); if (envchanged) *envchanged = 1; } else { diff --git a/path.c b/path.c index a1139d483b..7a5ac5b067 100644 --- a/path.c +++ b/path.c @@ -265,7 +265,7 @@ char *enter_repo(char *path, int strict) if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && validate_headref("HEAD") == 0) { - setenv(GIT_DIR_ENVIRONMENT, ".", 1); + set_git_dir("."); check_repository_format(); return path; } diff --git a/setup.c b/setup.c index 5a8e7b6b52..0de8dcd61b 100644 --- a/setup.c +++ b/setup.c @@ -332,7 +332,7 @@ const char *setup_git_directory_gently(int *nongit_ok) inside_git_dir = 1; if (!work_tree_env) inside_work_tree = 0; - setenv(GIT_DIR_ENVIRONMENT, ".", 1); + set_git_dir("."); return NULL; } chdir(".."); From 5e5619bd48dab2c8a32c604001ffaae6f807b1af Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Fri, 10 Aug 2007 01:09:34 -0700 Subject: [PATCH 029/253] Applied settings from config.mak to Makefile so we don't need config.mak anymore Signed-off-by: Dmitry Kakurin --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f3f08dbcc4..dd31119098 100644 --- a/Makefile +++ b/Makefile @@ -141,7 +141,7 @@ ALL_CFLAGS = $(CFLAGS) ALL_LDFLAGS = $(LDFLAGS) STRIP ?= strip -prefix = $(HOME) +prefix = bindir = $(prefix)/bin gitexecdir = $(bindir) sharedir = $(prefix)/share From a8c36f3212670a10d71203e91f97089f8954f624 Mon Sep 17 00:00:00 2001 From: Torgil Svensson Date: Fri, 10 Aug 2007 10:02:51 +0200 Subject: [PATCH 030/253] Make sane shell- and perl-scripts magic paths Make the magic paths absolute to msys instead of absolute to Windows. Signed-off-by: Torgil Svensson Acked-by: Johannes Schindelin Acked-by: Marius Storm-Olsen --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index dd31119098..0eada9d4c8 100644 --- a/Makefile +++ b/Makefile @@ -475,8 +475,8 @@ ifeq ($(uname_S),IRIX64) BASIC_LDFLAGS += -L/usr/lib32 endif ifneq (,$(findstring MINGW,$(uname_S))) - SHELL_PATH = $(shell cd /bin && pwd -W)/sh - PERL_PATH = $(shell cd /bin && pwd -W)/perl + SHELL_PATH = /bin/sh + PERL_PATH = /bin/perl NO_MMAP=YesPlease NO_PREAD=YesPlease NO_OPENSSL=YesPlease From 8e78bd571071805ca9f34a6bc93846f6e5b15f4e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 10 Aug 2007 11:33:40 +0100 Subject: [PATCH 031/253] verify_path(): do not allow absolute paths Signed-off-by: Johannes Schindelin --- read-cache.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/read-cache.c b/read-cache.c index e060392d1d..abc41561df 100644 --- a/read-cache.c +++ b/read-cache.c @@ -507,6 +507,11 @@ int verify_path(const char *path) { char c; +#ifdef __MINGW32__ + if (is_absolute_path(path)) + return error("Cannot handle absolute path: %s", path); +#endif + goto inside; for (;;) { if (!c) From 89697a4c152c798205c9af4782e4053274e54b3f Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Sun, 12 Aug 2007 05:30:07 -0700 Subject: [PATCH 032/253] Issue 20: Fix all build warnings Fixed all warnings. Code compiles cleanly now. Full test pass: OK Signed-off-by: Dmitry Kakurin --- builtin-ls-files.c | 2 +- builtin-prune-packed.c | 2 ++ builtin-prune.c | 2 ++ builtin-unpack-objects.c | 2 +- compat/mingw.c | 4 ++-- compat/pread.c | 2 ++ compat/regex.c | 26 ++++++++++++++++---------- config.c | 2 ++ connect.c | 2 +- diff.c | 2 +- exec_cmd.c | 5 ++--- fetch-pack.c | 2 +- index-pack.c | 2 +- merge-index.c | 2 +- pager.c | 2 +- read-cache.c | 2 +- receive-pack.c | 2 +- run-command.c | 4 ++-- sha1_file.c | 16 +++++++++------- show-index.c | 2 +- spawn-pipe.c | 6 ++---- unpack-trees.c | 2 +- 22 files changed, 53 insertions(+), 40 deletions(-) diff --git a/builtin-ls-files.c b/builtin-ls-files.c index d36181a755..e83b145b5c 100644 --- a/builtin-ls-files.c +++ b/builtin-ls-files.c @@ -211,7 +211,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce) putchar(line_terminator); } else { - printf("%s%06o %s %d\t", + printf("%s%06lo %s %u\t", tag, ntohl(ce->ce_mode), abbrev ? find_unique_abbrev(ce->sha1,abbrev) diff --git a/builtin-prune-packed.c b/builtin-prune-packed.c index 6473d327e6..fa9ad7bfdf 100644 --- a/builtin-prune-packed.c +++ b/builtin-prune-packed.c @@ -7,6 +7,8 @@ static const char prune_packed_usage[] = #define DRY_RUN 01 #define VERBOSE 02 +void sync(void); + static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts) { struct dirent *de; diff --git a/builtin-prune.c b/builtin-prune.c index 44df59e4a7..02e4c09b0e 100644 --- a/builtin-prune.c +++ b/builtin-prune.c @@ -8,6 +8,8 @@ static const char prune_usage[] = "git-prune [-n]"; static int show_only; +void sync(void); + static int prune_object(char *path, const char *filename, const unsigned char *sha1) { if (show_only) { diff --git a/builtin-unpack-objects.c b/builtin-unpack-objects.c index a6ff62fd8c..1500be5761 100644 --- a/builtin-unpack-objects.c +++ b/builtin-unpack-objects.c @@ -318,7 +318,7 @@ static void unpack_all(void) if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE) die("bad pack file"); if (!pack_version_ok(hdr->hdr_version)) - die("unknown pack file version %d", ntohl(hdr->hdr_version)); + die("unknown pack file version %lu", ntohl(hdr->hdr_version)); use(sizeof(struct pack_header)); if (!quiet) diff --git a/compat/mingw.c b/compat/mingw.c index ac8b8e0414..ca3ae4cb06 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -131,7 +131,7 @@ int pipe(int filedes[2]) CloseHandle(h[0]); return -1; } - fd = _open_osfhandle(h[0], O_NOINHERIT); + fd = _open_osfhandle( (int) h[0], O_NOINHERIT); if (fd < 0) { close(filedes[0]); close(filedes[1]); @@ -141,7 +141,7 @@ int pipe(int filedes[2]) } close(filedes[0]); filedes[0] = fd; - fd = _open_osfhandle(h[1], O_NOINHERIT); + fd = _open_osfhandle( (int) h[1], O_NOINHERIT); if (fd < 0) { close(filedes[0]); close(filedes[1]); diff --git a/compat/pread.c b/compat/pread.c index 978cac4ec9..ce29997724 100644 --- a/compat/pread.c +++ b/compat/pread.c @@ -1,5 +1,7 @@ #include "../git-compat-util.h" +int read_in_full(int fd, void *buf, size_t count); + ssize_t git_pread(int fd, void *buf, size_t count, off_t offset) { off_t current_offset; diff --git a/compat/regex.c b/compat/regex.c index 2d57c70b9c..1d39e08d47 100644 --- a/compat/regex.c +++ b/compat/regex.c @@ -1597,10 +1597,12 @@ regex_compile (pattern, size, syntax, bufp) if (syntax & RE_NO_BK_PARENS) goto normal_backslash; if (COMPILE_STACK_EMPTY) + { if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) goto normal_backslash; else return REG_ERPAREN; + } handle_close: if (fixup_alt_jump) @@ -1617,10 +1619,12 @@ regex_compile (pattern, size, syntax, bufp) /* See similar code for backslashed left paren above. */ if (COMPILE_STACK_EMPTY) + { if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) goto normal_char; else return REG_ERPAREN; + } /* Since we just checked for an empty stack above, this ``can't happen''. */ @@ -3191,14 +3195,14 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) matching and the regnum-th regend points to right after where we stopped matching the regnum-th subexpression. (The zeroth register keeps track of what the whole pattern matches.) */ - const char **regstart, **regend; + const char **regstart = NULL, **regend = NULL; /* If a group that's operated upon by a repetition operator fails to match anything, then the register for its start will need to be restored because it will have been set to wherever in the string we are when we last see its open-group operator. Similarly for a register's end. */ - const char **old_regstart, **old_regend; + const char **old_regstart = NULL, **old_regend = NULL; /* The is_active field of reg_info helps us keep track of which (possibly nested) subexpressions we are currently in. The matched_something @@ -3206,14 +3210,14 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) matched any of the pattern so far this time through the reg_num-th subexpression. These two fields get reset each time through any loop their register is in. */ - register_info_type *reg_info; + register_info_type *reg_info = NULL; /* The following record the register info as found in the above variables when we find a match better than any we've seen before. This happens as we backtrack through the failure points, which in turn happens only if we have not yet matched the entire string. */ unsigned best_regs_set = false; - const char **best_regstart, **best_regend; + const char **best_regstart = NULL, **best_regend = NULL; /* Logically, this is `best_regend[0]'. But we don't want to have to allocate space for that if we're not allocating space for anything @@ -3226,8 +3230,8 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) const char *match_end = NULL; /* Used when we pop values we don't care about. */ - const char **reg_dummy; - register_info_type *reg_info_dummy; + const char **reg_dummy = NULL; + register_info_type *reg_info_dummy = NULL; #ifdef DEBUG /* Counts the total number of registers pushed. */ @@ -4561,10 +4565,12 @@ common_op_match_null_string_p (p, end, reg_info) bytes; nonzero otherwise. */ static int -bcmp_translate (s1, s2, len, translate) - unsigned char *s1, *s2; - register int len; - char *translate; +bcmp_translate( + unsigned char *s1, + unsigned char *s2, + int len, + char *translate +) { register unsigned char *p1 = s1, *p2 = s2; while (len) diff --git a/config.c b/config.c index 977115a6e9..98ae20d551 100644 --- a/config.c +++ b/config.c @@ -15,6 +15,8 @@ static const char *config_file_name; static int config_linenr; static int zlib_compression_seen; +extern int getpagesize(); + static int get_next_char(void) { int c; diff --git a/connect.c b/connect.c index f50db639a4..dcae813b58 100644 --- a/connect.c +++ b/connect.c @@ -585,7 +585,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags) const char *argv[] = { NULL, host, command, NULL }; const char *ssh = getenv("GIT_SSH"); if (!ssh) ssh = "ssh"; - pid = spawnvpe_pipe(ssh, argv, environ, pipefd[1], pipefd[0]); + pid = spawnvpe_pipe(ssh, argv, (const char**) environ, pipefd[1], pipefd[0]); } else { const char *argv[] = { NULL, "-c", command, NULL }; diff --git a/diff.c b/diff.c index e64625129d..462d1e8563 100644 --- a/diff.c +++ b/diff.c @@ -1793,7 +1793,7 @@ static int spawn_prog(const char *pgm, const char **arg) int status; fflush(NULL); - pid = spawnvpe_pipe(pgm, arg, environ, NULL, NULL); + pid = spawnvpe_pipe(pgm, arg, (const char**) environ, NULL, NULL); if (pid < 0) die("unable to fork"); diff --git a/exec_cmd.c b/exec_cmd.c index a9886b87fd..2a8e48b048 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -4,7 +4,6 @@ #include "spawn-pipe.h" #define MAX_ARGS 32 -extern char **environ; static const char *current_exec_path; static const char *builtin_exec_path(void) @@ -138,7 +137,7 @@ int execv_git_cmd(const char **argv) trace_argv_printf(argv, -1, "trace: exec:"); /* execve() can only ever return if it fails */ - execve(git_command, (char **)argv, environ); + execve(git_command, (const char **) argv, (const char **) environ); trace_printf("trace: exec failed: %s\n", strerror(errno)); @@ -247,7 +246,7 @@ int spawnv_git_cmd(const char **argv, int pin[2], int pout[2]) trace_argv_printf(argv, -1, "trace: exec:"); - pid = spawnvppe_pipe(cmd, argv, environ, usedpaths, + pid = spawnvppe_pipe(cmd, argv, (const char**) environ, usedpaths, pin, pout); argv[0] = tmp; diff --git a/fetch-pack.c b/fetch-pack.c index e6abf1625a..b6470932fb 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -512,7 +512,7 @@ static int get_pack(int xd[2]) if (read_pack_header(fd[0], &header)) die("protocol error: bad pack header"); - snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u", + snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%lu,%lu", ntohl(header.hdr_version), ntohl(header.hdr_entries)); if (ntohl(header.hdr_entries) < unpack_limit) do_keep = 0; diff --git a/index-pack.c b/index-pack.c index 8403c36b63..32e9dd99df 100644 --- a/index-pack.c +++ b/index-pack.c @@ -141,7 +141,7 @@ static void parse_pack_header(void) if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) die("pack signature mismatch"); if (!pack_version_ok(hdr->hdr_version)) - die("pack version %d unsupported", ntohl(hdr->hdr_version)); + die("pack version %lu unsupported", ntohl(hdr->hdr_version)); nr_objects = ntohl(hdr->hdr_entries); use(sizeof(struct pack_header)); diff --git a/merge-index.c b/merge-index.c index fa719cb0b1..1cf4cb5b3f 100644 --- a/merge-index.c +++ b/merge-index.c @@ -48,7 +48,7 @@ static int merge_entry(int pos, const char *path) break; found++; strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); - sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode)); + sprintf(ownbuf[stage], "%lo", ntohl(ce->ce_mode)); arguments[stage] = hexbuf[stage]; arguments[stage + 4] = ownbuf[stage]; } while (++pos < active_nr); diff --git a/pager.c b/pager.c index a55f4e9d1e..4048894734 100644 --- a/pager.c +++ b/pager.c @@ -88,7 +88,7 @@ void setup_pager(void) #else /* spawn the pager */ pager_argv[2] = pager; - pager_pid = spawnvpe_pipe(pager_argv[0], pager_argv, environ, fd, NULL); + pager_pid = spawnvpe_pipe(pager_argv[0], pager_argv, (const char**) environ, fd, NULL); if (pager_pid < 0) return; diff --git a/read-cache.c b/read-cache.c index abc41561df..849e8d6b0f 100644 --- a/read-cache.c +++ b/read-cache.c @@ -149,7 +149,7 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st) changed |= DATA_CHANGED; return changed; default: - die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); + die("internal error: ce_mode is %lo", ntohl(ce->ce_mode)); } if (ce->ce_mtime.sec != htonl(st->st_mtime)) changed |= MTIME_CHANGED; diff --git a/receive-pack.c b/receive-pack.c index d3c422be58..94bc99b40d 100644 --- a/receive-pack.c +++ b/receive-pack.c @@ -352,7 +352,7 @@ static const char *unpack(void) hdr_err = parse_pack_header(&hdr); if (hdr_err) return hdr_err; - snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u", + snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%lu,%lu", ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); if (ntohl(hdr.hdr_entries) < unpack_limit) { diff --git a/run-command.c b/run-command.c index 5964e7d4dd..f208ed45fa 100644 --- a/run-command.c +++ b/run-command.c @@ -14,7 +14,7 @@ int start_command(struct child_process *cmd) int need_in, need_out; int fdin[2] = { -1, -1 }; int fdout[2] = { -1, -1 }; - char **env = environ; + const char **env = (const char **) environ; need_in = !cmd->no_stdin && cmd->in < 0; if (need_in) { @@ -75,7 +75,7 @@ int start_command(struct child_process *cmd) if (cmd->git_cmd) { cmd->pid = spawnv_git_cmd(cmd->argv, fdin, fdout); } else { - cmd->pid = spawnvpe_pipe(cmd->argv[0], cmd->argv, env, fdin, fdout); + cmd->pid = spawnvpe_pipe(cmd->argv[0], cmd->argv, (const char**) env, fdin, fdout); } } if (cmd->pid < 0) { diff --git a/sha1_file.c b/sha1_file.c index 4629fe81a4..1e3cf60ca0 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -425,15 +425,17 @@ static size_t peak_pack_mapped; static size_t pack_mapped; struct packed_git *packed_git; +extern size_t getpagesize(); + void pack_report(void) { fprintf(stderr, "pack_report: getpagesize() = %10" SZ_FMT "\n" "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n" "pack_report: core.packedGitLimit = %10" SZ_FMT "\n", - (size_t) getpagesize(), - packed_git_window_size, - packed_git_limit); + (unsigned long) getpagesize(), + (unsigned long) packed_git_window_size, + (unsigned long) packed_git_limit); fprintf(stderr, "pack_report: pack_used_ctr = %10u\n" "pack_report: pack_mmap_calls = %10u\n" @@ -443,7 +445,8 @@ void pack_report(void) pack_used_ctr, pack_mmap_calls, pack_open_windows, peak_pack_open_windows, - pack_mapped, peak_pack_mapped); + (unsigned long) pack_mapped, + (unsigned long) peak_pack_mapped); } static int check_packed_git_idx(const char *path, struct packed_git *p) @@ -634,7 +637,6 @@ static int open_packed_git_1(struct packed_git *p) struct pack_header hdr; unsigned char sha1[20]; unsigned char *idx_sha1; - long fd_flag; if (!p->index_data && open_pack_index(p)) return error("packfile %s index unavailable", p->pack_name); @@ -669,13 +671,13 @@ static int open_packed_git_1(struct packed_git *p) if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) return error("file %s is not a GIT packfile", p->pack_name); if (!pack_version_ok(hdr.hdr_version)) - return error("packfile %s is version %u and not supported" + return error("packfile %s is version %lu and not supported" " (try upgrading GIT to a newer version)", p->pack_name, ntohl(hdr.hdr_version)); /* Verify the pack matches its index. */ if (p->num_objects != ntohl(hdr.hdr_entries)) - return error("packfile %s claims to have %u objects" + return error("packfile %s claims to have %lu objects" " while index indicates %u objects", p->pack_name, ntohl(hdr.hdr_entries), p->num_objects); diff --git a/show-index.c b/show-index.c index 57ed9e87b7..6943e4bbb3 100644 --- a/show-index.c +++ b/show-index.c @@ -68,7 +68,7 @@ int main(int argc, char **argv) ntohl(off64[1]); off64_nr++; } - printf("%llu %s (%08x)\n", (unsigned long long) offset, + printf("%llu %s (%08lx)\n", (unsigned long long) offset, sha1_to_hex(entries[i].sha1), ntohl(entries[i].crc)); } diff --git a/spawn-pipe.c b/spawn-pipe.c index c8f0452823..e8872dc0ff 100644 --- a/spawn-pipe.c +++ b/spawn-pipe.c @@ -1,8 +1,6 @@ #include "git-compat-util.h" #include "spawn-pipe.h" -extern char **environ; - #ifdef __MINGW32__ static char *lookup_prog(const char *dir, const char *cmd, int tryexe) { @@ -193,7 +191,7 @@ int spawnvppe_pipe(const char *cmd, const char **argv, const char **env, qargv = xmalloc((argc+2)*sizeof(char*)); if (!interpr) { quote_argv(qargv, argv); - pid = spawnve(_P_NOWAIT, prog, qargv, env); + pid = spawnve(_P_NOWAIT, prog, qargv, (const char**) env); } else { qargv[0] = interpr; argv[0] = prog; @@ -219,7 +217,7 @@ int spawnvppe_pipe(const char *cmd, const char **argv, const char **env, const char **copy_environ() { - return copy_env(environ); + return copy_env( (const char**)environ); } const char **copy_env(const char **env) diff --git a/unpack-trees.c b/unpack-trees.c index dfd985b0ef..d017f667ce 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -638,7 +638,7 @@ static void show_stage_entry(FILE *o, if (!ce) fprintf(o, "%s (missing)\n", label); else - fprintf(o, "%s%06o %s %d\t%s\n", + fprintf(o, "%s%06lo %s %d\t%s\n", label, ntohl(ce->ce_mode), sha1_to_hex(ce->sha1), From 57bab054a9f7cd82f8889ef4fad58653d89f38fc Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 11 Aug 2007 22:15:39 +0200 Subject: [PATCH 033/253] help (msysgit): teach git help to open html from /doc/git/html/ Html pages will be opened using the default Windows application configured for html. This code path is taken for msysgit (mingw). It is assumed that html pages are installed at /doc/git/html. This needs to be ensured by the msysgit superproject to make this patch useful. html pages should be cloned from git.git's main repo. This is the easiest way to get up-to-date documentation, without requiring the complete tool chain to generate them locally. If html pages are not yet there, you can use the following commands to get them: mkdir -p /doc/git/html cd /doc/git/html/ git init git config remote.origin.url git://git.kernel.org/pub/scm/git/git.git git config remote.origin.fetch refs/heads/html:refs/remotes/origin/html git fetch git checkout --track -b html origin/html and update the html documentation with git pull Signed-off-by: Steffen Prohaska --- help.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/help.c b/help.c index 7d59efa50b..e7da8ae430 100644 --- a/help.c +++ b/help.c @@ -180,7 +180,25 @@ static void show_man_page(const char *git_cmd) page = p; } +#ifdef __MINGW32__ + { + char* prefix = "/doc/git/html/"; + int prefix_len = strlen (prefix); + char* suffix = ".html"; + int suffix_len = strlen (suffix); + int page_len = strlen (page); + int htmlpath_len = prefix_len + page_len + suffix_len; + char* htmlpath = xmalloc (htmlpath_len + 1); + strcpy (htmlpath, prefix); + strcpy (htmlpath + prefix_len, page); + strcpy (htmlpath + prefix_len + page_len, suffix); + htmlpath[htmlpath_len] = 0; + /* We need sh here to run shell script /bin/start. */ + execlp("sh", "start", "/bin/start", htmlpath, NULL ); + } +#else execlp("man", "man", page, NULL); +#endif } void help_unknown_cmd(const char *cmd) From fefe8db81717b895dcc10977dc10827c6eaa045f Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 11 Aug 2007 00:10:13 +0200 Subject: [PATCH 034/253] mergetool: refactored kdiff3 -> KDIFF3 Use shell variable KDIFF3 instead of kdiff3 to call kdiff3. This will be used in detection of the absolute absolute path. --- git-mergetool.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/git-mergetool.sh b/git-mergetool.sh index e6bbb6bbd6..90a69b3465 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -13,6 +13,8 @@ SUBDIRECTORY_OK=Yes . git-sh-setup require_work_tree +KDIFF3=kdiff3 + # Returns true if the mode reflects a symlink is_symlink () { test "$1" = 120000 @@ -191,10 +193,10 @@ merge_file () { case "$merge_tool" in kdiff3) if base_present ; then - (kdiff3 --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \ + ("$KDIFF3" --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \ -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1) else - (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \ + ("$KDIFF3" --auto -L1 "$path (Local)" --L2 "$path (Remote)" \ -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1) fi status=$? From 3318ad905e53243dbd59002e4c95106268f763cb Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 11 Aug 2007 00:34:34 +0200 Subject: [PATCH 035/253] mergetool: added support for kdiff3 on windows kdiff3's homepage is http://kdiff3.sourceforge.net/. kdiff3 is automatically added to the available mergetools if its path is found in the Windows Registry. The path is taken from the registry. Be sure to set git config core.autocrlf true because kdiff3 seems to follow Windows crlf convention. --- git-mergetool.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/git-mergetool.sh b/git-mergetool.sh index 90a69b3465..ac23891980 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -14,6 +14,7 @@ SUBDIRECTORY_OK=Yes require_work_tree KDIFF3=kdiff3 +KDIFF3SEPARATOR=-- # Returns true if the mode reflects a symlink is_symlink () { @@ -194,10 +195,10 @@ merge_file () { kdiff3) if base_present ; then ("$KDIFF3" --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \ - -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1) + -o "$path" $KDIFF3SEPERATOR "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1) else ("$KDIFF3" --auto -L1 "$path (Local)" --L2 "$path (Remote)" \ - -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1) + -o "$path" $KDIFF3SEPERATOR "$LOCAL" "$REMOTE" > /dev/null 2>&1) fi status=$? remove_backup @@ -321,6 +322,11 @@ if test -z "$merge_tool" ; then merge_tool_candidates="kdiff3 $merge_tool_candidates" fi fi + regentry="$(REG QUERY 'HKEY_LOCAL_MACHINE\SOFTWARE\KDiff3\diff-ext' 2>/dev/null)" && { + KDIFF3=$(echo "$regentry" | grep diffcommand | cut -f 3) + KDIFF3SEPARATOR= + merge_tool_candidates="$merge_tool_candidates kdiff3" + } if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then merge_tool_candidates="$merge_tool_candidates emerge" fi @@ -332,10 +338,12 @@ if test -z "$merge_tool" ; then for i in $merge_tool_candidates; do if test $i = emerge ; then cmd=emacs + elif test $i = kdiff3 ; then + cmd="$KDIFF3" else cmd=$i fi - if type $cmd > /dev/null 2>&1; then + if type "$cmd" > /dev/null 2>&1; then merge_tool=$i break fi @@ -347,7 +355,13 @@ if test -z "$merge_tool" ; then fi case "$merge_tool" in - kdiff3|tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff) + kdiff3) + if ! type "$KDIFF3" > /dev/null 2>&1; then + echo "The merge tool $merge_tool is not available" + exit 1 + fi + ;; + tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff) if ! type "$merge_tool" > /dev/null 2>&1; then echo "The merge tool $merge_tool is not available" exit 1 From 56be985fe902a4af8c7f0bbe4541ef783ead827f Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Tue, 14 Aug 2007 09:48:39 +0200 Subject: [PATCH 036/253] Avoid calling signal(SIGPIPE, ..) for MinGW builds. SIGPIPE isn't supported in MinGW. Signed-off-by: Marius Storm-Olsen --- builtin-verify-tag.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builtin-verify-tag.c b/builtin-verify-tag.c index dfcfcd0455..5c1314d071 100644 --- a/builtin-verify-tag.c +++ b/builtin-verify-tag.c @@ -100,9 +100,11 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix) i++; } +#ifndef __MINGW32__ /* sometimes the program was terminated because this signal * was received in the process of writing the gpg input: */ signal(SIGPIPE, SIG_IGN); +#endif while (i < argc) if (verify_tag(argv[i++], verbose)) had_error = 1; From 65b356f555a716d9f753e58911da2017f3d1c2fc Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 13 Aug 2007 08:04:14 +0200 Subject: [PATCH 037/253] mergetool: converting backslashes in kdiff3 path from Windows registry Backslashes are converted to slashes right after the Windows registry returned a path. This is needed to avoid problems with shell expansion. Signed-off-by: Steffen Prohaska --- git-mergetool.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-mergetool.sh b/git-mergetool.sh index ac23891980..e7d745062b 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -323,7 +323,7 @@ if test -z "$merge_tool" ; then fi fi regentry="$(REG QUERY 'HKEY_LOCAL_MACHINE\SOFTWARE\KDiff3\diff-ext' 2>/dev/null)" && { - KDIFF3=$(echo "$regentry" | grep diffcommand | cut -f 3) + KDIFF3=$(echo "$regentry" | grep diffcommand | cut -f 3 | sed -e 's@\\@/@g') KDIFF3SEPARATOR= merge_tool_candidates="$merge_tool_candidates kdiff3" } From 1a896a59003da71410fe34cf5687832d1f2a6797 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Tue, 14 Aug 2007 13:48:56 -0400 Subject: [PATCH 038/253] Correct logic from merge to fix t1020-subdirectory.sh. Some logic was lost from the merge from git.git. Signed-off-by: Mike Pape --- connect.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/connect.c b/connect.c index 9f51e8dcc8..ea810c4df9 100644 --- a/connect.c +++ b/connect.c @@ -525,7 +525,8 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags) ) { protocol = PROTO_SSH; *path++ = '\0'; - } + } else + path = host; } else path = end; From d1f83218dce62affd1d7a82a9e654cca08dade9c Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Wed, 15 Aug 2007 13:18:20 -0400 Subject: [PATCH 039/253] Hardlinks are not supported in MSys so we disable them. We do not want the default to be hardlinks for local clones and we show a warning if the user tries to use -l. Signed-off-by: Mike Pape --- Makefile | 9 +++++++++ git-clone.sh | 5 ++++- t/Makefile | 4 ++++ t/t5701-clone-local.sh | 7 +++++++ t/test-lib.sh | 2 ++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 36fa094462..456681b27c 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,9 @@ all: # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link. # Enable it on Windows. By default, symrefs are still used. # +# Define NO_HARDLINKS if you want to disable hard linking in git clone. +# Enable it on Windows. +# # Define NO_SVN_TESTS if you want to skip time-consuming SVN interoperability # tests. These tests take up a significant amount of the total test time # but are not needed unless you plan to talk to SVN repos. @@ -482,6 +485,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_PREAD=YesPlease NO_OPENSSL=YesPlease NO_SYMLINK_HEAD=YesPlease + NO_HARDLINKS=YesPlease NO_IPV6=YesPlease NO_ETC_PASSWD=YesPlease NO_SETENV=YesPlease @@ -724,6 +728,10 @@ ifdef ASCIIDOC8 export ASCIIDOC8 endif +ifdef NO_HARDLINKS + export NO_HARDLINKS +endif + # Shell quote (do not use $(call) to accommodate ancient setups); SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER)) @@ -799,6 +807,7 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh -e 's|@@PERL@@|$(PERL_PATH_SQ)|g' \ -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \ -e 's/@@NO_CURL@@/$(NO_CURL)/g' \ + -e 's/@@NO_HARDLINKS@@/$(NO_HARDLINKS)/g' \ $@.sh >$@+ && \ chmod +x $@+ && \ mv $@+ $@ diff --git a/git-clone.sh b/git-clone.sh index 2c0164aafe..72435b03aa 100644 --- a/git-clone.sh +++ b/git-clone.sh @@ -99,6 +99,7 @@ origin_override= use_separate_remote=t depth= no_progress= +test "@@NO_HARDLINKS@@" && use_local_hardlink=no test -t 1 || no_progress=--no-progress while case "$#,$1" in @@ -109,7 +110,9 @@ while *,--na|*,--nak|*,--nake|*,--naked|\ *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;; *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) - use_local_hardlink=yes ;; + (test "@@NO_HARDLINKS@@" && + echo >&2 "Warning: -l asked but hardlinks are not supported") || + use_local_hardlink=yes ;; *,--no-h|*,--no-ha|*,--no-har|*,--no-hard|*,--no-hardl|\ *,--no-hardli|*,--no-hardlin|*,--no-hardlink|*,--no-hardlinks) use_local_hardlink=no ;; diff --git a/t/Makefile b/t/Makefile index f8e396c108..2b808f96bb 100644 --- a/t/Makefile +++ b/t/Makefile @@ -18,6 +18,10 @@ ifdef NO_SYMLINKS GIT_TEST_OPTS += --no-symlinks endif +ifdef NO_HARDLINKS + GIT_TEST_OPTS += --no-hardlinks +endif + all: $(T) clean $(T): diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh index a3026ec4fc..d2f55ebfbc 100755 --- a/t/t5701-clone-local.sh +++ b/t/t5701-clone-local.sh @@ -3,6 +3,13 @@ test_description='test local clone' . ./test-lib.sh +if test "$no_hardlinks" +then + say 'Hard links not supported, skipping tests.' + test_done + exit +fi + D=`pwd` test_expect_success 'preparing origin repository' ' diff --git a/t/test-lib.sh b/t/test-lib.sh index 6081d172ab..143d95daca 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -89,6 +89,8 @@ do shift ;; --no-symlinks) no_symlinks=t; shift ;; + --no-hardlinks) + no_hardlinks=t; shift ;; *) break ;; esac From a0b4827288741c7939e40e269aa8298454a56eff Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Thu, 16 Aug 2007 21:16:48 +0200 Subject: [PATCH 040/253] Revert "Correct logic from merge to fix t1020-subdirectory.sh." This reverts commit 1a896a59003da71410fe34cf5687832d1f2a6797. The patch had an adverse effect on fetching with the git+ssh protocol, giving me the following errors Pseudo-terminal will not be allocated because stdin is not a terminal. ssh: ': no address associated with hostname. fatal: The remote end hung up unexpectedly Cannot get the repository state from git+ssh:// and [0: 172.20.1.78]: errno=Invalid argument [1: 192.168.1.3]: errno=Bad file descriptor [2: 192.168.35.1]: errno=Bad file descriptor [3: 192.168.150.1]: errno=Bad file descriptor fatal: unable to connect a socket (Bad file descriptor) Cannot get the repository state from git:// Signed-off-by: Marius Storm-Olsen --- connect.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/connect.c b/connect.c index ea810c4df9..9f51e8dcc8 100644 --- a/connect.c +++ b/connect.c @@ -525,8 +525,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags) ) { protocol = PROTO_SSH; *path++ = '\0'; - } else - path = host; + } } else path = end; From 94e7a6b7b4d2b8901b836a398e1d3733c64112e1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 17 Aug 2007 14:50:29 +0200 Subject: [PATCH 041/253] Revert fa807fcb: 'Revert "Makefile: remove $foo when $foo.exe...' It works in msysgit, so there is no reason why we should not remove the scripted versions. Especially since some tests were failing for the sole reason that _not_ the builtin, but the script was executed. Signed-off-by: Johannes Schindelin --- Makefile | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 456681b27c..62407ad0be 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # The default target of this Makefile is... -all: +all:: # Define V=1 to have a more verbose compile. # @@ -762,9 +762,12 @@ export TAR INSTALL DESTDIR SHELL_PATH ### Build rules -all: $(ALL_PROGRAMS) $(BUILT_INS) $(OTHER_PROGRAMS) +all:: $(ALL_PROGRAMS) $(BUILT_INS) $(OTHER_PROGRAMS) +ifneq (,$X) + $(foreach p,$(patsubst %$X,%,$(filter %$X,$(ALL_PROGRAMS) $(BUILT_INS) git$X)), $(RM) '$p';) +endif -all: +all:: ifndef NO_TCLTK $(QUIET_SUBDIR0)git-gui $(QUIET_SUBDIR1) all endif @@ -993,7 +996,7 @@ endif TEST_PROGRAMS = test-chmtime$X test-genrandom$X test-date$X test-delta$X test-sha1$X test-match-trees$X test-absolute-path$X -all: $(TEST_PROGRAMS) +all:: $(TEST_PROGRAMS) # GNU make supports exporting all variables by "export" without parameters. # However, the environment gets quite big, and some programs have problems @@ -1045,6 +1048,9 @@ endif '$(DESTDIR_SQ)$(gitexecdir_SQ)/git$X'; \ fi $(foreach p,$(BUILT_INS), $(RM) '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' && ln '$(DESTDIR_SQ)$(gitexecdir_SQ)/git$X' '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' ;) +ifneq (,$X) + $(foreach p,$(patsubst %$X,%,$(filter %$X,$(ALL_PROGRAMS) $(BUILT_INS) git$X)), $(RM) '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p';) +endif install-doc: $(MAKE) -C Documentation install From 331406ad302671f696f4c624737acfede7bf1d04 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Fri, 17 Aug 2007 19:18:09 -0400 Subject: [PATCH 042/253] Correct connect logic to allow for Windows C: paths. We need to add logic for windows to allow C: and not assume the : means it's another protocol. Signed-off-by: Mike Pape --- connect.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/connect.c b/connect.c index 9f51e8dcc8..1c92df0f21 100644 --- a/connect.c +++ b/connect.c @@ -518,13 +518,18 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags) path = strchr(end, c); if (path) { - if (c == ':' + if (c == ':') { #ifdef __MINGW32__ - && path - end > 1 /* host must have at least 2 chars to catch DOS C:/path */ + /* host must have at least 2 chars to + * catch DOS C:/path */ + if (path - end > 1) { +#endif + protocol = PROTO_SSH; + *path++ = '\0'; +#ifdef __MINGW32__ + } else + path = end; #endif - ) { - protocol = PROTO_SSH; - *path++ = '\0'; } } else path = end; From 20e93f53e3a220018c62e0055a083af6079b8a2c Mon Sep 17 00:00:00 2001 From: Johannes Schmidt-Ehrenberg Date: Mon, 13 Aug 2007 19:00:39 +0200 Subject: [PATCH 043/253] mergetool: fixed parsing of registry entry for kdiff3 The old code failed on Windows Vista. The output of reg.exe or something else may be a bit different. This patch improves the parsing code to be more robust. Signed-off-by: Steffen Prohaska --- git-mergetool.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-mergetool.sh b/git-mergetool.sh index e7d745062b..54f6d58a84 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -323,7 +323,7 @@ if test -z "$merge_tool" ; then fi fi regentry="$(REG QUERY 'HKEY_LOCAL_MACHINE\SOFTWARE\KDiff3\diff-ext' 2>/dev/null)" && { - KDIFF3=$(echo "$regentry" | grep diffcommand | cut -f 3 | sed -e 's@\\@/@g') + KDIFF3=$(echo "$regentry" | grep diffcommand | awk -F 'REG_SZ' '{ print $2 }' | sed -e 's/^ *//'| sed -e 's@\\@/@g') KDIFF3SEPARATOR= merge_tool_candidates="$merge_tool_candidates kdiff3" } From ae3d370abf6ae61f5b15d3ad5703023080124872 Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Fri, 24 Aug 2007 08:36:30 +0200 Subject: [PATCH 044/253] Add patch from Linus, to make write-tree read the configuration, in case it datestamps are so close that it needs to ce_smudge_racily_clean_entry(), thus read the content of the file and compare. The core.autocrlf value is needed in this case. Signed-off-by: Marius Storm-Olsen --- builtin-write-tree.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin-write-tree.c b/builtin-write-tree.c index 88f34ba7d6..b89d02efec 100644 --- a/builtin-write-tree.c +++ b/builtin-write-tree.c @@ -72,6 +72,7 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix) const char *prefix = NULL; unsigned char sha1[20]; + git_config(git_default_config); while (1 < argc) { const char *arg = argv[1]; if (!strcmp(arg, "--missing-ok")) From dd56555ea5cd03958dc2f987f2776efbab571452 Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Thu, 23 Aug 2007 22:03:35 +0200 Subject: [PATCH 045/253] Add a trace to more easily show that the index has been smudged. Signed-off-by: Marius Storm-Olsen --- read-cache.c | 1 + 1 file changed, 1 insertion(+) diff --git a/read-cache.c b/read-cache.c index 84c6b47eb8..23d16a40ef 100644 --- a/read-cache.c +++ b/read-cache.c @@ -1104,6 +1104,7 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce) * for "frotz" stays 6 which does not match the filesystem. */ ce->ce_size = htonl(0); + trace_printf("trace: index: Index object for '%s' smudged due to being racily clean\n", ce->name); } } From 194c1dbb5a04e29937b87d477a16927fa2c3be62 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Aug 2007 14:16:46 +0100 Subject: [PATCH 046/253] Provide git_exit() for MinGW Apparently, MinGW's exit function has problems with negative exit codes. Substitute them by 1. This fixes at least t1300, which failed because the exit code of git-config with an invalid file was 0. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 9 +++++++++ git-compat-util.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index ca3ae4cb06..6632192690 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -322,3 +322,12 @@ int mingw_socket(int domain, int type, int protocol) } return s; } + +#undef exit +int git_exit(int code) +{ + if (code < 0) + exit(1); + exit(code); +} + diff --git a/git-compat-util.h b/git-compat-util.h index ddafec997f..9e075b715c 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -472,6 +472,9 @@ int mingw_socket(int domain, int type, int protocol); extern void quote_argv(const char **dst, const char **src); extern const char *parse_interpreter(const char *cmd); +extern __attribute__((noreturn)) int git_exit(int code); +#define exit git_exit + #endif /* __MINGW32__ */ #endif From 5464a3f4b7fdc844253b5d64b0b311fde31ee1b2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Aug 2007 17:43:02 +0100 Subject: [PATCH 047/253] t5701: "wc -l" may add whitespace Signed-off-by: Johannes Schindelin --- t/t5701-clone-local.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh index d2f55ebfbc..7e7019fa31 100755 --- a/t/t5701-clone-local.sh +++ b/t/t5701-clone-local.sh @@ -54,8 +54,7 @@ test_expect_success 'With -no-hardlinks, local will make a copy' ' cd "$D" && git clone --bare --no-hardlinks x w && cd w && - linked=$(find objects -type f ! -links 1 | wc -l) && - test "$linked" = 0 + test $(find objects -type f ! -links 1 | wc -l) = 0 ' test_expect_success 'Even without -l, local will make a hardlink' ' @@ -63,8 +62,7 @@ test_expect_success 'Even without -l, local will make a hardlink' ' rm -fr w && git clone -l --bare x w && cd w && - copied=$(find objects -type f -links 1 | wc -l) && - test "$copied" = 0 + test $(find objects -type f -links 1 | wc -l) = 0 ' test_done From 685268d9a1d4db52c62e022a7543a5c092ac8fec Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Aug 2007 17:57:08 +0100 Subject: [PATCH 048/253] t5701: skip hardlink test Signed-off-by: Johannes Schindelin --- t/t5701-clone-local.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh index 7e7019fa31..e97f435fec 100755 --- a/t/t5701-clone-local.sh +++ b/t/t5701-clone-local.sh @@ -57,6 +57,10 @@ test_expect_success 'With -no-hardlinks, local will make a copy' ' test $(find objects -type f ! -links 1 | wc -l) = 0 ' +say "hardlinks not supported, skipping tests." +test_done +exit 0 + test_expect_success 'Even without -l, local will make a hardlink' ' cd "$D" && rm -fr w && From a19cff248ece3693f5dc6ad75a70a3e68606a1f9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 3 Sep 2007 17:42:54 +0100 Subject: [PATCH 049/253] verify-tag: also grok CR/LFs in the tag signature On some people's favorite platform, gpg outputs signatures with CR/LF line endings. So verify-tag has to play nice with them. Signed-off-by: Johannes Schindelin --- builtin-verify-tag.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin-verify-tag.c b/builtin-verify-tag.c index 5c1314d071..8cfefcf523 100644 --- a/builtin-verify-tag.c +++ b/builtin-verify-tag.c @@ -35,7 +35,7 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose) /* find the length without signature */ len = 0; - while (len < size && prefixcmp(buf + len, PGP_SIGNATURE "\n")) { + while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) { eol = memchr(buf + len, '\n', size - len); len += eol ? eol - (buf + len) + 1 : size - len; } From 7734ad404c48ea376fed1ce5e9e04f72ec4cb0bd Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 3 Sep 2007 17:36:47 +0100 Subject: [PATCH 050/253] MinGW: Convert CR/LF to LF in tag signatures On Windows, gpg outputs CR/LF signatures. But since the tag messages are already stripped of the CR by stripspace(), it is arguably nicer to do the same for the tag signature. Actually, this patch does not look for CR/LF, but strips all CRs from the signature. Signed-off-by: Johannes Schindelin --- builtin-tag.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/builtin-tag.c b/builtin-tag.c index d6d38ad123..547f0ba224 100644 --- a/builtin-tag.c +++ b/builtin-tag.c @@ -229,6 +229,20 @@ static ssize_t do_sign(char *buffer, size_t size, size_t max) if (len == max - size) return error("could not read the entire signature from gpg."); +#ifdef __MINGW32__ + /* strip CR from the line endings */ + { + int i, j; + for (i = j = 0; i < len; i++) + if (buffer[size + i] != '\r') { + if (i != j) + buffer[size + j] = buffer[size + i]; + j++; + } + len = j; + } +#endif + return size + len; } From b9dcb2cbe55afa21af7eed9f49e319b183954748 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 3 Sep 2007 19:55:09 +0100 Subject: [PATCH 051/253] Fix cpio for CR/LF line endings Under certain circumstances (I did not find out the complete details, but it happens when running the tests), the filterdirs() function in cpio outputs CR/LF. Since tar does not like that, the command failed. So strip out all CRs when filtering the directories. Signed-off-by: Johannes Schindelin --- cpio.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/cpio.sh b/cpio.sh index 2816e34851..432903813b 100644 --- a/cpio.sh +++ b/cpio.sh @@ -44,6 +44,7 @@ o) p) test -z "$null" || die "cpio: cannot use -0 in pass-through mode" filterdirs | + tr -d '\r' | tar --create --file=- --files-from=- | tar --extract --directory="$dir" --file=- ;; From 59f8c189a5d582d5eef28ca4021d04087dff2eff Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 23 Aug 2007 10:18:54 -0700 Subject: [PATCH 052/253] Fix racy-git handling in git-write-tree. After git-write-tree finishes computing the tree, it updates the index so that later operations can take advantage of fully populated cache tree. However, anybody writing the index file has to mark the entries that are racily clean. For each entry whose cached lstat(3) data in the index exactly matches what is obtained from the filesystem, if the timestamp on the index file was the same or older than the modification timestamp of the file, the blob contents and the work tree file, after convert_to_git(), need to be compared, and if they are different, its index entry needs to be marked not to match the lstat(3) data from the filesystem. In order for this to work, convert_to_git() needs to work correctly, which in turn means you need to read the config file to get the settings of core.crlf and friends. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- t/t0023-crlf-am.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 t/t0023-crlf-am.sh diff --git a/t/t0023-crlf-am.sh b/t/t0023-crlf-am.sh new file mode 100755 index 0000000000..6f8a4347d5 --- /dev/null +++ b/t/t0023-crlf-am.sh @@ -0,0 +1,44 @@ +#!/bin/sh + +test_description='Test am with auto.crlf' + +. ./test-lib.sh + +cat >patchfile <<\EOF +From 38be10072e45dd6b08ce40851e3fca60a31a340b Mon Sep 17 00:00:00 2001 +From: Marius Storm-Olsen +Date: Thu, 23 Aug 2007 13:00:00 +0200 +Subject: test1 + +--- + foo | 1 + + 1 files changed, 1 insertions(+), 0 deletions(-) + create mode 100644 foo + +diff --git a/foo b/foo +new file mode 100644 +index 0000000000000000000000000000000000000000..5716ca5987cbf97d6bb54920bea6adde242d87e6 +--- /dev/null ++++ b/foo +@@ -0,0 +1 @@ ++bar +EOF + +test_expect_success 'setup' ' + + git config core.autocrlf true && + echo foo >bar && + git add bar && + test_tick && + git commit -m initial + +' + +test_expect_success 'am' ' + + git am --binary -3 Date: Tue, 4 Sep 2007 02:00:46 +0100 Subject: [PATCH 053/253] t7004: work around weird quoting issue with MSys It seems to be impossible to pass the _string_ '*a*' if there is _any_ file in the current directory whose name contains the letter 'a'. So remove all such files for the test that needs to pass that string. Signed-off-by: Johannes Schindelin --- t/t7004-tag.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 2ac0003f9e..2aa663e622 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -185,8 +185,9 @@ cba EOF test_expect_success \ 'listing tags with substring as pattern must print those matching' ' - git-tag -l "*a*" > actual && - git diff expect actual + rm *a* && + git-tag -l "*a*" > current && + git diff expect current ' cat >expect < Date: Tue, 4 Sep 2007 02:01:52 +0100 Subject: [PATCH 054/253] t0001: fix test on MinGW Another instance where we need the "-W" parameter to "pwd". Signed-off-by: Johannes Schindelin --- t/t0001-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index b14b3ec394..ca5b631012 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -97,7 +97,7 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' ' mkdir git-dir-wt-1.git && GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init ) && - check_config git-dir-wt-1.git false "$(pwd)" + check_config git-dir-wt-1.git false "$(pwd -W)" ' test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' ' From 54d99a7724a0c7fb53bcdc7d850034e9890a0138 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 4 Sep 2007 02:02:27 +0100 Subject: [PATCH 055/253] fast-import: fix compilation on MinGW MinGW does not have getppid(). So do not print it. Signed-off-by: Johannes Schindelin --- fast-import.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fast-import.c b/fast-import.c index 078079d404..1d116e7878 100644 --- a/fast-import.c +++ b/fast-import.c @@ -392,7 +392,9 @@ static void write_crash_report(const char *err) fprintf(rpt, "fast-import crash report:\n"); fprintf(rpt, " fast-import process: %d\n", getpid()); +#ifndef __MINGW32__ fprintf(rpt, " parent process : %d\n", getppid()); +#endif fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL)); fputc('\n', rpt); From d23130a5fb430c54bb928dcd4446fd98a2a9173d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 4 Sep 2007 02:03:55 +0100 Subject: [PATCH 056/253] git-compat-util.h: declare mkstemp() before use On MinGW, we roll our own version of mkstemp(). Since xmkstemp() uses it, we need to declare it before that. Signed-off-by: Johannes Schindelin --- git-compat-util.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/git-compat-util.h b/git-compat-util.h index f817c28164..ba5a1a1018 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -314,6 +314,9 @@ static inline FILE *xfdopen(int fd, const char *mode) return stream; } +#ifdef __MINGW32__ +int mkstemp (char *__template); +#endif static inline int xmkstemp(char *template) { int fd; @@ -435,7 +438,6 @@ int kill(pid_t pid, int sig); unsigned int sleep (unsigned int __seconds); const char *inet_ntop(int af, const void *src, char *dst, size_t cnt); -int mkstemp (char *__template); int gettimeofday(struct timeval *tv, void *tz); int pipe(int filedes[2]); From 9c792c5545415dfcbb60f819b9555bbe874a508c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 4 Sep 2007 02:18:52 +0100 Subject: [PATCH 057/253] Fix t3200 on MinGW Since we try to launch the webbrowser with "git --help", one test in t3200 would always try to open a window with the man page for git-branch. Suppress that test for now. Signed-off-by: Johannes Schindelin --- t/t3200-branch.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index a891fa47d3..029f19d6fa 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -17,6 +17,7 @@ test_expect_success \ git-commit -m "Initial commit." && HEAD=$(git rev-parse --verify HEAD)' +true || test_expect_failure \ 'git branch --help should not have created a bogus branch' \ 'git branch --help /dev/null 2>/dev/null || : From 01e3a15dd29ccbb1bca252c0da6b37a3f2dce958 Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Wed, 5 Sep 2007 04:32:50 -0700 Subject: [PATCH 058/253] Fixed text file auto-detection: treat EOF character 032 at the end of file as printable Signed-off-by: Dmitry Kakurin --- convert.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/convert.c b/convert.c index 21908b1039..b288552280 100644 --- a/convert.c +++ b/convert.c @@ -58,6 +58,10 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat * else stats->printable++; } + + // If file ends with EOF then don't count this EOF as non-printable + if ( size >= 1 && buf[size-1] == '\032' ) + stats->nonprintable--; } /* From ade2a2dba35b2ee97e7a46bbc57a9bd1e3b24348 Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Wed, 12 Sep 2007 11:46:39 -0400 Subject: [PATCH 059/253] git-gui: Correct error of not finding /share/git-gui/lib/tclIndex. Where the Makefile tests for Cygwin, it also needs to test for MINGW. Signed-off-by: Mike Pape --- git-gui/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/git-gui/Makefile b/git-gui/Makefile index 1bac6fed46..d8c5e65370 100644 --- a/git-gui/Makefile +++ b/git-gui/Makefile @@ -8,6 +8,7 @@ GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE -include GIT-VERSION-FILE uname_O := $(shell sh -c 'uname -o 2>/dev/null || echo not') +uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') SCRIPT_SH = git-gui.sh GITGUI_BUILT_INS = git-citool @@ -86,7 +87,8 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh $(QUIET_GEN)rm -f $@ $@+ && \ GITGUI_RELATIVE= && \ if test '$(exedir_SQ)' = '$(libdir_SQ)'; then \ - if test "$(uname_O)" = Cygwin; \ + if test "$(uname_O)" = Cygwin \ + -o "$(findstring MINGW,$(uname_S))" = MINGW; \ then GITGUI_RELATIVE= ; \ else GITGUI_RELATIVE=1; \ fi; \ From 6a561a80d5e02cd6364c577b8fc628bf8090ebff Mon Sep 17 00:00:00 2001 From: Mike Pape Date: Wed, 12 Sep 2007 18:46:39 -0400 Subject: [PATCH 060/253] Revert "git-gui: Correct error of not finding /share/git-gui/lib/tclIndex." This reverts commit ade2a2dba35b2ee97e7a46bbc57a9bd1e3b24348. --- git-gui/Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/git-gui/Makefile b/git-gui/Makefile index d8c5e65370..1bac6fed46 100644 --- a/git-gui/Makefile +++ b/git-gui/Makefile @@ -8,7 +8,6 @@ GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE -include GIT-VERSION-FILE uname_O := $(shell sh -c 'uname -o 2>/dev/null || echo not') -uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') SCRIPT_SH = git-gui.sh GITGUI_BUILT_INS = git-citool @@ -87,8 +86,7 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh $(QUIET_GEN)rm -f $@ $@+ && \ GITGUI_RELATIVE= && \ if test '$(exedir_SQ)' = '$(libdir_SQ)'; then \ - if test "$(uname_O)" = Cygwin \ - -o "$(findstring MINGW,$(uname_S))" = MINGW; \ + if test "$(uname_O)" = Cygwin; \ then GITGUI_RELATIVE= ; \ else GITGUI_RELATIVE=1; \ fi; \ From 3b9b1450636df0ceeb791e1f3837b5f711ae391e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 19 Sep 2007 15:22:13 +0100 Subject: [PATCH 061/253] gitk: force focus to main window On msysGit, the focus is first on the (Tk) console. This console is then hidden, but keeps the focus. Work around that by forcing the focus onto the gitk window. This fixes issue 14. Signed-off-by: Johannes Schindelin --- gitk | 1 + 1 file changed, 1 insertion(+) diff --git a/gitk b/gitk index b3ca704bf7..89b6398963 100755 --- a/gitk +++ b/gitk @@ -8005,4 +8005,5 @@ if {[info exists permviews]} { addviewmenu $n } } +focus -force . getcommits From 416488563ef2f68c7ceaec016c4e96ab2fd45ba7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 19 Sep 2007 15:54:53 +0100 Subject: [PATCH 062/253] Revert bd2f73a6(Support the tools scripted in perl.) This is not needed in msysGit, since we call Perl from inside MSys, and therefore paths are handled gracefully. Incidentally, it fixes issue 46 (msysGit fails to install in paths beginning with C). This reverts bd2f73a6ba6daf6158112874414d338c7b0e528d. Signed-off-by: Johannes Schindelin --- perl/Makefile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/perl/Makefile b/perl/Makefile index f7dbfca616..5e079ad011 100644 --- a/perl/Makefile +++ b/perl/Makefile @@ -20,11 +20,7 @@ clean: $(RM) $(makfile).old ifdef NO_PERL_MAKEMAKER -# We exploit that /bin/sh transforms the DOS-Stype path in TEMP into -# the MSYS style pseudo-mount form. -# This colon-free from is needed because our perl scripts look at -# $(instdir_SQ) and split it at colons. -instdir_SQ = $(subst ','\'',$(shell cmd /x/d/c "set TEMP=$(prefix)/lib && sh -c 'echo \$$TEMP'")) +instdir_SQ = $(subst ','\'',$(prefix)/lib) $(makfile): ../GIT-CFLAGS Makefile echo all: > $@ echo ' :' >> $@ From 74c2a08ef9ec16ce4cb3daef0b65fad439448538 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 19 Sep 2007 16:22:00 +0100 Subject: [PATCH 063/253] Do not complain about "no common commits" in an empty repo If the repo is empty, we know that already, thank you very much. So shut fetch-pack up about that case. Fixes issue 3, too. Signed-off-by: Johannes Schindelin --- fetch-pack.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fetch-pack.c b/fetch-pack.c index b6470932fb..ff79e56ac4 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -289,7 +289,8 @@ done: } flushes--; } - return retval; + /* it is no error to fetch into a completely empty repo */ + return count ? retval : 0; } static struct commit_list *complete; From 4ff71c309b1454f6ec7381306449017fd686944b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 23 Sep 2007 14:39:21 +0100 Subject: [PATCH 064/253] Quick 'n dirty compile fix for missing msgfmt I only wanted to see the latest and greatest in terms of running git-gui in a directory that is not a git directory. We'll fix that missing msgfmt issue later. Signed-off-by: Johannes Schindelin --- git-gui/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-gui/Makefile b/git-gui/Makefile index 6236dd6ad3..065a090c91 100644 --- a/git-gui/Makefile +++ b/git-gui/Makefile @@ -141,7 +141,7 @@ $(PO_TEMPLATE): $(SCRIPT_SH) $(ALL_LIBFILES) update-po:: $(PO_TEMPLATE) $(foreach p, $(ALL_POFILES), echo Updating $p ; msgmerge -U $p $(PO_TEMPLATE) ; ) $(ALL_MSGFILES): %.msg : %.po - $(QUIET_MSGFMT0)$(MSGFMT) --statistics --tcl $< -l $(basename $(notdir $<)) -d $(dir $@) $(QUIET_MSGFMT1) + $(QUIET_MSGFMT0)touch $@ $(QUIET_MSGFMT1) lib/tclIndex: $(ALL_LIBFILES) $(QUIET_INDEX)if echo \ From 3be4fa22e9e50839cfb3fb50970b8a247774c592 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 23 Sep 2007 14:42:46 +0100 Subject: [PATCH 065/253] git-gui: actually show the choose repository dialog For some reason, "." was withdrawn, I think. Therefore we have to deiconify it, so the user sees something. Signed-off-by: Johannes Schindelin --- git-gui/lib/choose_repository.tcl | 1 + 1 file changed, 1 insertion(+) diff --git a/git-gui/lib/choose_repository.tcl b/git-gui/lib/choose_repository.tcl index 1bec8bd0a8..5f2d12fd07 100644 --- a/git-gui/lib/choose_repository.tcl +++ b/git-gui/lib/choose_repository.tcl @@ -123,6 +123,7 @@ constructor pick {} { grab $top focus $top " + wm deiconify $top tkwait variable @done if {$top eq {.}} { From 071ba61874592b990aded3f61bb444354985d765 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 24 Sep 2007 13:46:42 +0100 Subject: [PATCH 066/253] Revert "Quick 'n dirty compile fix for missing msgfmt" This reverts commit 4ff71c309b1454f6ec7381306449017fd686944b. --- git-gui/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-gui/Makefile b/git-gui/Makefile index 065a090c91..6236dd6ad3 100644 --- a/git-gui/Makefile +++ b/git-gui/Makefile @@ -141,7 +141,7 @@ $(PO_TEMPLATE): $(SCRIPT_SH) $(ALL_LIBFILES) update-po:: $(PO_TEMPLATE) $(foreach p, $(ALL_POFILES), echo Updating $p ; msgmerge -U $p $(PO_TEMPLATE) ; ) $(ALL_MSGFILES): %.msg : %.po - $(QUIET_MSGFMT0)touch $@ $(QUIET_MSGFMT1) + $(QUIET_MSGFMT0)$(MSGFMT) --statistics --tcl $< -l $(basename $(notdir $<)) -d $(dir $@) $(QUIET_MSGFMT1) lib/tclIndex: $(ALL_LIBFILES) $(QUIET_INDEX)if echo \ From f98e862b3105ba14891c71bbe6dfb9ea677f84cc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 24 Sep 2007 13:51:57 +0100 Subject: [PATCH 067/253] git-gui: add a simple msgfmt replacement The program "msgfmt" was our only dependency on gettext. Since it is more than just a hassle to compile gettext on MinGW, here is a (very simple) drop-in replacement, which Works For Us. Signed-off-by: Johannes Schindelin --- git-gui/Makefile | 2 +- git-gui/po/po2msg.sh | 103 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 git-gui/po/po2msg.sh diff --git a/git-gui/Makefile b/git-gui/Makefile index 6236dd6ad3..b9affa4d7c 100644 --- a/git-gui/Makefile +++ b/git-gui/Makefile @@ -129,7 +129,7 @@ $(GITGUI_BUILT_INS): git-gui $(QUIET_BUILT_IN)rm -f $@ && ln git-gui $@ XGETTEXT ?= xgettext -MSGFMT ?= msgfmt +MSGFMT ?= po/po2msg.sh msgsdir = $(gg_libdir)/msgs msgsdir_SQ = $(subst ','\'',$(msgsdir)) PO_TEMPLATE = po/git-gui.pot diff --git a/git-gui/po/po2msg.sh b/git-gui/po/po2msg.sh new file mode 100644 index 0000000000..da0765dd80 --- /dev/null +++ b/git-gui/po/po2msg.sh @@ -0,0 +1,103 @@ +#!/bin/sh +# Tcl ignores the next line -*- tcl -*- \ +exec tclsh "$0" -- "$@" + +# This is a really stupid program, which serves as an alternative to +# msgfmt. It _only_ translates to Tcl mode, does _not_ validate the +# input, and does _not_ output any statistics. + +proc u2a {s} { + set res "" + foreach i [split $s ""] { + scan $i %c c + if {$c<128} { + # escape '[', '\' and ']' + if {$c == 0x5b || $c == 0x5d} { + append res "\\" + } + append res $i + } else { + append res \\u[format %04.4x $c] + } + } + return $res +} + +set output_directory "." +set lang "dummy" +set files [list] + +# parse options +for {set i 1} {$i < $argc} {incr i} { + set arg [lindex $argv $i] + if {$arg == "--statistics" || $arg == "--tcl"} { + continue + } + if {$arg == "-l"} { + incr i + set lang [lindex $argv $i] + continue + } + if {$arg == "-d"} { + incr i + set tmp [lindex $argv $i] + regsub "\[^/\]$" $tmp "&/" output_directory + continue + } + lappend files $arg +} + +proc flush_msg {} { + global msgid msgstr mode lang out + + if {![info exists msgid] || $mode == ""} { + return + } + set mode "" + + if {$msgid == ""} { + set prefix "set ::msgcat::header" + } else { + set prefix "::msgcat::mcset $lang \"[u2a $msgid]\"" + } + + puts $out "$prefix \"[u2a $msgstr]\"" +} + +foreach file $files { + regsub "^.*/\(\[^/\]*\)\.po$" $file "$output_directory\\1.msg" outfile + set in [open $file "r"] + fconfigure $in -encoding utf-8 + set out [open $outfile "w"] + + set mode "" + while {[gets $in line] >= 0} { + if {[regexp "^#" $line]} { + flush_msg + continue + } elseif {[regexp "^msgid \"(.*)\"$" $line dummy match]} { + flush_msg + set msgid $match + set mode "msgid" + } elseif {[regexp "^msgstr \"(.*)\"$" $line dummy match]} { + set msgstr $match + set mode "msgstr" + } elseif {$line == ""} { + flush_msg + } elseif {[regexp "^\"(.*)\"$" $line dummy match]} { + if {$mode == "msgid"} { + append msgid $match + } elseif {$mode == "msgstr"} { + append msgstr $match + } else { + puts stderr "I do not know what to do: $match" + } + } else { + puts stderr "Cannot handle $line" + } + } + flush_msg + close $in + close $out +} + From 803d403a7393ab267b4da1e0d57d7ee8782f2128 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 29 Sep 2007 17:40:06 +0200 Subject: [PATCH 068/253] help (msysgit): print message telling the user that browser is launched git doesn't wait until the browser is launched but returns immediatly. A user may feel more comfortable if he knows that eventually the help will appear. Signed-off-by: Steffen Prohaska --- help.c | 1 + 1 file changed, 1 insertion(+) diff --git a/help.c b/help.c index e7da8ae430..98d00cc4d9 100644 --- a/help.c +++ b/help.c @@ -193,6 +193,7 @@ static void show_man_page(const char *git_cmd) strcpy (htmlpath + prefix_len, page); strcpy (htmlpath + prefix_len + page_len, suffix); htmlpath[htmlpath_len] = 0; + printf("Launching default browser to display html help...\n"); /* We need sh here to run shell script /bin/start. */ execlp("sh", "start", "/bin/start", htmlpath, NULL ); } From 65230e232c91527c16724a665e5d5f2ea670f9b6 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 1 Oct 2007 22:56:45 +0200 Subject: [PATCH 069/253] help (msysgit): fix locating html help for paths with spaces Derive the git installation dir from the executable path and directly use the Win32 API to open the HTML file. This avoids the mingw layer and command line escaping problems, which caused the previous implementation to fail if spaces were contained in the path to git. Signed-off-by: Steffen Prohaska --- help.c | 60 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/help.c b/help.c index 98d00cc4d9..a5ada18c76 100644 --- a/help.c +++ b/help.c @@ -165,6 +165,32 @@ static void list_common_cmds_help(void) puts("(use 'git help -a' to get a list of all installed git commands)"); } +#ifdef __MINGW32__ +char* get_install_dir() +{ + static char* pgm = 0; + if (pgm) { + return pgm; + } else { + char* p; + int pgm_len = strlen(_pgmptr); + pgm = xmalloc(pgm_len + 1); + strcpy(pgm, _pgmptr); + p = strrchr(pgm, '\\'); /* \bin\ <- p */ + if (p) { + *p = '\0'; + p = strrchr(pgm, '\\'); /* \ <- p */ + if (p) { + *p = '\0'; + return pgm; + } + } + } + fprintf(stderr, "Fatal Error: failed to locate installation root.\n"); + exit(1); +} +#endif + static void show_man_page(const char *git_cmd) { const char *page; @@ -181,22 +207,24 @@ static void show_man_page(const char *git_cmd) } #ifdef __MINGW32__ - { - char* prefix = "/doc/git/html/"; - int prefix_len = strlen (prefix); - char* suffix = ".html"; - int suffix_len = strlen (suffix); - int page_len = strlen (page); - int htmlpath_len = prefix_len + page_len + suffix_len; - char* htmlpath = xmalloc (htmlpath_len + 1); - strcpy (htmlpath, prefix); - strcpy (htmlpath + prefix_len, page); - strcpy (htmlpath + prefix_len + page_len, suffix); - htmlpath[htmlpath_len] = 0; - printf("Launching default browser to display html help...\n"); - /* We need sh here to run shell script /bin/start. */ - execlp("sh", "start", "/bin/start", htmlpath, NULL ); - } + { + char* install_dir = get_install_dir(); + int install_dir_len = strlen(install_dir); + char* html_dir = "\\doc\\git\\html\\"; + int html_dir_len = strlen(html_dir); + char* suffix = ".html"; + int suffix_len = strlen(suffix); + int page_len = strlen(page); + int htmlpath_len = install_dir_len + html_dir_len + page_len + suffix_len; + char* htmlpath = xmalloc(htmlpath_len + 1); + strcpy (htmlpath, install_dir); + strcpy (htmlpath + install_dir_len, html_dir); + strcpy (htmlpath + install_dir_len + html_dir_len, page); + strcpy (htmlpath + install_dir_len + html_dir_len + page_len, suffix); + htmlpath[htmlpath_len] = 0; + printf("Launching default browser to display html help...\n"); + ShellExecute(NULL, "open", htmlpath, NULL, NULL, 0); + } #else execlp("man", "man", page, NULL); #endif From fc98ff08304caa36420028f1b5cfaa6cfdccf3d6 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 2 Oct 2007 15:25:15 +0200 Subject: [PATCH 070/253] help (msysgit): beautify message telling user that HTML will be display Signed-off-by: Steffen Prohaska --- help.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.c b/help.c index a5ada18c76..f4a0208ff3 100644 --- a/help.c +++ b/help.c @@ -222,7 +222,7 @@ static void show_man_page(const char *git_cmd) strcpy (htmlpath + install_dir_len + html_dir_len, page); strcpy (htmlpath + install_dir_len + html_dir_len + page_len, suffix); htmlpath[htmlpath_len] = 0; - printf("Launching default browser to display html help...\n"); + printf("Launching default browser to display HTML help ...\n"); ShellExecute(NULL, "open", htmlpath, NULL, NULL, 0); } #else From ded3d5f4962928feec8060cab0bbd96e7443a3dd Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 4 Oct 2007 07:45:31 +0200 Subject: [PATCH 071/253] help (msysgit): add comment about assumption on _pgmptr --- help.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/help.c b/help.c index f4a0208ff3..1c0946c649 100644 --- a/help.c +++ b/help.c @@ -186,6 +186,8 @@ char* get_install_dir() } } } + /* Note, according to the msdn documentation we have a full path + if started through the shell and this error should never happen. */ fprintf(stderr, "Fatal Error: failed to locate installation root.\n"); exit(1); } From 73b51e93d2927af2fa9f332f31c25ca2319bd0dd Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 17 Aug 2007 18:40:36 +0200 Subject: [PATCH 072/253] Work around a Windows oddity when a pipe with no reader is written to. The first WriteFile() returns ERROR_BROKEN_PIPE, subsequent WriteFile()s return ERROR_NO_DATA, which is not translated to EPIPE, but EINVAL. Hmpf! --- write_or_die.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/write_or_die.c b/write_or_die.c index e125e11d3b..40b046acb0 100644 --- a/write_or_die.c +++ b/write_or_die.c @@ -34,7 +34,16 @@ void maybe_flush_or_die(FILE *f, const char *desc) return; } if (fflush(f)) { +#ifndef __MINGW32__ if (errno == EPIPE) +#else + /* + * On Windows, EPIPE is returned only by the first write() + * after the reading end has closed its handle; subsequent + * write()s return EINVAL. + */ + if (errno == EPIPE || errno == EINVAL) +#endif exit(0); die("write failure on %s: %s", desc, strerror(errno)); } From fbb7b31c456f4c950a744a93d8521e8ef0145038 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 10 Oct 2007 09:00:12 +0200 Subject: [PATCH 073/253] Fix invocation of external git commands with arguments with spaces. If an external git command (not a shell script) was invoked with arguments that contain spaces, these arguments would be split into separate arguments. They must be quoted. This also affected installations where $prefix contained a space, as in "C:\Program Files\GIT". Both errors can be triggered by invoking git hash-object "a b" where "a b" is an existing file. --- compat/mingw.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 6632192690..f2b0ad3428 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -298,7 +298,12 @@ void mingw_execve(const char *cmd, const char **argv, const char **env) { /* check if git_command is a shell script */ if (!try_shell_exec(cmd, argv, env)) { - int ret = spawnve(_P_WAIT, cmd, argv, env); + const char **qargv; + int n; + for (n = 0; argv[n];) n++; + qargv = xmalloc((n+1)*sizeof(char*)); + quote_argv(qargv, argv); + int ret = spawnve(_P_WAIT, cmd, qargv, env); if (ret != -1) exit(ret); } From 2f346edb63d87f5c196f6254ce05e411548da04f Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 18 Oct 2007 21:27:46 +0200 Subject: [PATCH 074/253] attr: fix segfault in gitattributes parsing code git may segfault if gitattributes contains an invalid entry. A test is added to t0020 that triggers the segfault. The parsing code is fixed to avoid the crash. Signed-off-by: Steffen Prohaska --- attr.c | 5 ++++- t/t0020-crlf.sh | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/attr.c b/attr.c index 129399310a..6e82507be7 100644 --- a/attr.c +++ b/attr.c @@ -214,8 +214,11 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, num_attr = 0; cp = name + namelen; cp = cp + strspn(cp, blank); - while (*cp) + while (*cp) { cp = parse_attr(src, lineno, cp, &num_attr, res); + if (!cp) + return NULL; + } if (pass) break; res = xcalloc(1, diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh index 0807d9f01a..62bc4bb077 100755 --- a/t/t0020-crlf.sh +++ b/t/t0020-crlf.sh @@ -371,4 +371,11 @@ test_expect_success 'in-tree .gitattributes (4)' ' } ' +test_expect_success 'invalid .gitattributes (must not crash)' ' + + echo "three +crlf" >>.gitattributes && + git diff + +' + test_done From 4e8904080d62be07a139436aa44044e05e380a2c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Wed, 24 Oct 2007 22:47:14 +0200 Subject: [PATCH 075/253] Makefile: avoid absolute Windows path in built executables MINGW converts an intial '/' in paths to the full Windows path of MINGW's rootdir. Therefore, the result of the build cannot be installed in another directory. This is fixed by replacing the initial '/' by its octal escape sequence that represents '/' in C strings. This sequence avoids the path conversion. Note, the Makefile contains the full path and can not be used any longer to build git with a different prefix. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 77a011f4a9..8f2f0300f2 100644 --- a/Makefile +++ b/Makefile @@ -735,12 +735,12 @@ endif # Shell quote (do not use $(call) to accommodate ancient setups); SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER)) -ETC_GITCONFIG_SQ = $(subst ','\'',$(ETC_GITCONFIG)) +ETC_GITCONFIG_SQ = \057etc/gitconfig DESTDIR_SQ = $(subst ','\'',$(DESTDIR)) bindir_SQ = $(subst ','\'',$(bindir)) gitexecdir_SQ = $(subst ','\'',$(gitexecdir)) -template_dir_SQ = $(subst ','\'',$(template_dir)) +template_dir_SQ = \057share/git-core/templates prefix_SQ = $(subst ','\'',$(prefix)) sysconfdir_SQ = $(subst ','\'',$(sysconfdir)) From 334cf5f96d18d4c97cb4f653cd5e9f14a85997ca Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 25 Oct 2007 00:15:21 +0200 Subject: [PATCH 076/253] git-gui: Protect against bad translation strings If a translation string uses a format character we don't have an argument for then it may throw an error when we attempt to format the translation. In this case switch back to the default format that comes with the program (aka the English translation). Signed-off-by: Shawn O. Pearce Signed-off-by: Steffen Prohaska --- git-gui/git-gui.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh index 9bc5626286..0547c3a624 100755 --- a/git-gui/git-gui.sh +++ b/git-gui/git-gui.sh @@ -88,13 +88,20 @@ if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} { package require msgcat -proc mc {fmt args} { - set fmt [::msgcat::mc $fmt] +proc _mc_trim {fmt} { set cmk [string first @@ $fmt] if {$cmk > 0} { - set fmt [string range $fmt 0 [expr {$cmk - 1}]] + return [string range $fmt 0 [expr {$cmk - 1}]] } - return [eval [list format $fmt] $args] + return $fmt +} + +proc mc {en_fmt args} { + set fmt [_mc_trim [::msgcat::mc $en_fmt]] + if {[catch {set msg [eval [list format $fmt] $args]} err]} { + set msg [eval [list format [_mc_trim $en_fmt]] $args] + } + return $msg } proc strcat {args} { From 4d2a9e8f6059753c549680e0b6f30a2dcbc73c08 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 25 Oct 2007 19:11:06 +0200 Subject: [PATCH 077/253] refactor help.c to git_install_prefix() and make_native_separator() git_install_prefix() returns the install prefix of msysgit. For all other architectures it returns "". The path returned contains slashes. make_native_separator() takes a unix path (slashes) and converts the separators in place. On Windows slashes are converted to backslashes. All the code was available in help.c and is now moved to path.c. The new function will be used to located the templates and /etc/gitconfig. Signed-off-by: Steffen Prohaska --- cache.h | 3 +++ help.c | 47 +++++------------------------------------------ path.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/cache.h b/cache.h index b7f57fdc2e..e87d0a350b 100644 --- a/cache.h +++ b/cache.h @@ -381,6 +381,9 @@ static inline int is_dev_null(const char *str) return !strcmp(str, "/dev/null"); } const char *make_absolute_path(const char *path); +/* Convert slashes in place. On Windows to backslashes. */ +char *make_native_separator(char *path); +const char *git_install_prefix(); /* Read and unpack a sha1 file into memory, write memory to a sha1 file */ extern int sha1_object_info(const unsigned char *, unsigned long *); diff --git a/help.c b/help.c index 1c0946c649..e59b856d9f 100644 --- a/help.c +++ b/help.c @@ -165,34 +165,6 @@ static void list_common_cmds_help(void) puts("(use 'git help -a' to get a list of all installed git commands)"); } -#ifdef __MINGW32__ -char* get_install_dir() -{ - static char* pgm = 0; - if (pgm) { - return pgm; - } else { - char* p; - int pgm_len = strlen(_pgmptr); - pgm = xmalloc(pgm_len + 1); - strcpy(pgm, _pgmptr); - p = strrchr(pgm, '\\'); /* \bin\ <- p */ - if (p) { - *p = '\0'; - p = strrchr(pgm, '\\'); /* \ <- p */ - if (p) { - *p = '\0'; - return pgm; - } - } - } - /* Note, according to the msdn documentation we have a full path - if started through the shell and this error should never happen. */ - fprintf(stderr, "Fatal Error: failed to locate installation root.\n"); - exit(1); -} -#endif - static void show_man_page(const char *git_cmd) { const char *page; @@ -210,20 +182,11 @@ static void show_man_page(const char *git_cmd) #ifdef __MINGW32__ { - char* install_dir = get_install_dir(); - int install_dir_len = strlen(install_dir); - char* html_dir = "\\doc\\git\\html\\"; - int html_dir_len = strlen(html_dir); - char* suffix = ".html"; - int suffix_len = strlen(suffix); - int page_len = strlen(page); - int htmlpath_len = install_dir_len + html_dir_len + page_len + suffix_len; - char* htmlpath = xmalloc(htmlpath_len + 1); - strcpy (htmlpath, install_dir); - strcpy (htmlpath + install_dir_len, html_dir); - strcpy (htmlpath + install_dir_len + html_dir_len, page); - strcpy (htmlpath + install_dir_len + html_dir_len + page_len, suffix); - htmlpath[htmlpath_len] = 0; + const char *htmlpath = make_native_separator( + mkpath("%s/doc/git/html/%s.html" + , git_install_prefix() + , page) + ); printf("Launching default browser to display HTML help ...\n"); ShellExecute(NULL, "open", htmlpath, NULL, NULL, 0); } diff --git a/path.c b/path.c index 7a5ac5b067..e42b234fa1 100644 --- a/path.c +++ b/path.c @@ -370,3 +370,51 @@ const char *make_absolute_path(const char *path) return buf; } + +const char* git_install_prefix() +{ +#ifdef __MINGW32__ + static char* prefix; + + if (prefix) { + return prefix; + } + + char* p; + int pgm_len = strlen(_pgmptr); + prefix = xmalloc(pgm_len + 1); + strcpy(prefix, _pgmptr); + p = strrchr(prefix, '\\'); /* \bin\ <- p */ + if (p) { + *p = '\0'; + p = strrchr(prefix, '\\'); /* \ <- p */ + if (p) { + *p = '\0'; + for (p = prefix; *p; p++) + if (*p == '\\') + *p = '/'; + return prefix; + } + } + + /* Note, according to the msdn documentation we have a full path + if started through the shell and this error should never happen. */ + fprintf(stderr, "Fatal Error: failed to locate installation root.\n"); + exit(1); +#else + return ""; +#endif +} + +char *make_native_separator(char* path) { +#ifdef __MINGW32__ + char* c; + for (c = path; *c; c++) { + if (*c == '/') + *c = '\\'; + } + return path; +#else + return path; +#endif +} From 0fbe22da392f39e1d144f78ef08b2dbdb882c164 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 25 Oct 2007 19:14:56 +0200 Subject: [PATCH 078/253] init: find template directory on Windows (msys) The template directory must be searched for using the full Windows path. But internally a Unix path starting with '/' is used. The internal Unix path is now prefixed with git_install_prefix() before accessing the file system Only absolute Unix paths are converted. Relative paths can be used as is. Signed-off-by: Steffen Prohaska --- builtin-init-db.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/builtin-init-db.c b/builtin-init-db.c index 5da2515f85..117fedd586 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -145,6 +145,17 @@ static void copy_templates(const char *git_dir, int len, const char *template_di template_dir = prefix_path(exec_path, strlen(exec_path), template_dir); } +#if __MINGW32__ + /* + * If it is an absolute Unix path it is prefixed with + * the git_install_prefix(). + */ + else if (template_dir[0] == '/') { + const char* prefix = git_install_prefix(); + template_dir = prefix_path(prefix, strlen(prefix), + template_dir); + } +#endif } strcpy(template_path, template_dir); template_len = strlen(template_path); From 636116b8a79c7c810afe8ae8e4c37e0a1de6f7b7 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 25 Oct 2007 19:16:11 +0200 Subject: [PATCH 079/253] config.c: find /etc/gitconfig on Windows (msys) /etc/gitconfig must be searched for using the full Windows path. But internally a Unix path starting with '/' is used. An absolute Unix path is now prefixed with git_install_prefix(). Signed-off-by: Steffen Prohaska --- config.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config.c b/config.c index da2d1c1667..be0b1f0ed2 100644 --- a/config.c +++ b/config.c @@ -466,6 +466,17 @@ const char *git_etc_gitconfig(void) system_wide = prefix_path(exec_path, strlen(exec_path), system_wide); } +#if __MINGW32__ + /* + * If it is an absolute Unix path it is prefixed with + * the git_install_prefix(). + */ + else if (system_wide[0] == '/') { + const char* prefix = git_install_prefix(); + system_wide = prefix_path(prefix, strlen(prefix), + system_wide); + } +#endif } return system_wide; } From 5f50b3edfa54d04fb751dc847687541a4c611f7c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 3 Nov 2007 18:01:31 +0100 Subject: [PATCH 080/253] Revert "Returning -1 does not fail like it should in MSys." This reverts commit 327005a9ef9c3b784e4742cece0710de6a549313. git-config should print a message and exit with error if --file is used and the file provided does not exist. This was not the case. The problem was introduced by the reverted commit. Signed-off-by: Steffen Prohaska --- config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.c b/config.c index 0badbeb62e..edea061892 100644 --- a/config.c +++ b/config.c @@ -443,7 +443,7 @@ int git_config_from_file(config_fn_t fn, const char *filename) int ret; FILE *f = fopen(filename, "r"); - ret = 1; + ret = -1; if (f) { config_file = f; config_file_name = filename; From c1561567eff8ae46e88aadc51c7f5062cca34776 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 3 Nov 2007 18:14:27 +0100 Subject: [PATCH 081/253] fix warning (printf format) Signed-off-by: Steffen Prohaska --- builtin-ls-files.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin-ls-files.c b/builtin-ls-files.c index b70da1863b..b55c79ec1a 100644 --- a/builtin-ls-files.c +++ b/builtin-ls-files.c @@ -208,7 +208,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce) if (!show_stage) { fputs(tag, stdout); } else { - printf("%s%06o %s %d\t", + printf("%s%06lo %s %d\t", tag, ntohl(ce->ce_mode), abbrev ? find_unique_abbrev(ce->sha1,abbrev) From 156d5a32ad4dc70460a62980547bcfda788ace58 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 3 Nov 2007 18:17:31 +0100 Subject: [PATCH 082/253] fix warning (const ptr) Signed-off-by: Steffen Prohaska --- exec_cmd.c | 4 ++-- run-command.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index 6aa842bb4f..a2d82d3311 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -121,7 +121,7 @@ int execv_git_cmd(const char **argv) trace_argv_printf(argv, -1, "trace: exec:"); /* execvp() can only ever return if it fails */ - execvp(cmd.buf, (char **)argv); + execvp(cmd.buf, argv); trace_printf("trace: exec failed: %s\n", strerror(errno)); @@ -175,7 +175,7 @@ int spawnv_git_cmd(const char **argv, int pin[2], int pout[2]) trace_argv_printf(argv, -1, "trace: exec:"); - pid = spawnvpe_pipe(cmd.buf, argv, environ, + pid = spawnvpe_pipe(cmd.buf, argv, (const char **)environ, pin, pout); argv[0] = tmp; diff --git a/run-command.c b/run-command.c index d2e5f183d2..822449c5f5 100644 --- a/run-command.c +++ b/run-command.c @@ -15,7 +15,7 @@ int start_command(struct child_process *cmd) int fdin[2] = { -1, -1 }; int fdout[2] = { -1, -1 }; int fderr[2] = { -1, -1 }; - char **env = environ; + const char **env = (const char **)environ; need_in = !cmd->no_stdin && cmd->in < 0; if (need_in) { From e169a6e89f8b9affc8131e3b3b0752064c1dbc86 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 3 Nov 2007 18:24:08 +0100 Subject: [PATCH 083/253] fix warning (unitialized variable) Signed-off-by: Steffen Prohaska --- transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transport.c b/transport.c index 5132d289da..d44fe7cee7 100644 --- a/transport.c +++ b/transport.c @@ -107,7 +107,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list) return; for (;;) { - int cmp, len; + int cmp = cmp, len; if (!fgets(buffer, sizeof(buffer), f)) { fclose(f); From 0f2bc7c7d28274ba2e130a03817c9ca2d677901e Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 3 Nov 2007 18:27:12 +0100 Subject: [PATCH 084/253] fix warning (unused variable) --- upload-pack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/upload-pack.c b/upload-pack.c index 0d7aa18bfe..e004c024cd 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -142,7 +142,7 @@ static void create_pack_file(void) struct async rev_list; struct child_process pack_objects; int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr); - char data[8193], progress[128]; + char data[8193]; char abort_msg[] = "aborting due to possible repository " "corruption on the remote side."; int buffered = -1; From e176f0fb1519dc1a9c3e55aa00f03ffb85fc3143 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 3 Nov 2007 18:32:18 +0100 Subject: [PATCH 085/253] fix warning (int used as pointer) Signed-off-by: Steffen Prohaska --- test-chmtime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-chmtime.c b/test-chmtime.c index b6dc548a2a..8a1e6b8479 100644 --- a/test-chmtime.c +++ b/test-chmtime.c @@ -22,7 +22,7 @@ int git_utime (const char *file_name, const struct utimbuf *times) time_t_to_filetime(times->modtime, &mft); time_t_to_filetime(times->actime, &aft); - if (!SetFileTime(_get_osfhandle(fh), NULL, &aft, &mft)) { + if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) { errno = EINVAL; rc = -1; } else From 0ed5d28283854fc47f130d65f6b658398eef80d8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 3 Sep 2007 17:36:47 +0100 Subject: [PATCH 086/253] MinGW: Convert CR/LF to LF in tag signatures On Windows, gpg outputs CR/LF signatures. But since the tag messages are already stripped of the CR by stripspace(), it is arguably nicer to do the same for the tag signature. Actually, this patch does not look for CR/LF, but strips all CRs from the signature. [ sp: ported code to use strbuf ] Signed-off-by: Johannes Schindelin Signed-off-by: Steffen Prohaska --- builtin-tag.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/builtin-tag.c b/builtin-tag.c index 66e5a58307..ae8538897b 100644 --- a/builtin-tag.c +++ b/builtin-tag.c @@ -224,6 +224,20 @@ static int do_sign(struct strbuf *buffer) if (len < 0) return error("could not read the entire signature from gpg."); +#ifdef __MINGW32__ + /* strip CR from the line endings */ + { + int i, j; + for (i = j = 0; i < buffer->len; i++) + if (buffer->buf[i] != '\r') { + if (i != j) + buffer->buf[j] = buffer->buf[i]; + j++; + } + strbuf_setlen(buffer, j); + } +#endif + return 0; } From 24737fec8695e6c4aa569fbb39d83c950a276844 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 20 Oct 2007 21:59:28 +0200 Subject: [PATCH 087/253] git remote does not work from the t/ directory - skip the test. --- t/t5505-remote.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 636aec2f71..a56e5a32c2 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -4,6 +4,9 @@ test_description='git remote porcelain-ish' . ./test-lib.sh +say "git remote does not work in t/ - skipping" +test_done + GIT_CONFIG=.git/config export GIT_CONFIG From a67246dc8aa004c6aaa0060f04e6fda427099079 Mon Sep 17 00:00:00 2001 From: Brian Gernhardt Date: Sun, 4 Nov 2007 10:31:26 -0500 Subject: [PATCH 088/253] t3502: Disambiguate between file and rev by adding -- On a case insensitive file system, this test fails because git-diff doesn't know if it is asking for the file "A" or the tag "a". Adding "--" at the end of the ambiguous commands allows the test to finish properly. Signed-off-by: Brian Gernhardt Signed-off-by: Junio C Hamano --- t/t3502-cherry-pick-merge.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/t/t3502-cherry-pick-merge.sh b/t/t3502-cherry-pick-merge.sh index 3274c6141b..7c92e261fc 100755 --- a/t/t3502-cherry-pick-merge.sh +++ b/t/t3502-cherry-pick-merge.sh @@ -36,7 +36,7 @@ test_expect_success 'cherry-pick a non-merge with -m should fail' ' git reset --hard && git checkout a^0 && ! git cherry-pick -m 1 b && - git diff --exit-code a + git diff --exit-code a -- ' @@ -45,7 +45,7 @@ test_expect_success 'cherry pick a merge without -m should fail' ' git reset --hard && git checkout a^0 && ! git cherry-pick c && - git diff --exit-code a + git diff --exit-code a -- ' @@ -98,7 +98,7 @@ test_expect_success 'revert a merge (1)' ' git reset --hard && git checkout c^0 && git revert -m 1 c && - git diff --exit-code a + git diff --exit-code a -- ' @@ -107,7 +107,7 @@ test_expect_success 'revert a merge (2)' ' git reset --hard && git checkout c^0 && git revert -m 2 c && - git diff --exit-code b + git diff --exit-code b -- ' From 9dcfde61ed7334e925fca7e62dbb5791547a5f5b Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 4 Nov 2007 23:50:23 +0100 Subject: [PATCH 089/253] help: Teach help to try harder to locate help Help now tries to takes the argument as is and prefixed with "git-". If no html page is found an error is reported. With this change you can run "git help user-manual". Signed-off-by: Steffen Prohaska --- help.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/help.c b/help.c index b81ddf0fb5..02e59b75e0 100644 --- a/help.c +++ b/help.c @@ -7,6 +7,7 @@ #include "builtin.h" #include "exec_cmd.h" #include "common-cmds.h" +#include "dir.h" //#include /* most GUI terminals set COLUMNS (although some don't export it) */ @@ -242,6 +243,29 @@ void list_common_cmds_help(void) static void show_man_page(const char *git_cmd) { +#ifdef __MINGW32__ + { + char *htmlpath = make_native_separator( + mkpath("%s/doc/git/html/%s.html" + , git_install_prefix() + , git_cmd) + ); + if (!file_exists(htmlpath)) { + htmlpath = make_native_separator( + mkpath("%s/doc/git/html/git-%s.html" + , git_install_prefix() + , git_cmd) + ); + if (!file_exists(htmlpath)) { + fprintf(stderr, "Can't find help for '%s'.\n" + , git_cmd); + exit(1); + } + } + printf("Launching default browser to display HTML help ...\n"); + ShellExecute(NULL, "open", htmlpath, NULL, NULL, 0); + } +#else const char *page; if (!prefixcmp(git_cmd, "git")) @@ -255,17 +279,6 @@ static void show_man_page(const char *git_cmd) page = p; } -#ifdef __MINGW32__ - { - const char *htmlpath = make_native_separator( - mkpath("%s/doc/git/html/%s.html" - , git_install_prefix() - , page) - ); - printf("Launching default browser to display HTML help ...\n"); - ShellExecute(NULL, "open", htmlpath, NULL, NULL, 0); - } -#else execlp("man", "man", page, NULL); #endif } From 069930410211632f2f8beea303771088226b523f Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 16:09:26 +0100 Subject: [PATCH 090/253] Use relative paths to make installation relocatable mingw uses relative paths for template_dir and ETC_GITCONFIG to make an installation relocatable. Relative paths will be expanded relative to git_exec_dir(). This mechanism is now included in official git.git. We used a different mechanism to create a relocatable installation. We used to prefix absolute Unix paths with the installation prefix, derived from the path of git.exe. This commit reverts our implementation to match mingw. Part of the change are reverts of the following commits, which are no longer needed: 4e8904080d62be07a139436aa44044e05e380a2c 0fbe22da392f39e1d144f78ef08b2dbdb882c164. 636116b8a79c7c810afe8ae8e4c37e0a1de6f7b7. Signed-off-by: Steffen Prohaska --- Makefile | 7 ++++--- builtin-init-db.c | 11 ----------- config.c | 11 ----------- 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 4c749a0357..8586fcd397 100644 --- a/Makefile +++ b/Makefile @@ -523,7 +523,8 @@ ifneq (,$(findstring MINGW,$(uname_S))) EXTLIBS += -lws2_32 X = .exe NOEXECTEMPL = .noexec - prefix = + template_dir = ../share/git-core/templates/ + ETC_GITCONFIG = ../etc/gitconfig endif ifneq (,$(findstring arm,$(uname_M))) ARM_SHA1 = YesPlease @@ -774,12 +775,12 @@ endif # Shell quote (do not use $(call) to accommodate ancient setups); SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER)) -ETC_GITCONFIG_SQ = \057etc/gitconfig +ETC_GITCONFIG_SQ = $(subst ','\'',$(ETC_GITCONFIG)) DESTDIR_SQ = $(subst ','\'',$(DESTDIR)) bindir_SQ = $(subst ','\'',$(bindir)) gitexecdir_SQ = $(subst ','\'',$(gitexecdir)) -template_dir_SQ = \057share/git-core/templates +template_dir_SQ = $(subst ','\'',$(template_dir)) prefix_SQ = $(subst ','\'',$(prefix)) sysconfdir_SQ = $(subst ','\'',$(sysconfdir)) diff --git a/builtin-init-db.c b/builtin-init-db.c index 2feec40eb6..bb132ec086 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -145,17 +145,6 @@ static void copy_templates(const char *git_dir, int len, const char *template_di template_dir = prefix_path(exec_path, strlen(exec_path), template_dir); } -#if __MINGW32__ - /* - * If it is an absolute Unix path it is prefixed with - * the git_install_prefix(). - */ - else if (template_dir[0] == '/') { - const char* prefix = git_install_prefix(); - template_dir = prefix_path(prefix, strlen(prefix), - template_dir); - } -#endif } strcpy(template_path, template_dir); template_len = strlen(template_path); diff --git a/config.c b/config.c index 0b167ceb75..7b9ded1655 100644 --- a/config.c +++ b/config.c @@ -473,17 +473,6 @@ const char *git_etc_gitconfig(void) system_wide = prefix_path(exec_path, strlen(exec_path), system_wide); } -#if __MINGW32__ - /* - * If it is an absolute Unix path it is prefixed with - * the git_install_prefix(). - */ - else if (system_wide[0] == '/') { - const char* prefix = git_install_prefix(); - system_wide = prefix_path(prefix, strlen(prefix), - system_wide); - } -#endif } return system_wide; } From e9d8b9207e7ae7be0cf33a7cdc18d0e14c12c02a Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 16:43:13 +0100 Subject: [PATCH 091/253] help: search html documentation relativ to git_exec_dir() Global gitconfig and templates are searched relativ to git_exec_dir() to make the installation relocatable. This commit changes help to do the same for finding the html documentation. Signed-off-by: Steffen Prohaska --- help.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/help.c b/help.c index 90d5a03345..f89164a760 100644 --- a/help.c +++ b/help.c @@ -245,15 +245,16 @@ static void show_man_page(const char *git_cmd) { #ifdef __MINGW32__ { + const char* exec_path = git_exec_path(); char *htmlpath = make_native_separator( - mkpath("%s/doc/git/html/%s.html" - , git_install_prefix() - , git_cmd) + mkpath("%s/../doc/git/html/%s.html" + , exec_path + , git_cmd) ); if (!file_exists(htmlpath)) { htmlpath = make_native_separator( - mkpath("%s/doc/git/html/git-%s.html" - , git_install_prefix() + mkpath("%s/../doc/git/html/git-%s.html" + , exec_path , git_cmd) ); if (!file_exists(htmlpath)) { From 51355b1ede40a910bf4a62f9a8d0cbd7abcdecf7 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 16:45:19 +0100 Subject: [PATCH 092/253] remove git_install_prefix() git_install_prefix() is no longer needed. Relocatable paths are searched relative to git_exec_dir(). This commit removes git_install_prefix(). Signed-off-by: Steffen Prohaska --- cache.h | 1 - path.c | 35 ----------------------------------- 2 files changed, 36 deletions(-) diff --git a/cache.h b/cache.h index 1fbc9a1987..550e8ad192 100644 --- a/cache.h +++ b/cache.h @@ -391,7 +391,6 @@ static inline int is_dev_null(const char *str) const char *make_absolute_path(const char *path); /* Convert slashes in place. On Windows to backslashes. */ char *make_native_separator(char *path); -const char *git_install_prefix(); /* Read and unpack a sha1 file into memory, write memory to a sha1 file */ extern int sha1_object_info(const unsigned char *, unsigned long *); diff --git a/path.c b/path.c index 01f35d5e7d..2a76269705 100644 --- a/path.c +++ b/path.c @@ -359,41 +359,6 @@ const char *make_absolute_path(const char *path) return buf; } -const char* git_install_prefix() -{ -#ifdef __MINGW32__ - static char* prefix; - - if (prefix) { - return prefix; - } - - char* p; - int pgm_len = strlen(_pgmptr); - prefix = xmalloc(pgm_len + 1); - strcpy(prefix, _pgmptr); - p = strrchr(prefix, '\\'); /* \bin\ <- p */ - if (p) { - *p = '\0'; - p = strrchr(prefix, '\\'); /* \ <- p */ - if (p) { - *p = '\0'; - for (p = prefix; *p; p++) - if (*p == '\\') - *p = '/'; - return prefix; - } - } - - /* Note, according to the msdn documentation we have a full path - if started through the shell and this error should never happen. */ - fprintf(stderr, "Fatal Error: failed to locate installation root.\n"); - exit(1); -#else - return ""; -#endif -} - char *make_native_separator(char* path) { #ifdef __MINGW32__ char* c; From 5e23af17e42294bae625713c754ad98838b5e9bc Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 22:40:12 +0100 Subject: [PATCH 093/253] Remove unnecessary decl of sync() Signed-off-by: Steffen Prohaska --- builtin-prune-packed.c | 2 -- builtin-prune.c | 2 -- 2 files changed, 4 deletions(-) diff --git a/builtin-prune-packed.c b/builtin-prune-packed.c index b61b73a900..23faf3129f 100644 --- a/builtin-prune-packed.c +++ b/builtin-prune-packed.c @@ -8,8 +8,6 @@ static const char prune_packed_usage[] = #define DRY_RUN 01 #define VERBOSE 02 -void sync(void); - static struct progress *progress; static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts) diff --git a/builtin-prune.c b/builtin-prune.c index 02e4c09b0e..44df59e4a7 100644 --- a/builtin-prune.c +++ b/builtin-prune.c @@ -8,8 +8,6 @@ static const char prune_usage[] = "git-prune [-n]"; static int show_only; -void sync(void); - static int prune_object(char *path, const char *filename, const unsigned char *sha1) { if (show_only) { From 32dead9686779b65214c688bbe23eebdae2980f5 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 22:51:40 +0100 Subject: [PATCH 094/253] Use getpagesize() from git-compat-util.h Signed-off-by: Steffen Prohaska --- config.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/config.c b/config.c index 7b9ded1655..ed96213c44 100644 --- a/config.c +++ b/config.c @@ -15,8 +15,6 @@ static const char *config_file_name; static int config_linenr; static int zlib_compression_seen; -extern int getpagesize(); - static int get_next_char(void) { int c; From 66417d164e18193aef3bd935ab6e7755f5161f73 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 22:00:39 +0100 Subject: [PATCH 095/253] Add ifdef __MINGW32__ to avoid warning about unused "progress" Signed-off-by: Steffen Prohaska --- upload-pack.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/upload-pack.c b/upload-pack.c index 97ffa879f3..115f7a1e56 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -143,6 +143,9 @@ static void create_pack_file(void) struct child_process pack_objects; int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr); char data[8193]; +#ifndef __MINGW32__ + char progress[128]; +#endif char abort_msg[] = "aborting due to possible repository " "corruption on the remote side."; int buffered = -1; From af18e7d9d3abad0e42477a3c15c0e3f0bdc9ae15 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 21:07:32 +0100 Subject: [PATCH 096/253] Fix prototypes for mingw_execve and mingw_execvp to match Posix This changes the prototypes to match http://www.opengroup.org/onlinepubs/7990989775/xsh/exec.html Note, spawnvpe uses a different type for argv and envp than execve. So at some point we need to cast. This commit shifts the cast into the compat functions. From the outside, execve and execvp match Posix. Signed-off-by: Steffen Prohaska --- compat/mingw.c | 19 ++++++++++--------- git-compat-util.h | 6 +++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index b905673a40..b841f6d0ca 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -352,7 +352,7 @@ static const char *quote_arg(const char *arg) return q; } -void quote_argv(const char **dst, const char **src) +void quote_argv(const char **dst, const char *const *src) { while (*src) *dst++ = quote_arg(*src++); @@ -394,7 +394,7 @@ const char *parse_interpreter(const char *cmd) return p+1; } -static int try_shell_exec(const char *cmd, const char **argv, const char **env) +static int try_shell_exec(const char *cmd, char *const *argv, char *const *env) { const char **sh_argv; int n; @@ -412,14 +412,14 @@ static int try_shell_exec(const char *cmd, const char **argv, const char **env) sh_argv = xmalloc((n+2)*sizeof(char*)); sh_argv[0] = interpr; sh_argv[1] = quote_arg(cmd); - quote_argv(&sh_argv[2], &argv[1]); - n = spawnvpe(_P_WAIT, interpr, sh_argv, env); + quote_argv(&sh_argv[2], (const char *const *)&argv[1]); + n = spawnvpe(_P_WAIT, interpr, sh_argv, (const char *const *)env); if (n == -1) return 1; /* indicate that we tried but failed */ exit(n); } -void mingw_execve(const char *cmd, const char **argv, const char **env) +void mingw_execve(const char *cmd, char *const *argv, char *const *env) { /* check if git_command is a shell script */ if (!try_shell_exec(cmd, argv, env)) { @@ -427,8 +427,9 @@ void mingw_execve(const char *cmd, const char **argv, const char **env) int n; for (n = 0; argv[n];) n++; qargv = xmalloc((n+1)*sizeof(char*)); - quote_argv(qargv, argv); - int ret = spawnve(_P_WAIT, cmd, qargv, env); + quote_argv(qargv, (const char *const *)argv); + int ret = spawnve(_P_WAIT, cmd, qargv, + (const char *const *)env); if (ret != -1) exit(ret); } @@ -522,13 +523,13 @@ void mingw_free_path_split(char **path) free(path); } -void mingw_execvp(const char *cmd, const char **argv) +void mingw_execvp(const char *cmd, char *const *argv) { char **path = mingw_get_path_split(); char *prog = mingw_path_lookup(cmd, path); if (prog) { - mingw_execve(prog, argv, (const char **) environ); + mingw_execve(prog, argv, environ); free(prog); } else errno = ENOENT; diff --git a/git-compat-util.h b/git-compat-util.h index 921a6c08b4..eb4d96944e 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -454,9 +454,9 @@ static inline int kill(pid_t pid, int sig) static inline unsigned int alarm(unsigned int seconds) { return 0; } -void mingw_execve(const char *cmd, const char **argv, const char **env); +void mingw_execve(const char *cmd, char *const *argv, char * const *env); #define execve mingw_execve -extern void mingw_execvp(const char *cmd, const char **argv); +extern void mingw_execvp(const char *cmd, char *const *argv); #define execvp mingw_execvp typedef int pid_t; static inline int waitpid(pid_t pid, unsigned *status, unsigned options) @@ -526,7 +526,7 @@ static inline int getppid(void) { return 1; } static inline void sync(void) {} extern int getpagesize(void); /* defined in MinGW's libgcc.a */ -extern void quote_argv(const char **dst, const char **src); +extern void quote_argv(const char **dst, const char *const *src); extern const char *parse_interpreter(const char *cmd); extern char *mingw_path_lookup(const char *cmd, char **path); extern char **mingw_get_path_split(void); From ad941f23363d2f37cef8dd6a59b61be2b740997c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 22:43:26 +0100 Subject: [PATCH 097/253] exec_cmd.c: Add cast that is needed for Posix execvp Signed-off-by: Steffen Prohaska --- exec_cmd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec_cmd.c b/exec_cmd.c index a2d82d3311..de4c174b68 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -121,7 +121,7 @@ int execv_git_cmd(const char **argv) trace_argv_printf(argv, -1, "trace: exec:"); /* execvp() can only ever return if it fails */ - execvp(cmd.buf, argv); + execvp(cmd.buf, (char **)argv); trace_printf("trace: exec failed: %s\n", strerror(errno)); From 565861de3b6357339fdcd087fa52fc4c14b6de24 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 23:01:56 +0100 Subject: [PATCH 098/253] spawn-pipe.c: Remove unnecessary cast Signed-off-by: Steffen Prohaska --- spawn-pipe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spawn-pipe.c b/spawn-pipe.c index 04146d5631..e685eff643 100644 --- a/spawn-pipe.c +++ b/spawn-pipe.c @@ -103,7 +103,7 @@ int spawnvppe_pipe(const char *cmd, const char **argv, const char **env, qargv = xmalloc((argc+2)*sizeof(char*)); if (!interpr) { quote_argv(qargv, argv); - pid = spawnve(_P_NOWAIT, prog, qargv, (const char**) env); + pid = spawnve(_P_NOWAIT, prog, qargv, env); } else { qargv[0] = interpr; argv[0] = prog; From d3dcd107758907e38751fb50631e9acf315a6bad Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 18 Nov 2007 12:23:52 +0100 Subject: [PATCH 099/253] git.c: Take '--bare' handling from mingw master Merge commit 53009e47c2b55d6c23925bb77116545f2308cef9 introduced an if() statement that is not present in mingw's master. This commit changes the code to match mingw's master. Signed-off-by: Steffen Prohaska --- git.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/git.c b/git.c index 615093453b..e2c79a1efc 100644 --- a/git.c +++ b/git.c @@ -72,11 +72,9 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) } else if (!strcmp(cmd, "--bare")) { static char git_dir[PATH_MAX+1]; is_bare_repository_cfg = 1; - if (!getenv(GIT_DIR_ENVIRONMENT)) { - set_git_dir(getcwd(git_dir, sizeof(git_dir))); - if (envchanged) - *envchanged = 1; - } + set_git_dir(getcwd(git_dir, sizeof(git_dir))); + if (envchanged) + *envchanged = 1; } else { fprintf(stderr, "Unknown option: %s\n", cmd); usage(git_usage_string); From 31023be9997d657dc653fe3358be50d1e7268a31 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 20:48:14 +0100 Subject: [PATCH 100/253] compat/pread.c: Add foward decl to fix warning read_in_full()'s is used in compat/pread.c. read_in_full() is declared in cache.h. But we can't include cache.h because too many macros are defined there. Using read_in_full() without including cache.h is dangerous because we wouldn't recognize if its prototyp changed. gcc issues a warning about that. This commit adds a forward decl to git-compat-util.h. git-compat-util.h is included by compat/pread.c _and_ cache.h. Hence, changes in cache.h would be detected. Signed-off-by: Steffen Prohaska --- compat/pread.c | 2 -- git-compat-util.h | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compat/pread.c b/compat/pread.c index ce29997724..978cac4ec9 100644 --- a/compat/pread.c +++ b/compat/pread.c @@ -1,7 +1,5 @@ #include "../git-compat-util.h" -int read_in_full(int fd, void *buf, size_t count); - ssize_t git_pread(int fd, void *buf, size_t count, off_t offset) { off_t current_offset; diff --git a/git-compat-util.h b/git-compat-util.h index eb4d96944e..f4e0d08357 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -153,6 +153,10 @@ extern int git_munmap(void *start, size_t length); #define pread git_pread extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset); #endif +/* Forward decl that will remind us if its twin in cache.h changes. + This function in used in compat/pread.c. But we can't include + cache.h there. */ +extern int read_in_full(int fd, void *buf, size_t count); #ifdef NO_SETENV #define setenv gitsetenv From 43664aa169b6efeb80245772f7ad9ff1c6e5edb0 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 19:16:53 +0100 Subject: [PATCH 101/253] Revert ntohl()/printf format to Junio's git This commits reverts printf format strings to the original version from Juion's git. This is a preparation for the mingw specific fix that follows in the next commit. Signed-off-by: Steffen Prohaska --- builtin-fetch-pack.c | 2 +- builtin-ls-files.c | 2 +- builtin-unpack-objects.c | 2 +- index-pack.c | 2 +- merge-index.c | 2 +- read-cache.c | 2 +- receive-pack.c | 2 +- sha1_file.c | 4 ++-- show-index.c | 2 +- unpack-trees.c | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c index 3510bfd089..a2ca746dd0 100644 --- a/builtin-fetch-pack.c +++ b/builtin-fetch-pack.c @@ -509,7 +509,7 @@ static int get_pack(int xd[2], char **pack_lockfile) if (read_pack_header(fd[0], &header)) die("protocol error: bad pack header"); - snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%lu,%lu", + snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u", ntohl(header.hdr_version), ntohl(header.hdr_entries)); if (ntohl(header.hdr_entries) < unpack_limit) do_keep = 0; diff --git a/builtin-ls-files.c b/builtin-ls-files.c index dde79545e2..e0b856f432 100644 --- a/builtin-ls-files.c +++ b/builtin-ls-files.c @@ -208,7 +208,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce) if (!show_stage) { fputs(tag, stdout); } else { - printf("%s%06lo %s %d\t", + printf("%s%06o %s %d\t", tag, ntohl(ce->ce_mode), abbrev ? find_unique_abbrev(ce->sha1,abbrev) diff --git a/builtin-unpack-objects.c b/builtin-unpack-objects.c index 948e4bec51..1e51865c52 100644 --- a/builtin-unpack-objects.c +++ b/builtin-unpack-objects.c @@ -318,7 +318,7 @@ static void unpack_all(void) if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE) die("bad pack file"); if (!pack_version_ok(hdr->hdr_version)) - die("unknown pack file version %lu", ntohl(hdr->hdr_version)); + die("unknown pack file version %d", ntohl(hdr->hdr_version)); use(sizeof(struct pack_header)); if (!quiet) diff --git a/index-pack.c b/index-pack.c index 6268da94b8..9fd6982a97 100644 --- a/index-pack.c +++ b/index-pack.c @@ -143,7 +143,7 @@ static void parse_pack_header(void) if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) die("pack signature mismatch"); if (!pack_version_ok(hdr->hdr_version)) - die("pack version %lu unsupported", ntohl(hdr->hdr_version)); + die("pack version %d unsupported", ntohl(hdr->hdr_version)); nr_objects = ntohl(hdr->hdr_entries); use(sizeof(struct pack_header)); diff --git a/merge-index.c b/merge-index.c index 1cf4cb5b3f..fa719cb0b1 100644 --- a/merge-index.c +++ b/merge-index.c @@ -48,7 +48,7 @@ static int merge_entry(int pos, const char *path) break; found++; strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); - sprintf(ownbuf[stage], "%lo", ntohl(ce->ce_mode)); + sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode)); arguments[stage] = hexbuf[stage]; arguments[stage + 4] = ownbuf[stage]; } while (++pos < active_nr); diff --git a/read-cache.c b/read-cache.c index 128835b684..7748c3ece7 100644 --- a/read-cache.c +++ b/read-cache.c @@ -152,7 +152,7 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st) case 0: /* Special case: unmerged file in index */ return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; default: - die("internal error: ce_mode is %lo", ntohl(ce->ce_mode)); + die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); } if (ce->ce_mtime.sec != htonl(st->st_mtime)) changed |= MTIME_CHANGED; diff --git a/receive-pack.c b/receive-pack.c index a66a6e45d2..38e35c06b9 100644 --- a/receive-pack.c +++ b/receive-pack.c @@ -352,7 +352,7 @@ static const char *unpack(void) hdr_err = parse_pack_header(&hdr); if (hdr_err) return hdr_err; - snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%lu,%lu", + snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u", ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); if (ntohl(hdr.hdr_entries) < unpack_limit) { diff --git a/sha1_file.c b/sha1_file.c index 6b0e4c1d02..bd0e7cd21c 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -670,13 +670,13 @@ static int open_packed_git_1(struct packed_git *p) if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) return error("file %s is not a GIT packfile", p->pack_name); if (!pack_version_ok(hdr.hdr_version)) - return error("packfile %s is version %lu and not supported" + return error("packfile %s is version %u and not supported" " (try upgrading GIT to a newer version)", p->pack_name, ntohl(hdr.hdr_version)); /* Verify the pack matches its index. */ if (p->num_objects != ntohl(hdr.hdr_entries)) - return error("packfile %s claims to have %lu objects" + return error("packfile %s claims to have %u objects" " while index indicates %u objects", p->pack_name, ntohl(hdr.hdr_entries), p->num_objects); diff --git a/show-index.c b/show-index.c index 9749e2da48..7253991fff 100644 --- a/show-index.c +++ b/show-index.c @@ -68,7 +68,7 @@ int main(int argc, char **argv) ntohl(off64[1]); off64_nr++; } - printf("%" PRIuMAX " %s (%08lx)\n", (uintmax_t) offset, + printf("%" PRIuMAX " %s (%08x)\n", (uintmax_t) offset, sha1_to_hex(entries[i].sha1), ntohl(entries[i].crc)); } diff --git a/unpack-trees.c b/unpack-trees.c index 18f586cefc..aea16adde8 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -638,7 +638,7 @@ static void show_stage_entry(FILE *o, if (!ce) fprintf(o, "%s (missing)\n", label); else - fprintf(o, "%s%06lo %s %d\t%s\n", + fprintf(o, "%s%06o %s %d\t%s\n", label, ntohl(ce->ce_mode), sha1_to_hex(ce->sha1), From 329bdf025be694c6944c63550af1cd30b84612ab Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 19:16:53 +0100 Subject: [PATCH 102/253] Fix ntohl() related warnings about printf formatting On Windows, ntohl() returns unsinged long. On Unix it returns uint32_t. This makes choosing a suitable printf format string hard. This commit introduces a mingw specific helper function git_ntohl() that casts to unsigned int before returning. This makes gcc's printf format check happy. It should be safe because we expect ntohl to use 32-bit numbers. Signed-off-by: Steffen Prohaska --- git-compat-util.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index f4e0d08357..42564918de 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -593,6 +593,10 @@ static inline int mingw_fcntl(int fd, int cmd, long arg) { return cmd == F_GETFD || cmd == F_SETFD ? 0 : (errno = EINVAL, -1); } #define fcntl mingw_fcntl +static inline unsigned int git_ntohl(unsigned int x) +{ return (unsigned int)ntohl(x); } +#define ntohl git_ntohl + extern __attribute__((noreturn)) int git_exit(int code); #define exit git_exit From 64529de34e408212bcb5e0b44573ddc2081bf84c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 24 Nov 2007 11:53:06 +0100 Subject: [PATCH 103/253] Enable some disabled tests, remove workarounds msysgit learnt to handle more tests. This commit enables some tests that were disabled before and reverts some workarounds that are no longer needed. Signed-off-by: Steffen Prohaska --- t/t3200-branch.sh | 1 - t/t3404-rebase-interactive.sh | 8 ++++---- t/t3900-i18n-commit.sh | 4 ---- t/t4121-apply-diffs.sh | 2 -- t/t5100-mailinfo.sh | 4 ---- t/t5302-pack-index.sh | 2 -- t/t6023-merge-file.sh | 3 --- t/t6200-fmt-merge-msg.sh | 1 - t/t7003-filter-branch.sh | 3 --- t/t7004-tag.sh | 4 ++-- t/t9300-fast-import.sh | 4 ---- 11 files changed, 6 insertions(+), 30 deletions(-) diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 029f19d6fa..a891fa47d3 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -17,7 +17,6 @@ test_expect_success \ git-commit -m "Initial commit." && HEAD=$(git rev-parse --verify HEAD)' -true || test_expect_failure \ 'git branch --help should not have created a bogus branch' \ 'git branch --help /dev/null 2>/dev/null || : diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index c031d31698..f1039d1a21 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -164,7 +164,7 @@ test_expect_success 'retain authorship' ' test_tick && GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" && git tag twerp && - git rebase -i --onto master HEAD~1 && + git rebase -i --onto master HEAD^ && git show HEAD | grep "^Author: Twerp Snog" ' @@ -208,7 +208,7 @@ test_expect_success 'preserve merges with -p' ' test_expect_success '--continue tries to commit' ' test_tick && - ! git rebase -i --onto new-branch1 HEAD~1 && + ! git rebase -i --onto new-branch1 HEAD^ && echo resolved > file1 && git add file1 && FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue && @@ -217,9 +217,9 @@ test_expect_success '--continue tries to commit' ' ' test_expect_success 'verbose flag is heeded, even after --continue' ' - git reset --hard $(git rev-parse HEAD@{1}) && + git reset --hard HEAD@{1} && test_tick && - ! git rebase -v -i --onto new-branch1 HEAD~1 && + ! git rebase -v -i --onto new-branch1 HEAD^ && echo resolved > file1 && git add file1 && git rebase --continue > output && diff --git a/t/t3900-i18n-commit.sh b/t/t3900-i18n-commit.sh index 12812f924a..94b1c24b0a 100755 --- a/t/t3900-i18n-commit.sh +++ b/t/t3900-i18n-commit.sh @@ -7,10 +7,6 @@ test_description='commit and log output encodings' . ./test-lib.sh -say "iconv not supported, skipping tests." -test_done -exit 0 - compare_with () { git show -s $1 | sed -e '1,/^$/d' -e 's/^ //' >current && git diff current "$2" diff --git a/t/t4121-apply-diffs.sh b/t/t4121-apply-diffs.sh index d93b61d8c4..aff551a1d7 100755 --- a/t/t4121-apply-diffs.sh +++ b/t/t4121-apply-diffs.sh @@ -3,8 +3,6 @@ test_description='git apply for contextually independent diffs' . ./test-lib.sh -test_done - echo '1 2 3 diff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh index 0dcab1fbfe..9b1a74542a 100755 --- a/t/t5100-mailinfo.sh +++ b/t/t5100-mailinfo.sh @@ -7,10 +7,6 @@ test_description='git mailinfo and git mailsplit test' . ./test-lib.sh -say "git-mailinfo does not work yet; skipping tests." -test_done -exit 0 - test_expect_success 'split sample box' \ 'git mailsplit -o. ../t5100/sample.mbox >last && last=`cat last` && diff --git a/t/t5302-pack-index.sh b/t/t5302-pack-index.sh index 8f62429306..2a2878b572 100755 --- a/t/t5302-pack-index.sh +++ b/t/t5302-pack-index.sh @@ -59,7 +59,6 @@ test_expect_success \ 'cmp "test-1-${pack1}.idx" "1.idx" && cmp "test-2-${pack2}.idx" "2.idx"' -false && { test_expect_success \ 'index v2: force some 64-bit offsets with pack-objects' \ 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 merge.err && grep "Cannot merge binary files" merge.err diff --git a/t/t6200-fmt-merge-msg.sh b/t/t6200-fmt-merge-msg.sh index 878f51a8d9..526d7d1c44 100755 --- a/t/t6200-fmt-merge-msg.sh +++ b/t/t6200-fmt-merge-msg.sh @@ -9,7 +9,6 @@ test_description='fmt-merge-msg test' datestamp=1151939923 setdate () { - sleep 1 GIT_COMMITTER_DATE="$datestamp +0200" GIT_AUTHOR_DATE="$datestamp +0200" datestamp=`expr "$datestamp" + 1` diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh index ed467c7009..2089351f7d 100755 --- a/t/t7003-filter-branch.sh +++ b/t/t7003-filter-branch.sh @@ -3,9 +3,6 @@ test_description='git-filter-branch' . ./test-lib.sh -say "filter-branch has not been taken care of - skipping tests" -test_done - make_commit () { lower=$(echo $1 | tr A-Z a-z) echo $lower > $lower diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 42d4e42dec..b9ac9e3eb3 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -116,9 +116,9 @@ mytag EOF test_expect_success \ 'trying to delete tags without params should succeed and do nothing' ' - git tag -l | cat > actual && git diff expect actual && + git tag -l > actual && git diff expect actual && git-tag -d && - git tag -l | cat > actual && git diff expect actual + git tag -l > actual && git diff expect actual ' test_expect_success \ diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh index d92bf36f9c..0595041af5 100755 --- a/t/t9300-fast-import.sh +++ b/t/t9300-fast-import.sh @@ -7,10 +7,6 @@ test_description='test git-fast-import utility' . ./test-lib.sh . ../diff-lib.sh ;# test-lib chdir's into trash -say "git-fast-import has not been taken care of, skipping test." -test_done -exit 0 - file2_data='file2 second line of EOF' From 68c35c48f0db379e8138ba21b294ad04c3ca8b3b Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 24 Nov 2007 20:04:30 +0100 Subject: [PATCH 104/253] Disable test of output of failing git-upload-pack More work is needed in mingw before the output of git-upload-pack is identical to the output on Unix. This commit disables verification of the output. Signed-off-by: Steffen Prohaska --- t/t5530-upload-pack-error.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t5530-upload-pack-error.sh b/t/t5530-upload-pack-error.sh index cc8949e3ef..014064a3bd 100755 --- a/t/t5530-upload-pack-error.sh +++ b/t/t5530-upload-pack-error.sh @@ -55,6 +55,7 @@ test_expect_success 'upload-pack fails due to error in rev-list' ' ! echo "0032want $(git rev-parse HEAD) 00000009done 0000" | git-upload-pack . > /dev/null 2> output.err && + true || grep "waitpid (async) failed" output.err ' From 424e2045aa0b07071780fa0a97c75962b779256e Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 25 Nov 2007 17:48:17 +0100 Subject: [PATCH 105/253] help (mingw): Avoid current working directory when displaying html help We should specify a working directory to ShellExecute for the following reason. ShellExecute uses the current working directory if not told otherwise. html.c did not override this default. Therefore the current working directory was in use as long as the browser displaying the HTML help was open. As long as a directory is in use, it can't be deleted. This commit changes help.c to avoid this problem. The HTML browser is launched in the root directory "\\". Signed-off-by: Steffen Prohaska --- help.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.c b/help.c index f89164a760..a9ec6a77e7 100644 --- a/help.c +++ b/help.c @@ -264,7 +264,7 @@ static void show_man_page(const char *git_cmd) } } printf("Launching default browser to display HTML help ...\n"); - ShellExecute(NULL, "open", htmlpath, NULL, NULL, 0); + ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0); } #else const char *page; From 79fba817db0dad5f4c4bd3982b20d5d7f5accd9d Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 17 Nov 2007 19:10:18 +0100 Subject: [PATCH 106/253] sha1_file.c: Fix size_t related printf format warnings The old way of fixing warnings did not succeed on MinGW. MinGW does not support C99 printf format strings for size_t [1]. But gcc on MinGW issues warnings if C99 printf format is not used. Hence, the old stragegy to avoid warnings fails. [1] http://www.mingw.org/MinGWiki/index.php/C99 This commits passes arguments of type size_t through a tiny helper functions that casts to the type expected by the format string. Signed-off-by: Steffen Prohaska --- sha1_file.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sha1_file.c b/sha1_file.c index bd0e7cd21c..1984e35823 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -25,8 +25,10 @@ #ifdef NO_C99_FORMAT #define SZ_FMT "lu" +static unsigned long sz_fmt(size_t s) { return (unsigned long)s; } #else #define SZ_FMT "zu" +static size_t sz_fmt(size_t s) { return s; } #endif const unsigned char null_sha1[20]; @@ -431,9 +433,9 @@ void pack_report(void) "pack_report: getpagesize() = %10" SZ_FMT "\n" "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n" "pack_report: core.packedGitLimit = %10" SZ_FMT "\n", - (unsigned long) getpagesize(), - (unsigned long) packed_git_window_size, - (unsigned long) packed_git_limit); + sz_fmt(getpagesize()), + sz_fmt(packed_git_window_size), + sz_fmt(packed_git_limit)); fprintf(stderr, "pack_report: pack_used_ctr = %10u\n" "pack_report: pack_mmap_calls = %10u\n" @@ -443,8 +445,7 @@ void pack_report(void) pack_used_ctr, pack_mmap_calls, pack_open_windows, peak_pack_open_windows, - (unsigned long) pack_mapped, - (unsigned long) peak_pack_mapped); + sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped)); } static int check_packed_git_idx(const char *path, struct packed_git *p) From 98603195e96615b491384724bfff6564d31ad067 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 1 Dec 2007 17:49:10 +0100 Subject: [PATCH 107/253] skip t5512 because remote does not yet work Signed-off-by: Steffen Prohaska --- t/t5512-ls-remote.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh index 6ec5f7c48b..b3a6d6f341 100755 --- a/t/t5512-ls-remote.sh +++ b/t/t5512-ls-remote.sh @@ -4,6 +4,9 @@ test_description='git ls-remote' . ./test-lib.sh +say "git remote does not work in t/ - skipping" +test_done + test_expect_success setup ' >file && From a1e84247e94e39fd8ff5dace0cfb9fa35c562a61 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 8 Dec 2007 15:33:04 +0100 Subject: [PATCH 108/253] t7501-commit: Skip testing '--interactive' msysgit does not yet support Git.pm, which is required by git commit --interactive. This commit modifies t7501 to skip testing git commit --interactive. Signed-off-by: Steffen Prohaska --- t/t7501-commit.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh index 19c4b2c556..dd4bf28526 100755 --- a/t/t7501-commit.sh +++ b/t/t7501-commit.sh @@ -119,6 +119,7 @@ test_expect_success \ "echo 'gak' >file && \ git-commit -m 'author' --author 'Rubber Duck ' -a" +echo "SKIP because msysgit does not support Git.pm" || test_expect_success \ "interactive add" \ "echo 7 | git-commit --interactive | grep 'What now'" From 8f4e9dca13aba080505aa95897de2833b1f421d6 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 16 Dec 2007 15:40:11 +0100 Subject: [PATCH 109/253] Enhance the dup-dance in wt-status to work around t7502-commit.sh hang Seems like Windows has some problems if you do too much dup()ing around. Work around these problems by dup()ing yet one more time. Signed-off-by: Johannes Schindelin --- wt-status.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wt-status.c b/wt-status.c index 51c1879691..0b8a73fd44 100644 --- a/wt-status.c +++ b/wt-status.c @@ -321,15 +321,17 @@ static void wt_status_print_untracked(struct wt_status *s) static void wt_status_print_verbose(struct wt_status *s) { struct rev_info rev; - int saved_stdout; + int saved_stdout, redirected_stdout; fflush(s->fp); /* Sigh, the entire diff machinery is hardcoded to output to * stdout. Do the dup-dance...*/ saved_stdout = dup(STDOUT_FILENO); - if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0) + redirected_stdout = dup(fileno(s->fp)); + if (saved_stdout < 0 || dup2(redirected_stdout, STDOUT_FILENO) < 0) die("couldn't redirect stdout\n"); + close(redirected_stdout); init_revisions(&rev, NULL); setup_revisions(0, NULL, &rev, s->reference); From 28456eeb0e887f87d6029fd49d7b4ad1e04b4e99 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 16 Dec 2007 23:01:37 +0100 Subject: [PATCH 110/253] Revert "Enhance the dup-dance in wt-status to work around t7502-commit.sh hang" This reverts commit 8f4e9dca13aba080505aa95897de2833b1f421d6. We'll instead take the solution from mingw/master with the next merge. --- wt-status.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/wt-status.c b/wt-status.c index 0b8a73fd44..51c1879691 100644 --- a/wt-status.c +++ b/wt-status.c @@ -321,17 +321,15 @@ static void wt_status_print_untracked(struct wt_status *s) static void wt_status_print_verbose(struct wt_status *s) { struct rev_info rev; - int saved_stdout, redirected_stdout; + int saved_stdout; fflush(s->fp); /* Sigh, the entire diff machinery is hardcoded to output to * stdout. Do the dup-dance...*/ saved_stdout = dup(STDOUT_FILENO); - redirected_stdout = dup(fileno(s->fp)); - if (saved_stdout < 0 || dup2(redirected_stdout, STDOUT_FILENO) < 0) + if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0) die("couldn't redirect stdout\n"); - close(redirected_stdout); init_revisions(&rev, NULL); setup_revisions(0, NULL, &rev, s->reference); From 301de2ab75f77c4be6f3d83ff2b4201b6b3bd25c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 16 Dec 2007 22:46:49 +0100 Subject: [PATCH 111/253] help (mingw): Cleaned up whitespace indentation The separate block for the ShellExecute code is no longer needed and therefore cleand up. Also included is minor rewording of the error message if the HTML page is not found. Signed-off-by: Steffen Prohaska --- help.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/help.c b/help.c index dcb6a3f6c5..146d131820 100644 --- a/help.c +++ b/help.c @@ -336,28 +336,26 @@ static void show_info_page(const char *git_cmd) static void show_html_page(const char *git_cmd) { #ifdef __MINGW32__ - { - const char* exec_path = git_exec_path(); - char *htmlpath = make_native_separator( - mkpath("%s/../doc/git/html/%s.html" - , exec_path - , git_cmd) - ); + const char* exec_path = git_exec_path(); + char *htmlpath = make_native_separator( + mkpath("%s/../doc/git/html/%s.html" + , exec_path + , git_cmd) + ); + if (!file_exists(htmlpath)) { + htmlpath = make_native_separator( + mkpath("%s/../doc/git/html/git-%s.html" + , exec_path + , git_cmd) + ); if (!file_exists(htmlpath)) { - htmlpath = make_native_separator( - mkpath("%s/../doc/git/html/git-%s.html" - , exec_path - , git_cmd) - ); - if (!file_exists(htmlpath)) { - fprintf(stderr, "Can't find help for '%s'.\n" - , git_cmd); - exit(1); - } + fprintf(stderr, "Can't find HTML help for '%s'.\n" + , git_cmd); + exit(1); } - printf("Launching default browser to display HTML help ...\n"); - ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0); } + printf("Launching default browser to display HTML help ...\n"); + ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0); #else const char *page = cmd_to_page(git_cmd); execl_git_cmd("help--browse", page, NULL); From bc8e453867b6ae950ae4333647f7f5e368ba7b15 Mon Sep 17 00:00:00 2001 From: Brian Downing Date: Fri, 16 Nov 2007 22:28:13 -0600 Subject: [PATCH 112/253] mingw-compat: Add simplified merge sort implementation from glibc qsort in Windows 2000 (and possibly other older Windows' C libraries) is a Quicksort with the usual O(n^2) worst case. Unfortunately, sorting Git trees seems to get very close to that worst case quite often: $ /git/gitbad runstatus # On branch master qsort, nmemb = 30842 done, 237838087 comparisons. This patch adds a simplified version of the merge sort that is glibc's qsort(3). As a merge sort, this needs a temporary array equal in size to the array that is to be sorted. The complexity that was removed is: * Doing direct stores for word-size and -aligned data. * Falling back to quicksort if the allocation required to perform the merge sort would likely push the machine into swap. Even with these simplifications, this seems to outperform the Windows qsort(3) implementation, even in Windows XP (where it is "fixed" and doesn't trigger O(n^2) complexity on trees). [jes: moved into compat/qsort.c, as per Johannes Sixt's suggestion] Signed-off-by: Brian Downing Signed-off-by: Steffen Prohaska Signed-off-by: Johannes Schindelin --- Makefile | 8 +++++++ compat/qsort.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ git-compat-util.h | 6 +++++ 3 files changed, 74 insertions(+) create mode 100644 compat/qsort.c diff --git a/Makefile b/Makefile index a40e71c30d..448a22e123 100644 --- a/Makefile +++ b/Makefile @@ -136,6 +136,9 @@ all:: # Define THREADED_DELTA_SEARCH if you have pthreads and wish to exploit # parallel delta searching when packing objects. # +# Define NEEDS_QUICK_QSORT if your qsort() implementation has O(n^2) +# worst case complexity. +# GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE @"$(SHELL_PATH)" ./GIT-VERSION-GEN @@ -523,6 +526,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_SVN_TESTS = YesPlease NO_PERL_MAKEMAKER = YesPlease NO_R_TO_GCC_LINKER = YesPlease + NEEDS_QUICK_QSORT = YesPlease NO_EXTRA_PROGRAMS = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" @@ -741,6 +745,10 @@ ifdef NO_MEMMEM COMPAT_CFLAGS += -DNO_MEMMEM COMPAT_OBJS += compat/memmem.o endif +ifdef NEEDS_QUICK_QSORT + COMPAT_CFLAGS += -DNEEDS_QUICK_QSORT + COMPAT_OBJS += compat/qsort.o +endif ifdef THREADED_DELTA_SEARCH BASIC_CFLAGS += -DTHREADED_DELTA_SEARCH diff --git a/compat/qsort.c b/compat/qsort.c new file mode 100644 index 0000000000..734866e5bc --- /dev/null +++ b/compat/qsort.c @@ -0,0 +1,60 @@ +#include "../git-compat-util.h" + +/* This merge sort implementation is simplified from glibc's. */ +static void msort_with_tmp(void *b, size_t n, size_t s, + int (*cmp)(const void *, const void *), + char *t) +{ + char *tmp; + char *b1, *b2; + size_t n1, n2; + + if (n <= 1) + return; + + n1 = n / 2; + n2 = n - n1; + b1 = b; + b2 = (char *)b + (n1 * s); + + msort_with_tmp(b1, n1, s, cmp, t); + msort_with_tmp(b2, n2, s, cmp, t); + + tmp = t; + + while (n1 > 0 && n2 > 0) { + if (cmp(b1, b2) <= 0) { + memcpy(tmp, b1, s); + tmp += s; + b1 += s; + --n1; + } else { + memcpy(tmp, b2, s); + tmp += s; + b2 += s; + --n2; + } + } + if (n1 > 0) + memcpy(tmp, b1, n1 * s); + memcpy(b, t, (n - n2) * s); +} + +void git_qsort(void *b, size_t n, size_t s, + int (*cmp)(const void *, const void *)) +{ + const size_t size = n * s; + + if (size < 1024) { + char buf[size]; /* gcc-ism */ + + /* The temporary array is small, so put it on + the stack. */ + msort_with_tmp(b, n, s, cmp, buf); + } else { + /* It's somewhat large, so malloc it. */ + char *tmp = malloc(size); + msort_with_tmp(b, n, s, cmp, tmp); + free(tmp); + } +} diff --git a/git-compat-util.h b/git-compat-util.h index 5a81bc9e76..0f92d1d570 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -645,4 +645,10 @@ extern __attribute__((noreturn)) int git_exit(int code); #endif /* __MINGW32__ */ +#ifdef NEEDS_QUICK_QSORT +void git_qsort(void *base, size_t nmemb, size_t size, + int(*compar)(const void *, const void *)); +#define qsort git_qsort +#endif + #endif From aeb782846ebff9c3c8abf835ab7406e2a5a685ae Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 29 Jan 2008 06:45:36 +0100 Subject: [PATCH 113/253] t5520: Skip '--rebase rebased upstream' on msys (remote doesn't work) We know that git remote does not work in t/. Therefore, we skip this test. Signed-off-by: Steffen Prohaska --- t/t5520-pull.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh index 9484129ca5..ca83768e05 100755 --- a/t/t5520-pull.sh +++ b/t/t5520-pull.sh @@ -76,6 +76,7 @@ test_expect_success 'branch.to-rebase.rebase' ' test new = $(git show HEAD:file2) ' +say "Remote does not work in t/ - skipping." || test_expect_success '--rebase with rebased upstream' ' git remote add -f me . && From a0e491d36a4bf1c19b68be90d82f79a3da95b2f0 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 24 Feb 2008 16:01:14 +0100 Subject: [PATCH 114/253] Makefile(msysgit): Do no longer set symlinks=false during installation We track /etc/gitconfig in msysgit and therefore do no longer need to set symlinks=false during installation. This commit removes the unnecessary command from the install target and thus reduces the differences to official git's Makefile. Signed-off-by: Steffen Prohaska --- Makefile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Makefile b/Makefile index 1272b474b4..1b6081782d 100644 --- a/Makefile +++ b/Makefile @@ -1111,8 +1111,6 @@ install: all $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(sysconfdir_SQ)' $(INSTALL) $(ALL_PROGRAMS) '$(DESTDIR_SQ)$(gitexecdir_SQ)' $(INSTALL) git$X '$(DESTDIR_SQ)$(bindir_SQ)' - GIT_CONFIG='$(DESTDIR_SQ)$(sysconfdir_SQ)/gitconfig' \ - $(DESTDIR_SQ)$(bindir_SQ)/git$X config core.symlinks false $(MAKE) -C templates DESTDIR='$(DESTDIR_SQ)' install $(MAKE) -C perl prefix='$(prefix_SQ)' DESTDIR='$(DESTDIR_SQ)' install ifndef NO_TCLTK From dbe5a3424bb8ff239b896ab0e888ef402dffa915 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 24 Feb 2008 17:01:54 +0100 Subject: [PATCH 115/253] Skip t9500 because we do not support gitweb in msysgit. Signed-off-by: Steffen Prohaska --- t/t9500-gitweb-standalone-no-errors.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/t9500-gitweb-standalone-no-errors.sh b/t/t9500-gitweb-standalone-no-errors.sh index 796cd7dba0..72d8e74927 100755 --- a/t/t9500-gitweb-standalone-no-errors.sh +++ b/t/t9500-gitweb-standalone-no-errors.sh @@ -68,6 +68,9 @@ safe_chmod () { . ./test-lib.sh +say 'gitweb not supported, skipping tests.' +test_done + perl -MEncode -e 'decode_utf8("", Encode::FB_CROAK)' >/dev/null 2>&1 || { test_expect_success 'skipping gitweb tests, perl version is too old' : test_done From 3400af9e38925a8ae3a9c619c542e55d258b6f3c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 1 Jan 2008 12:54:28 +0100 Subject: [PATCH 116/253] Revert "Added is_dev_null check because Windows /dev/null is nul." This reverts commit 3d9012418c93b5e5f0ada041e1fc071cac48cbfa, which is no longer needed as discussed in http://article.gmane.org/gmane.comp.version-control.msysgit/1134 From Mike Pape's mail: The problem was test t4116-apply-reverse was failing because /dev/null was passed to git diff. This gets converted to nul on Windows, so I added the patch to test for this. However, it looks like Hannes just changed the test in 1f25f36fec4dd895cc6fb73e9cfdecb41e34d155 to not use /dev/null. Not sure if there are other patches that deal with the /dev/null -> nul issue (or if it's even an issue). Conflicts: cache.h Signed-off-by: Steffen Prohaska --- builtin-apply.c | 8 ++++---- cache.h | 9 --------- diff-lib.c | 6 +----- 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index ac12628f31..a3f075df4b 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -285,7 +285,7 @@ static unsigned long linelen(const char *buffer, unsigned long size) return len; } -static int is_dev_null_line(const char *str) +static int is_dev_null(const char *str) { return !memcmp("/dev/null", str, 9) && isspace(str[9]); } @@ -392,7 +392,7 @@ static int guess_p_value(const char *nameline) char *name, *cp; int val = -1; - if (is_dev_null_line(nameline)) + if (is_dev_null(nameline)) return -1; name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB); if (!name) @@ -440,12 +440,12 @@ static void parse_traditional_patch(const char *first, const char *second, struc p_value_known = 1; } } - if (is_dev_null_line(first)) { + if (is_dev_null(first)) { patch->is_new = 1; patch->is_delete = 0; name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB); patch->new_name = name; - } else if (is_dev_null_line(second)) { + } else if (is_dev_null(second)) { patch->is_new = 0; patch->is_delete = 1; name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); diff --git a/cache.h b/cache.h index 1b48cfbece..b169900431 100644 --- a/cache.h +++ b/cache.h @@ -487,15 +487,6 @@ static inline int is_absolute_path(const char *path) { return path[0] == '/' || has_dos_drive_prefix(path); } - -static inline int is_dev_null(const char *str) -{ -#ifdef __MINGW32__ - if (!strcmp(str, "nul")) - return 1; -#endif - return !strcmp(str, "/dev/null"); -} const char *make_absolute_path(const char *path); /* Convert slashes in place. On Windows to backslashes. */ char *make_native_separator(char *path); diff --git a/diff-lib.c b/diff-lib.c index 3b0a6c4351..52dbac34a4 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -35,7 +35,7 @@ static int get_mode(const char *path, int *mode) { struct stat st; - if (!path || is_dev_null(path)) + if (!path || !strcmp(path, "/dev/null")) *mode = 0; else if (!strcmp(path, "-")) *mode = create_ce_mode(0666); @@ -234,10 +234,6 @@ static int is_outside_repo(const char *path, int nongit, const char *prefix) int i; if (nongit || !strcmp(path, "-") || is_absolute_path(path)) return 1; -#ifdef __MINGW32__ - if (!strcmp(path, "nul")) - return 1; -#endif if (prefixcmp(path, "../")) return 0; if (!prefix) From 997c13c3898451e1299f0ef2da8b457b1902f2c6 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 1 Jan 2008 14:39:06 +0100 Subject: [PATCH 117/253] Revert "Provide git_exit() for MinGW" This reverts commit 194c1dbb5a04e29937b87d477a16927fa2c3be62. Instead of mapping exit values < 0 to 1, the original exit code should be fixed. For example, commit 2488df84a28b5eaae5495a59e390b51874d60a03 fixes this for run_command. All tests still pass after this revert. The git_exit() workaround seems no longer necessary. Signed-off-by: Steffen Prohaska --- compat/mingw.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index ff2fc832d3..7c8fd0e158 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -979,12 +979,3 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler) timer_fn = handler; return old; } - -#undef exit -int git_exit(int code) -{ - if (code < 0) - exit(1); - exit(code); -} - From 9ef5c15d66da91360eeca7e89d512a6b506f6d57 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 1 Jan 2008 20:31:30 +0100 Subject: [PATCH 118/253] Revert replacing getenv() with get_git_dir() This partially reverts commit 855f254b2b5b083a63fc8d7709a42e2cbdc5a136. All tests still pass after this revert. The original commit seems unnecessary. Signed-off-by: Steffen Prohaska --- builtin-init-db.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builtin-init-db.c b/builtin-init-db.c index 6c46af9228..79eaf8d6ed 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -367,7 +367,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) /* * Set up the default .git directory contents */ - git_dir = get_git_dir(); + git_dir = getenv(GIT_DIR_ENVIRONMENT); + if (!git_dir) + git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; safe_create_dir(git_dir, 0); /* Check to see if the repository version is right. From 24fcae02c33ca0fb05b088d55d12bf97fa2c23f3 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 1 Jan 2008 20:34:52 +0100 Subject: [PATCH 119/253] Revert replacing setenv() with set_git_dir() This partially reverts commit 855f254b2b5b083a63fc8d7709a42e2cbdc5a136. All tests still pass after this revert. The original commit seems unnecessary. Signed-off-by: Steffen Prohaska --- git.c | 6 +++--- path.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/git.c b/git.c index 5ae18e926f..327215cc91 100644 --- a/git.c +++ b/git.c @@ -45,14 +45,14 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) fprintf(stderr, "No directory given for --git-dir.\n" ); usage(git_usage_string); } - set_git_dir( (*argv)[1] ); + setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; handled++; } else if (!prefixcmp(cmd, "--git-dir=")) { - set_git_dir(cmd + 10); + setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1); if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--work-tree")) { @@ -72,7 +72,7 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) } else if (!strcmp(cmd, "--bare")) { static char git_dir[PATH_MAX+1]; is_bare_repository_cfg = 1; - set_git_dir(getcwd(git_dir, sizeof(git_dir))); + setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0); if (envchanged) *envchanged = 1; } else { diff --git a/path.c b/path.c index 4e19c4cdaa..fe0361aab4 100644 --- a/path.c +++ b/path.c @@ -255,7 +255,7 @@ char *enter_repo(char *path, int strict) if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && validate_headref("HEAD") == 0) { - set_git_dir("."); + setenv(GIT_DIR_ENVIRONMENT, ".", 1); check_repository_format(); return path; } From 96ddc42f70dd1b080c51af5a50929f2b5811e415 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 30 Dec 2007 16:04:05 +0100 Subject: [PATCH 120/253] Revert "Hardlinks are not supported in MSys so we disable them." This reverts commit d1f83218dce62affd1d7a82a9e654cca08dade9c. Hardlinks are supported by MSYS (on NTFS). Conflicts: Makefile git-clone.sh t/Makefile t/test-lib.sh Signed-off-by: Steffen Prohaska --- Makefile | 10 +--------- git-clone.sh | 4 +--- t/Makefile | 4 ---- t/t5701-clone-local.sh | 11 ----------- t/test-lib.sh | 2 -- 5 files changed, 2 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 095e820415..fa5956736c 100644 --- a/Makefile +++ b/Makefile @@ -54,9 +54,6 @@ all:: # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link. # Enable it on Windows. By default, symrefs are still used. # -# Define NO_HARDLINKS if you want to disable hard linking in git clone. -# Enable it on Windows. -# # Define NO_SVN_TESTS if you want to skip time-consuming SVN interoperability # tests. These tests take up a significant amount of the total test time # but are not needed unless you plan to talk to SVN repos. @@ -544,7 +541,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_PREAD = YesPlease NO_OPENSSL = YesPlease NO_SYMLINK_HEAD = YesPlease - NO_HARDLINKS = YesPlease NO_IPV6 = YesPlease NO_SETENV = YesPlease NO_UNSETENV = YesPlease @@ -841,10 +837,6 @@ ifdef ASCIIDOC8 export ASCIIDOC8 endif -ifdef NO_HARDLINKS - export NO_HARDLINKS -endif - # Shell quote (do not use $(call) to accommodate ancient setups); SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER)) @@ -923,7 +915,7 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh -e 's|@@PERL@@|$(PERL_PATH_SQ)|g' \ -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \ -e 's/@@NO_CURL@@/$(NO_CURL)/g' \ - -e 's/@@NO_HARDLINKS@@/$(NO_HARDLINKS)/g' \ + -e 's|@@HTMLDIR@@|$(htmldir_SQ)|g' \ $@.sh >$@+ && \ chmod +x $@+ && \ mv $@+ $@ diff --git a/git-clone.sh b/git-clone.sh index edfa1529bc..b783de4300 100755 --- a/git-clone.sh +++ b/git-clone.sh @@ -176,7 +176,7 @@ origin_override= use_separate_remote=t depth= no_progress= -test "@@NO_HARDLINKS@@" && use_local_hardlink=no +local_explicitly_asked_for= test -t 1 || no_progress=--no-progress while test $# != 0 @@ -188,8 +188,6 @@ do bare=yes ;; -l|--local) local_explicitly_asked_for=yes - (test "@@NO_HARDLINKS@@" && - echo >&2 "Warning: -l asked but hardlinks are not supported") || use_local_hardlink=yes ;; --no-hardlinks) diff --git a/t/Makefile b/t/Makefile index 4f884466dd..72d7884232 100644 --- a/t/Makefile +++ b/t/Makefile @@ -14,10 +14,6 @@ SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH)) T = $(wildcard t[0-9][0-9][0-9][0-9]-*.sh) TSVN = $(wildcard t91[0-9][0-9]-*.sh) -ifdef NO_HARDLINKS - GIT_TEST_OPTS += --no-hardlinks -endif - all: $(T) clean $(T): diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh index 0bf36a67a0..8dfaaa456e 100755 --- a/t/t5701-clone-local.sh +++ b/t/t5701-clone-local.sh @@ -3,13 +3,6 @@ test_description='test local clone' . ./test-lib.sh -if test "$no_hardlinks" -then - say 'Hard links not supported, skipping tests.' - test_done - exit -fi - D=`pwd` test_expect_success 'preparing origin repository' ' @@ -66,10 +59,6 @@ test_expect_success 'With -no-hardlinks, local will make a copy' ' test 0 = $linked ' -say "hardlinks not supported, skipping tests." -test_done -exit 0 - test_expect_success 'Even without -l, local will make a hardlink' ' cd "$D" && rm -fr w && diff --git a/t/test-lib.sh b/t/test-lib.sh index fc4a45323e..0815190a2f 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -91,8 +91,6 @@ do --no-python) # noop now... shift ;; - --no-hardlinks) - no_hardlinks=t; shift ;; *) break ;; esac From 0c75c2ff5c78ae7fcba2bc1a5928c7aa2d522986 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 11:21:54 +0200 Subject: [PATCH 121/253] t3903: Mark tests as broken on MinGW MinGW's bash eats curly braces in "git stash drop stash@{1}". This causes two tests in t3903 to fail. Those breakages are marked expected. You can work around the breakage by escaping the curly braces as "git stash drop stash@\\{1\\}". Signed-off-by: Steffen Prohaska --- t/t3903-stash.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 2d3ee3b78c..d717a02b54 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -88,7 +88,11 @@ test_expect_success 'drop top stash' ' test 1 = $(git show HEAD:file) ' -test_expect_success 'drop middle stash' ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'drop middle stash' ' git reset --hard && echo 8 > file && git stash && @@ -108,7 +112,7 @@ test_expect_success 'drop middle stash' ' test 1 = $(git show HEAD:file) ' -test_expect_success 'stash pop' ' +$test_expect 'stash pop' ' git reset --hard && git stash pop && test 3 = $(cat file) && From 0b4d5c6b39bb6e3dfeeddc71d1c86c30f25196bd Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 11:52:29 +0200 Subject: [PATCH 122/253] t7201: Mark tests as broken on MinGW MinGW's path conversion mangles ":/". This breakages is marked expected. Unfortunately, the heuristic in msys_p2w() does not allow for a workaround. Signed-off-by: Steffen Prohaska --- t/t7201-co.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 3111baa9e3..5dd79bd018 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -207,7 +207,11 @@ test_expect_success 'checkout to detach HEAD with branchname^' ' fi ' -test_expect_success 'checkout to detach HEAD with :/message' ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'checkout to detach HEAD with :/message' ' git checkout -f master && git clean -f && git checkout ":/Initial" && From e1d3b40b7e7addc8adc065c99d7affa7611e2428 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 12:18:32 +0200 Subject: [PATCH 123/253] test-lib: Set is_mingw on MinGW We need to skip some tests on MinGW because they test features that are not available on Windows. This commit adds a shell variable is_mingw. You can test is_mingw to skip a test only on MinGW. Signed-off-by: Steffen Prohaska --- t/test-lib.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/test-lib.sh b/t/test-lib.sh index 30a181d548..11ef041335 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -438,5 +438,6 @@ case $(uname -s) in find () { /usr/bin/find "$@" } + is_mingw=t ;; esac From e0203e1029f45b506a67a5815065fc78d6c1c078 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 12:20:46 +0200 Subject: [PATCH 124/253] t5503: Skip four tests on MinGW The tests fail on MinGW because GIT_DEBUG_SEND_PACK is not supported. See http://article.gmane.org/gmane.comp.version-control.msysgit/1992 Signed-off-by: Steffen Prohaska --- t/t5503-tagfollow.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/t5503-tagfollow.sh b/t/t5503-tagfollow.sh index 86e5b9bc26..398b13184c 100755 --- a/t/t5503-tagfollow.sh +++ b/t/t5503-tagfollow.sh @@ -41,6 +41,7 @@ cat - <expect want $A #E EOF +test "$is_mingw" || test_expect_success 'fetch A (new commit : 1 connection)' ' rm -f $U ( @@ -71,6 +72,7 @@ want $C want $T #E EOF +test "$is_mingw" || test_expect_success 'fetch C, T (new branch, tag : 1 connection)' ' rm -f $U ( @@ -107,6 +109,7 @@ want $B want $S #E EOF +test "$is_mingw" || test_expect_success 'fetch B, S (commit and tag : 1 connection)' ' rm -f $U ( @@ -127,6 +130,7 @@ want $B want $S #E EOF +test "$is_mingw" || test_expect_success 'new clone fetch master and tags' ' git branch -D cat rm -f $U From 1a14b8acccc659eb63e1e18309c356910b0eed95 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 12:31:32 +0200 Subject: [PATCH 125/253] t5505: On MinGW, use Windows path in expected output of "git remote show" "git remote show" prints the Windows path. This commit modifies the expected output to also use the Windows path, instead of the Unix path. Signed-off-by: Steffen Prohaska --- t/t5505-remote.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 0a7fea865d..ddfa4740ed 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -97,9 +97,13 @@ test_expect_success 'remove remote' ' ) ' +case $(uname -s) in +*MINGW*) PWD="pwd -W";; +*) PWD=pwd;; +esac cat > test/expect << EOF * remote origin - URL: $(pwd)/one/.git + URL: $($PWD)/one/.git Remote branch merged with 'git pull' while on branch master master New remote branch (next fetch will store in remotes/origin) From b4078242a7b03c967c2829205b5461afb82d19b4 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 12:43:44 +0200 Subject: [PATCH 126/253] t7401: Tell diff to ignore whitespace on MinGW "git submodule" prints CRLF lineendings, which break the comparison with expected outputs. This commit sets the diff command to ignore whitespace changes. Signed-off-by: Steffen Prohaska --- t/t7401-submodule-summary.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/t7401-submodule-summary.sh b/t/t7401-submodule-summary.sh index 86ed2a2a2d..f34d8f0bb2 100755 --- a/t/t7401-submodule-summary.sh +++ b/t/t7401-submodule-summary.sh @@ -8,6 +8,10 @@ test_description='Summary support for submodules This test tries to verify the sanity of summary subcommand of git-submodule. ' +case $(uname -s) in +*MINGW*) GIT_TEST_CMP="diff -uw";; +esac + . ./test-lib.sh add_file () { From 90fba70984a429aa21972932c0775638d15a44d6 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Apr 2008 13:48:24 +0200 Subject: [PATCH 127/253] t5511: Disable test with "::" on MinGW We must not use arguments with "::" on MinGW. MSYS' path conversion heuristic mangles such arguments. The heuristic considers them path lists and converts ":" to ";". This commit disables a test that uses "::". Signed-off-by: Steffen Prohaska --- t/t5511-refspec.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t5511-refspec.sh b/t/t5511-refspec.sh index 670a8f1c99..d061f6a4c2 100755 --- a/t/t5511-refspec.sh +++ b/t/t5511-refspec.sh @@ -46,6 +46,7 @@ test_refspec fetch 'refs/heads/*:refs/remotes/frotz/*' test_refspec fetch 'refs/heads/*:refs/remotes/frotz' invalid test_refspec fetch 'refs/heads:refs/remotes/frotz/*' invalid test_refspec fetch 'refs/heads/master:refs/remotes/frotz/xyzzy' +test "$is_mingw" || test_refspec fetch 'refs/heads/master::refs/remotes/frotz/xyzzy' invalid test_refspec fetch 'refs/heads/maste :refs/remotes/frotz/xyzzy' invalid From a1fca27d1ee3fd7af09e702a326a8897ee4c0a13 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 19 Apr 2008 11:21:16 +0200 Subject: [PATCH 128/253] MinGW: Mark some tests as "still broken" instead of skipping them --- t/t3903-stash.sh | 4 ++++ t/t5503-tagfollow.sh | 29 +++++++++++++++++++++-------- t/t5511-refspec.sh | 12 ++++++++++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index d717a02b54..0858aadb87 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -112,6 +112,10 @@ $test_expect 'drop middle stash' ' test 1 = $(git show HEAD:file) ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac $test_expect 'stash pop' ' git reset --hard && git stash pop && diff --git a/t/t5503-tagfollow.sh b/t/t5503-tagfollow.sh index 398b13184c..f6828281bd 100755 --- a/t/t5503-tagfollow.sh +++ b/t/t5503-tagfollow.sh @@ -41,8 +41,12 @@ cat - <expect want $A #E EOF -test "$is_mingw" || -test_expect_success 'fetch A (new commit : 1 connection)' ' + +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'fetch A (new commit : 1 connection)' ' rm -f $U ( cd cloned && @@ -72,8 +76,11 @@ want $C want $T #E EOF -test "$is_mingw" || -test_expect_success 'fetch C, T (new branch, tag : 1 connection)' ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'fetch C, T (new branch, tag : 1 connection)' ' rm -f $U ( cd cloned && @@ -109,8 +116,11 @@ want $B want $S #E EOF -test "$is_mingw" || -test_expect_success 'fetch B, S (commit and tag : 1 connection)' ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'fetch B, S (commit and tag : 1 connection)' ' rm -f $U ( cd cloned && @@ -130,8 +140,11 @@ want $B want $S #E EOF -test "$is_mingw" || -test_expect_success 'new clone fetch master and tags' ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'new clone fetch master and tags' ' git branch -D cat rm -f $U ( diff --git a/t/t5511-refspec.sh b/t/t5511-refspec.sh index d061f6a4c2..e773b5d7bd 100755 --- a/t/t5511-refspec.sh +++ b/t/t5511-refspec.sh @@ -4,6 +4,8 @@ test_description='refspec parsing' . ./test-lib.sh +test_expect=test_expect_success + test_refspec () { kind=$1 refspec=$2 expect=$3 @@ -19,7 +21,7 @@ test_refspec () { title="$kind $refspec (invalid)" test='test_must_fail git ls-remote frotz' fi - test_expect_success "$title" "$test" + $test_expect "$title" "$test" } test_refspec push '' invalid @@ -46,8 +48,14 @@ test_refspec fetch 'refs/heads/*:refs/remotes/frotz/*' test_refspec fetch 'refs/heads/*:refs/remotes/frotz' invalid test_refspec fetch 'refs/heads:refs/remotes/frotz/*' invalid test_refspec fetch 'refs/heads/master:refs/remotes/frotz/xyzzy' -test "$is_mingw" || + +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac test_refspec fetch 'refs/heads/master::refs/remotes/frotz/xyzzy' invalid +test_expect=test_expect_success + test_refspec fetch 'refs/heads/maste :refs/remotes/frotz/xyzzy' invalid test_refspec push 'master~1:refs/remotes/frotz/backup' From 6c22598c24c822c6911b272d196e72cda6e79ebf Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Fri, 2 May 2008 09:40:40 +0200 Subject: [PATCH 129/253] Revert "test-lib: Set is_mingw on MinGW" This reverts commit e1d3b40b7e7addc8adc065c99d7affa7611e2428. --- t/test-lib.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 11ef041335..30a181d548 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -438,6 +438,5 @@ case $(uname -s) in find () { /usr/bin/find "$@" } - is_mingw=t ;; esac From 54b9e8917c44c456c445dece61389f0478505352 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Thu, 5 Jun 2008 01:49:03 -0400 Subject: [PATCH 130/253] connect.c: Fix custom port SSH with PuTTY plink on Windows OpenSSH uses -p to specify custom ports, while PuTTY uses -P. Git should detect if plink is in GIT_SSH and modify its flags as necessary. Also fixed is a possible hang due to faulty stdin forwarding to plink when plink attempts to be interactive. Signed-off-by: Edward Z. Yang Signed-off-by: Steffen Prohaska --- connect.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index 574f42fa47..6bd7ec9117 100644 --- a/connect.c +++ b/connect.c @@ -596,14 +596,24 @@ struct child_process *git_connect(int fd[2], const char *url_orig, die("command line too long"); conn->in = conn->out = -1; - conn->argv = arg = xcalloc(6, sizeof(*arg)); + /* be sure to increase this size if you add more args */ + conn->argv = arg = xcalloc(8, sizeof(*arg)); if (protocol == PROTO_SSH) { const char *ssh = getenv("GIT_SSH"); + int putty = ssh && strstr(ssh, "plink"); if (!ssh) ssh = "ssh"; *arg++ = ssh; + if (putty) { + /* stdin forwarding doesn't work, so give informative error messages + * (-v) and don't hang (-batch). + */ + *arg++ = "-batch"; + *arg++ = "-v"; + } if (port) { - *arg++ = "-p"; + /* P is for PuTTY, p is for OpenSSH */ + *arg++ = putty ? "-P" : "-p"; *arg++ = port; } *arg++ = host; From 3ad18f2446748cb3b2d21454c01f917315fcd806 Mon Sep 17 00:00:00 2001 From: Peter Harris Date: Sun, 2 Mar 2008 16:30:29 -0500 Subject: [PATCH 131/253] Scan for \r in addition to \n when reading shbang lines Signed-off-by: Steffen Prohaska --- compat/mingw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index a5b43bcf6f..f036b3383c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -448,8 +448,8 @@ static const char *parse_interpreter(const char *cmd) if (buf[0] != '#' || buf[1] != '!') return NULL; buf[n] = '\0'; - p = strchr(buf, '\n'); - if (!p) + p = buf + strcspn(buf, "\r\n"); + if (!*p) return NULL; *p = '\0'; From e77b5a4f7c0d3fef4b0f3d0996baaadf2529b440 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 1 Mar 2008 19:48:40 -0500 Subject: [PATCH 132/253] Add ANSI control code emulation for the Windows console This adds only the minimum necessary to keep git pull/merge's diffstat from wrapping. Notably absent is support for the K (erase) operation, and support for POSIX write. Cygwin does not need the WIN_ANSI define, since it has its own (more complete) ANSI emulation. Signed-off-by: Peter Harris --- Makefile | 6 + compat/winansi.c | 309 ++++++++++++++++++++++++++++++++++++++++++++++ git-compat-util.h | 7 ++ 3 files changed, 322 insertions(+) create mode 100644 compat/winansi.c diff --git a/Makefile b/Makefile index 0248759e9a..ea7f0418a1 100644 --- a/Makefile +++ b/Makefile @@ -695,6 +695,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_R_TO_GCC_LINKER = YesPlease INTERNAL_QSORT = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease + WIN_ANSI = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" @@ -940,6 +941,11 @@ ifdef NO_EXTERNAL_GREP BASIC_CFLAGS += -DNO_EXTERNAL_GREP endif +ifdef WIN_ANSI + COMPAT_CFLAGS += -DWIN_ANSI + COMPAT_OBJS += compat/winansi.o +endif + ifeq ($(TCLTK_PATH),) NO_TCLTK=NoThanks endif diff --git a/compat/winansi.c b/compat/winansi.c new file mode 100644 index 0000000000..86c3fd2db3 --- /dev/null +++ b/compat/winansi.c @@ -0,0 +1,309 @@ +#include +#include "../git-compat-util.h" + +/* + Functions to be wrapped: +*/ +#undef printf +#undef fputs + +/* + ANSI codes to implement: m, K +*/ + +static HANDLE console; +static WORD plain_attr; +static WORD attr; +static int negative; + +static void init(void) +{ + CONSOLE_SCREEN_BUFFER_INFO sbi; + + static int initialized = 0; + if (initialized) + return; + + console = GetStdHandle(STD_OUTPUT_HANDLE); + if (console == INVALID_HANDLE_VALUE) + console = NULL; + + if (!console) + return; + + GetConsoleScreenBufferInfo(console, &sbi); + attr = plain_attr = sbi.wAttributes; + negative = 0; + + initialized = 1; +} + + +#define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) +#define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) + +static void set_console_attr(void) +{ + WORD attributes = attr; + if (negative) { + attributes &= ~FOREGROUND_ALL; + attributes &= ~BACKGROUND_ALL; + + /* This could probably use a bitmask instead of a series of ifs */ + if (attr & FOREGROUND_RED) + attributes |= BACKGROUND_RED; + if (attr & FOREGROUND_GREEN) + attributes |= BACKGROUND_GREEN; + if (attr & FOREGROUND_BLUE) + attributes |= BACKGROUND_BLUE; + + if (attr & BACKGROUND_RED) + attributes |= FOREGROUND_RED; + if (attr & BACKGROUND_GREEN) + attributes |= FOREGROUND_GREEN; + if (attr & BACKGROUND_BLUE) + attributes |= FOREGROUND_BLUE; + } + SetConsoleTextAttribute(console, attributes); +} + +static const char *set_attr(const char *str) +{ + const char *func; + size_t len = strspn(str, "0123456789;"); + func = str + len; + + switch (*func) { + case 'm': + do { + long val = strtol(str, (char **)&str, 10); + switch (val) { + case 0: /* reset */ + attr = plain_attr; + negative = 0; + break; + case 1: /* bold */ + attr |= FOREGROUND_INTENSITY; + break; + case 2: /* faint */ + case 22: /* normal */ + attr &= ~FOREGROUND_INTENSITY; + break; + case 3: /* italic */ + /* Unsupported */ + break; + case 4: /* underline */ + case 21: /* double underline */ + /* Wikipedia says this flag does nothing */ + /* Furthermore, mingw doesn't define this flag + attr |= COMMON_LVB_UNDERSCORE; */ + break; + case 24: /* no underline */ + /* attr &= ~COMMON_LVB_UNDERSCORE; */ + break; + case 5: /* slow blink */ + case 6: /* fast blink */ + /* We don't have blink, but we do have background intensity */ + attr |= BACKGROUND_INTENSITY; + break; + case 25: /* no blink */ + attr &= ~BACKGROUND_INTENSITY; + break; + case 7: /* negative */ + negative = 1; + break; + case 27: /* positive */ + negative = 0; + break; + case 8: /* conceal */ + case 28: /* reveal */ + /* Unsupported */ + break; + case 30: /* Black */ + attr &= ~FOREGROUND_ALL; + break; + case 31: /* Red */ + attr &= ~FOREGROUND_ALL; + attr |= FOREGROUND_RED; + break; + case 32: /* Green */ + attr &= ~FOREGROUND_ALL; + attr |= FOREGROUND_GREEN; + break; + case 33: /* Yellow */ + attr &= ~FOREGROUND_ALL; + attr |= FOREGROUND_RED | FOREGROUND_GREEN; + break; + case 34: /* Blue */ + attr &= ~FOREGROUND_ALL; + attr |= FOREGROUND_BLUE; + break; + case 35: /* Magenta */ + attr &= ~FOREGROUND_ALL; + attr |= FOREGROUND_RED | FOREGROUND_BLUE; + break; + case 36: /* Cyan */ + attr &= ~FOREGROUND_ALL; + attr |= FOREGROUND_GREEN | FOREGROUND_BLUE; + break; + case 37: /* White */ + attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; + break; + case 38: /* Unknown */ + break; + case 39: /* reset */ + attr &= ~FOREGROUND_ALL; + attr |= (plain_attr & FOREGROUND_ALL); + break; + case 40: /* Black */ + attr &= ~BACKGROUND_ALL; + break; + case 41: /* Red */ + attr &= ~BACKGROUND_ALL; + attr |= BACKGROUND_RED; + break; + case 42: /* Green */ + attr &= ~BACKGROUND_ALL; + attr |= BACKGROUND_GREEN; + break; + case 43: /* Yellow */ + attr &= ~BACKGROUND_ALL; + attr |= BACKGROUND_RED | BACKGROUND_GREEN; + break; + case 44: /* Blue */ + attr &= ~BACKGROUND_ALL; + attr |= BACKGROUND_BLUE; + break; + case 45: /* Magenta */ + attr &= ~BACKGROUND_ALL; + attr |= BACKGROUND_RED | BACKGROUND_BLUE; + break; + case 46: /* Cyan */ + attr &= ~BACKGROUND_ALL; + attr |= BACKGROUND_GREEN | BACKGROUND_BLUE; + break; + case 47: /* White */ + attr |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; + break; + case 48: /* Unknown */ + break; + case 49: /* reset */ + attr &= ~BACKGROUND_ALL; + attr |= (plain_attr & BACKGROUND_ALL); + break; + default: + /* Unsupported code */ + break; + } + str++; + } while (*(str-1) == ';'); + + set_console_attr(); + break; + case 'K': + /* TODO */ + break; + default: + /* Unsupported code */ + break; + } + + return func + 1; +} + +static int ansi_emulate(const char *str, FILE *stream) +{ + int rv = 0; + const char *pos = str; + + while (*pos) { + pos = strstr(str, "\033["); + if (pos) { + size_t len = pos - str; + + if (len) { + size_t output_len = fwrite(str, 1, len, stream); + rv += output_len; + if (output_len < len) + return rv; + } + + str = pos + 2; + rv += 2; + + fflush(stream); + + pos = set_attr(str); + rv += pos - str; + str = pos; + } else { + rv += strlen(str); + fputs(str, stream); + return rv; + } + } + return rv; +} + +int git_fputs(const char *str, FILE *stream) +{ + int rv; + + init(); + + if (!console) + return fputs(str, stream); + + if (!isatty(fileno(stream))) + return fputs(str, stream); + + rv = ansi_emulate(str, stream); + + if (rv >= 0) + return 0; + else + return EOF; +} + +int git_printf(const char *format, ...) +{ + va_list list; + + char small_buf[256]; + char *buf = small_buf; + int len, rv; + + init(); + + if (!console) + goto abort; + + if (!isatty(fileno(stdout))) + goto abort; + + va_start(list, format); + len = vsnprintf(small_buf, sizeof(small_buf), format, list); + va_end(list); + + if (len > sizeof(small_buf) - 1) { + buf = malloc(len + 1); + if (!buf) + goto abort; + + va_start(list, format); + len = vsnprintf(buf, len + 1, format, list); + va_end(list); + } + + rv = ansi_emulate(buf, stdout); + + if (buf != small_buf) + free(buf); + return rv; + +abort: + va_start(list, format); + rv = vprintf(format, list); + va_end(list); + return rv; +} diff --git a/git-compat-util.h b/git-compat-util.h index 625e5e9c80..b810d710d7 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -495,4 +495,11 @@ void git_qsort(void *base, size_t nmemb, size_t size, # define FORCE_DIR_SET_GID 0 #endif +#ifdef WIN_ANSI +extern int git_fputs(const char *str, FILE *stream); +extern int git_printf(const char *format, ...) __attribute__((format (printf, 1, 2))); +#define fputs git_fputs +#define printf(...) git_printf(__VA_ARGS__) +#endif + #endif From a58901c9bd2de40f6409a77a3f638106bdffaacd Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 15 Jun 2008 15:53:57 -0400 Subject: [PATCH 133/253] Fix custom ports with plink PuTTY requires -P while OpenSSH requires -p; if plink is detected as GIT_SSH, use the alternate flag. Signed-off-by: Edward Z. Yang Signed-off-by: Steffen Prohaska --- connect.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/connect.c b/connect.c index 574f42fa47..11fa151eb3 100644 --- a/connect.c +++ b/connect.c @@ -596,14 +596,17 @@ struct child_process *git_connect(int fd[2], const char *url_orig, die("command line too long"); conn->in = conn->out = -1; + /* be sure to increase this size if you add more args */ conn->argv = arg = xcalloc(6, sizeof(*arg)); if (protocol == PROTO_SSH) { const char *ssh = getenv("GIT_SSH"); + int putty = ssh && strstr(ssh, "plink"); if (!ssh) ssh = "ssh"; *arg++ = ssh; if (port) { - *arg++ = "-p"; + /* P is for PuTTY, p is for OpenSSH */ + *arg++ = putty ? "-P" : "-p"; *arg++ = port; } *arg++ = host; From 33227548ba69857102b928e373a0e023ee59c7da Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 15 Jun 2008 15:53:57 -0400 Subject: [PATCH 134/253] Fix custom ports with plink (without debugging output) PuTTY requires -P while OpenSSH requires -p; if plink is detected as GIT_SSH, use the alternate flag. [spr: This commit switched off plink's debugging output, which was enabled by the previous commit. ] Signed-off-by: Edward Z. Yang Signed-off-by: Steffen Prohaska --- connect.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/connect.c b/connect.c index 6bd7ec9117..11fa151eb3 100644 --- a/connect.c +++ b/connect.c @@ -597,20 +597,13 @@ struct child_process *git_connect(int fd[2], const char *url_orig, conn->in = conn->out = -1; /* be sure to increase this size if you add more args */ - conn->argv = arg = xcalloc(8, sizeof(*arg)); + conn->argv = arg = xcalloc(6, sizeof(*arg)); if (protocol == PROTO_SSH) { const char *ssh = getenv("GIT_SSH"); int putty = ssh && strstr(ssh, "plink"); if (!ssh) ssh = "ssh"; *arg++ = ssh; - if (putty) { - /* stdin forwarding doesn't work, so give informative error messages - * (-v) and don't hang (-batch). - */ - *arg++ = "-batch"; - *arg++ = "-v"; - } if (port) { /* P is for PuTTY, p is for OpenSSH */ *arg++ = putty ? "-P" : "-p"; From 17e3d3f605ce39dd85dcded65cd6ba08ccf7ee73 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Wed, 2 Jul 2008 16:18:12 +0200 Subject: [PATCH 135/253] Revert "fast-import: fix compilation on MinGW" This reverts commit 54d99a7724a0c7fb53bcdc7d850034e9890a0138. A dummy implementation of getppid was added to the compat layer, so this ifdef is no longer needed. --- fast-import.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/fast-import.c b/fast-import.c index 271b93cf76..e72b286794 100644 --- a/fast-import.c +++ b/fast-import.c @@ -391,9 +391,7 @@ static void write_crash_report(const char *err) fprintf(rpt, "fast-import crash report:\n"); fprintf(rpt, " fast-import process: %d\n", getpid()); -#ifndef __MINGW32__ fprintf(rpt, " parent process : %d\n", getppid()); -#endif fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL)); fputc('\n', rpt); From 5278ad45e345e4e7f0418b4733f54823b7199801 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Wed, 2 Jul 2008 16:27:40 +0200 Subject: [PATCH 136/253] Revert "Avoid calling signal(SIGPIPE, ..) for MinGW builds." This reverts commit 56be985fe902a4af8c7f0bbe4541ef783ead827f. SIGPIPE is defined in the compat layer, so this ifdef is no longer needed. --- builtin-verify-tag.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/builtin-verify-tag.c b/builtin-verify-tag.c index 540e3b9b75..92eaa89a45 100644 --- a/builtin-verify-tag.c +++ b/builtin-verify-tag.c @@ -100,11 +100,9 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix) i++; } -#ifndef __MINGW32__ /* sometimes the program was terminated because this signal * was received in the process of writing the gpg input: */ signal(SIGPIPE, SIG_IGN); -#endif while (i < argc) if (verify_tag(argv[i++], verbose)) had_error = 1; From c00ce6091514f55931cb15d8dec014767ad918b4 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Wed, 2 Jul 2008 16:32:48 +0200 Subject: [PATCH 137/253] convert.c: Fix style (no functional changes) --- convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convert.c b/convert.c index f24ac25c49..1d3e8b02f1 100644 --- a/convert.c +++ b/convert.c @@ -63,7 +63,7 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat * } // If file ends with EOF then don't count this EOF as non-printable - if ( size >= 1 && buf[size-1] == '\032' ) + if (size >= 1 && buf[size-1] == '\032') stats->nonprintable--; } From adf63bc4e33d9fc5b2e7079aec484d6563788db3 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Wed, 2 Jul 2008 19:11:11 +0200 Subject: [PATCH 138/253] Revert "verify_path(): do not allow absolute paths" This reverts commit 8e78bd571071805ca9f34a6bc93846f6e5b15f4e. We do support absolute paths on Windows now. --- read-cache.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/read-cache.c b/read-cache.c index 5b109606e0..8e5fbb6192 100644 --- a/read-cache.c +++ b/read-cache.c @@ -634,11 +634,6 @@ int verify_path(const char *path) { char c; -#ifdef __MINGW32__ - if (is_absolute_path(path)) - return error("Cannot handle absolute path: %s", path); -#endif - goto inside; for (;;) { if (!c) From 861bebb42573c2812db44ff5a5dcfa4b7cd28224 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 3 Jul 2008 07:56:21 +0200 Subject: [PATCH 139/253] Revert "Fake reencoding success under NO_ICONV instead of returning NULL." We have a working iconv now, so this workaround is no longer needed. --- utf8.c | 7 ------- utf8.h | 4 ++++ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/utf8.c b/utf8.c index b07d43e580..dc3735364f 100644 --- a/utf8.c +++ b/utf8.c @@ -388,11 +388,4 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e iconv_close(conv); return out; } -#else -char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding) -{ - if (!in_encoding) - return NULL; - return xstrdup(in); -} #endif diff --git a/utf8.h b/utf8.h index f22ef3133c..98cce1b038 100644 --- a/utf8.h +++ b/utf8.h @@ -10,6 +10,10 @@ int is_encoding_utf8(const char *name); int print_wrapped_text(const char *text, int indent, int indent2, int len); +#ifndef NO_ICONV char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding); +#else +#define reencode_string(a,b,c) NULL +#endif #endif From 065eb05f3fca2b9a2a7a1fb39ca7dffd7f932f45 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 3 Jul 2008 08:18:47 +0200 Subject: [PATCH 140/253] connect.c: Remove comment that will not be sent upstream --- connect.c | 1 - 1 file changed, 1 deletion(-) diff --git a/connect.c b/connect.c index 11fa151eb3..0d007f366f 100644 --- a/connect.c +++ b/connect.c @@ -596,7 +596,6 @@ struct child_process *git_connect(int fd[2], const char *url_orig, die("command line too long"); conn->in = conn->out = -1; - /* be sure to increase this size if you add more args */ conn->argv = arg = xcalloc(6, sizeof(*arg)); if (protocol == PROTO_SSH) { const char *ssh = getenv("GIT_SSH"); From 3f3d496f8b8bf0bb2e74b0bf8268d3a19cbf553d Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Thu, 3 Jul 2008 08:21:43 +0200 Subject: [PATCH 141/253] help.c: Fix line endings (CRLF -> LF) --- help.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/help.c b/help.c index 2ed68389fe..fc45f463e3 100644 --- a/help.c +++ b/help.c @@ -28,12 +28,12 @@ enum help_format { HELP_FORMAT_WEB, }; -static int show_all = 0; -#ifdef __MINGW32__ -static enum help_format help_format = HELP_FORMAT_WEB; +static int show_all = 0; +#ifdef __MINGW32__ +static enum help_format help_format = HELP_FORMAT_WEB; #else static enum help_format help_format = HELP_FORMAT_MAN; -#endif +#endif static struct option builtin_help_options[] = { OPT_BOOLEAN('a', "all", &show_all, "print all available commands"), OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN), From a31c17daf43e67020d3a1d7a39d1e251709e0633 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 14 Jul 2008 07:16:21 +0200 Subject: [PATCH 142/253] help.c (Windows): Revert help.c in preparation for merge of better solution --- help.c | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/help.c b/help.c index 811f8dbb11..ca9632b6c5 100644 --- a/help.c +++ b/help.c @@ -9,7 +9,6 @@ #include "common-cmds.h" #include "parse-options.h" #include "run-command.h" -#include "dir.h" static struct man_viewer_list { struct man_viewer_list *next; @@ -29,11 +28,7 @@ enum help_format { }; static int show_all = 0; -#ifdef __MINGW32__ -static enum help_format help_format = HELP_FORMAT_WEB; -#else static enum help_format help_format = HELP_FORMAT_MAN; -#endif static struct option builtin_help_options[] = { OPT_BOOLEAN('a', "all", &show_all, "print all available commands"), OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN), @@ -649,35 +644,12 @@ static void get_html_page_path(struct strbuf *page_path, const char *page) static void show_html_page(const char *git_cmd) { -#ifdef __MINGW32__ - const char* exec_path = git_exec_path(); - char *htmlpath = make_native_separator( - mkpath("%s/../doc/git/html/%s.html" - , exec_path - , git_cmd) - ); - if (!file_exists(htmlpath)) { - htmlpath = make_native_separator( - mkpath("%s/../doc/git/html/git-%s.html" - , exec_path - , git_cmd) - ); - if (!file_exists(htmlpath)) { - fprintf(stderr, "Can't find HTML help for '%s'.\n" - , git_cmd); - exit(1); - } - } - printf("Launching default browser to display HTML help ...\n"); - ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0); -#else const char *page = cmd_to_page(git_cmd); struct strbuf page_path; /* it leaks but we exec bellow */ get_html_page_path(&page_path, page); execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL); -#endif } void help_unknown_cmd(const char *cmd) From 04bad247702047dcf53c87247d72c07513cb90a6 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 14 Jul 2008 07:23:02 +0200 Subject: [PATCH 143/253] help (Windows): Use msysgit-specific relative path to htmldir --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 06df0ba5be..5b5f1603f1 100644 --- a/Makefile +++ b/Makefile @@ -747,6 +747,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NOEXECTEMPL = .noexec template_dir = ../share/git-core/templates/ ETC_GITCONFIG = ../etc/gitconfig + htmldir=../doc/git/html/ endif ifneq (,$(findstring arm,$(uname_M))) ARM_SHA1 = YesPlease From 9b096dc9bb239309ba87456b7e2af56f436f91a3 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 14 Jul 2008 07:28:49 +0200 Subject: [PATCH 144/253] help (Windows): Revert leftovers of old implementation --- cache.h | 2 -- path.c | 13 ------------- 2 files changed, 15 deletions(-) diff --git a/cache.h b/cache.h index 2c4d9b3776..0d8eddac77 100644 --- a/cache.h +++ b/cache.h @@ -527,8 +527,6 @@ static inline int is_absolute_path(const char *path) const char *make_absolute_path(const char *path); const char *make_nonrelative_path(const char *path); const char *make_relative_path(const char *abs, const char *base); -/* Convert slashes in place. On Windows to backslashes. */ -char *make_native_separator(char *path); int normalize_absolute_path(char *buf, const char *path); int longest_ancestor_length(const char *path, const char *prefix_list); diff --git a/path.c b/path.c index 96503d96cd..504eae061f 100644 --- a/path.c +++ b/path.c @@ -439,16 +439,3 @@ int longest_ancestor_length(const char *path, const char *prefix_list) return max_len; } - -char *make_native_separator(char* path) { -#ifdef __MINGW32__ - char* c; - for (c = path; *c; c++) { - if (*c == '/') - *c = '\\'; - } - return path; -#else - return path; -#endif -} From 5d54e5d4f2c0ccd3670fc12eaa86047fd0822cf4 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 14 Jul 2008 08:00:16 +0200 Subject: [PATCH 145/253] setup (Windows): Revert leftovers of old implementation The handling of paths was resolved differently in the MinGW port that is merged to official git. This commit takes this implementation. Signed-off-by: Steffen Prohaska --- setup.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/setup.c b/setup.c index 1fd30c4b43..6cf909463d 100644 --- a/setup.c +++ b/setup.c @@ -381,7 +381,6 @@ const char *setup_git_directory_gently(int *nongit_ok) const char *gitdirenv; const char *gitfile_dir; int len, offset, ceil_offset; - int minoffset = 0; /* * Let's assume that we are in a git repository. @@ -432,8 +431,6 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!getcwd(cwd, sizeof(cwd)-1)) die("Unable to read current working directory"); - if (has_dos_drive_prefix(cwd)) - minoffset = 2; ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) @@ -464,11 +461,11 @@ const char *setup_git_directory_gently(int *nongit_ok) inside_git_dir = 1; if (!work_tree_env) inside_work_tree = 0; - set_git_dir("."); + setenv(GIT_DIR_ENVIRONMENT, ".", 1); check_repository_format_gently(nongit_ok); return NULL; } - while (offset > minoffset && --offset > ceil_offset && cwd[offset] != '/'); + while (--offset > ceil_offset && cwd[offset] != '/'); if (offset <= ceil_offset) { if (nongit_ok) { if (chdir(cwd)) From a79b6908829b5804dbfab78050fa9991f59597f9 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Tue, 22 Jul 2008 22:07:23 +0200 Subject: [PATCH 146/253] Makefile: cleanup leftovers from old winansi implementation --- Makefile | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Makefile b/Makefile index f7da7e7870..0cdfcd2762 100644 --- a/Makefile +++ b/Makefile @@ -739,7 +739,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_R_TO_GCC_LINKER = YesPlease INTERNAL_QSORT = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease - WIN_ANSI = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" @@ -987,11 +986,6 @@ ifdef NO_EXTERNAL_GREP BASIC_CFLAGS += -DNO_EXTERNAL_GREP endif -ifdef WIN_ANSI - COMPAT_CFLAGS += -DWIN_ANSI - COMPAT_OBJS += compat/winansi.o -endif - ifeq ($(TCLTK_PATH),) NO_TCLTK=NoThanks endif From 1ddfb58a5edcbdaf7371fe0036026c57aeed6a2c Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 26 Jul 2008 11:27:43 +0200 Subject: [PATCH 147/253] Modify mingw_main() workaround to avoid link errors With MinGW's gcc.exe (GCC) 3.4.5 (mingw special) GNU ld version 2.17.50 20060824 the old define caused link errors: git.o: In function `main': C:/msysgit/git/git.c:500: undefined reference to `mingw_main' collect2: ld returned 1 exit status The modified define works. Signed-off-by: Steffen Prohaska --- compat/mingw.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compat/mingw.h b/compat/mingw.h index 290a9e6f82..a52e657c51 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -228,9 +228,10 @@ char **env_setenv(char **env, const char *name); * A replacement of main() that ensures that argv[0] has a path */ -#define main(c,v) main(int argc, const char **argv) \ +#define main(c,v) dummy_decl_mingw_main(); \ +static int mingw_main(); \ +int main(int argc, const char **argv) \ { \ - static int mingw_main(); \ argv[0] = xstrdup(_pgmptr); \ return mingw_main(argc, argv); \ } \ From 967b742f714410e2cf4d14fb3a0522000ec1d4a7 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 27 Jul 2008 09:53:02 +0200 Subject: [PATCH 148/253] Makefile: Fix crlf newline Signed-off-by: Steffen Prohaska --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0797e070f6..b2bc0efe10 100644 --- a/Makefile +++ b/Makefile @@ -755,7 +755,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/fnmatch.o compat/regex.o compat/winansi.o EXTLIBS += -lws2_32 - X = .exe + X = .exe NOEXECTEMPL = .noexec gitexecdir = ../libexec/git-core template_dir = ../share/git-core/templates/ From 7c2ca986515e2009ab29080e57ae8b38c921ba4d Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 16 Aug 2008 16:11:28 +0200 Subject: [PATCH 149/253] gitk: Fix wrong merge This fixes the wrong conflict resolution of merge 5673e9999d7e120f4b68c2c5a61c5ec48ec38cad --- gitk-git/gitk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index c6b06df56b..9636d15115 100644 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -9993,4 +9993,4 @@ if {[info exists permviews]} { } } focus -force . -getcommits +getcommits {} From fa7fbeb525b11c21a5caa2ee4d8a027405f2be6f Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 16 Aug 2008 16:39:25 +0200 Subject: [PATCH 150/253] Windows: Add workaround for MSYS' path conversion MSYS' automatic path conversion causes problems when passing paths as defines ('-D' arguments to the compiler). MSYS tries to be smart and converts absolute paths to native Windows paths, e.g. if MSYS sees "/bin" it converts it to "c:/msysgit/bin". But we want completely unmodified paths; e.g. if we set bindir in the Makefile to "/bin", the define BINDIR shall expand to "/bin". Conversion to absolute Windows path will takes place later, during runtime. This commit adds a workaround by replacing "/" with its octal representation "\057", effectively hiding the path from MSYS' path conversion mechanism. MSYS does no longer see the absolute path and therefore leaves it alone. --- Makefile | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 8d81095765..f716fda416 100644 --- a/Makefile +++ b/Makefile @@ -1049,6 +1049,12 @@ template_dir_SQ = $(subst ','\'',$(template_dir)) htmldir_SQ = $(subst ','\'',$(htmldir)) prefix_SQ = $(subst ','\'',$(prefix)) +ETC_GITCONFIG_SQ_C = $(subst /,\057,$(ETC_GITCONFIG_SQ)) +bindir_SQ_C = $(subst /,\057,$(bindir_SQ)) +gitexecdir_SQ_C = $(subst /,\057,$(gitexecdir_SQ)) +htmldir_SQ_C = $(subst /,\057,$(htmldir_SQ)) +template_dir_SQ_C = $(subst /,\057,$(template_dir_SQ)) + SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH)) PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH)) TCLTK_PATH_SQ = $(subst ','\'',$(TCLTK_PATH)) @@ -1100,7 +1106,7 @@ git$X: git.o $(BUILTIN_OBJS) $(GITLIBS) help.o: help.c common-cmds.h GIT-CFLAGS $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) \ - '-DGIT_HTML_PATH="$(htmldir_SQ)"' \ + '-DGIT_HTML_PATH="$(htmldir_SQ_C)"' \ '-DGIT_MAN_PATH="$(mandir_SQ)"' \ '-DGIT_INFO_PATH="$(infodir_SQ)"' $< @@ -1207,12 +1213,12 @@ git.o git.spec \ $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $< exec_cmd.o: exec_cmd.c GIT-CFLAGS - $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' $< + $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH="$(gitexecdir_SQ_C)"' -DBINDIR='"$(bindir_SQ_C)"' $< builtin-init-db.o: builtin-init-db.c GIT-CFLAGS - $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $< + $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ_C)"' $< config.o: config.c GIT-CFLAGS - $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DETC_GITCONFIG='"$(ETC_GITCONFIG_SQ)"' $< + $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DETC_GITCONFIG='"$(ETC_GITCONFIG_SQ_C)"' $< http.o: http.c GIT-CFLAGS $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $< From 7b6c6496374073d4519a035b5c2053395a60e014 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 10 Aug 2008 17:52:36 +0200 Subject: [PATCH 151/253] system_path(): Add prefix computation at runtime if RUNTIME_PREFIX set This commit modifies system_path() to compute the prefix at runtime if configured to do so. If RUNTIME_PREFIX is defined, system_path() tries to strip known directories that executables can be located in from the path of the executable. If the path is successfully stripped it is used as the prefix. For example, if the executable is "/msysgit/bin/git" and BINDIR is "/bin", then the prefix is computed as "/msysgit". We report an error if the runtime prefix computation fails, which can happen if the executable is not installed at a known location. The user should know that the global configuration is not picked up, because this can cause unexpected behavior. If we explicitly want to ignore system wide paths, we can set the environment variable GIT_CONFIG_NOSYSTEM, as our tests do. The implementation requires that argv0_path is set up properly, which is currently the case only on Windows. argv0_path must point to the absolute path of the directory of the executable, which is verified by two calls to assert(). On Windows, the wrapper for main() (see compat/mingw.h) guarantees that this is the case. On Unix, further work is required before RUNTIME_PREFIX can be enabled. --- Makefile | 3 +++ exec_cmd.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index f716fda416..b229bff5fb 100644 --- a/Makefile +++ b/Makefile @@ -989,6 +989,9 @@ ifdef INTERNAL_QSORT COMPAT_CFLAGS += -DINTERNAL_QSORT COMPAT_OBJS += compat/qsort.o endif +ifdef RUNTIME_PREFIX + COMPAT_CFLAGS += -DRUNTIME_PREFIX +endif ifdef THREADED_DELTA_SEARCH BASIC_CFLAGS += -DTHREADED_DELTA_SEARCH diff --git a/exec_cmd.c b/exec_cmd.c index ce6741eb68..9fa89b8502 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -9,11 +9,48 @@ static const char *argv0_path; const char *system_path(const char *path) { - if (!is_absolute_path(path) && argv0_path) { - struct strbuf d = STRBUF_INIT; - strbuf_addf(&d, "%s/%s", argv0_path, path); - path = strbuf_detach(&d, NULL); +#ifdef RUNTIME_PREFIX + static const char *prefix; + + assert(argv0_path); + assert(is_absolute_path(argv0_path)); + + if (!prefix) { + const char *strip[] = { + GIT_EXEC_PATH, + BINDIR, + 0 + }; + const char **s; + + for (s = strip; *s; s++) { + const char *sargv = argv0_path + strlen(argv0_path); + const char *ss = *s + strlen(*s); + while (argv0_path < sargv && *s < ss + && (*sargv == *ss || + (is_dir_sep(*sargv) && is_dir_sep(*ss)))) { + sargv--; + ss--; + } + if (*s == ss) { + struct strbuf d = STRBUF_INIT; + strbuf_add(&d, argv0_path, sargv - argv0_path); + prefix = strbuf_detach(&d, NULL); + break; + } + } } + + if (!prefix) { + fprintf(stderr, "RUNTIME_PREFIX requested for path '%s', " + "but prefix computation failed.\n", path); + return path; + } + + struct strbuf d = STRBUF_INIT; + strbuf_addf(&d, "%s/%s", prefix, path); + path = strbuf_detach(&d, NULL); +#endif return path; } From f2fdec9debe6803204ce9dd734058188110e3cf9 Mon Sep 17 00:00:00 2001 From: Steve Haslam Date: Wed, 30 Jul 2008 12:27:18 +0100 Subject: [PATCH 152/253] Refactor git_set_argv0_path() to git_extract_argv0_path() This commit moves the code that computes the dirname of argv[0] from git.c's main() to git_set_argv0_path() and renames the function to git_extract_argv0_path(). This makes the code in git.c's main less cluttered, and we can use the direname computation from other main() functions too. [spr: split Steve's original commit and wrote new commit message. ] Signed-off-by: Steve Haslam Signed-off-by: Steffen Prohaska --- exec_cmd.c | 15 +++++++++++++-- exec_cmd.h | 2 +- git.c | 20 +++++--------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index 9fa89b8502..46ebf7ec82 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -54,9 +54,20 @@ const char *system_path(const char *path) return path; } -void git_set_argv0_path(const char *path) +const char *git_extract_argv0_path(const char *argv0) { - argv0_path = path; + const char *slash = argv0 + strlen(argv0); + + do + --slash; + while (slash >= argv0 && !is_dir_sep(*slash)); + + if (slash >= argv0) { + argv0_path = xstrndup(argv0, slash - argv0); + return slash + 1; + } + + return argv0; } void git_set_argv_exec_path(const char *exec_path) diff --git a/exec_cmd.h b/exec_cmd.h index 594f961387..392e9032c3 100644 --- a/exec_cmd.h +++ b/exec_cmd.h @@ -2,7 +2,7 @@ #define GIT_EXEC_CMD_H extern void git_set_argv_exec_path(const char *exec_path); -extern void git_set_argv0_path(const char *path); +extern const char* git_extract_argv0_path(const char *path); extern const char* git_exec_path(void); extern void setup_path(void); extern const char **prepare_git_cmd(const char **argv); diff --git a/git.c b/git.c index fdb0f71019..cfcedc20dc 100644 --- a/git.c +++ b/git.c @@ -416,23 +416,13 @@ static void execv_dashed_external(const char **argv) int main(int argc, const char **argv) { - const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help"; - char *slash = (char *)cmd + strlen(cmd); + const char *cmd; int done_alias = 0; - /* - * Take the basename of argv[0] as the command - * name, and the dirname as the default exec_path - * if we don't have anything better. - */ - do - --slash; - while (cmd <= slash && !is_dir_sep(*slash)); - if (cmd <= slash) { - *slash++ = 0; - git_set_argv0_path(cmd); - cmd = slash; - } + if (argv[0] && *argv[0]) + cmd = git_extract_argv0_path(argv[0]); + else + cmd = "git-help"; /* * "git-xxxx" is the same as "git xxxx", but we obviously: From 2a7e0eeb480c083fc66065a9505fbbfecae2cf4d Mon Sep 17 00:00:00 2001 From: Steve Haslam Date: Wed, 30 Jul 2008 12:27:18 +0100 Subject: [PATCH 153/253] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack. If the user specified the full path to git-upload-pack as the -u option to "git clone" when cloning a remote repository, and git was not on the default PATH on the remote machine, git-upload-pack was failing to exec git-pack-objects. By making the argv[0] path (if any) available to setup_path(), this will allow finding the "git" executable in the same directory as "git-upload-pack". The default built in to exec_cmd.c is to look for "git" in the ".../libexec/git-core" directory, but it is not installed there (any longer). Much the same applies to invoking git-receive-pack from a non-PATH location using the "--exec" argument to "git push". [ spr: split Steve's original commit into two commits. ] Signed-off-by: Steve Haslam Signed-off-by: Steffen Prohaska --- receive-pack.c | 3 +++ upload-pack.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/receive-pack.c b/receive-pack.c index d44c19e6b5..3699b166c7 100644 --- a/receive-pack.c +++ b/receive-pack.c @@ -467,6 +467,9 @@ int main(int argc, char **argv) int i; char *dir = NULL; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + argv++; for (i = 1; i < argc; i++) { char *arg = *argv++; diff --git a/upload-pack.c b/upload-pack.c index e5adbc011e..c469a60d79 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -616,6 +616,9 @@ int main(int argc, char **argv) int i; int strict = 0; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + for (i = 1; i < argc; i++) { char *arg = argv[i]; From 9599d567403f6a0bf185f8a3c4e14a77b69004a5 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 17 Aug 2008 08:58:21 +0200 Subject: [PATCH 154/253] Add calls to git_extract_argv0_path() in programs that call git_config_* Programs that use git_config need to find the global configuration. When runtime prefix computation is enabled, this requires that git_extract_argv0_path() is called early in the program's main(). This commit adds the necessary calls in the programs that use git_config. --- daemon.c | 3 +++ fast-import.c | 4 ++++ hash-object.c | 4 ++++ index-pack.c | 4 ++++ unpack-file.c | 4 ++++ var.c | 4 ++++ 6 files changed, 23 insertions(+) diff --git a/daemon.c b/daemon.c index 8dcde73200..172854e74c 100644 --- a/daemon.c +++ b/daemon.c @@ -1055,6 +1055,9 @@ int main(int argc, char **argv) gid_t gid = 0; int i; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + /* Without this we cannot rely on waitpid() to tell * what happened to our children. */ diff --git a/fast-import.c b/fast-import.c index d85b3a561f..239309f96d 100644 --- a/fast-import.c +++ b/fast-import.c @@ -150,6 +150,7 @@ Format of STDIN stream: #include "refs.h" #include "csum-file.h" #include "quote.h" +#include "exec_cmd.h" #define PACK_ID_BITS 16 #define MAX_PACK_ID ((1<"); if (get_sha1(argv[1], sha1)) diff --git a/var.c b/var.c index f1eb314e89..33457dc6ab 100644 --- a/var.c +++ b/var.c @@ -4,6 +4,7 @@ * Copyright (C) Eric Biederman, 2005 */ #include "cache.h" +#include "exec_cmd.h" static const char var_usage[] = "git var [-l | ]"; @@ -56,6 +57,9 @@ int main(int argc, char **argv) usage(var_usage); } + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory_gently(&nongit); val = NULL; From 8327b82bf72faaa5bcf0cee176312f1f89bae1c3 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 17 Aug 2008 11:30:33 +0200 Subject: [PATCH 155/253] Modify setup_path() to only add git_exec_path() to PATH We should search git programs only in the highest-priority location. The old code added the directories "argv_exec_path", "getenv(EXEC_PATH_ENVIRONMENT)", and "system_path(GIT_EXEC_PATH)" to PATH. The same order is implemented in git_exec_path(), which returns the highest priority location to search for executables. If the user explicitly overrides the default location (by --exec-path or GIT_EXEC_PATH) we can expect that all the required programs are there. It does not make sense that only some of the required programs are located at the highest priority location and other programs are picked up from a lower priority exec-path. If exec-path is overridden a complete set of commands should be provided, otherwise several different versions might easily get mixed, which is likely to spread confusion. Accessing the location with highest priority only is also required for testing of executables built with RUNTIME_PREFIX. Calling system_path(GIT_EXEC_PATH) is avoided if a higher-priority location is provided, which is the case for the tests. The call to system_path() must be avoided, if RUNTIME_PREFIX is set, because the call would fail if the executable is not installed at its final destination. But we test before installing. --- exec_cmd.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index 46ebf7ec82..2a86670baa 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -111,9 +111,7 @@ void setup_path(void) strbuf_init(&new_path, 0); - add_path(&new_path, argv_exec_path); - add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT)); - add_path(&new_path, system_path(GIT_EXEC_PATH)); + add_path(&new_path, git_exec_path()); add_path(&new_path, argv0_path); if (old_path) From 1a78826c218ecf8bbda95802e8e0fc912a1d499b Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 16 Aug 2008 16:43:58 +0200 Subject: [PATCH 156/253] Windows: Revert to default paths and convert them by RUNTIME_PREFIX The RUNTIME_PREFIX mechanism allows us to use the default (absolute) paths on Windows too. Defining RUNTIME_PREFIX explicitly requests for translation of paths during runtime, depending on the path to the executable. --- Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b229bff5fb..b7c4e02d6c 100644 --- a/Makefile +++ b/Makefile @@ -755,6 +755,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) SNPRINTF_RETURNS_BOGUS = YesPlease NO_SVN_TESTS = YesPlease NO_PERL_MAKEMAKER = YesPlease + RUNTIME_PREFIX = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1 @@ -762,9 +763,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o EXTLIBS += -lws2_32 X = .exe - gitexecdir = ../libexec/git-core - template_dir = ../share/git-core/templates/ - ETC_GITCONFIG = ../etc/gitconfig endif ifneq (,$(findstring arm,$(uname_M))) ARM_SHA1 = YesPlease From 8ed472bcbb8a5eb47354050606129f13487c6e53 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 17 Aug 2008 08:58:21 +0200 Subject: [PATCH 157/253] Add call to git_extract_argv0_path() to various programs Programs that use git_config need to find the global configuration. When runtime prefix computation is enabled, this requires that git_extract_argv0_path() is called early in the program's main(). This commit adds the necessary calls. --- http-push.c | 3 +++ imap-send.c | 4 ++++ merge-index.c | 4 ++++ merge-tree.c | 4 ++++ mktag.c | 4 ++++ mktree.c | 4 ++++ pack-redundant.c | 4 ++++ patch-id.c | 4 ++++ update-server-info.c | 4 ++++ 9 files changed, 35 insertions(+) diff --git a/http-push.c b/http-push.c index 6805288857..47fa1cb6b3 100644 --- a/http-push.c +++ b/http-push.c @@ -2170,6 +2170,9 @@ int main(int argc, char **argv) struct ref *ref; char *rewritten_url = NULL; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); remote = xcalloc(sizeof(*remote), 1); diff --git a/imap-send.c b/imap-send.c index 1ec1310921..6714f9114c 100644 --- a/imap-send.c +++ b/imap-send.c @@ -23,6 +23,7 @@ */ #include "cache.h" +#include "exec_cmd.h" typedef struct store_conf { char *name; @@ -1293,6 +1294,9 @@ main(int argc, char **argv) int r; int total, n = 0; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + /* init the random number generator */ arc4_init(); diff --git a/merge-index.c b/merge-index.c index 7827e87a92..5d89eadba0 100644 --- a/merge-index.c +++ b/merge-index.c @@ -1,5 +1,6 @@ #include "cache.h" #include "run-command.h" +#include "exec_cmd.h" static const char *pgm; static const char *arguments[9]; @@ -93,6 +94,9 @@ int main(int argc, char **argv) if (argc < 3) usage("git-merge-index [-o] [-q] (-a | [--] *)"); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); read_cache(); diff --git a/merge-tree.c b/merge-tree.c index 02fc10f7e6..51061ae604 100644 --- a/merge-tree.c +++ b/merge-tree.c @@ -2,6 +2,7 @@ #include "tree-walk.h" #include "xdiff-interface.h" #include "blob.h" +#include "exec_cmd.h" static const char merge_tree_usage[] = "git-merge-tree "; static int resolve_directories = 1; @@ -345,6 +346,9 @@ int main(int argc, char **argv) if (argc != 4) usage(merge_tree_usage); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); buf1 = get_tree_descriptor(t+0, argv[1]); diff --git a/mktag.c b/mktag.c index 0b34341f71..44490a6729 100644 --- a/mktag.c +++ b/mktag.c @@ -1,5 +1,6 @@ #include "cache.h" #include "tag.h" +#include "exec_cmd.h" /* * A signature file has a very simple fixed format: four lines @@ -159,6 +160,9 @@ int main(int argc, char **argv) if (argc != 1) usage("git-mktag < signaturefile"); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); strbuf_init(&buf, 0); diff --git a/mktree.c b/mktree.c index e0da110a98..0e83fc935b 100644 --- a/mktree.c +++ b/mktree.c @@ -6,6 +6,7 @@ #include "cache.h" #include "quote.h" #include "tree.h" +#include "exec_cmd.h" static struct treeent { unsigned mode; @@ -70,6 +71,9 @@ int main(int ac, char **av) unsigned char sha1[20]; int line_termination = '\n'; + if (av[0] && *av[0]) + git_extract_argv0_path(av[0]); + setup_git_directory(); while ((1 < ac) && av[1][0] == '-') { diff --git a/pack-redundant.c b/pack-redundant.c index 25b81a445c..8ff450b319 100644 --- a/pack-redundant.c +++ b/pack-redundant.c @@ -7,6 +7,7 @@ */ #include "cache.h" +#include "exec_cmd.h" #define BLKSIZE 512 @@ -601,6 +602,9 @@ int main(int argc, char **argv) unsigned char *sha1; char buf[42]; /* 40 byte sha1 + \n + \0 */ + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); for (i = 1; i < argc; i++) { diff --git a/patch-id.c b/patch-id.c index 9349bc5580..775e2954bd 100644 --- a/patch-id.c +++ b/patch-id.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "exec_cmd.h" static void flush_current_id(int patchlen, unsigned char *id, SHA_CTX *c) { @@ -79,6 +80,9 @@ int main(int argc, char **argv) if (argc != 1) usage(patch_id_usage); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + generate_id_list(); return 0; } diff --git a/update-server-info.c b/update-server-info.c index 7e8209ea4b..286a4dd517 100644 --- a/update-server-info.c +++ b/update-server-info.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "exec_cmd.h" static const char update_server_info_usage[] = "git update-server-info [--force]"; @@ -19,6 +20,9 @@ int main(int ac, char **av) if (i != ac) usage(update_server_info_usage); + if (av[0] && *av[0]) + git_extract_argv0_path(av[0]); + setup_git_directory(); return !!update_server_info(force); From ee10a9759a22a9a8885761436049ceaefc0b8086 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 10 Aug 2008 17:52:36 +0200 Subject: [PATCH 158/253] Move computation of absolute paths from Makefile to runtime and compute prefix on the fly if RUNTIME_PREFIX set This is the first commit of a series that adds support for relocatable binaries (called RUNTIME_PREFIX). Such binaries can be moved together with the system configuration files to a different directory, as long as the relative paths from the binary to the configuration files stays the same. This functionality is essential on Windows where we deliver git binaries with an installer that allows to freely choose the installation location. The commit series implements RUNTIME_PREFIX only on Windows. The architecture is such that adding support on Unix should not be too hard. This first commits makes all paths relative in the Makefile and teaches system_path() to add the prefix instead. We used to compute absolute paths in the Makefile and passed them to C as defines. We now pass relative paths to C and call system_path() to add the prefix at runtime. If RUNTIME_PREFIX is unset we use the static prefix. This will be the default on Unix. Thus, the behavior is unchanged compared to the old implementation. If RUNTIME_PREFIX is set the prefix is computed from the location of the executable. In this case, system_path() tries to strip known directories that executables can be located in from the path of the executable. If the path is successfully stripped it is used as the prefix. For example, if the executable is "/msysgit/bin/git" and BINDIR is "bin", then the prefix computed is "/msysgit". If the runtime prefix computation fails, we fall back to the static prefix specified in the makefile. This can be the case if the executable is not installed at a known location. Note that our test system sets GIT_CONFIG_NOSYSTEM to tell git to ignore global configuration files during testing. Hence testing does not trigger the fall back. Note that the implementation requires argv0_path to be set to an absolute path, which is currently the case only on Windows. argv0_path must point to the directory of the executable. We use assert() to verify this during debugging. On Windows, the wrapper for main() (see compat/mingw.h) guarantees that this is the case. On Unix, further work is required before RUNTIME_PREFIX can be enabled. --- Makefile | 45 ++++++++++++++++++++++++++--------------- builtin-help.c | 4 ++-- exec_cmd.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index aabf0130b9..da17fe950a 100644 --- a/Makefile +++ b/Makefile @@ -179,28 +179,32 @@ STRIP ?= strip # Among the variables below, these: # gitexecdir # template_dir +# mandir +# infodir # htmldir # ETC_GITCONFIG (but not sysconfdir) -# can be specified as a relative path ../some/where/else (which must begin -# with ../); this is interpreted as relative to $(bindir) and "git" at +# can be specified as a relative path some/where/else; +# this is interpreted as relative to $(prefix) and "git" at # runtime figures out where they are based on the path to the executable. # This can help installing the suite in a relocatable way. prefix = $(HOME) -bindir = $(prefix)/bin -mandir = $(prefix)/share/man -infodir = $(prefix)/share/info -gitexecdir = $(prefix)/libexec/git-core +bindir_relative = bin +bindir = $(prefix)/$(bindir_relative) +mandir = share/man +infodir = share/info +gitexecdir = libexec/git-core sharedir = $(prefix)/share -template_dir = $(sharedir)/git-core/templates -htmldir=$(sharedir)/doc/git-doc +template_dir = share/git-core/templates +htmldir = share/doc/git-doc ifeq ($(prefix),/usr) sysconfdir = /etc +ETC_GITCONFIG = $(sysconfdir)/gitconfig else sysconfdir = $(prefix)/etc +ETC_GITCONFIG = etc/gitconfig endif lib = lib -ETC_GITCONFIG = $(sysconfdir)/gitconfig # DESTDIR= # default configuration for gitweb @@ -1027,6 +1031,9 @@ ifdef INTERNAL_QSORT COMPAT_CFLAGS += -DINTERNAL_QSORT COMPAT_OBJS += compat/qsort.o endif +ifdef RUNTIME_PREFIX + COMPAT_CFLAGS += -DRUNTIME_PREFIX +endif ifdef NO_PTHREADS THREADED_DELTA_SEARCH = @@ -1086,6 +1093,7 @@ ETC_GITCONFIG_SQ = $(subst ','\'',$(ETC_GITCONFIG)) DESTDIR_SQ = $(subst ','\'',$(DESTDIR)) bindir_SQ = $(subst ','\'',$(bindir)) +bindir_relative_SQ = $(subst ','\'',$(bindir_relative)) mandir_SQ = $(subst ','\'',$(mandir)) infodir_SQ = $(subst ','\'',$(infodir)) gitexecdir_SQ = $(subst ','\'',$(gitexecdir)) @@ -1251,7 +1259,12 @@ git.o git.spec \ $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $< exec_cmd.o: exec_cmd.c GIT-CFLAGS - $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' $< + $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) \ + '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \ + '-DBINDIR="$(bindir_relative_SQ)"' \ + '-DPREFIX="$(prefix_SQ)"' \ + $< + builtin-init-db.o: builtin-init-db.c GIT-CFLAGS $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $< @@ -1397,17 +1410,17 @@ remove-dashes: ### Installation rules -ifeq ($(firstword $(subst /, ,$(template_dir))),..) -template_instdir = $(bindir)/$(template_dir) -else +ifeq ($(abspath $(template_dir)),$(template_dir)) template_instdir = $(template_dir) +else +template_instdir = $(prefix)/$(template_dir) endif export template_instdir -ifeq ($(firstword $(subst /, ,$(gitexecdir))),..) -gitexec_instdir = $(bindir)/$(gitexecdir) -else +ifeq ($(abspath $(gitexecdir)),$(gitexecdir)) gitexec_instdir = $(gitexecdir) +else +gitexec_instdir = $(prefix)/$(gitexecdir) endif gitexec_instdir_SQ = $(subst ','\'',$(gitexec_instdir)) export gitexec_instdir diff --git a/builtin-help.c b/builtin-help.c index f076efa921..9b57a74618 100644 --- a/builtin-help.c +++ b/builtin-help.c @@ -329,7 +329,7 @@ static void setup_man_path(void) * old_path, the ':' at the end will let 'man' to try * system-wide paths after ours to find the manual page. If * there is old_path, we need ':' as delimiter. */ - strbuf_addstr(&new_path, GIT_MAN_PATH); + strbuf_addstr(&new_path, system_path(GIT_MAN_PATH)); strbuf_addch(&new_path, ':'); if (old_path) strbuf_addstr(&new_path, old_path); @@ -375,7 +375,7 @@ static void show_man_page(const char *git_cmd) static void show_info_page(const char *git_cmd) { const char *page = cmd_to_page(git_cmd); - setenv("INFOPATH", GIT_INFO_PATH, 1); + setenv("INFOPATH", system_path(GIT_INFO_PATH), 1); execlp("info", "info", "gitman", page, NULL); } diff --git a/exec_cmd.c b/exec_cmd.c index cdd35f9195..669b82e35a 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -9,11 +9,57 @@ static const char *argv0_path; const char *system_path(const char *path) { - if (!is_absolute_path(path) && argv0_path) { - struct strbuf d = STRBUF_INIT; - strbuf_addf(&d, "%s/%s", argv0_path, path); - path = strbuf_detach(&d, NULL); + static const char *prefix; + + if (is_absolute_path(path)) { + return path; } + +#ifdef RUNTIME_PREFIX + assert(argv0_path); + assert(is_absolute_path(argv0_path)); + + if (!prefix) { + const char *strip[] = { + GIT_EXEC_PATH, + BINDIR, + 0 + }; + const char **s; + + for (s = strip; *s; s++) { + const char *sargv = argv0_path + strlen(argv0_path); + const char *ss = *s + strlen(*s); + while (argv0_path < sargv && *s < ss + && (*sargv == *ss || + (is_dir_sep(*sargv) && is_dir_sep(*ss)))) { + sargv--; + ss--; + } + if (*s == ss) { + struct strbuf d = STRBUF_INIT; + /* We also skip the trailing directory separator. */ + assert(sargv - argv0_path - 1 >= 0); + strbuf_add(&d, argv0_path, sargv - argv0_path - 1); + prefix = strbuf_detach(&d, NULL); + break; + } + } + } + + if (!prefix) { + prefix = PREFIX; + fprintf(stderr, "RUNTIME_PREFIX requested, " + "but prefix computation failed. " + "Using static fallback '%s'.\n", prefix); + } +#else + prefix = PREFIX; +#endif + + struct strbuf d = STRBUF_INIT; + strbuf_addf(&d, "%s/%s", prefix, path); + path = strbuf_detach(&d, NULL); return path; } From 7501e1c7089414ec414dec8b2c95840ba6c95813 Mon Sep 17 00:00:00 2001 From: Steve Haslam Date: Wed, 30 Jul 2008 12:27:18 +0100 Subject: [PATCH 159/253] Refactor git_set_argv0_path() to git_extract_argv0_path() This commit moves the code that computes the dirname of argv[0] from git.c's main() to git_set_argv0_path() and renames the function to git_extract_argv0_path(). This makes the code in git.c's main less cluttered, and we can use the dirname computation from other main() functions too. [spr: split Steve's original commit and wrote new commit message. ] Signed-off-by: Steve Haslam Signed-off-by: Steffen Prohaska --- exec_cmd.c | 15 +++++++++++++-- exec_cmd.h | 2 +- git.c | 20 +++++--------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index 669b82e35a..dde5cfd14f 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -63,9 +63,20 @@ const char *system_path(const char *path) return path; } -void git_set_argv0_path(const char *path) +const char *git_extract_argv0_path(const char *argv0) { - argv0_path = path; + const char *slash = argv0 + strlen(argv0); + + do + --slash; + while (slash >= argv0 && !is_dir_sep(*slash)); + + if (slash >= argv0) { + argv0_path = xstrndup(argv0, slash - argv0); + return slash + 1; + } + + return argv0; } void git_set_argv_exec_path(const char *exec_path) diff --git a/exec_cmd.h b/exec_cmd.h index 594f961387..392e9032c3 100644 --- a/exec_cmd.h +++ b/exec_cmd.h @@ -2,7 +2,7 @@ #define GIT_EXEC_CMD_H extern void git_set_argv_exec_path(const char *exec_path); -extern void git_set_argv0_path(const char *path); +extern const char* git_extract_argv0_path(const char *path); extern const char* git_exec_path(void); extern void setup_path(void); extern const char **prepare_git_cmd(const char **argv); diff --git a/git.c b/git.c index 940a498962..6c3595decc 100644 --- a/git.c +++ b/git.c @@ -419,23 +419,13 @@ static void execv_dashed_external(const char **argv) int main(int argc, const char **argv) { - const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help"; - char *slash = (char *)cmd + strlen(cmd); + const char *cmd; int done_alias = 0; - /* - * Take the basename of argv[0] as the command - * name, and the dirname as the default exec_path - * if we don't have anything better. - */ - do - --slash; - while (cmd <= slash && !is_dir_sep(*slash)); - if (cmd <= slash) { - *slash++ = 0; - git_set_argv0_path(cmd); - cmd = slash; - } + if (argv[0] && *argv[0]) + cmd = git_extract_argv0_path(argv[0]); + else + cmd = "git-help"; /* * "git-xxxx" is the same as "git xxxx", but we obviously: From ea476195ab8c45e123302ec5481274f59d11d38c Mon Sep 17 00:00:00 2001 From: Steve Haslam Date: Wed, 30 Jul 2008 12:27:18 +0100 Subject: [PATCH 160/253] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack. If the user specified the full path to git-upload-pack as the -u option to "git clone" when cloning a remote repository, and git was not on the default PATH on the remote machine, git-upload-pack was failing to exec git-pack-objects. By making the argv[0] path (if any) available to setup_path(), this will allow finding the "git" executable in the same directory as "git-upload-pack". The default built in to exec_cmd.c is to look for "git" in the ".../libexec/git-core" directory, but it is not installed there (any longer). Much the same applies to invoking git-receive-pack from a non-PATH location using the "--exec" argument to "git push". [ spr: split Steve's original commit into two commits. ] Signed-off-by: Steve Haslam Signed-off-by: Steffen Prohaska --- builtin-receive-pack.c | 3 +++ upload-pack.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c index db67c3162c..bbedcfe5e4 100644 --- a/builtin-receive-pack.c +++ b/builtin-receive-pack.c @@ -579,6 +579,9 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) int i; char *dir = NULL; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + argv++; for (i = 1; i < argc; i++) { const char *arg = *argv++; diff --git a/upload-pack.c b/upload-pack.c index e5adbc011e..c469a60d79 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -616,6 +616,9 @@ int main(int argc, char **argv) int i; int strict = 0; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + for (i = 1; i < argc; i++) { char *arg = argv[i]; From 61cc52a1ccfd426af3523d681dec63a8fa7c2842 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 17 Aug 2008 08:58:21 +0200 Subject: [PATCH 161/253] Add calls to git_extract_argv0_path() in programs that call git_config_* Programs that use git_config need to find the global configuration. When runtime prefix computation is enabled, this requires that git_extract_argv0_path() is called early in the program's main(). This commit adds the necessary calls. --- daemon.c | 3 +++ fast-import.c | 4 ++++ hash-object.c | 4 ++++ http-push.c | 3 +++ imap-send.c | 4 ++++ index-pack.c | 4 ++++ merge-index.c | 4 ++++ merge-tree.c | 4 ++++ mktag.c | 4 ++++ mktree.c | 4 ++++ pack-redundant.c | 4 ++++ patch-id.c | 4 ++++ unpack-file.c | 4 ++++ update-server-info.c | 4 ++++ var.c | 4 ++++ 15 files changed, 58 insertions(+) diff --git a/daemon.c b/daemon.c index 1cef3098d2..fed789495d 100644 --- a/daemon.c +++ b/daemon.c @@ -949,6 +949,9 @@ int main(int argc, char **argv) gid_t gid = 0; int i; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + for (i = 1; i < argc; i++) { char *arg = argv[i]; diff --git a/fast-import.c b/fast-import.c index a6bce66196..7762205b70 100644 --- a/fast-import.c +++ b/fast-import.c @@ -150,6 +150,7 @@ Format of STDIN stream: #include "refs.h" #include "csum-file.h" #include "quote.h" +#include "exec_cmd.h" #define PACK_ID_BITS 16 #define MAX_PACK_ID ((1< (-a | [--] *)"); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); read_cache(); diff --git a/merge-tree.c b/merge-tree.c index 2d1413efbb..165ccfc608 100644 --- a/merge-tree.c +++ b/merge-tree.c @@ -2,6 +2,7 @@ #include "tree-walk.h" #include "xdiff-interface.h" #include "blob.h" +#include "exec_cmd.h" static const char merge_tree_usage[] = "git-merge-tree "; static int resolve_directories = 1; @@ -344,6 +345,9 @@ int main(int argc, char **argv) if (argc != 4) usage(merge_tree_usage); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); buf1 = get_tree_descriptor(t+0, argv[1]); diff --git a/mktag.c b/mktag.c index ba3d495e07..ff32f8ab04 100644 --- a/mktag.c +++ b/mktag.c @@ -1,5 +1,6 @@ #include "cache.h" #include "tag.h" +#include "exec_cmd.h" /* * A signature file has a very simple fixed format: four lines @@ -159,6 +160,9 @@ int main(int argc, char **argv) if (argc != 1) usage("git-mktag < signaturefile"); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); if (strbuf_read(&buf, 0, 4096) < 0) { diff --git a/mktree.c b/mktree.c index 514fd9b15a..8ac0d576ed 100644 --- a/mktree.c +++ b/mktree.c @@ -6,6 +6,7 @@ #include "cache.h" #include "quote.h" #include "tree.h" +#include "exec_cmd.h" static struct treeent { unsigned mode; @@ -70,6 +71,9 @@ int main(int ac, char **av) unsigned char sha1[20]; int line_termination = '\n'; + if (av[0] && *av[0]) + git_extract_argv0_path(av[0]); + setup_git_directory(); while ((1 < ac) && av[1][0] == '-') { diff --git a/pack-redundant.c b/pack-redundant.c index 25b81a445c..8ff450b319 100644 --- a/pack-redundant.c +++ b/pack-redundant.c @@ -7,6 +7,7 @@ */ #include "cache.h" +#include "exec_cmd.h" #define BLKSIZE 512 @@ -601,6 +602,9 @@ int main(int argc, char **argv) unsigned char *sha1; char buf[42]; /* 40 byte sha1 + \n + \0 */ + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory(); for (i = 1; i < argc; i++) { diff --git a/patch-id.c b/patch-id.c index 871f1d20c0..ff460e282a 100644 --- a/patch-id.c +++ b/patch-id.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "exec_cmd.h" static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c) { @@ -79,6 +80,9 @@ int main(int argc, char **argv) if (argc != 1) usage(patch_id_usage); + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + generate_id_list(); return 0; } diff --git a/unpack-file.c b/unpack-file.c index bcdc8bbb3b..f8bfda7d33 100644 --- a/unpack-file.c +++ b/unpack-file.c @@ -1,5 +1,6 @@ #include "cache.h" #include "blob.h" +#include "exec_cmd.h" static char *create_temp_file(unsigned char *sha1) { @@ -25,6 +26,9 @@ int main(int argc, char **argv) { unsigned char sha1[20]; + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + if (argc != 2) usage("git-unpack-file "); if (get_sha1(argv[1], sha1)) diff --git a/update-server-info.c b/update-server-info.c index 7e8209ea4b..286a4dd517 100644 --- a/update-server-info.c +++ b/update-server-info.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "exec_cmd.h" static const char update_server_info_usage[] = "git update-server-info [--force]"; @@ -19,6 +20,9 @@ int main(int ac, char **av) if (i != ac) usage(update_server_info_usage); + if (av[0] && *av[0]) + git_extract_argv0_path(av[0]); + setup_git_directory(); return !!update_server_info(force); diff --git a/var.c b/var.c index f1eb314e89..33457dc6ab 100644 --- a/var.c +++ b/var.c @@ -4,6 +4,7 @@ * Copyright (C) Eric Biederman, 2005 */ #include "cache.h" +#include "exec_cmd.h" static const char var_usage[] = "git var [-l | ]"; @@ -56,6 +57,9 @@ int main(int argc, char **argv) usage(var_usage); } + if (argv[0] && *argv[0]) + git_extract_argv0_path(argv[0]); + setup_git_directory_gently(&nongit); val = NULL; From 1b14cecc2c4acf2fef0dbd3b95e3b4dfe7246fa9 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 17 Aug 2008 11:30:33 +0200 Subject: [PATCH 162/253] Modify setup_path() to only add git_exec_path() to PATH Searching git programs only in the highest priority location is sufficient. It does not make sense that some of the required programs are located at the highest priority location but other programs are picked up from a lower priority exec-path. If exec-path is overridden a complete set of commands should be provided, otherwise several different versions could get mixed, which is likely to cause confusion. If a user explicitly overrides the default location (by --exec-path or GIT_EXEC_PATH), we now expect that that all the required programs are found there. Instead of adding the directories "argv_exec_path", "getenv(EXEC_PATH_ENVIRONMENT)", and "system_path(GIT_EXEC_PATH)" to PATH, we now rely on git_exec_path(), which implements the same order, but only returns the highest priority location to search for executables. Accessing only the location with highest priority is also required for testing executables built with RUNTIME_PREFIX. The call to system_path() should be avoided if RUNTIME_PREFIX is set and the executable is not installed at its final destination. Because we test before installing, we want to avoid calling system_path() during tests. The modifications brought by this commit avoid calling system_path(GIT_EXEC_PATH) if a higher-priority location is provided, which is the case when running the tests. --- exec_cmd.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index dde5cfd14f..0de03c2bed 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -118,9 +118,7 @@ void setup_path(void) const char *old_path = getenv("PATH"); struct strbuf new_path = STRBUF_INIT; - add_path(&new_path, argv_exec_path); - add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT)); - add_path(&new_path, system_path(GIT_EXEC_PATH)); + add_path(&new_path, git_exec_path()); add_path(&new_path, argv0_path); if (old_path) From e8d0d369da5a9bb6e76846251f27e0f384f490ec Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 16 Aug 2008 16:43:58 +0200 Subject: [PATCH 163/253] Windows: Revert to default paths and convert them by RUNTIME_PREFIX The RUNTIME_PREFIX mechanism allows us to use the default (absolute) paths on Windows too. Defining RUNTIME_PREFIX explicitly requests for translation of paths during runtime, depending on the path to the executable. --- Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Makefile b/Makefile index da17fe950a..2c4a3f0285 100644 --- a/Makefile +++ b/Makefile @@ -789,6 +789,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) SNPRINTF_RETURNS_BOGUS = YesPlease NO_SVN_TESTS = YesPlease NO_PERL_MAKEMAKER = YesPlease + RUNTIME_PREFIX = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch @@ -797,9 +798,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o EXTLIBS += -lws2_32 X = .exe - gitexecdir = ../libexec/git-core - template_dir = ../share/git-core/templates/ - ETC_GITCONFIG = ../etc/gitconfig endif ifneq (,$(findstring arm,$(uname_M))) ARM_SHA1 = YesPlease From d439f6092dff6a5ef9615458addb45eecaf1fa44 Mon Sep 17 00:00:00 2001 From: Petr Kodl Date: Sat, 24 Jan 2009 15:04:39 +0100 Subject: [PATCH 164/253] hardlink implementation for mingw Signed-off-by: Johannes Schindelin --- compat/mingw.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++ compat/mingw.h | 3 +- 2 files changed, 136 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index e25cb4fb23..869bd4acbe 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1003,3 +1003,138 @@ void mingw_open_html(const char *unixpath) printf("Launching default browser to display HTML ...\n"); ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0); } + +static int err_win_to_posix(DWORD winerr) +{ + int error = ENOSYS; + switch(winerr) { + case ERROR_ACCESS_DENIED: error = EACCES; break; + case ERROR_ACCOUNT_DISABLED: error = EACCES; break; + case ERROR_ACCOUNT_RESTRICTION: error = EACCES; break; + case ERROR_ALREADY_ASSIGNED: error = EBUSY; break; + case ERROR_ALREADY_EXISTS: error = EEXIST; break; + case ERROR_ARITHMETIC_OVERFLOW: error = ERANGE; break; + case ERROR_BAD_COMMAND: error = EIO; break; + case ERROR_BAD_DEVICE: error = ENODEV; break; + case ERROR_BAD_DRIVER_LEVEL: error = ENXIO; break; + case ERROR_BAD_EXE_FORMAT: error = ENOEXEC; break; + case ERROR_BAD_FORMAT: error = ENOEXEC; break; + case ERROR_BAD_LENGTH: error = EINVAL; break; + case ERROR_BAD_PATHNAME: error = ENOENT; break; + case ERROR_BAD_PIPE: error = EPIPE; break; + case ERROR_BAD_UNIT: error = ENODEV; break; + case ERROR_BAD_USERNAME: error = EINVAL; break; + case ERROR_BROKEN_PIPE: error = EPIPE; break; + case ERROR_BUFFER_OVERFLOW: error = ENAMETOOLONG; break; + case ERROR_BUSY: error = EBUSY; break; + case ERROR_BUSY_DRIVE: error = EBUSY; break; + case ERROR_CALL_NOT_IMPLEMENTED: error = ENOSYS; break; + case ERROR_CANNOT_MAKE: error = EACCES; break; + case ERROR_CANTOPEN: error = EIO; break; + case ERROR_CANTREAD: error = EIO; break; + case ERROR_CANTWRITE: error = EIO; break; + case ERROR_CRC: error = EIO; break; + case ERROR_CURRENT_DIRECTORY: error = EACCES; break; + case ERROR_DEVICE_IN_USE: error = EBUSY; break; + case ERROR_DEV_NOT_EXIST: error = ENODEV; break; + case ERROR_DIRECTORY: error = EINVAL; break; + case ERROR_DIR_NOT_EMPTY: error = ENOTEMPTY; break; + case ERROR_DISK_CHANGE: error = EIO; break; + case ERROR_DISK_FULL: error = ENOSPC; break; + case ERROR_DRIVE_LOCKED: error = EBUSY; break; + case ERROR_ENVVAR_NOT_FOUND: error = EINVAL; break; + case ERROR_EXE_MARKED_INVALID: error = ENOEXEC; break; + case ERROR_FILENAME_EXCED_RANGE: error = ENAMETOOLONG; break; + case ERROR_FILE_EXISTS: error = EEXIST; break; + case ERROR_FILE_INVALID: error = ENODEV; break; + case ERROR_FILE_NOT_FOUND: error = ENOENT; break; + case ERROR_GEN_FAILURE: error = EIO; break; + case ERROR_HANDLE_DISK_FULL: error = ENOSPC; break; + case ERROR_INSUFFICIENT_BUFFER: error = ENOMEM; break; + case ERROR_INVALID_ACCESS: error = EACCES; break; + case ERROR_INVALID_ADDRESS: error = EFAULT; break; + case ERROR_INVALID_BLOCK: error = EFAULT; break; + case ERROR_INVALID_DATA: error = EINVAL; break; + case ERROR_INVALID_DRIVE: error = ENODEV; break; + case ERROR_INVALID_EXE_SIGNATURE: error = ENOEXEC; break; + case ERROR_INVALID_FLAGS: error = EINVAL; break; + case ERROR_INVALID_FUNCTION: error = ENOSYS; break; + case ERROR_INVALID_HANDLE: error = EBADF; break; + case ERROR_INVALID_LOGON_HOURS: error = EACCES; break; + case ERROR_INVALID_NAME: error = EINVAL; break; + case ERROR_INVALID_OWNER: error = EINVAL; break; + case ERROR_INVALID_PARAMETER: error = EINVAL; break; + case ERROR_INVALID_PASSWORD: error = EPERM; break; + case ERROR_INVALID_PRIMARY_GROUP: error = EINVAL; break; + case ERROR_INVALID_SIGNAL_NUMBER: error = EINVAL; break; + case ERROR_INVALID_TARGET_HANDLE: error = EIO; break; + case ERROR_INVALID_WORKSTATION: error = EACCES; break; + case ERROR_IO_DEVICE: error = EIO; break; + case ERROR_IO_INCOMPLETE: error = EINTR; break; + case ERROR_LOCKED: error = EBUSY; break; + case ERROR_LOCK_VIOLATION: error = EACCES; break; + case ERROR_LOGON_FAILURE: error = EACCES; break; + case ERROR_MAPPED_ALIGNMENT: error = EINVAL; break; + case ERROR_META_EXPANSION_TOO_LONG: error = E2BIG; break; + case ERROR_MORE_DATA: error = EPIPE; break; + case ERROR_NEGATIVE_SEEK: error = ESPIPE; break; + case ERROR_NOACCESS: error = EFAULT; break; + case ERROR_NONE_MAPPED: error = EINVAL; break; + case ERROR_NOT_ENOUGH_MEMORY: error = ENOMEM; break; + case ERROR_NOT_READY: error = EAGAIN; break; + case ERROR_NOT_SAME_DEVICE: error = EXDEV; break; + case ERROR_NO_DATA: error = EPIPE; break; + case ERROR_NO_MORE_SEARCH_HANDLES: error = EIO; break; + case ERROR_NO_PROC_SLOTS: error = EAGAIN; break; + case ERROR_NO_SUCH_PRIVILEGE: error = EACCES; break; + case ERROR_OPEN_FAILED: error = EIO; break; + case ERROR_OPEN_FILES: error = EBUSY; break; + case ERROR_OPERATION_ABORTED: error = EINTR; break; + case ERROR_OUTOFMEMORY: error = ENOMEM; break; + case ERROR_PASSWORD_EXPIRED: error = EACCES; break; + case ERROR_PATH_BUSY: error = EBUSY; break; + case ERROR_PATH_NOT_FOUND: error = ENOENT; break; + case ERROR_PIPE_BUSY: error = EBUSY; break; + case ERROR_PIPE_CONNECTED: error = EPIPE; break; + case ERROR_PIPE_LISTENING: error = EPIPE; break; + case ERROR_PIPE_NOT_CONNECTED: error = EPIPE; break; + case ERROR_PRIVILEGE_NOT_HELD: error = EACCES; break; + case ERROR_READ_FAULT: error = EIO; break; + case ERROR_SEEK: error = EIO; break; + case ERROR_SEEK_ON_DEVICE: error = ESPIPE; break; + case ERROR_SHARING_BUFFER_EXCEEDED: error = ENFILE; break; + case ERROR_SHARING_VIOLATION: error = EACCES; break; + case ERROR_STACK_OVERFLOW: error = ENOMEM; break; + case ERROR_SWAPERROR: error = ENOENT; break; + case ERROR_TOO_MANY_MODULES: error = EMFILE; break; + case ERROR_TOO_MANY_OPEN_FILES: error = EMFILE; break; + case ERROR_UNRECOGNIZED_MEDIA: error = ENXIO; break; + case ERROR_UNRECOGNIZED_VOLUME: error = ENODEV; break; + case ERROR_WAIT_NO_CHILDREN: error = ECHILD; break; + case ERROR_WRITE_FAULT: error = EIO; break; + case ERROR_WRITE_PROTECT: error = EROFS; break; + } + return error; +} + + +int link(const char *oldpath, const char *newpath) +{ + typedef BOOL (*T)(const char*, const char*, LPSECURITY_ATTRIBUTES); + static T create_hard_link = NULL; + if (!create_hard_link) { + create_hard_link = (T) GetProcAddress( + GetModuleHandle("kernel32.dll"), "CreateHardLinkA"); + if (!create_hard_link) + create_hard_link = (T)-1; + } + if (create_hard_link == (T)-1) { + errno = ENOSYS; + return -1; + } + if (!create_hard_link(newpath, oldpath, NULL)) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + return 0; +} diff --git a/compat/mingw.h b/compat/mingw.h index 4f275cb8e6..20c02c7494 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -67,8 +67,6 @@ static inline int readlink(const char *path, char *buf, size_t bufsiz) { errno = ENOSYS; return -1; } static inline int symlink(const char *oldpath, const char *newpath) { errno = ENOSYS; return -1; } -static inline int link(const char *oldpath, const char *newpath) -{ errno = ENOSYS; return -1; } static inline int fchmod(int fildes, mode_t mode) { errno = ENOSYS; return -1; } static inline int fork(void) @@ -134,6 +132,7 @@ int getpagesize(void); /* defined in MinGW's libgcc.a */ struct passwd *getpwuid(int uid); int setitimer(int type, struct itimerval *in, struct itimerval *out); int sigaction(int sig, struct sigaction *in, struct sigaction *out); +int link(const char *oldpath, const char *newpath); /* * replacements of existing functions From db75811cfaec90fa8a114f46c84d408f5e6ca7c9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 5 Feb 2009 17:15:31 +0100 Subject: [PATCH 165/253] MinGW tests: override 'pwd', too With MinGW based Git, we must not use the POSIXified version of paths. So we need to use 'pwd -W' instead of 'pwd' without options. Signed-off-by: Johannes Schindelin --- t/t5505-remote.sh | 3 --- t/test-lib.sh | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index b4dc0d2fbe..1f59960d90 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -107,9 +107,6 @@ test_expect_success 'remove remote' ' ) ' -case $(uname -s) in -*MINGW*) pwd() { builtin pwd -W; } ;; -esac cat > test/expect << EOF * remote origin URL: $(pwd)/one diff --git a/t/test-lib.sh b/t/test-lib.sh index cce3636c1b..7a5f77b765 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -536,5 +536,8 @@ case $(uname -s) in find () { /usr/bin/find "$@" } + pwd() { + builtin pwd -W + } ;; esac From ea81ef5fea2fb40e665171ac30a3bfb3468e87b8 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 7 Feb 2009 00:03:47 +0100 Subject: [PATCH 166/253] builtin-receive-pack: Remove leftovers of old RUNTIME_PREFIX implementation --- builtin-receive-pack.c | 3 --- merge-index.c | 2 -- mktag.c | 2 -- 3 files changed, 7 deletions(-) diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c index 8da9c1ff3a..6de186c397 100644 --- a/builtin-receive-pack.c +++ b/builtin-receive-pack.c @@ -607,9 +607,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) int i; char *dir = NULL; - if (argv[0] && *argv[0]) - git_extract_argv0_path(argv[0]); - argv++; for (i = 1; i < argc; i++) { const char *arg = *argv++; diff --git a/merge-index.c b/merge-index.c index 4c51b14eab..aa9cf23a39 100644 --- a/merge-index.c +++ b/merge-index.c @@ -96,8 +96,6 @@ int main(int argc, char **argv) git_extract_argv0_path(argv[0]); - git_extract_argv0_path(argv[0]); - setup_git_directory(); read_cache(); diff --git a/mktag.c b/mktag.c index 21370328e2..99a356e9ee 100644 --- a/mktag.c +++ b/mktag.c @@ -162,8 +162,6 @@ int main(int argc, char **argv) git_extract_argv0_path(argv[0]); - git_extract_argv0_path(argv[0]); - setup_git_directory(); if (strbuf_read(&buf, 0, 4096) < 0) { From 89012a7916918de08031e7c415afd79d9e68dce2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 18 Feb 2009 16:22:36 +0100 Subject: [PATCH 167/253] Remove last remnant of NOEXECTEMPL Upstream Git learnt the trick from us, but decided to call it 'sample' instead of 'noexec'. Signed-off-by: Johannes Schindelin --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index a9bbf3d8a3..be00623c81 100644 --- a/Makefile +++ b/Makefile @@ -809,7 +809,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o EXTLIBS += -lws2_32 X = .exe - NOEXECTEMPL = .noexec htmldir=doc/git/html/ endif ifneq (,$(findstring arm,$(uname_M))) From eebdca3a654bfb95018514d5b5b8803c1fcd3343 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 19:51:54 +0100 Subject: [PATCH 168/253] Revert unnecessary quoting of SHELL_PATH In bf788d7(Makefile: be nice when running in a path containing spaces), the Makefile was changed so that the SHELL_PATH is quoted, but that is actually not necessary, as we still have POSIX paths at that point, and we use /bin/sh which has no space in it. Signed-off-by: Johannes Schindelin --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index be00623c81..0b0628a98c 100644 --- a/Makefile +++ b/Makefile @@ -161,7 +161,7 @@ all:: # broken, or spawning external process is slower than built-in grep git has). GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE - @"$(SHELL_PATH)" ./GIT-VERSION-GEN + @$(SHELL_PATH) ./GIT-VERSION-GEN -include GIT-VERSION-FILE uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') From e673e45f5893fc969b58c81553832efd8cfd0435 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 19:54:24 +0100 Subject: [PATCH 169/253] MinGW: use POSIXy signature of waitpid() Git's source code expects waitpid() to return a signed int status. Signed-off-by: Johannes Schindelin --- compat/mingw.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/mingw.h b/compat/mingw.h index d6005ad545..6219e18843 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -109,7 +109,7 @@ static inline int mingw_unlink(const char *pathname) } #define unlink mingw_unlink -static inline int waitpid(pid_t pid, unsigned *status, unsigned options) +static inline int waitpid(pid_t pid, int *status, unsigned options) { if (options == 0) return _cwait(status, pid, 0); From 6d3e71d428b1ee007339d679c26a336c8aefeade Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 19:56:41 +0100 Subject: [PATCH 170/253] MinGW: only override PRIuMAX for GCC older than version 4 GCC has no problems with PRIuMAX with recent GCC versions, but these versions do not like I64u any longer. Signed-off-by: Johannes Schindelin --- compat/mingw.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compat/mingw.h b/compat/mingw.h index 6219e18843..8898006278 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -200,7 +200,9 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') #define is_dir_sep(c) ((c) == '/' || (c) == '\\') #define PATH_SEP ';' +#if !defined(__GNUC__) || __GNUC__ < 3 #define PRIuMAX "I64u" +#endif void mingw_open_html(const char *path); #define open_html mingw_open_html From 85f2fd02bd2602e10415bf8aa4e085a9658e2d18 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:12:41 +0100 Subject: [PATCH 171/253] Makefile: move the last MinGW specific settings to the appropriate place If we ever hope to use plain upstream git.git in msysGit, we need to play nice with other platforms. So move the MinGW specific settings where they do not affect anybody else. Signed-off-by: Johannes Schindelin --- Makefile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 0b0628a98c..68c2d846dd 100644 --- a/Makefile +++ b/Makefile @@ -191,7 +191,7 @@ STRIP ?= strip # runtime figures out where they are based on the path to the executable. # This can help installing the suite in a relocatable way. -prefix = +prefix = $(HOME) bindir_relative = bin bindir = $(prefix)/$(bindir_relative) mandir = share/man @@ -235,7 +235,7 @@ AR = ar RM = rm -f TAR = tar FIND = find -INSTALL = /bin/install +INSTALL = install RPMBUILD = rpmbuild TCL_PATH = tclsh TCLTK_PATH = wish @@ -592,7 +592,7 @@ BUILTIN_OBJS += builtin-verify-tag.o BUILTIN_OBJS += builtin-write-tree.o GITLIBS = $(LIB_FILE) $(XDIFF_LIB) -EXTLIBS = /mingw/lib/libz.a +EXTLIBS = # # Platform specific tweaks @@ -779,6 +779,9 @@ ifneq (,$(findstring CYGWIN,$(uname_S))) COMPAT_OBJS += compat/cygwin.o endif ifneq (,$(findstring MINGW,$(uname_S))) + prefix = + INSTALL = /bin/install + EXTLIBS += /mingw/lib/libz.a NO_MMAP = YesPlease NO_PREAD = YesPlease NO_OPENSSL = YesPlease From 5461f4e7863a655a6f5792edc8dc9768a62948e7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:22:14 +0100 Subject: [PATCH 172/253] Undo MSys specific munging of ssh transport URLs We have our own msys-1.0.dll, which needs no more munging of the ssh transport URLs. Besides, almost everything related to remotes is builtin by now. Signed-off-by: Johannes Schindelin --- git-parse-remote.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) mode change 100644 => 100755 git-parse-remote.sh diff --git a/git-parse-remote.sh b/git-parse-remote.sh old mode 100644 new mode 100755 index 7c0d242eec..695a4094bb --- a/git-parse-remote.sh +++ b/git-parse-remote.sh @@ -51,8 +51,7 @@ get_remote_url () { ;; *) die "internal error: get-remote-url $1" ;; - esac | sed "s|^\(.[^:][^:]*\):\(/[^/]\)|ssh://\1\2|" - # work around MinGW path mangling + esac } get_default_remote () { From 8d785de3c205c870f6ce3e459c27e3f2b849b1a7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:25:13 +0100 Subject: [PATCH 173/253] gitk: remove workaround for missing msgfmt In the meantime, gitk's Makefile has learnt to detect the absence of msgfmt and use the drop-in replacement in Tcl (which was developed for msysGit, but has been adopted both by gitk and git-gui). Signed-off-by: Johannes Schindelin --- gitk-git/Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/gitk-git/Makefile b/gitk-git/Makefile index 2dda57cbae..e1b6045605 100644 --- a/gitk-git/Makefile +++ b/gitk-git/Makefile @@ -19,10 +19,6 @@ TCLTK_PATH_SQ = $(subst ','\'',$(TCLTK_PATH)) ## po-file creation rules XGETTEXT ?= xgettext -uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') -ifneq (,$(findstring MINGW,$(uname_S))) - NO_MSGFMT=1 -endif ifdef NO_MSGFMT MSGFMT ?= $(TCL_PATH) po/po2msg.sh else From 7cb52f1ebf546159e5460db087d9343d1b4139ed Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:28:26 +0100 Subject: [PATCH 174/253] No longer force use of /usr/bin/sort In msysGit, the PATH contains the MSys paths before anything else, so we can use "sort" (instead of "/usr/bin/sort"), just like git.git. Signed-off-by: Johannes Schindelin --- check-builtins.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-builtins.sh b/check-builtins.sh index cd09309838..d6fe6cf174 100755 --- a/check-builtins.sh +++ b/check-builtins.sh @@ -9,7 +9,7 @@ EOF } | make -f - sayIt 2>/dev/null | sed -n -e 's/.*XXX \(.*\) YYY.*/\1/p' | -/usr/bin/sort | +sort | { bad=0 while read builtin From c09458c701353b8e0ad92fe2d0245636e17cb8b8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:29:49 +0100 Subject: [PATCH 175/253] Remove hopelessly obsolete README.MinGW Rather than letting it get even more stale, remove README.MinGW. Signed-off-by: Johannes Schindelin --- README.MinGW | 77 ---------------------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 README.MinGW diff --git a/README.MinGW b/README.MinGW deleted file mode 100644 index 74eb104c2c..0000000000 --- a/README.MinGW +++ /dev/null @@ -1,77 +0,0 @@ -This is a port of GIT to Windows using MinGW. - -The goal of this port is to have the tools runnable from the Windows -command line, not from MinGW's rxvt+bash environment. - -(Note: This file was written after-the-fact and may contain errors. -If you are trying this, please make notes and update it.) - - -INSTALLATION ------------- - -In order to compile this code you need: - -- MSYS, e.g. MSYS-1.0.11-2004.04.30-1.exe -- MinGW, e.g. MinGW-5.0.2.exe -- mingwruntime, e.g. mingw-runtime-3.9.tar.gz -- compilers and tools: - binutils-2.15.91-20040904-1.tar.gz - gcc-core-3.4.2-20040916-1.tar.gz - gcc-g++-3.4.2-20040916-1.tar.gz - gdb-6.3-2.exe - mingw32-make-3.80.0-3.tar.gz - unzip-5.51-1-bin.zip (this is not from MinGW, iirc) - msysDTK-1.0.1.exe (contains ssh, perl) - bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 -- additional libraries: - zlib-1.2.3-mingwPORT-1.tar - w32api-3.6.tar.gz - tcltk-8.4.1-1.exe (for gitk, git-gui) - libiconv-1.9.2-1-{lib,bin,lib}.zip (for git-am, - from http://gnuwin32.sourceforge.net/packages/libiconv.htm) - -I am using these settings in config.mak to have pointers to the right tools: - -TCL_PATH=tclsh84 -TCLTK_PATH=wish84 -SHELL_PATH=D:/MSYS/1.0/bin/sh -PERL_PATH=D:/MSYS/1.0/bin/perl - - -STATUS ------- - -This code base will not compile on a POSIX system, although any help -to introduce the necessary #ifdefs is welcome. As such the status quo -is in no way intended to be merged upstream. - -This works: - -- All the plumbings. -- Many porcelains, in particular, checkout, add, rm, commit, diff, - branch, merge, rebase, log, show, bisect, grep... -- pull, clone, fetch, push via native git protocal as well as ssh. -- Local pull, clone, fetch, push. -- gitk, if invoked as "wish84 \bin\gitk". Ditto for git-gui. - -This does not work: - -- daemon, svn, *import, cvs* -- and certainly a lot more that I never have found a need to look at. - -Caveats (aka bugs): - -- Internally, the ported tools must do their own command line quoting - when other plumbings are executed. This sort of quoting is currently - implemented *very* simplistic: It just watches out for whitespace - and double quote `"' characters. This may become a problem if you have - exotic characters in your file names. - -- It seems that MSYS's default bash, 2.05b, has a bug that breaks git-am - (and, hence, git-rebase). If you see this error: - - Patch is empty. Was is split wrong? - - you need bash 3.1: Just unpack bash-3.1*.tar.bz2 and copy its bash.exe - over $MSYS/bin/sh.exe. From 3cb35a31b04fb8e5cbfda3affcaa87dfee620355 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:32:47 +0100 Subject: [PATCH 176/253] MinGW: use off64_t and lseek64() On Windows, off_t is restricted to 32-bit, even if it is perfectly capable of using 64-bit offsets; Force the use of 64-bit offsets by overriding the declarations of off_t and lseek() with their 64-bit counterparts. Signed-off-by: Johannes Schindelin --- compat/mingw.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compat/mingw.h b/compat/mingw.h index 8898006278..bfdfdb4940 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -117,6 +117,9 @@ static inline int waitpid(pid_t pid, int *status, unsigned options) return -1; } +#define off_t off64_t +#define lseek lseek64 + /* * implementations of missing functions */ From b824fc9cefb0317b98f6fc34c1192d409e859ea2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 18 Feb 2009 15:32:55 +0100 Subject: [PATCH 177/253] Introduce the function strip_path_suffix() The function strip_path_suffix() will try to strip a given suffix from a given path. The suffix must start at a directory boundary (i.e. "core" is not a path suffix of "libexec/git-core", but "git-core" is). Arbitrary runs of directory separators ("slashes") are assumed identical. Example: prefix = strip_path_suffix("C:\\msysgit/\\libexec\\git-core", "libexec///git-core"); will set prefix to "C:\\msysgit". Signed-off-by: Johannes Schindelin --- cache.h | 1 + path.c | 32 ++++++++++++++++++++++++++++++++ t/t0060-path-utils.sh | 4 ++++ test-path-utils.c | 5 +++++ 4 files changed, 42 insertions(+) diff --git a/cache.h b/cache.h index 2d889deb26..362116dc74 100644 --- a/cache.h +++ b/cache.h @@ -629,6 +629,7 @@ const char *make_nonrelative_path(const char *path); const char *make_relative_path(const char *abs, const char *base); int normalize_absolute_path(char *buf, const char *path); int longest_ancestor_length(const char *path, const char *prefix_list); +char *strip_path_suffix(const char *path, const char *suffix); /* Read and unpack a sha1 file into memory, write memory to a sha1 file */ extern int sha1_object_info(const unsigned char *, unsigned long *); diff --git a/path.c b/path.c index 108d9e9599..719be21e75 100644 --- a/path.c +++ b/path.c @@ -457,3 +457,35 @@ int longest_ancestor_length(const char *path, const char *prefix_list) return max_len; } + +/* strip arbitrary amount of directory separators at end of path */ +static inline int chomp_trailing_dir_sep(const char *path, int len) +{ + while (len && is_dir_sep(path[len - 1])) + len--; + return len; +} + +/* sets prefix if the suffix matches */ +char *strip_path_suffix(const char *path, const char *suffix) +{ + int path_len = strlen(path), suffix_len = strlen(suffix); + + while (suffix_len) { + if (!path_len) + return NULL; + + if (is_dir_sep(path[path_len - 1])) { + if (!is_dir_sep(suffix[suffix_len - 1])) + return NULL; + path_len = chomp_trailing_dir_sep(path, path_len); + suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); + } + else if (path[--path_len] != suffix[--suffix_len]) + return NULL; + } + + if (path_len && !is_dir_sep(path[path_len - 1])) + return NULL; + return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); +} diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 6e7501f352..1df1d8948a 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -84,4 +84,8 @@ ancestor /foo/bar :://foo/.:: 4 ancestor /foo/bar //foo/./::/bar 4 ancestor /foo/bar ::/bar -1 +test_expect_success 'strip_path_suffix' ' + test c:/msysgit = $(test-path-utils strip_path_suffix \ + c:/msysgit/libexec//git-core libexec/git-core) +' test_done diff --git a/test-path-utils.c b/test-path-utils.c index 2c0f5a37e8..003680dcda 100644 --- a/test-path-utils.c +++ b/test-path-utils.c @@ -22,5 +22,10 @@ int main(int argc, char **argv) printf("%d\n", len); } + if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) { + char *prefix = strip_path_suffix(argv[2], argv[3]); + printf("%s\n", prefix ? prefix : "(null)"); + } + return 0; } From 047f0f01f21824cc925102ad4e8e623c4806e0aa Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 18 Feb 2009 15:41:37 +0100 Subject: [PATCH 178/253] system_path(): simplify using strip_path_suffix(), and add suffix "git" At least for the author of this patch, the logic in system_path() was too hard to understand. Using the function strip_path_suffix() documents the idea of the code better. The real change is to add the suffix "git", so that a runtime prefix will be computed correctly even when the executable was called in /git/ as is the case in msysGit (Windows insists to search the current directory before the PATH when looking for an executable). Signed-off-by: Johannes Schindelin --- exec_cmd.c | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index f234066def..217c12577f 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -23,35 +23,10 @@ const char *system_path(const char *path) assert(argv0_path); assert(is_absolute_path(argv0_path)); - if (!prefix) { - const char *strip[] = { - GIT_EXEC_PATH, - BINDIR, - 0 - }; - const char **s; - - for (s = strip; *s; s++) { - const char *sargv = argv0_path + strlen(argv0_path); - const char *ss = *s + strlen(*s); - while (argv0_path < sargv && *s < ss - && (*sargv == *ss || - (is_dir_sep(*sargv) && is_dir_sep(*ss)))) { - sargv--; - ss--; - } - if (*s == ss) { - struct strbuf d = STRBUF_INIT; - /* We also skip the trailing directory separator. */ - assert(sargv - argv0_path - 1 >= 0); - strbuf_add(&d, argv0_path, sargv - argv0_path - 1); - prefix = strbuf_detach(&d, NULL); - break; - } - } - } - - if (!prefix) { + if (!prefix && + !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) && + !(prefix = strip_path_suffix(argv0_path, BINDIR)) && + !(prefix = strip_path_suffix(argv0_path, "git"))) { prefix = PREFIX; fprintf(stderr, "RUNTIME_PREFIX requested, " "but prefix computation failed. " From fc8f39c0dad332a7c909d5a379eb01d594cf3594 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 19 Feb 2009 21:40:10 +0100 Subject: [PATCH 179/253] strip_path_suffix(): fix comment Signed-off-by: Johannes Schindelin --- path.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/path.c b/path.c index 719be21e75..b31d0e1fb1 100644 --- a/path.c +++ b/path.c @@ -466,7 +466,11 @@ static inline int chomp_trailing_dir_sep(const char *path, int len) return len; } -/* sets prefix if the suffix matches */ +/* + * If path ends with suffix (complete path components), returns the + * part before suffix (sans trailing directory separators). + * Otherwise returns NULL. + */ char *strip_path_suffix(const char *path, const char *suffix) { int path_len = strlen(path), suffix_len = strlen(suffix); From 96fd02ed68fb068ba75d980bcd280bbdb5169bba Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 4 Mar 2009 19:15:02 +0100 Subject: [PATCH 180/253] Revert "MinGW: use off64_t and lseek64()" This reverts commit 3cb35a31b04fb8e5cbfda3affcaa87dfee620355. The fix was incorrect. Signed-off-by: Johannes Schindelin --- compat/mingw.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/compat/mingw.h b/compat/mingw.h index bfdfdb4940..8898006278 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -117,9 +117,6 @@ static inline int waitpid(pid_t pid, int *status, unsigned options) return -1; } -#define off_t off64_t -#define lseek lseek64 - /* * implementations of missing functions */ From ff9031617a6b8e3f49d2362d91e34029dace5ce2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 4 Mar 2009 18:46:54 +0100 Subject: [PATCH 181/253] Quiet make: do not leave Windows behind On Windows, we have to check whether there are scripts which would override .exe files, but this check missed the "quietification". Signed-off-by: Johannes Schindelin --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 105c31f51d..3c54b629df 100644 --- a/Makefile +++ b/Makefile @@ -1139,7 +1139,7 @@ SHELL = $(SHELL_PATH) all:: shell_compatibility_test $(ALL_PROGRAMS) $(BUILT_INS) $(OTHER_PROGRAMS) GIT-BUILD-OPTIONS ifneq (,$X) - $(foreach p,$(patsubst %$X,%,$(filter %$X,$(ALL_PROGRAMS) $(BUILT_INS) git$X)), test '$p' -ef '$p$X' || $(RM) '$p';) + $(QUIET_BUILT_IN)$(foreach p,$(patsubst %$X,%,$(filter %$X,$(ALL_PROGRAMS) $(BUILT_INS) git$X)), test '$p' -ef '$p$X' || $(RM) '$p';) endif all:: From f30905f5dd684e4ca57408f4e8f512d46791702f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 4 Mar 2009 18:34:42 +0100 Subject: [PATCH 182/253] Add an (optional, since expensive) test for >2gb clones Define GIT_TEST_CLONE_2GB=t if you want the test not to be skipped. The test works by constructing a repository larger than 2gb, and then cloning it. The repository is forced larger than 2gb by setting compression and delta depth to zero, and then adding just enough unique objects of a given size. The objects consist of a running decimal number in ASCII, padded by spaces. Should that break in the future, e.g. when pack v4 becomes default, there is a commented-out call to test-genrandom which can be substituted, but that uses more cycles than the current method. Signed-off-by: Johannes Schindelin --- t/t5705-clone-2gb.sh | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 t/t5705-clone-2gb.sh diff --git a/t/t5705-clone-2gb.sh b/t/t5705-clone-2gb.sh new file mode 100755 index 0000000000..9f52154cac --- /dev/null +++ b/t/t5705-clone-2gb.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +test_description='Test cloning a repository larger than 2 gigabyte' +. ./test-lib.sh + +test -z "$GIT_TEST_CLONE_2GB" && +say "Skipping expensive 2GB clone test; enable it with GIT_TEST_CLONE_2GB=t" && +test_done && +exit + +test_expect_success 'setup' ' + + git config pack.compression 0 && + git config pack.depth 0 && + blobsize=$((20*1024*1024)) && + blobcount=$((2*1024*1024*1024/$blobsize+1)) && + i=1 && + (while test $i -le $blobcount + do + printf "Generating blob $i/$blobcount\r" >&2 && + printf "blob\nmark :$i\ndata $blobsize\n" && + #test-genrandom $i $blobsize && + printf "%-${blobsize}s" $i && + echo "M 100644 :$i $i" >> commit + i=$(($i+1)) || + echo $? > exit-status + done && + echo "commit refs/heads/master" && + echo "author A U Thor 123456789 +0000" && + echo "committer C O Mitter 123456789 +0000" && + echo "data 5" && + echo ">2gb" && + cat commit) | + git fast-import && + test ! -f exit-status + +' + +test_expect_success 'clone' ' + + git clone --bare --no-hardlinks . clone + +' + +test_done From 081c091d507cc2e7b8f318d9e999335d9e96caf8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 4 Mar 2009 19:15:02 +0100 Subject: [PATCH 183/253] MinGW: 64-bit file offsets The type 'off_t' should be used everywhere so that the bit-depth of that type can be adjusted in the standard C library, and you just need to recompile your program to benefit from the extended precision. Only that it was not done that way in the MS runtime library. This patch reroutes off_t to off64_t and provides the other necessary changes so that finally, clones larger than 2 gigabyte work on Windows (provided you are on a file system that allows files larger than 2gb). Initial patch by Sickboy . Signed-off-by: Johannes Schindelin --- compat/mingw.c | 8 +++++--- compat/mingw.h | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 869bd4acbe..c011754023 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -46,7 +46,8 @@ static int do_lstat(const char *file_name, struct stat *buf) buf->st_uid = 0; buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); - buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */ + buf->st_size = fdata.nFileSizeLow | + (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); @@ -101,7 +102,7 @@ int mingw_fstat(int fd, struct stat *buf) } /* direct non-file handles to MS's fstat() */ if (GetFileType(fh) != FILE_TYPE_DISK) - return fstat(fd, buf); + return _fstati64(fd, buf); if (GetFileInformationByHandle(fh, &fdata)) { buf->st_ino = 0; @@ -109,7 +110,8 @@ int mingw_fstat(int fd, struct stat *buf) buf->st_uid = 0; buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); - buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */ + buf->st_size = fdata.nFileSizeLow | + (((off_t)fdata.nFileSizeHigh)<<32); buf->st_dev = buf->st_rdev = 0; /* not used by Git */ buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); diff --git a/compat/mingw.h b/compat/mingw.h index 8898006278..92fb3103c2 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -162,11 +162,14 @@ int mingw_rename(const char*, const char*); /* Use mingw_lstat() instead of lstat()/stat() and * mingw_fstat() instead of fstat() on Windows. */ +#define off_t off64_t +#define stat _stati64 +#define lseek _lseeki64 int mingw_lstat(const char *file_name, struct stat *buf); int mingw_fstat(int fd, struct stat *buf); #define fstat mingw_fstat #define lstat mingw_lstat -#define stat(x,y) mingw_lstat(x,y) +#define stat64(x,y) mingw_lstat(x,y) int mingw_utime(const char *file_name, const struct utimbuf *times); #define utime mingw_utime From fc048d74273cbf98c412916dd3332d03bf334c79 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 15:19:39 +0100 Subject: [PATCH 184/253] Brown paper bag fix for MinGW 64-bit stat When overriding the identifier "stat" so that "struct stat" will be substituted with "struct _stati64" everywhere, I tried to fix the calls to the _function_ stat(), too, but I forgot to change the earlier attempt "stat64" to "_stati64" there. So, the stat() calls were overridden by calls to _stati64() instead. Unfortunately, there is a function _stati64() so that I missed that calls to stat() were not actually overridden by calls to mingw_lstat(), but t4200-rerere.sh showed the error. Signed-off-by: Johannes Schindelin --- compat/mingw.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/mingw.h b/compat/mingw.h index 92fb3103c2..a0b74fb7ca 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -169,7 +169,7 @@ int mingw_lstat(const char *file_name, struct stat *buf); int mingw_fstat(int fd, struct stat *buf); #define fstat mingw_fstat #define lstat mingw_lstat -#define stat64(x,y) mingw_lstat(x,y) +#define _stati64(x,y) mingw_lstat(x,y) int mingw_utime(const char *file_name, const struct utimbuf *times); #define utime mingw_utime From dba002b9e521c639847650fbaeb8b87b66b9562e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 16:36:09 +0100 Subject: [PATCH 185/253] MinGW: fix diff --no-index /dev/null ... When launching "diff --no-index" with a parameter "/dev/null", the MSys bash converts the "/dev/null" to a "nul", which usually makes sense. But diff --no-index got confused and tried to access a _file_ called "nul". While at it, the comment in t4012, expressed as ":# " was turned into ": " so that MSys' path name mangling does not kick in. With this patch, t4012 passes in msysGit. Signed-off-by: Johannes Schindelin --- diff-no-index.c | 4 ++++ t/t4012-diff-binary.sh | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/diff-no-index.c b/diff-no-index.c index 0a14268ba9..598687b50a 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -38,6 +38,10 @@ static int get_mode(const char *path, int *mode) if (!path || !strcmp(path, "/dev/null")) *mode = 0; +#ifdef _WIN32 + else if (!strcasecmp(path, "nul")) + *mode = 0; +#endif else if (!strcmp(path, "-")) *mode = create_ce_mode(0666); else if (lstat(path, &st)) diff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh index 3cf5b5c4ea..f64aa48d24 100755 --- a/t/t4012-diff-binary.sh +++ b/t/t4012-diff-binary.sh @@ -87,7 +87,7 @@ nul_to_q() { test_expect_success 'diff --no-index with binary creation' ' echo Q | q_to_nul >binary && - (:# hide error code from diff, which just indicates differences + (: hide error code from diff, which just indicates differences git diff --binary --no-index /dev/null binary >current || true ) && From 23c5477dfb63cee46806b9f9c8d9d73d93db776e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 17:40:15 +0100 Subject: [PATCH 186/253] t4129: skip tests if core.filemode = false If we cannot test file modes, we cannot test file modes. Signed-off-by: Johannes Schindelin --- t/t4129-apply-samemode.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/t/t4129-apply-samemode.sh b/t/t4129-apply-samemode.sh index adfcbb5a3b..a74101abba 100755 --- a/t/t4129-apply-samemode.sh +++ b/t/t4129-apply-samemode.sh @@ -16,6 +16,11 @@ test_expect_success setup ' git diff --stat -p >patch-1.txt ' +test "$(git config --bool core.filemode)" = false && +say "executable bit not honored - skipping" && +test_done && +exit + test_expect_success 'same mode (no index)' ' git reset --hard && chmod +x file && From 3a76fdaf98efbb1dc2f71352c811ab6d2710b74f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 19:12:37 +0100 Subject: [PATCH 187/253] MinGW: GCC >= 4 does not need SNPRINTF_SIZE_CORR anymore Signed-off-by: Johannes Schindelin --- Makefile | 1 - compat/snprintf.c | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3c54b629df..801303ebc8 100644 --- a/Makefile +++ b/Makefile @@ -804,7 +804,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_POSIX_ONLY_PROGRAMS = YesPlease NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch - COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o EXTLIBS += -lws2_32 diff --git a/compat/snprintf.c b/compat/snprintf.c index 357e733074..7d780508a7 100644 --- a/compat/snprintf.c +++ b/compat/snprintf.c @@ -6,8 +6,12 @@ * number of characters to write without the trailing NUL. */ #ifndef SNPRINTF_SIZE_CORR +#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ < 3 +#define SNPRINTF_SIZE_CORR 1 +#else #define SNPRINTF_SIZE_CORR 0 #endif +#endif #undef vsnprintf int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) From 3d27683b91df7d4fd6b70e32ed12b08721a46dbd Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 19:44:03 +0100 Subject: [PATCH 188/253] Revert "MinGW: only override PRIuMAX for GCC older than version 4" This reverts commit 6d3e71d428b1ee007339d679c26a336c8aefeade. Apparently, GCC >= 4 does not like %I64u as modifier to show unsigned long longs. However, using %llu -- even if the compiler warning goes away -- fails spectacularly: The C runtime insists that %llu takes 32-bit numbers. So for now, we _have_ to live with compiler warnings galore. Signed-off-by: Johannes Schindelin --- compat/mingw.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/compat/mingw.h b/compat/mingw.h index a0b74fb7ca..b82903c74a 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -203,9 +203,7 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') #define is_dir_sep(c) ((c) == '/' || (c) == '\\') #define PATH_SEP ';' -#if !defined(__GNUC__) || __GNUC__ < 3 #define PRIuMAX "I64u" -#endif void mingw_open_html(const char *path); #define open_html mingw_open_html From 04f4f675ae629eccb30be521868d9c1698d56170 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 20:29:54 +0100 Subject: [PATCH 189/253] MinGW: t6200 needs to understand Windows paths Signed-off-by: Johannes Schindelin --- t/t6200-fmt-merge-msg.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t6200-fmt-merge-msg.sh b/t/t6200-fmt-merge-msg.sh index 8f5a06f7dd..763c46038a 100755 --- a/t/t6200-fmt-merge-msg.sh +++ b/t/t6200-fmt-merge-msg.sh @@ -82,6 +82,7 @@ test_expect_success 'merge-msg test #1' ' test_cmp expected actual ' +TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) cat >expected < Date: Sat, 7 Mar 2009 20:48:08 +0100 Subject: [PATCH 190/253] MinGW: the path separator to split GITPERLLIB is ';' on Win32 Signed-off-by: Johannes Schindelin --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 801303ebc8..995a11c9f7 100644 --- a/Makefile +++ b/Makefile @@ -209,6 +209,7 @@ ETC_GITCONFIG = etc/gitconfig endif lib = lib # DESTDIR= +pathsep = : # default configuration for gitweb GITWEB_CONFIG = gitweb_config.perl @@ -776,6 +777,7 @@ ifneq (,$(findstring CYGWIN,$(uname_S))) COMPAT_OBJS += compat/cygwin.o endif ifneq (,$(findstring MINGW,$(uname_S))) + pathsep = ; prefix = INSTALL = /bin/install EXTLIBS += /mingw/lib/libz.a @@ -1204,7 +1206,7 @@ $(patsubst %.perl,%,$(SCRIPT_PERL)): % : %.perl sed -e '1{' \ -e ' s|#!.*perl|#!$(PERL_PATH_SQ)|' \ -e ' h' \ - -e ' s=.*=use lib (split(/:/, $$ENV{GITPERLLIB} || "@@INSTLIBDIR@@"));=' \ + -e ' s=.*=use lib (split(/$(pathsep)/, $$ENV{GITPERLLIB} || "@@INSTLIBDIR@@"));=' \ -e ' H' \ -e ' x' \ -e '}' \ From d7bc3dd8f7a2091e9d7dec69bed96d53653d9a79 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 7 Mar 2009 23:12:43 +0100 Subject: [PATCH 191/253] t5520: do not skip test that does not fail anymore Signed-off-by: Johannes Schindelin --- t/t5520-pull.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh index 8377733f7b..725771fac1 100755 --- a/t/t5520-pull.sh +++ b/t/t5520-pull.sh @@ -88,7 +88,6 @@ test_expect_success 'branch.to-rebase.rebase' ' test new = $(git show HEAD:file2) ' -say "Remote does not work in t/ - skipping." || test_expect_success '--rebase with rebased upstream' ' git remote add -f me . && From b1744a5fde45def23aa5778bf4a22df8f070422d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 11 Mar 2009 17:58:32 +0100 Subject: [PATCH 192/253] t7300: fix clean up on Windows On Windows, you cannot remove files that are in use, not even with 'rm -rf'. So we need to run 'exec --- t/t7300-clean.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh index 1636fac2a4..929d5d4d3b 100755 --- a/t/t7300-clean.sh +++ b/t/t7300-clean.sh @@ -373,9 +373,9 @@ test_expect_success 'removal failure' ' mkdir foo && touch foo/bar && - exec Date: Thu, 12 Mar 2009 15:25:10 +0100 Subject: [PATCH 193/253] MinGW: fix signature of CreateHardLinkA() If the declaration of CreateHardLinkA() does not contain the magic word "WINAPI", the stack will be clobbered in the error case. This fixes msysGit issue 204. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index c011754023..71e12dd478 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1122,7 +1122,7 @@ static int err_win_to_posix(DWORD winerr) int link(const char *oldpath, const char *newpath) { - typedef BOOL (*T)(const char*, const char*, LPSECURITY_ATTRIBUTES); + typedef BOOL WINAPI (*T)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES); static T create_hard_link = NULL; if (!create_hard_link) { create_hard_link = (T) GetProcAddress( From 7e5a4c563667d011c0655679be3190461884a78e Mon Sep 17 00:00:00 2001 From: Janos Laube Date: Sat, 7 Mar 2009 03:54:10 +0100 Subject: [PATCH 194/253] support for win32 mmap Add USE_WIN32_MMAP which triggers the use of windows' native file memory mapping functionality in git_mmap()/git_munmap() functions. As git functions currently use mmap with MAP_PRIVATE set only, this implementation supports only that mode for now. On Windows offsets for memory mapped files need to match the allocation granularity. Take this into account when calculating the packed git- windowsize and file offsets. At the moment the only function which makes use of offsets in conjunction with mmap is use_pack() in sha1-file.c. Git fast-import's code path tries to map a portion of the temporary packfile which exceeds the current filesize, i.e. offset+length is greater than the filesize. The NO_MMAP code worked with that since pread() just reads the filecontent until EOF and returns gracefully, while MapViewOfFile() aborts the mapping and returns 'Access Denied'. Working around that by determining the filesize and adjusting the length parameter. Signed-off-by: Janos Laube --- Makefile | 6 +++++- compat/mingw.h | 5 +++++ compat/win32mmap.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ git-compat-util.h | 12 ++++++++--- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 compat/win32mmap.c diff --git a/Makefile b/Makefile index 4097892986..4ac0e99906 100644 --- a/Makefile +++ b/Makefile @@ -781,7 +781,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) prefix = INSTALL = /bin/install EXTLIBS += /mingw/lib/libz.a - NO_MMAP = YesPlease NO_PREAD = YesPlease NO_OPENSSL = YesPlease NO_SYMLINK_HEAD = YesPlease @@ -805,6 +804,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) RUNTIME_PREFIX = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease + USE_WIN32_MMAP = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o @@ -974,6 +974,10 @@ ifdef NO_MMAP COMPAT_CFLAGS += -DNO_MMAP COMPAT_OBJS += compat/mmap.o endif +ifdef USE_WIN32_MMAP + COMPAT_CFLAGS += -DUSE_WIN32_MMAP + COMPAT_OBJS += compat/win32mmap.o +endif ifdef NO_PREAD COMPAT_CFLAGS += -DNO_PREAD COMPAT_OBJS += compat/pread.o diff --git a/compat/mingw.h b/compat/mingw.h index b82903c74a..b1156b865e 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -159,6 +159,11 @@ int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz); int mingw_rename(const char*, const char*); #define rename mingw_rename +#ifdef USE_WIN32_MMAP +int mingw_getpagesize(void); +#define getpagesize mingw_getpagesize +#endif + /* Use mingw_lstat() instead of lstat()/stat() and * mingw_fstat() instead of fstat() on Windows. */ diff --git a/compat/win32mmap.c b/compat/win32mmap.c new file mode 100644 index 0000000000..66314b8430 --- /dev/null +++ b/compat/win32mmap.c @@ -0,0 +1,53 @@ +#include "../git-compat-util.h" + +/* Note that this doesn't return the actual pagesize, but + * the allocation granularity. If future Windows specific git code + * needs the real getpagesize function, we need to find another solution. + */ +int mingw_getpagesize(void) +{ + SYSTEM_INFO si; + GetSystemInfo(&si); + return si.dwAllocationGranularity; +} + +void *git_mmap +(void *start, size_t length, int prot, int flags, int fd, off_t offset) +{ + HANDLE hmap; + void *temp; + size_t len; + struct stat st; + uint64_t o = offset; + uint32_t l = o & 0xFFFFFFFF; + uint32_t h = (o >> 32) & 0xFFFFFFFF; + + if (!fstat(fd, &st)) + len = xsize_t(st.st_size); + else + die("mmap: could not determine filesize"); + + if ((length + offset) > len) + length = len - offset; + + if (!(flags & MAP_PRIVATE)) + die("Invalid usage of mmap when built with USE_WIN32_MMAP"); + + hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY, + 0, 0, 0); + + if (!hmap) + return MAP_FAILED; + + temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start); + + if (!CloseHandle(hmap)) + warning("unable to close file mapping handle\n"); + + return temp ? temp : MAP_FAILED; +} + +int git_munmap(void *start, size_t length) +{ + return !UnmapViewOfFile(start); +} diff --git a/git-compat-util.h b/git-compat-util.h index 878d83dd08..1eef4eb5b9 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -166,7 +166,7 @@ static inline const char *skip_prefix(const char *str, const char *prefix) return strncmp(str, prefix, len) ? NULL : str + len; } -#ifdef NO_MMAP +#if defined(NO_MMAP) || defined(USE_WIN32_MMAP) #ifndef PROT_READ #define PROT_READ 1 @@ -180,13 +180,19 @@ static inline const char *skip_prefix(const char *str, const char *prefix) extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); extern int git_munmap(void *start, size_t length); +#else /* NO_MMAP || USE_WIN32_MMAP */ + +#include + +#endif /* NO_MMAP || USE_WIN32_MMAP */ + +#ifdef NO_MMAP + /* This value must be multiple of (pagesize * 2) */ #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024) #else /* NO_MMAP */ -#include - /* This value must be multiple of (pagesize * 2) */ #define DEFAULT_PACKED_GIT_WINDOW_SIZE \ (sizeof(void*) >= 8 \ From 8593d8429cbd09ce813c8f42f5c00bb7d5cf9e7e Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 10 Mar 2009 22:54:17 +0100 Subject: [PATCH 195/253] recv_sideband: Bands #2 and #3 always go to stderr This removes the last parameter of recv_sideband, by which the callers told which channel bands #2 and #3 should be written to. Sayeth Shawn Pearce: The definition of the streams in the current sideband protocol are rather well defined for the one protocol that uses it, fetch-pack/receive-pack: stream #1: pack data stream #2: stderr messages, progress, meant for tty stream #3: abort message, remote is dead, goodbye! Since both callers of the function passed 2 for the parameter, we hereby remove it and send bands #2 and #3 to stderr explicitly using fprintf. This has the nice side-effect that these two streams pass through our ANSI emulation layer on Windows. Signed-off-by: Johannes Sixt Acked-by: Nicolas Pitre Signed-off-by: Junio C Hamano --- builtin-archive.c | 2 +- builtin-fetch-pack.c | 2 +- sideband.c | 19 ++++++++----------- sideband.h | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/builtin-archive.c b/builtin-archive.c index 60adef9363..ab50cebba0 100644 --- a/builtin-archive.c +++ b/builtin-archive.c @@ -52,7 +52,7 @@ static int run_remote_archiver(int argc, const char **argv, die("git archive: expected a flush"); /* Now, start reading from fd[0] and spit it out to stdout */ - rv = recv_sideband("archive", fd[0], 1, 2); + rv = recv_sideband("archive", fd[0], 1); close(fd[0]); close(fd[1]); rv |= finish_connect(conn); diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c index c2e5adc884..2b360994bf 100644 --- a/builtin-fetch-pack.c +++ b/builtin-fetch-pack.c @@ -482,7 +482,7 @@ static int sideband_demux(int fd, void *data) { int *xd = data; - return recv_sideband("fetch-pack", xd[0], fd, 2); + return recv_sideband("fetch-pack", xd[0], fd); } static int get_pack(int xd[2], char **pack_lockfile) diff --git a/sideband.c b/sideband.c index cca3360546..899b1ff366 100644 --- a/sideband.c +++ b/sideband.c @@ -19,7 +19,7 @@ #define FIX_SIZE 10 /* large enough for any of the above */ -int recv_sideband(const char *me, int in_stream, int out, int err) +int recv_sideband(const char *me, int in_stream, int out) { unsigned pf = strlen(PREFIX); unsigned sf; @@ -41,8 +41,7 @@ int recv_sideband(const char *me, int in_stream, int out, int err) if (len == 0) break; if (len < 1) { - len = sprintf(buf, "%s: protocol error: no band designator\n", me); - safe_write(err, buf, len); + fprintf(stderr, "%s: protocol error: no band designator\n", me); return SIDEBAND_PROTOCOL_ERROR; } band = buf[pf] & 0xff; @@ -50,8 +49,8 @@ int recv_sideband(const char *me, int in_stream, int out, int err) switch (band) { case 3: buf[pf] = ' '; - buf[pf+1+len] = '\n'; - safe_write(err, buf, pf+1+len+1); + buf[pf+1+len] = '\0'; + fprintf(stderr, "%s\n", buf); return SIDEBAND_REMOTE_ERROR; case 2: buf[pf] = ' '; @@ -95,12 +94,12 @@ int recv_sideband(const char *me, int in_stream, int out, int err) memcpy(save, b + brk, sf); b[brk + sf - 1] = b[brk - 1]; memcpy(b + brk - 1, suffix, sf); - safe_write(err, b, brk + sf); + fprintf(stderr, "%.*s", brk + sf, b); memcpy(b + brk, save, sf); len -= brk; } else { int l = brk ? brk : len; - safe_write(err, b, l); + fprintf(stderr, "%.*s", l, b); len -= l; } @@ -112,10 +111,8 @@ int recv_sideband(const char *me, int in_stream, int out, int err) safe_write(out, buf + pf+1, len); continue; default: - len = sprintf(buf, - "%s: protocol error: bad band #%d\n", - me, band); - safe_write(err, buf, len); + fprintf(stderr, "%s: protocol error: bad band #%d\n", + me, band); return SIDEBAND_PROTOCOL_ERROR; } } diff --git a/sideband.h b/sideband.h index a84b6917c7..d72db35d1e 100644 --- a/sideband.h +++ b/sideband.h @@ -7,7 +7,7 @@ #define DEFAULT_PACKET_MAX 1000 #define LARGE_PACKET_MAX 65520 -int recv_sideband(const char *me, int in_stream, int out, int err); +int recv_sideband(const char *me, int in_stream, int out); ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max); #endif From 3db39264cd3334ae5e9ab933f0298540bbe389a5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 10 Mar 2009 22:58:09 +0100 Subject: [PATCH 196/253] winansi: support ESC [ K (erase in line) Signed-off-by: Johannes Schindelin Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- compat/winansi.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/compat/winansi.c b/compat/winansi.c index e2d96dfe6f..44dc293ad3 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -18,8 +18,6 @@ This file is git-specific. Therefore, this file does not attempt to implement any codes that are not used by git. - - TODO: K */ static HANDLE console; @@ -79,6 +77,20 @@ static void set_console_attr(void) SetConsoleTextAttribute(console, attributes); } +static void erase_in_line(void) +{ + CONSOLE_SCREEN_BUFFER_INFO sbi; + + if (!console) + return; + + GetConsoleScreenBufferInfo(console, &sbi); + FillConsoleOutputCharacterA(console, ' ', + sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition, + NULL); +} + + static const char *set_attr(const char *str) { const char *func; @@ -218,7 +230,7 @@ static const char *set_attr(const char *str) set_console_attr(); break; case 'K': - /* TODO */ + erase_in_line(); break; default: /* Unsupported code */ From f7512386097c4f20e3c147e5de8888e49e4835c5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 21 Mar 2009 01:20:57 +0100 Subject: [PATCH 197/253] Respect core.autocrlf when preparing temporary files for external diff When preparing temporary files for an external diff, the files should be handled as if they were worktree files. This makes things consistent for the case when one side of the diff was found in the current working directory (and therefore creating a temporary file could be avoided altogether). This fixes msysGit issue 177. Signed-off-by: Johannes Schindelin --- diff.c | 13 ++++++++++--- t/t4020-diff-external.sh | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/diff.c b/diff.c index 75d9fab8f8..699ae6aca3 100644 --- a/diff.c +++ b/diff.c @@ -1946,17 +1946,23 @@ void diff_free_filespec_data(struct diff_filespec *s) s->cnt_data = NULL; } -static void prep_temp_blob(struct diff_tempfile *temp, +static void prep_temp_blob(const char *path, struct diff_tempfile *temp, void *blob, unsigned long size, const unsigned char *sha1, int mode) { int fd; + struct strbuf buf = STRBUF_INIT; fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX"); if (fd < 0) die("unable to create temp-file: %s", strerror(errno)); + if (convert_to_working_tree(path, + (const char *)blob, (size_t)size, &buf)) { + blob = buf.buf; + size = buf.len; + } if (write_in_full(fd, blob, size) != size) die("unable to write temp-file"); close(fd); @@ -1964,6 +1970,7 @@ static void prep_temp_blob(struct diff_tempfile *temp, strcpy(temp->hex, sha1_to_hex(sha1)); temp->hex[40] = 0; sprintf(temp->mode, "%06o", mode); + strbuf_release(&buf); } static struct diff_tempfile *prepare_temp_file(const char *name, @@ -2004,7 +2011,7 @@ static struct diff_tempfile *prepare_temp_file(const char *name, die("readlink(%s)", name); if (ret == sizeof(buf)) die("symlink too long: %s", name); - prep_temp_blob(temp, buf, ret, + prep_temp_blob(name, temp, buf, ret, (one->sha1_valid ? one->sha1 : null_sha1), (one->sha1_valid ? @@ -2030,7 +2037,7 @@ static struct diff_tempfile *prepare_temp_file(const char *name, else { if (diff_populate_filespec(one, 0)) die("cannot read data blob for %s", one->path); - prep_temp_blob(temp, one->data, one->size, + prep_temp_blob(name, temp, one->data, one->size, one->sha1, one->mode); } return temp; diff --git a/t/t4020-diff-external.sh b/t/t4020-diff-external.sh index 281680d95a..f8c99f1a98 100755 --- a/t/t4020-diff-external.sh +++ b/t/t4020-diff-external.sh @@ -136,4 +136,20 @@ test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' ' GIT_EXTERNAL_DIFF=echo git diff ' +echo "#!$SHELL_PATH" >fake-diff.sh +cat >> fake-diff.sh <<\EOF +cat $2 >> crlfed.txt +EOF +chmod a+x fake-diff.sh + +keep_only_cr () { + tr -dc '\015' +} + +test_expect_success 'external diff with autocrlf = true' ' + git config core.autocrlf true && + GIT_EXTERNAL_DIFF=./fake-diff.sh git diff && + test $(wc -l < crlfed.txt) = $(cat crlfed.txt | keep_only_cr | wc -c) +' + test_done From a094080e8eec0bffcd17da6804ee09bc5309da6c Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 22 Jan 2009 11:30:16 +0100 Subject: [PATCH 198/253] Do not issue the warning about the fallback of the PREFIX. It confuses gitk when it is browsing this repository on Windows. --- exec_cmd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/exec_cmd.c b/exec_cmd.c index 408e4e55e1..13e036fc37 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -28,9 +28,10 @@ const char *system_path(const char *path) !(prefix = strip_path_suffix(argv0_path, BINDIR)) && !(prefix = strip_path_suffix(argv0_path, "git"))) { prefix = PREFIX; - fprintf(stderr, "RUNTIME_PREFIX requested, " - "but prefix computation failed. " - "Using static fallback '%s'.\n", prefix); + /* + * RUNTIME_PREFIX requested, but prefix computation failed. + * Using static fallback. + */ } #endif From ccd3859ceb5622294f2a4b61524974a0c1ee22f7 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 22 Jul 2008 13:42:58 +0200 Subject: [PATCH 199/253] Windows: Better support PAGER settings with spaces in the path On Windows, the pager was always run via a shell, which is not the case on Unix, where the shell variant was only used as a fallback. Thus, setting the pager, for example, like this: PAGER="C:/Program Files/msys/bin/less" would fail; an extra set of quotes is needed: PAGER="\"C:/Program Files/msys/bin/less\"" With this patch the former setting works as well. Signed-off-by: Johannes Sixt --- pager.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pager.c b/pager.c index 4921843577..77e9dd6ae4 100644 --- a/pager.c +++ b/pager.c @@ -9,7 +9,6 @@ static int spawned_pager; -#ifndef __MINGW32__ static void pager_preexec(void) { /* @@ -24,7 +23,6 @@ static void pager_preexec(void) setenv("LESS", "FRSX", 0); } -#endif static const char *pager_argv[] = { "sh", "-c", NULL, NULL }; static struct child_process pager_process; @@ -68,13 +66,15 @@ void setup_pager(void) /* spawn the pager */ pager_argv[2] = pager; - pager_process.argv = pager_argv; - pager_process.in = -1; -#ifndef __MINGW32__ pager_process.preexec_cb = pager_preexec; -#endif - if (start_command(&pager_process)) - return; + pager_process.argv = &pager_argv[2]; + pager_process.in = -1; + if (start_command(&pager_process)) { + pager_process.argv = pager_argv; + pager_process.in = -1; + if (start_command(&pager_process)) + return; + } /* original process continues, but writes to the pipe */ dup2(pager_process.in, 1); From ddce70517e7486c64107f5ffd1383f0a3a2db34d Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 26 Mar 2009 21:11:50 +0100 Subject: [PATCH 200/253] Skip tests that fail due to incomplete implementations, missing tools... --- t/Makefile | 24 ++++++++++++++++++++++++ t/t4150-am.sh | 6 +----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/t/Makefile b/t/Makefile index bf816fc850..a0ed388ba5 100644 --- a/t/Makefile +++ b/t/Makefile @@ -42,3 +42,27 @@ valgrind: GIT_TEST_OPTS=--valgrind $(MAKE) .PHONY: pre-clean $(T) aggregate-results clean valgrind + +-include Makefile.local + +GIT_SKIP_TESTS := $(GIT_SKIP_TESTS_LOCAL) +# git am does not treat absolute path to mbox file correctly +GIT_SKIP_TESTS += t4150.18 +# git upload-pack does not write an error message in this case +# (reason unknown; seems to be a race condition) +GIT_SKIP_TESTS += t5530.6 +# output contains CRLF (I think) +GIT_SKIP_TESTS += t7401.14 +# output contains CRLF (I think) +GIT_SKIP_TESTS += t7508.1[68] +# cannot run fake.sendmail for some reason +GIT_SKIP_TESTS += t9001 +# there's a problem with GIT_DIR in test 12 (why only there?) +# needs fixups with $PWD instead of $(pwd) - tries to run rsh c +GIT_SKIP_TESTS += t9200 +# CGI.pm not found +GIT_SKIP_TESTS += t9500 +export GIT_SKIP_TESTS + +NO_SVN_TESTS=SkipThem +export NO_SVN_TESTS diff --git a/t/t4150-am.sh b/t/t4150-am.sh index a8aa02e936..5e65afa0c1 100755 --- a/t/t4150-am.sh +++ b/t/t4150-am.sh @@ -245,11 +245,7 @@ test_expect_success 'am works from file (relative path given) in subdirectory' ' test -z "$(git diff second)" ' -case $(uname -s) in -*MINGW*) test_expect=test_expect_failure;; -*) test_expect=test_expect_success;; -esac -$test_expect 'am works from file (absolute path given) in subdirectory' ' +test_expect_success 'am works from file (absolute path given) in subdirectory' ' rm -fr subdir && git checkout first && P=$(pwd) && From 4eb0463971e7a806ac8b7cf4c6cfede2b7f4fff8 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 16 Nov 2007 13:42:51 +0100 Subject: [PATCH 201/253] Make report() from usage.c public as vreport(). We will use it for customized error reporting routines. --- git-compat-util.h | 1 + usage.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index f09f244061..d94c683e0c 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -150,6 +150,7 @@ #endif /* General helper functions */ +extern void vreport(const char *prefix, const char *err, va_list params); extern void usage(const char *err) NORETURN; extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index 820d09f92b..349f3fb350 100644 --- a/usage.c +++ b/usage.c @@ -5,7 +5,7 @@ */ #include "git-compat-util.h" -static void report(const char *prefix, const char *err, va_list params) +void vreport(const char *prefix, const char *err, va_list params) { char msg[1024]; vsnprintf(msg, sizeof(msg), err, params); @@ -20,18 +20,18 @@ static NORETURN void usage_builtin(const char *err) static NORETURN void die_builtin(const char *err, va_list params) { - report("fatal: ", err, params); + vreport("fatal: ", err, params); exit(128); } static void error_builtin(const char *err, va_list params) { - report("error: ", err, params); + vreport("error: ", err, params); } static void warn_builtin(const char *warn, va_list params) { - report("warning: ", warn, params); + vreport("warning: ", warn, params); } /* If we are in a dlopen()ed .so write to a global variable would segfault From 85763c86da5f0826a879f37056f2de033cb5ca12 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 16 Nov 2007 13:46:37 +0100 Subject: [PATCH 202/253] Implement thread-specific die() routines; use one in start_async(). --- run-command.c | 16 ++++++++++++++++ t/Makefile | 3 --- usage.c | 32 +++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/run-command.c b/run-command.c index b05c734d05..09725b983f 100644 --- a/run-command.c +++ b/run-command.c @@ -293,9 +293,22 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const } #ifdef __MINGW32__ +static int close_fd = -1; +NORETURN void die_from_thread(const char *err, va_list params) +{ + vreport("fatal: ", err, params); + if (close_fd >= 0) + close(close_fd); + _endthreadex(128); + exit(128); /* silence compiler */ +} + static __stdcall unsigned run_thread(void *data) { struct async *async = data; + close_fd = async->fd_for_proc; + extern NORETURN void die_from_thread(const char *err, va_list params); + set_die_routine(die_from_thread); return async->proc(async->fd_for_proc, async->data); } #endif @@ -349,6 +362,9 @@ int finish_async(struct async *async) else if (!GetExitCodeThread(async->tid, &ret)) ret = error("cannot get thread exit code: %lu", GetLastError()); CloseHandle(async->tid); + if (ret) + /* test suite tests for this string */ + error("waitpid (async) failed"); #endif return ret; } diff --git a/t/Makefile b/t/Makefile index a0ed388ba5..9af58b9bc6 100644 --- a/t/Makefile +++ b/t/Makefile @@ -48,9 +48,6 @@ valgrind: GIT_SKIP_TESTS := $(GIT_SKIP_TESTS_LOCAL) # git am does not treat absolute path to mbox file correctly GIT_SKIP_TESTS += t4150.18 -# git upload-pack does not write an error message in this case -# (reason unknown; seems to be a race condition) -GIT_SKIP_TESTS += t5530.6 # output contains CRLF (I think) GIT_SKIP_TESTS += t7401.14 # output contains CRLF (I think) diff --git a/usage.c b/usage.c index 349f3fb350..9e35e8a390 100644 --- a/usage.c +++ b/usage.c @@ -34,16 +34,38 @@ static void warn_builtin(const char *warn, va_list params) vreport("warning: ", warn, params); } +typedef void (*die_fn_t)(const char *err, va_list params) NORETURN; + +static DWORD tls_index; + +static void tls_init(void) __attribute__((constructor)); +static void tls_init(void) +{ + tls_index = TlsAlloc(); +} + +struct routines { + die_fn_t die_routine; +}; /* If we are in a dlopen()ed .so write to a global variable would segfault * (ugh), so keep things static. */ static void (*usage_routine)(const char *err) NORETURN = usage_builtin; -static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin; static void (*error_routine)(const char *err, va_list params) = error_builtin; static void (*warn_routine)(const char *err, va_list params) = warn_builtin; void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN) { - die_routine = routine; + struct routines *r = TlsGetValue(tls_index); + if (r == NULL) { + /* avoid die()! */ + r = calloc(sizeof(*r), 1); + if (r == NULL) { + fprintf(stderr, "cannot allocate thread-local storage"); + return; + } + TlsSetValue(tls_index, r); + } + r->die_routine = routine; } void usage(const char *err) @@ -54,9 +76,13 @@ void usage(const char *err) void die(const char *err, ...) { va_list params; + struct routines *r = TlsGetValue(tls_index); va_start(params, err); - die_routine(err, params); + if (r == NULL || r->die_routine == NULL) + die_builtin(err, params); + else + r->die_routine(err, params); va_end(params); } From f80f1db8b51e34741aa54eec93d1ce7b26655e78 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sun, 16 Mar 2008 21:47:53 +0100 Subject: [PATCH 203/253] Need diff -u -b in t7401 because some lines end in CRLF. --- t/t7401-submodule-summary.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/t7401-submodule-summary.sh b/t/t7401-submodule-summary.sh index d2d6d5be25..50030c1a49 100755 --- a/t/t7401-submodule-summary.sh +++ b/t/t7401-submodule-summary.sh @@ -14,6 +14,8 @@ esac . ./test-lib.sh +export GIT_TEST_CMP='diff -u -b' # some lines end in CRLF + add_file () { sm=$1 shift From fc7fe85cbcae92e5bfc68eb6d17b4857b704c1f7 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 7 Nov 2007 11:19:00 +0100 Subject: [PATCH 204/253] gitk: Swap positions of 'next' and 'prev' buttons in the 'Find' section. The button order 'prev' left of 'next' feels more natural than the other way round, in particular, compared to the order of the forward and backward arrows that are in the line above. Signed-off-by: Johannes Sixt --- gitk-git/gitk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index a2c589f749..f43ca48dff 100644 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -2057,7 +2057,7 @@ proc makewindow {} { button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1} button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1} label .tf.lbar.flab2 -text " [mc "commit"] " - pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \ + pack .tf.lbar.flabel .tf.lbar.fprev .tf.lbar.fnext .tf.lbar.flab2 \ -side left -fill y set gdttype [mc "containing:"] set gm [tk_optionMenu .tf.lbar.gdttype gdttype \ From 50f37d18b6c7c84ed81938226ca1bfbc00628fb8 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 22 Jan 2009 11:26:41 +0100 Subject: [PATCH 205/253] Implement test-stat the prints the modification time. Compile with gcc -o test-stat -Wall test-stat.c -DEMULATE or gcc -o test-stat -Wall test-stat.c --- test-stat.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 test-stat.c diff --git a/test-stat.c b/test-stat.c new file mode 100644 index 0000000000..ff859412f0 --- /dev/null +++ b/test-stat.c @@ -0,0 +1,170 @@ +#include +#include +#include + +#ifdef EMULATE +#define HINT fprintf(stderr, "emulated stat\n") +#include +#include +#include + +/* Use mingw_lstat() instead of lstat()/stat() and + * mingw_fstat() instead of fstat() on Windows. + */ +int mingw_lstat(const char *file_name, struct stat *buf); +int mingw_fstat(int fd, struct stat *buf); +#define fstat mingw_fstat +#define lstat mingw_lstat +#define stat(x,y) mingw_lstat(x,y) + +#define PATH_MAX 260 + +static inline int file_attr_to_st_mode (DWORD attr) +{ + int fMode = S_IREAD; + if (attr & FILE_ATTRIBUTE_DIRECTORY) + fMode |= S_IFDIR; + else + fMode |= S_IFREG; + if (!(attr & FILE_ATTRIBUTE_READONLY)) + fMode |= S_IWRITE; + return fMode; +} + +static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata) +{ + if (GetFileAttributesExA(fname, GetFileExInfoStandard, fdata)) + return 0; + + switch (GetLastError()) { + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + case ERROR_LOCK_VIOLATION: + case ERROR_SHARING_BUFFER_EXCEEDED: + return EACCES; + case ERROR_BUFFER_OVERFLOW: + return ENAMETOOLONG; + case ERROR_NOT_ENOUGH_MEMORY: + return ENOMEM; + default: + return ENOENT; + } +} + +static inline time_t filetime_to_time_t(const FILETIME *ft) +{ + long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; + winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */ + winTime /= 10000000; /* Nano to seconds resolution */ + return (time_t)winTime; +} + +/* We keep the do_lstat code in a separate function to avoid recursion. + * When a path ends with a slash, the stat will fail with ENOENT. In + * this case, we strip the trailing slashes and stat again. + */ +static int do_lstat(const char *file_name, struct stat *buf) +{ + WIN32_FILE_ATTRIBUTE_DATA fdata; + + if (!(errno = get_file_attr(file_name, &fdata))) { + buf->st_ino = 0; + buf->st_gid = 0; + buf->st_uid = 0; + buf->st_nlink = 1; + buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); + buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */ + buf->st_dev = buf->st_rdev = 0; /* not used by Git */ + buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); + buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); + buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + return 0; + } + return -1; +} + +/* We provide our own lstat/fstat functions, since the provided + * lstat/fstat functions are so slow. These stat functions are + * tailored for Git's usage (read: fast), and are not meant to be + * complete. Note that Git stat()s are redirected to mingw_lstat() + * too, since Windows doesn't really handle symlinks that well. + */ +int mingw_lstat(const char *file_name, struct stat *buf) +{ + int namelen; + static char alt_name[PATH_MAX]; + + if (!do_lstat(file_name, buf)) + return 0; + + /* if file_name ended in a '/', Windows returned ENOENT; + * try again without trailing slashes + */ + if (errno != ENOENT) + return -1; + + namelen = strlen(file_name); + if (namelen && file_name[namelen-1] != '/') + return -1; + while (namelen && file_name[namelen-1] == '/') + --namelen; + if (!namelen || namelen >= PATH_MAX) + return -1; + + memcpy(alt_name, file_name, namelen); + alt_name[namelen] = 0; + return do_lstat(alt_name, buf); +} + +#undef fstat +int mingw_fstat(int fd, struct stat *buf) +{ + HANDLE fh = (HANDLE)_get_osfhandle(fd); + BY_HANDLE_FILE_INFORMATION fdata; + + if (fh == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + /* direct non-file handles to MS's fstat() */ + if (GetFileType(fh) != FILE_TYPE_DISK) + return fstat(fd, buf); + + if (GetFileInformationByHandle(fh, &fdata)) { + buf->st_ino = 0; + buf->st_gid = 0; + buf->st_uid = 0; + buf->st_nlink = 1; + buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); + buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */ + buf->st_dev = buf->st_rdev = 0; /* not used by Git */ + buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); + buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); + buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + return 0; + } + errno = EBADF; + return -1; +} + +#else +#define HINT (void)0 +#endif + +int main(int argc, char**argv) +{ + int i; + int err = 0; + HINT; + for (i = 1; i < argc; i++) + { + struct stat st; + if (stat(argv[i], &st)) { + perror(argv[i]); + err = 1; + continue; + } + printf("%s: %s\n", argv[i], ctime(&st.st_mtime)); + } + return err; +} From abd1fa1a1b8fa85962aa38dbb9465e68b09d1299 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 31 Mar 2009 15:09:06 +0200 Subject: [PATCH 206/253] test-suite: Log everything to a file in non-verbose mode Using 'return' from a test snippet is now a no-go because it would skip the assignment of the resulting exit code and it would also skip set +x. Signed-off-by: Johannes Sixt --- t/.gitignore | 1 + t/Makefile | 2 +- t/t0040-parse-options.sh | 3 + t/t1503-rev-parse-verify.sh | 1 + t/t5505-remote.sh | 1 + t/t6022-merge-rename.sh | 177 +++++++------------------------- t/t6034-merge-rename-nocruft.sh | 65 +++--------- t/t9300-fast-import.sh | 16 +-- t/test-lib.sh | 15 ++- 9 files changed, 68 insertions(+), 213 deletions(-) diff --git a/t/.gitignore b/t/.gitignore index 7dcbb232cd..dbe58eb9b3 100644 --- a/t/.gitignore +++ b/t/.gitignore @@ -1,2 +1,3 @@ /trash directory* /test-results +/test-log diff --git a/t/Makefile b/t/Makefile index 9af58b9bc6..6eefc08fea 100644 --- a/t/Makefile +++ b/t/Makefile @@ -21,7 +21,7 @@ $(T): @echo "*** $@ ***"; GIT_CONFIG=.git/config '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS) pre-clean: - $(RM) -r test-results + $(RM) -r test-results test-log clean: $(RM) -r 'trash directory'.* test-results diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index e38241c80a..da272e0124 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -39,6 +39,7 @@ Standard options EOF test_expect_success 'test help' ' + set +x && test_must_fail test-parse-options -h > output 2> output.err && test ! -s output && test_cmp expect.err output.err @@ -158,6 +159,7 @@ error: did you mean \`--boolean\` (with two dashes ?) EOF test_expect_success 'detect possible typos' ' + set +x && test_must_fail test-parse-options -boolean > output 2> output.err && test ! -s output && test_cmp typo.err output.err @@ -223,6 +225,7 @@ Callback: "not set", 1 EOF test_expect_success 'OPT_CALLBACK() and callback errors work' ' + set +x && test_must_fail test-parse-options --no-length > output 2> output.err && test_cmp expect output && test_cmp expect.err output.err diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh index cc65394947..4c677340f4 100755 --- a/t/t1503-rev-parse-verify.sh +++ b/t/t1503-rev-parse-verify.sh @@ -71,6 +71,7 @@ test_expect_success 'fails with any bad rev or many good revs' ' ' test_expect_success 'fails silently when using -q' ' + set +x && test_must_fail git rev-parse --verify --quiet 2>error && test -z "$(cat error)" && test_must_fail git rev-parse -q --verify foo 2>error && diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 5ec668d6d8..7267b1ae70 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -247,6 +247,7 @@ EOF test_expect_success 'set-head --auto fails w/multiple HEADs' ' (cd test && + set +x && test_must_fail git remote set-head --auto two >output 2>&1 && test_cmp expect output) ' diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh index e3f7ae8120..d69a364cae 100755 --- a/t/t6022-merge-rename.sh +++ b/t/t6022-merge-rename.sh @@ -95,30 +95,15 @@ git checkout master' test_expect_success 'pull renaming branch into unrenaming one' \ ' git show-branch - git pull . white && { - echo "BAD: should have conflicted" - return 1 - } - git ls-files -s - test "$(git ls-files -u B | wc -l)" -eq 3 || { - echo "BAD: should have left stages for B" - return 1 - } - test "$(git ls-files -s N | wc -l)" -eq 1 || { - echo "BAD: should have merged N" - return 1 - } + test_must_fail git pull . white && + git ls-files -s && + test "$(git ls-files -u B | wc -l)" -eq 3 && + test "$(git ls-files -s N | wc -l)" -eq 1 && sed -ne "/^g/{ p q - }" B | grep master || { - echo "BAD: should have listed our change first" - return 1 - } - test "$(git diff white N | wc -l)" -eq 0 || { - echo "BAD: should have taken colored branch" - return 1 - } + }" B | grep master && + test "$(git diff white N | wc -l)" -eq 0 ' test_expect_success 'pull renaming branch into another renaming one' \ @@ -126,95 +111,44 @@ test_expect_success 'pull renaming branch into another renaming one' \ rm -f B git reset --hard git checkout red - git pull . white && { - echo "BAD: should have conflicted" - return 1 - } - test "$(git ls-files -u B | wc -l)" -eq 3 || { - echo "BAD: should have left stages" - return 1 - } - test "$(git ls-files -s N | wc -l)" -eq 1 || { - echo "BAD: should have merged N" - return 1 - } + test_must_fail git pull . white && + test "$(git ls-files -u B | wc -l)" -eq 3 && + test "$(git ls-files -s N | wc -l)" -eq 1 && sed -ne "/^g/{ p q - }" B | grep red || { - echo "BAD: should have listed our change first" - return 1 - } - test "$(git diff white N | wc -l)" -eq 0 || { - echo "BAD: should have taken colored branch" - return 1 - } + }" B | grep red && + test "$(git diff white N | wc -l)" -eq 0 ' test_expect_success 'pull unrenaming branch into renaming one' \ ' git reset --hard git show-branch - git pull . master && { - echo "BAD: should have conflicted" - return 1 - } - test "$(git ls-files -u B | wc -l)" -eq 3 || { - echo "BAD: should have left stages" - return 1 - } - test "$(git ls-files -s N | wc -l)" -eq 1 || { - echo "BAD: should have merged N" - return 1 - } + test_must_fail git pull . master && + test "$(git ls-files -u B | wc -l)" -eq 3 && + test "$(git ls-files -s N | wc -l)" -eq 1 && sed -ne "/^g/{ p q - }" B | grep red || { - echo "BAD: should have listed our change first" - return 1 - } - test "$(git diff white N | wc -l)" -eq 0 || { - echo "BAD: should have taken colored branch" - return 1 - } + }" B | grep red && + test "$(git diff white N | wc -l)" -eq 0 ' test_expect_success 'pull conflicting renames' \ ' git reset --hard git show-branch - git pull . blue && { - echo "BAD: should have conflicted" - return 1 - } - test "$(git ls-files -u A | wc -l)" -eq 1 || { - echo "BAD: should have left a stage" - return 1 - } - test "$(git ls-files -u B | wc -l)" -eq 1 || { - echo "BAD: should have left a stage" - return 1 - } - test "$(git ls-files -u C | wc -l)" -eq 1 || { - echo "BAD: should have left a stage" - return 1 - } - test "$(git ls-files -s N | wc -l)" -eq 1 || { - echo "BAD: should have merged N" - return 1 - } + test_must_fail git pull . blue && + test "$(git ls-files -u A | wc -l)" -eq 1 && + test "$(git ls-files -u B | wc -l)" -eq 1 && + test "$(git ls-files -u C | wc -l)" -eq 1 && + test "$(git ls-files -s N | wc -l)" -eq 1 && sed -ne "/^g/{ p q - }" B | grep red || { - echo "BAD: should have listed our change first" - return 1 - } - test "$(git diff white N | wc -l)" -eq 0 || { - echo "BAD: should have taken colored branch" - return 1 - } + }" B | grep red && + test "$(git diff white N | wc -l)" -eq 0 ' test_expect_success 'interference with untracked working tree file' ' @@ -222,14 +156,8 @@ test_expect_success 'interference with untracked working tree file' ' git reset --hard git show-branch echo >A this file should not matter - git pull . white && { - echo "BAD: should have conflicted" - return 1 - } - test -f A || { - echo "BAD: should have left A intact" - return 1 - } + test_must_fail git pull . white && + test -f A ' test_expect_success 'interference with untracked working tree file' ' @@ -239,14 +167,8 @@ test_expect_success 'interference with untracked working tree file' ' git show-branch rm -f A echo >A this file should not matter - git pull . red && { - echo "BAD: should have conflicted" - return 1 - } - test -f A || { - echo "BAD: should have left A intact" - return 1 - } + test_must_fail git pull . red && + test -f A ' test_expect_success 'interference with untracked working tree file' ' @@ -256,14 +178,8 @@ test_expect_success 'interference with untracked working tree file' ' git checkout -f master git tag -f anchor git show-branch - git pull . yellow || { - echo "BAD: should have cleanly merged" - return 1 - } - test -f M && { - echo "BAD: should have removed M" - return 1 - } + git pull . yellow + test ! -f M && git reset --hard anchor ' @@ -276,14 +192,8 @@ test_expect_success 'updated working tree file should prevent the merge' ' git show-branch echo >>M one line addition cat M >M.saved - git pull . yellow && { - echo "BAD: should have complained" - return 1 - } - diff M M.saved || { - echo "BAD: should have left M intact" - return 1 - } + test_must_fail git pull . yellow && + diff M M.saved && rm -f M.saved ' @@ -297,14 +207,8 @@ test_expect_success 'updated working tree file should prevent the merge' ' echo >>M one line addition cat M >M.saved git update-index M - git pull . yellow && { - echo "BAD: should have complained" - return 1 - } - diff M M.saved || { - echo "BAD: should have left M intact" - return 1 - } + test_must_fail git pull . yellow && + diff M M.saved && rm -f M.saved ' @@ -316,18 +220,9 @@ test_expect_success 'interference with untracked working tree file' ' git tag -f anchor git show-branch echo >M this file should not matter - git pull . master || { - echo "BAD: should have cleanly merged" - return 1 - } - test -f M || { - echo "BAD: should have left M intact" - return 1 - } - git ls-files -s | grep M && { - echo "BAD: M must be untracked in the result" - return 1 - } + git pull . master && + test -f M && + ! (git ls-files -s | grep M) && git reset --hard anchor ' diff --git a/t/t6034-merge-rename-nocruft.sh b/t/t6034-merge-rename-nocruft.sh index 65be95fbaa..d1b1d1a441 100755 --- a/t/t6034-merge-rename-nocruft.sh +++ b/t/t6034-merge-rename-nocruft.sh @@ -73,33 +73,12 @@ test_expect_success 'merge white into red (A->B,M->N)' \ ' git checkout -b red-white red && git merge white && - git write-tree >/dev/null || { - echo "BAD: merge did not complete" - return 1 - } - - test -f B || { - echo "BAD: B does not exist in working directory" - return 1 - } - test -f N || { - echo "BAD: N does not exist in working directory" - return 1 - } - test -f R || { - echo "BAD: R does not exist in working directory" - return 1 - } - - test -f A && { - echo "BAD: A still exists in working directory" - return 1 - } - test -f M && { - echo "BAD: M still exists in working directory" - return 1 - } - return 0 + git write-tree >/dev/null && + test -f B && + test -f N && + test -f R && + ! test -f A && + ! test -f M ' # This test broke in 8371234ecaaf6e14fe3f2082a855eff1bbd79ae9 @@ -108,32 +87,12 @@ test_expect_success 'merge blue into white (A->B, mod A, A untracked)' \ git checkout -b white-blue white && echo dirty >A && git merge blue && - git write-tree >/dev/null || { - echo "BAD: merge did not complete" - return 1 - } - - test -f A || { - echo "BAD: A does not exist in working directory" - return 1 - } - test `cat A` = dirty || { - echo "BAD: A content is wrong" - return 1 - } - test -f B || { - echo "BAD: B does not exist in working directory" - return 1 - } - test -f N || { - echo "BAD: N does not exist in working directory" - return 1 - } - test -f M && { - echo "BAD: M still exists in working directory" - return 1 - } - return 0 + git write-tree >/dev/null && + test -f A && + test `cat A` = dirty && + test -f B && + test -f N && + ! test -f M ' test_done diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh index 821be7ce8d..a7a13b7e57 100755 --- a/t/t9300-fast-import.sh +++ b/t/t9300-fast-import.sh @@ -399,20 +399,8 @@ from refs/heads/branch INPUT_END test_expect_success \ 'F: non-fast-forward update skips' \ - 'if git fast-import &1 if test "$verbose" = "t" then + set_x= set_nox= exec 4>&2 3>&1 else - exec 4>/dev/null 3>/dev/null + test_log_dir="$TEST_DIRECTORY/test-log" + mkdir -p "$test_log_dir" + test_log_path="$test_log_dir/${0%.sh}-$$" + set_x='set -x;' + set_nox='set +x' + exec 3>"$test_log_path" 4>&3 fi test_failure=0 @@ -303,8 +310,9 @@ test_debug () { } test_run_ () { - eval >&3 2>&4 "$1" - eval_ret="$?" + eval >&3 2>&4 $set_x "$1" ' + eval_ret=$? + $set_nox' return 0 } @@ -540,7 +548,6 @@ test_done () { # Test the binaries we have just built. The tests are kept in # t/ subdirectory and are run in 'trash directory' subdirectory. -TEST_DIRECTORY=$(pwd) if test -z "$valgrind" then if test -z "$GIT_TEST_INSTALLED" From 1e52e22d9e874e135f0f724ef87d952afdfc5316 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 3 Apr 2009 08:49:59 +0200 Subject: [PATCH 207/253] Windows: Work around intermittent failures in mingw_rename We have replaced rename() with a version that can rename a file to a destination that already exists. Nevertheless, many users, the author included, observe failures in the code that are not reproducible. The theory is that the failures are due to some other process that happens to have opened the destination file briefly at the wrong moment. (And there is no way on Windows to delete or replace a file that is currently open.) One such process could be Windows Explorer that is updating its directory listing because it detected a change in the .git directory or a virus scanner. The failure is more often observed while there is heavy git activity (for example while the test suite is running or during a rebase operation). We work around the failure by retrying the rename operation if it failed due to ERROR_ACCESS_DENIED. The retries are delayed a bit: The first only by giving up the time slice, the next after the minimal scheduling granularity, and if more retries are needed, then we wait some non-trivial amount of time with exponential back-off. Signed-off-by: Johannes Sixt --- compat/mingw.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 2839d9df6e..9f647e15cc 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -934,7 +934,9 @@ int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz) #undef rename int mingw_rename(const char *pold, const char *pnew) { - DWORD attrs; + DWORD attrs, gle; + int tries = 0; + static const int delay[] = { 0, 1, 10, 20, 40 }; /* * Try native rename() first to get errno right. @@ -944,10 +946,12 @@ int mingw_rename(const char *pold, const char *pnew) return 0; if (errno != EEXIST) return -1; +repeat: if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING)) return 0; /* TODO: translate more errors */ - if (GetLastError() == ERROR_ACCESS_DENIED && + gle = GetLastError(); + if (gle == ERROR_ACCESS_DENIED && (attrs = GetFileAttributes(pnew)) != INVALID_FILE_ATTRIBUTES) { if (attrs & FILE_ATTRIBUTE_DIRECTORY) { errno = EISDIR; @@ -957,10 +961,24 @@ int mingw_rename(const char *pold, const char *pnew) SetFileAttributes(pnew, attrs & ~FILE_ATTRIBUTE_READONLY)) { if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING)) return 0; + gle = GetLastError(); /* revert file attributes on failure */ SetFileAttributes(pnew, attrs); } } + if (tries < ARRAY_SIZE(delay) && gle == ERROR_ACCESS_DENIED) { + /* + * We assume that some other process had the source or + * destination file open at the wrong moment and retry. + * In order to give the other process a higher chance to + * complete its operation, we give up our time slice now. + * If we have to retry again, we do sleep a bit. + */ + Sleep(delay[tries]); + tries++; + warning("rename retry #%d %s -> %s", tries, pold, pnew); + goto repeat; + } errno = EACCES; return -1; } From 7ee5b02494aadbe53e5a50f085ce0c81908b5781 Mon Sep 17 00:00:00 2001 From: Markus Heidelberg Date: Sun, 5 Apr 2009 14:42:27 +0200 Subject: [PATCH 208/253] doc/merge-config: list ecmerge as a built-in merge tool Signed-off-by: Markus Heidelberg --- Documentation/merge-config.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Documentation/merge-config.txt b/Documentation/merge-config.txt index 1ff08ff2cc..9c44af8e4a 100644 --- a/Documentation/merge-config.txt +++ b/Documentation/merge-config.txt @@ -22,7 +22,8 @@ merge.stat:: merge.tool:: Controls which merge resolution program is used by linkgit:git-mergetool[1]. Valid built-in values are: "kdiff3", - "tkdiff", "meld", "xxdiff", "emerge", "vimdiff", "gvimdiff", and + "tkdiff", "meld", "xxdiff", "emerge", "vimdiff", "gvimdiff", + "ecmerge" and "opendiff". Any other value is treated is custom merge tool and there must be a corresponding mergetool..cmd option. From 1fadfbe9b6df8f5e8733741986003f08ba9df2df Mon Sep 17 00:00:00 2001 From: Markus Heidelberg Date: Sun, 5 Apr 2009 14:42:57 +0200 Subject: [PATCH 209/253] git-mergetool/difftool: make (g)vimdiff workable under Windows Under Windows vimdiff and gvimdiff are not available as symbolic links, but as batch files vimdiff.bat and gvimdiff.bat. These files weren't found by 'type vimdiff' which led to the following error: The merge tool vimdiff is not available as 'vimdiff' Even if they were found, it wouldn't work to invoke these batch files from git-mergetool. To solve this, use vim and gvim (vim.exe and gvim.exe) and pass the -d command line switch over to them. Signed-off-by: Markus Heidelberg --- contrib/difftool/git-difftool-helper | 10 ++++++++-- git-mergetool.sh | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/contrib/difftool/git-difftool-helper b/contrib/difftool/git-difftool-helper index 9c0a13452a..e481913c91 100755 --- a/contrib/difftool/git-difftool-helper +++ b/contrib/difftool/git-difftool-helper @@ -86,11 +86,11 @@ launch_merge_tool () { ;; vimdiff) - "$merge_tool_path" -c "wincmd l" "$LOCAL" "$REMOTE" + "$merge_tool_path" -d -c "wincmd l" "$LOCAL" "$REMOTE" ;; gvimdiff) - "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$REMOTE" + "$merge_tool_path" -d -c "wincmd l" -f "$LOCAL" "$REMOTE" ;; xxdiff) @@ -160,6 +160,12 @@ init_merge_tool_path() { merge_tool_path=$(git config mergetool."$1".path) if test -z "$merge_tool_path"; then case "$1" in + vimdiff) + merge_tool_path=vim + ;; + gvimdiff) + merge_tool_path=gvim + ;; emerge) merge_tool_path=emacs ;; diff --git a/git-mergetool.sh b/git-mergetool.sh index 87fa88af55..6e611e94a4 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -214,12 +214,12 @@ merge_file () { ;; vimdiff) touch "$BACKUP" - "$merge_tool_path" -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE" + "$merge_tool_path" -d -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE" check_unchanged ;; gvimdiff) touch "$BACKUP" - "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE" + "$merge_tool_path" -d -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE" check_unchanged ;; xxdiff) @@ -359,6 +359,12 @@ init_merge_tool_path() { merge_tool_path=`git config mergetool.$1.path` if test -z "$merge_tool_path" ; then case "$1" in + vimdiff) + merge_tool_path=vim + ;; + gvimdiff) + merge_tool_path=gvim + ;; emerge) merge_tool_path=emacs ;; From 57d87482147e062ac1024f293f513a2400825fcb Mon Sep 17 00:00:00 2001 From: Markus Heidelberg Date: Sun, 5 Apr 2009 14:43:27 +0200 Subject: [PATCH 210/253] git-mergetool: add new merge tool TortoiseMerge TortoiseMerge comes with TortoiseSVN or TortoiseGit for Windows. It can only be used as a merge tool with an existing base file. It cannot be used without a base nor as a diff tool. The documentation only mentions the slash '/' as command line option prefix, which refused to work, but the parser also accepts the dash '-' See http://code.google.com/p/msysgit/issues/detail?id=226 Signed-off-by: Markus Heidelberg --- Documentation/git-mergetool.txt | 3 ++- Documentation/merge-config.txt | 2 +- git-mergetool.sh | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Documentation/git-mergetool.txt b/Documentation/git-mergetool.txt index 5d3c632872..5edac4d267 100644 --- a/Documentation/git-mergetool.txt +++ b/Documentation/git-mergetool.txt @@ -26,7 +26,8 @@ OPTIONS --tool=:: Use the merge resolution program specified by . Valid merge tools are: - kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, and opendiff + kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, + tortoisemerge and opendiff + If a merge resolution program is not specified, 'git-mergetool' will use the configuration variable `merge.tool`. If the diff --git a/Documentation/merge-config.txt b/Documentation/merge-config.txt index 9c44af8e4a..8c10f66702 100644 --- a/Documentation/merge-config.txt +++ b/Documentation/merge-config.txt @@ -23,7 +23,7 @@ merge.tool:: Controls which merge resolution program is used by linkgit:git-mergetool[1]. Valid built-in values are: "kdiff3", "tkdiff", "meld", "xxdiff", "emerge", "vimdiff", "gvimdiff", - "ecmerge" and + "ecmerge", tortoisemerge and "opendiff". Any other value is treated is custom merge tool and there must be a corresponding mergetool..cmd option. diff --git a/git-mergetool.sh b/git-mergetool.sh index 6e611e94a4..be9717a2f1 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -265,6 +265,16 @@ merge_file () { fi status=$? ;; + tortoisemerge) + if base_present ; then + touch "$BACKUP" + "$merge_tool_path" -base:"$BASE" -mine:"$LOCAL" -theirs:"$REMOTE" -merged:"$MERGED" + check_unchanged + else + echo "TortoiseMerge cannot be used without a base" 1>&2 + status=1 + fi + ;; *) if test -n "$merge_tool_cmd"; then if test "$merge_tool_trust_exit_code" = "false"; then @@ -345,7 +355,7 @@ valid_custom_tool() valid_tool() { case "$1" in - kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge) + kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge | tortoisemerge) ;; # happy *) if ! valid_custom_tool "$1"; then @@ -404,9 +414,9 @@ fi if test -z "$merge_tool" ; then if test -n "$DISPLAY"; then if test -n "$GNOME_DESKTOP_SESSION_ID" ; then - merge_tool_candidates="meld kdiff3 tkdiff xxdiff gvimdiff" + merge_tool_candidates="meld kdiff3 tkdiff xxdiff tortoisemerge gvimdiff" else - merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff" + merge_tool_candidates="kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff" fi fi if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then From 7ba615a300fe2742e8d32f0313c6ee9a1a1aaed3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 7 Apr 2009 17:40:03 +0200 Subject: [PATCH 211/253] Add a simple getpass() for MinGW This should be replaced with a graphical getpass() at some stage. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index d50186e5f5..2ab5bbec1f 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1157,3 +1157,18 @@ int link(const char *oldpath, const char *newpath) } return 0; } + +char *getpass(const char *prompt) +{ + struct strbuf buf = STRBUF_INIT; + + fputs(prompt, stderr); + for (;;) { + char c = _getch(); + if (c == '\r' || c == '\n') + break; + strbuf_addch(&buf, c); + } + fputs("\n", stderr); + return strbuf_detach(&buf, NULL); +} From 064f4d6929437a215874fc3defd1f22467ee8596 Mon Sep 17 00:00:00 2001 From: Peter Harris Date: Tue, 24 Mar 2009 13:00:33 -0400 Subject: [PATCH 212/253] Enable THREADED_DELTA_SEARCH for mingw Signed-off-by: Johannes Schindelin --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b71bdd2421..9e2b41c4b1 100644 --- a/Makefile +++ b/Makefile @@ -811,7 +811,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease NO_MEMMEM = YesPlease - NO_PTHREADS = YesPlease + THREADED_DELTA_SEARCH = YesPlease NEEDS_LIBICONV = YesPlease OLD_ICONV = YesPlease NO_C99_FORMAT = YesPlease From a36adafee41ad44aa35b419fe1db147964ac7128 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 13 Apr 2009 21:43:27 +0200 Subject: [PATCH 213/253] Revert accidentally merged commits This reverts the following commits: abd1fa1 test-suite: Log everything to a file in non-verbose mode 50f37d1 Implement test-stat the prints the modification time. fc7fe85 gitk: Swap positions of 'next' and 'prev' buttons in the 'Find' section. f80f1db Need diff -u -b in t7401 because some lines end in CRLF. 85763c8 Implement thread-specific die() routines; use one in start_async(). 4eb0463 Make report() from usage.c public as vreport(). ddce705 Skip tests that fail due to incomplete implementations, missing tools... a094080 Do not issue the warning about the fallback of the PREFIX. These were brought in by the merge commit dc8b641 that merged my private work-in-progress branch. Only these two commits remain: 1e52e22 Windows: Work around intermittent failures in mingw_rename ccd3859 Windows: Better support PAGER settings with spaces in the path Signed-off-by: Johannes Sixt --- exec_cmd.c | 7 +- git-compat-util.h | 1 - gitk-git/gitk | 2 +- run-command.c | 16 --- t/.gitignore | 1 - t/Makefile | 23 +---- t/t0040-parse-options.sh | 3 - t/t1503-rev-parse-verify.sh | 1 - t/t4150-am.sh | 6 +- t/t5505-remote.sh | 1 - t/t6022-merge-rename.sh | 177 +++++++++++++++++++++++++------- t/t6034-merge-rename-nocruft.sh | 65 +++++++++--- t/t7401-submodule-summary.sh | 2 - t/t9300-fast-import.sh | 16 ++- t/test-lib.sh | 15 +-- test-stat.c | 170 ------------------------------ usage.c | 40 ++------ 17 files changed, 229 insertions(+), 317 deletions(-) delete mode 100644 test-stat.c diff --git a/exec_cmd.c b/exec_cmd.c index 13e036fc37..408e4e55e1 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -28,10 +28,9 @@ const char *system_path(const char *path) !(prefix = strip_path_suffix(argv0_path, BINDIR)) && !(prefix = strip_path_suffix(argv0_path, "git"))) { prefix = PREFIX; - /* - * RUNTIME_PREFIX requested, but prefix computation failed. - * Using static fallback. - */ + fprintf(stderr, "RUNTIME_PREFIX requested, " + "but prefix computation failed. " + "Using static fallback '%s'.\n", prefix); } #endif diff --git a/git-compat-util.h b/git-compat-util.h index d94c683e0c..f09f244061 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -150,7 +150,6 @@ #endif /* General helper functions */ -extern void vreport(const char *prefix, const char *err, va_list params); extern void usage(const char *err) NORETURN; extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/gitk-git/gitk b/gitk-git/gitk index f43ca48dff..a2c589f749 100644 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -2057,7 +2057,7 @@ proc makewindow {} { button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1} button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1} label .tf.lbar.flab2 -text " [mc "commit"] " - pack .tf.lbar.flabel .tf.lbar.fprev .tf.lbar.fnext .tf.lbar.flab2 \ + pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \ -side left -fill y set gdttype [mc "containing:"] set gm [tk_optionMenu .tf.lbar.gdttype gdttype \ diff --git a/run-command.c b/run-command.c index 09725b983f..b05c734d05 100644 --- a/run-command.c +++ b/run-command.c @@ -293,22 +293,9 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const } #ifdef __MINGW32__ -static int close_fd = -1; -NORETURN void die_from_thread(const char *err, va_list params) -{ - vreport("fatal: ", err, params); - if (close_fd >= 0) - close(close_fd); - _endthreadex(128); - exit(128); /* silence compiler */ -} - static __stdcall unsigned run_thread(void *data) { struct async *async = data; - close_fd = async->fd_for_proc; - extern NORETURN void die_from_thread(const char *err, va_list params); - set_die_routine(die_from_thread); return async->proc(async->fd_for_proc, async->data); } #endif @@ -362,9 +349,6 @@ int finish_async(struct async *async) else if (!GetExitCodeThread(async->tid, &ret)) ret = error("cannot get thread exit code: %lu", GetLastError()); CloseHandle(async->tid); - if (ret) - /* test suite tests for this string */ - error("waitpid (async) failed"); #endif return ret; } diff --git a/t/.gitignore b/t/.gitignore index dbe58eb9b3..7dcbb232cd 100644 --- a/t/.gitignore +++ b/t/.gitignore @@ -1,3 +1,2 @@ /trash directory* /test-results -/test-log diff --git a/t/Makefile b/t/Makefile index 6eefc08fea..bf816fc850 100644 --- a/t/Makefile +++ b/t/Makefile @@ -21,7 +21,7 @@ $(T): @echo "*** $@ ***"; GIT_CONFIG=.git/config '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS) pre-clean: - $(RM) -r test-results test-log + $(RM) -r test-results clean: $(RM) -r 'trash directory'.* test-results @@ -42,24 +42,3 @@ valgrind: GIT_TEST_OPTS=--valgrind $(MAKE) .PHONY: pre-clean $(T) aggregate-results clean valgrind - --include Makefile.local - -GIT_SKIP_TESTS := $(GIT_SKIP_TESTS_LOCAL) -# git am does not treat absolute path to mbox file correctly -GIT_SKIP_TESTS += t4150.18 -# output contains CRLF (I think) -GIT_SKIP_TESTS += t7401.14 -# output contains CRLF (I think) -GIT_SKIP_TESTS += t7508.1[68] -# cannot run fake.sendmail for some reason -GIT_SKIP_TESTS += t9001 -# there's a problem with GIT_DIR in test 12 (why only there?) -# needs fixups with $PWD instead of $(pwd) - tries to run rsh c -GIT_SKIP_TESTS += t9200 -# CGI.pm not found -GIT_SKIP_TESTS += t9500 -export GIT_SKIP_TESTS - -NO_SVN_TESTS=SkipThem -export NO_SVN_TESTS diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index da272e0124..e38241c80a 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -39,7 +39,6 @@ Standard options EOF test_expect_success 'test help' ' - set +x && test_must_fail test-parse-options -h > output 2> output.err && test ! -s output && test_cmp expect.err output.err @@ -159,7 +158,6 @@ error: did you mean \`--boolean\` (with two dashes ?) EOF test_expect_success 'detect possible typos' ' - set +x && test_must_fail test-parse-options -boolean > output 2> output.err && test ! -s output && test_cmp typo.err output.err @@ -225,7 +223,6 @@ Callback: "not set", 1 EOF test_expect_success 'OPT_CALLBACK() and callback errors work' ' - set +x && test_must_fail test-parse-options --no-length > output 2> output.err && test_cmp expect output && test_cmp expect.err output.err diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh index 4c677340f4..cc65394947 100755 --- a/t/t1503-rev-parse-verify.sh +++ b/t/t1503-rev-parse-verify.sh @@ -71,7 +71,6 @@ test_expect_success 'fails with any bad rev or many good revs' ' ' test_expect_success 'fails silently when using -q' ' - set +x && test_must_fail git rev-parse --verify --quiet 2>error && test -z "$(cat error)" && test_must_fail git rev-parse -q --verify foo 2>error && diff --git a/t/t4150-am.sh b/t/t4150-am.sh index 5e65afa0c1..a8aa02e936 100755 --- a/t/t4150-am.sh +++ b/t/t4150-am.sh @@ -245,7 +245,11 @@ test_expect_success 'am works from file (relative path given) in subdirectory' ' test -z "$(git diff second)" ' -test_expect_success 'am works from file (absolute path given) in subdirectory' ' +case $(uname -s) in +*MINGW*) test_expect=test_expect_failure;; +*) test_expect=test_expect_success;; +esac +$test_expect 'am works from file (absolute path given) in subdirectory' ' rm -fr subdir && git checkout first && P=$(pwd) && diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 7267b1ae70..5ec668d6d8 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -247,7 +247,6 @@ EOF test_expect_success 'set-head --auto fails w/multiple HEADs' ' (cd test && - set +x && test_must_fail git remote set-head --auto two >output 2>&1 && test_cmp expect output) ' diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh index d69a364cae..e3f7ae8120 100755 --- a/t/t6022-merge-rename.sh +++ b/t/t6022-merge-rename.sh @@ -95,15 +95,30 @@ git checkout master' test_expect_success 'pull renaming branch into unrenaming one' \ ' git show-branch - test_must_fail git pull . white && - git ls-files -s && - test "$(git ls-files -u B | wc -l)" -eq 3 && - test "$(git ls-files -s N | wc -l)" -eq 1 && + git pull . white && { + echo "BAD: should have conflicted" + return 1 + } + git ls-files -s + test "$(git ls-files -u B | wc -l)" -eq 3 || { + echo "BAD: should have left stages for B" + return 1 + } + test "$(git ls-files -s N | wc -l)" -eq 1 || { + echo "BAD: should have merged N" + return 1 + } sed -ne "/^g/{ p q - }" B | grep master && - test "$(git diff white N | wc -l)" -eq 0 + }" B | grep master || { + echo "BAD: should have listed our change first" + return 1 + } + test "$(git diff white N | wc -l)" -eq 0 || { + echo "BAD: should have taken colored branch" + return 1 + } ' test_expect_success 'pull renaming branch into another renaming one' \ @@ -111,44 +126,95 @@ test_expect_success 'pull renaming branch into another renaming one' \ rm -f B git reset --hard git checkout red - test_must_fail git pull . white && - test "$(git ls-files -u B | wc -l)" -eq 3 && - test "$(git ls-files -s N | wc -l)" -eq 1 && + git pull . white && { + echo "BAD: should have conflicted" + return 1 + } + test "$(git ls-files -u B | wc -l)" -eq 3 || { + echo "BAD: should have left stages" + return 1 + } + test "$(git ls-files -s N | wc -l)" -eq 1 || { + echo "BAD: should have merged N" + return 1 + } sed -ne "/^g/{ p q - }" B | grep red && - test "$(git diff white N | wc -l)" -eq 0 + }" B | grep red || { + echo "BAD: should have listed our change first" + return 1 + } + test "$(git diff white N | wc -l)" -eq 0 || { + echo "BAD: should have taken colored branch" + return 1 + } ' test_expect_success 'pull unrenaming branch into renaming one' \ ' git reset --hard git show-branch - test_must_fail git pull . master && - test "$(git ls-files -u B | wc -l)" -eq 3 && - test "$(git ls-files -s N | wc -l)" -eq 1 && + git pull . master && { + echo "BAD: should have conflicted" + return 1 + } + test "$(git ls-files -u B | wc -l)" -eq 3 || { + echo "BAD: should have left stages" + return 1 + } + test "$(git ls-files -s N | wc -l)" -eq 1 || { + echo "BAD: should have merged N" + return 1 + } sed -ne "/^g/{ p q - }" B | grep red && - test "$(git diff white N | wc -l)" -eq 0 + }" B | grep red || { + echo "BAD: should have listed our change first" + return 1 + } + test "$(git diff white N | wc -l)" -eq 0 || { + echo "BAD: should have taken colored branch" + return 1 + } ' test_expect_success 'pull conflicting renames' \ ' git reset --hard git show-branch - test_must_fail git pull . blue && - test "$(git ls-files -u A | wc -l)" -eq 1 && - test "$(git ls-files -u B | wc -l)" -eq 1 && - test "$(git ls-files -u C | wc -l)" -eq 1 && - test "$(git ls-files -s N | wc -l)" -eq 1 && + git pull . blue && { + echo "BAD: should have conflicted" + return 1 + } + test "$(git ls-files -u A | wc -l)" -eq 1 || { + echo "BAD: should have left a stage" + return 1 + } + test "$(git ls-files -u B | wc -l)" -eq 1 || { + echo "BAD: should have left a stage" + return 1 + } + test "$(git ls-files -u C | wc -l)" -eq 1 || { + echo "BAD: should have left a stage" + return 1 + } + test "$(git ls-files -s N | wc -l)" -eq 1 || { + echo "BAD: should have merged N" + return 1 + } sed -ne "/^g/{ p q - }" B | grep red && - test "$(git diff white N | wc -l)" -eq 0 + }" B | grep red || { + echo "BAD: should have listed our change first" + return 1 + } + test "$(git diff white N | wc -l)" -eq 0 || { + echo "BAD: should have taken colored branch" + return 1 + } ' test_expect_success 'interference with untracked working tree file' ' @@ -156,8 +222,14 @@ test_expect_success 'interference with untracked working tree file' ' git reset --hard git show-branch echo >A this file should not matter - test_must_fail git pull . white && - test -f A + git pull . white && { + echo "BAD: should have conflicted" + return 1 + } + test -f A || { + echo "BAD: should have left A intact" + return 1 + } ' test_expect_success 'interference with untracked working tree file' ' @@ -167,8 +239,14 @@ test_expect_success 'interference with untracked working tree file' ' git show-branch rm -f A echo >A this file should not matter - test_must_fail git pull . red && - test -f A + git pull . red && { + echo "BAD: should have conflicted" + return 1 + } + test -f A || { + echo "BAD: should have left A intact" + return 1 + } ' test_expect_success 'interference with untracked working tree file' ' @@ -178,8 +256,14 @@ test_expect_success 'interference with untracked working tree file' ' git checkout -f master git tag -f anchor git show-branch - git pull . yellow - test ! -f M && + git pull . yellow || { + echo "BAD: should have cleanly merged" + return 1 + } + test -f M && { + echo "BAD: should have removed M" + return 1 + } git reset --hard anchor ' @@ -192,8 +276,14 @@ test_expect_success 'updated working tree file should prevent the merge' ' git show-branch echo >>M one line addition cat M >M.saved - test_must_fail git pull . yellow && - diff M M.saved && + git pull . yellow && { + echo "BAD: should have complained" + return 1 + } + diff M M.saved || { + echo "BAD: should have left M intact" + return 1 + } rm -f M.saved ' @@ -207,8 +297,14 @@ test_expect_success 'updated working tree file should prevent the merge' ' echo >>M one line addition cat M >M.saved git update-index M - test_must_fail git pull . yellow && - diff M M.saved && + git pull . yellow && { + echo "BAD: should have complained" + return 1 + } + diff M M.saved || { + echo "BAD: should have left M intact" + return 1 + } rm -f M.saved ' @@ -220,9 +316,18 @@ test_expect_success 'interference with untracked working tree file' ' git tag -f anchor git show-branch echo >M this file should not matter - git pull . master && - test -f M && - ! (git ls-files -s | grep M) && + git pull . master || { + echo "BAD: should have cleanly merged" + return 1 + } + test -f M || { + echo "BAD: should have left M intact" + return 1 + } + git ls-files -s | grep M && { + echo "BAD: M must be untracked in the result" + return 1 + } git reset --hard anchor ' diff --git a/t/t6034-merge-rename-nocruft.sh b/t/t6034-merge-rename-nocruft.sh index d1b1d1a441..65be95fbaa 100755 --- a/t/t6034-merge-rename-nocruft.sh +++ b/t/t6034-merge-rename-nocruft.sh @@ -73,12 +73,33 @@ test_expect_success 'merge white into red (A->B,M->N)' \ ' git checkout -b red-white red && git merge white && - git write-tree >/dev/null && - test -f B && - test -f N && - test -f R && - ! test -f A && - ! test -f M + git write-tree >/dev/null || { + echo "BAD: merge did not complete" + return 1 + } + + test -f B || { + echo "BAD: B does not exist in working directory" + return 1 + } + test -f N || { + echo "BAD: N does not exist in working directory" + return 1 + } + test -f R || { + echo "BAD: R does not exist in working directory" + return 1 + } + + test -f A && { + echo "BAD: A still exists in working directory" + return 1 + } + test -f M && { + echo "BAD: M still exists in working directory" + return 1 + } + return 0 ' # This test broke in 8371234ecaaf6e14fe3f2082a855eff1bbd79ae9 @@ -87,12 +108,32 @@ test_expect_success 'merge blue into white (A->B, mod A, A untracked)' \ git checkout -b white-blue white && echo dirty >A && git merge blue && - git write-tree >/dev/null && - test -f A && - test `cat A` = dirty && - test -f B && - test -f N && - ! test -f M + git write-tree >/dev/null || { + echo "BAD: merge did not complete" + return 1 + } + + test -f A || { + echo "BAD: A does not exist in working directory" + return 1 + } + test `cat A` = dirty || { + echo "BAD: A content is wrong" + return 1 + } + test -f B || { + echo "BAD: B does not exist in working directory" + return 1 + } + test -f N || { + echo "BAD: N does not exist in working directory" + return 1 + } + test -f M && { + echo "BAD: M still exists in working directory" + return 1 + } + return 0 ' test_done diff --git a/t/t7401-submodule-summary.sh b/t/t7401-submodule-summary.sh index 50030c1a49..d2d6d5be25 100755 --- a/t/t7401-submodule-summary.sh +++ b/t/t7401-submodule-summary.sh @@ -14,8 +14,6 @@ esac . ./test-lib.sh -export GIT_TEST_CMP='diff -u -b' # some lines end in CRLF - add_file () { sm=$1 shift diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh index a7a13b7e57..821be7ce8d 100755 --- a/t/t9300-fast-import.sh +++ b/t/t9300-fast-import.sh @@ -399,8 +399,20 @@ from refs/heads/branch INPUT_END test_expect_success \ 'F: non-fast-forward update skips' \ - 'test_must_fail git fast-import &1 if test "$verbose" = "t" then - set_x= set_nox= exec 4>&2 3>&1 else - test_log_dir="$TEST_DIRECTORY/test-log" - mkdir -p "$test_log_dir" - test_log_path="$test_log_dir/${0%.sh}-$$" - set_x='set -x;' - set_nox='set +x' - exec 3>"$test_log_path" 4>&3 + exec 4>/dev/null 3>/dev/null fi test_failure=0 @@ -310,9 +303,8 @@ test_debug () { } test_run_ () { - eval >&3 2>&4 $set_x "$1" ' - eval_ret=$? - $set_nox' + eval >&3 2>&4 "$1" + eval_ret="$?" return 0 } @@ -548,6 +540,7 @@ test_done () { # Test the binaries we have just built. The tests are kept in # t/ subdirectory and are run in 'trash directory' subdirectory. +TEST_DIRECTORY=$(pwd) if test -z "$valgrind" then if test -z "$GIT_TEST_INSTALLED" diff --git a/test-stat.c b/test-stat.c deleted file mode 100644 index ff859412f0..0000000000 --- a/test-stat.c +++ /dev/null @@ -1,170 +0,0 @@ -#include -#include -#include - -#ifdef EMULATE -#define HINT fprintf(stderr, "emulated stat\n") -#include -#include -#include - -/* Use mingw_lstat() instead of lstat()/stat() and - * mingw_fstat() instead of fstat() on Windows. - */ -int mingw_lstat(const char *file_name, struct stat *buf); -int mingw_fstat(int fd, struct stat *buf); -#define fstat mingw_fstat -#define lstat mingw_lstat -#define stat(x,y) mingw_lstat(x,y) - -#define PATH_MAX 260 - -static inline int file_attr_to_st_mode (DWORD attr) -{ - int fMode = S_IREAD; - if (attr & FILE_ATTRIBUTE_DIRECTORY) - fMode |= S_IFDIR; - else - fMode |= S_IFREG; - if (!(attr & FILE_ATTRIBUTE_READONLY)) - fMode |= S_IWRITE; - return fMode; -} - -static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata) -{ - if (GetFileAttributesExA(fname, GetFileExInfoStandard, fdata)) - return 0; - - switch (GetLastError()) { - case ERROR_ACCESS_DENIED: - case ERROR_SHARING_VIOLATION: - case ERROR_LOCK_VIOLATION: - case ERROR_SHARING_BUFFER_EXCEEDED: - return EACCES; - case ERROR_BUFFER_OVERFLOW: - return ENAMETOOLONG; - case ERROR_NOT_ENOUGH_MEMORY: - return ENOMEM; - default: - return ENOENT; - } -} - -static inline time_t filetime_to_time_t(const FILETIME *ft) -{ - long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; - winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */ - winTime /= 10000000; /* Nano to seconds resolution */ - return (time_t)winTime; -} - -/* We keep the do_lstat code in a separate function to avoid recursion. - * When a path ends with a slash, the stat will fail with ENOENT. In - * this case, we strip the trailing slashes and stat again. - */ -static int do_lstat(const char *file_name, struct stat *buf) -{ - WIN32_FILE_ATTRIBUTE_DATA fdata; - - if (!(errno = get_file_attr(file_name, &fdata))) { - buf->st_ino = 0; - buf->st_gid = 0; - buf->st_uid = 0; - buf->st_nlink = 1; - buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); - buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */ - buf->st_dev = buf->st_rdev = 0; /* not used by Git */ - buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); - buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); - buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); - return 0; - } - return -1; -} - -/* We provide our own lstat/fstat functions, since the provided - * lstat/fstat functions are so slow. These stat functions are - * tailored for Git's usage (read: fast), and are not meant to be - * complete. Note that Git stat()s are redirected to mingw_lstat() - * too, since Windows doesn't really handle symlinks that well. - */ -int mingw_lstat(const char *file_name, struct stat *buf) -{ - int namelen; - static char alt_name[PATH_MAX]; - - if (!do_lstat(file_name, buf)) - return 0; - - /* if file_name ended in a '/', Windows returned ENOENT; - * try again without trailing slashes - */ - if (errno != ENOENT) - return -1; - - namelen = strlen(file_name); - if (namelen && file_name[namelen-1] != '/') - return -1; - while (namelen && file_name[namelen-1] == '/') - --namelen; - if (!namelen || namelen >= PATH_MAX) - return -1; - - memcpy(alt_name, file_name, namelen); - alt_name[namelen] = 0; - return do_lstat(alt_name, buf); -} - -#undef fstat -int mingw_fstat(int fd, struct stat *buf) -{ - HANDLE fh = (HANDLE)_get_osfhandle(fd); - BY_HANDLE_FILE_INFORMATION fdata; - - if (fh == INVALID_HANDLE_VALUE) { - errno = EBADF; - return -1; - } - /* direct non-file handles to MS's fstat() */ - if (GetFileType(fh) != FILE_TYPE_DISK) - return fstat(fd, buf); - - if (GetFileInformationByHandle(fh, &fdata)) { - buf->st_ino = 0; - buf->st_gid = 0; - buf->st_uid = 0; - buf->st_nlink = 1; - buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); - buf->st_size = fdata.nFileSizeLow; /* Can't use nFileSizeHigh, since it's not a stat64 */ - buf->st_dev = buf->st_rdev = 0; /* not used by Git */ - buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); - buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); - buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); - return 0; - } - errno = EBADF; - return -1; -} - -#else -#define HINT (void)0 -#endif - -int main(int argc, char**argv) -{ - int i; - int err = 0; - HINT; - for (i = 1; i < argc; i++) - { - struct stat st; - if (stat(argv[i], &st)) { - perror(argv[i]); - err = 1; - continue; - } - printf("%s: %s\n", argv[i], ctime(&st.st_mtime)); - } - return err; -} diff --git a/usage.c b/usage.c index 9e35e8a390..820d09f92b 100644 --- a/usage.c +++ b/usage.c @@ -5,7 +5,7 @@ */ #include "git-compat-util.h" -void vreport(const char *prefix, const char *err, va_list params) +static void report(const char *prefix, const char *err, va_list params) { char msg[1024]; vsnprintf(msg, sizeof(msg), err, params); @@ -20,52 +20,30 @@ static NORETURN void usage_builtin(const char *err) static NORETURN void die_builtin(const char *err, va_list params) { - vreport("fatal: ", err, params); + report("fatal: ", err, params); exit(128); } static void error_builtin(const char *err, va_list params) { - vreport("error: ", err, params); + report("error: ", err, params); } static void warn_builtin(const char *warn, va_list params) { - vreport("warning: ", warn, params); + report("warning: ", warn, params); } -typedef void (*die_fn_t)(const char *err, va_list params) NORETURN; - -static DWORD tls_index; - -static void tls_init(void) __attribute__((constructor)); -static void tls_init(void) -{ - tls_index = TlsAlloc(); -} - -struct routines { - die_fn_t die_routine; -}; /* If we are in a dlopen()ed .so write to a global variable would segfault * (ugh), so keep things static. */ static void (*usage_routine)(const char *err) NORETURN = usage_builtin; +static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin; static void (*error_routine)(const char *err, va_list params) = error_builtin; static void (*warn_routine)(const char *err, va_list params) = warn_builtin; void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN) { - struct routines *r = TlsGetValue(tls_index); - if (r == NULL) { - /* avoid die()! */ - r = calloc(sizeof(*r), 1); - if (r == NULL) { - fprintf(stderr, "cannot allocate thread-local storage"); - return; - } - TlsSetValue(tls_index, r); - } - r->die_routine = routine; + die_routine = routine; } void usage(const char *err) @@ -76,13 +54,9 @@ void usage(const char *err) void die(const char *err, ...) { va_list params; - struct routines *r = TlsGetValue(tls_index); va_start(params, err); - if (r == NULL || r->die_routine == NULL) - die_builtin(err, params); - else - r->die_routine(err, params); + die_routine(err, params); va_end(params); } From 3d3ecb65e34a62b83648516150ec8a99e1b3ace7 Mon Sep 17 00:00:00 2001 From: Pat Thoyts Date: Wed, 15 Apr 2009 17:14:03 +0100 Subject: [PATCH 214/253] gitk: avoid crash if closed while reading references As recorded in msysGit issue 125 if the user closes gitk while it reports itself as still reading references then Tk will crash in the geometry management code. This has been fixed for Tk 8.5.7 and above. This patch avoids the problem by flushing outstanding geometry events before calling the readrefs procedure. See also http://code.google.com/p/msysgit/issues/detail?id=125 Signed-off-by: Pat Thoyts Signed-off-by: Johannes Schindelin --- gitk-git/gitk | 1 + 1 file changed, 1 insertion(+) diff --git a/gitk-git/gitk b/gitk-git/gitk index a2c589f749..e78459feda 100644 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -10887,6 +10887,7 @@ makewindow # wait for the window to become visible tkwait visibility . wm title . "[file tail $argv0]: [file tail [pwd]]" +update readrefs if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} { From 933833643ac7e5c08cb8ea3cb00e1fa128e57960 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 19 Apr 2009 17:27:59 +0200 Subject: [PATCH 215/253] Makefile: Clean up after USE_WIN32_MMAP went upstream This commit removes left overs of 7e5a4c563667d011c0655679be3190461884a78e, which was applied by Junio in a slightly modified version in b130a72b274441bb5d687de93efef4d990c40c0a. --- Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Makefile b/Makefile index 9e2b41c4b1..b851df818a 100644 --- a/Makefile +++ b/Makefile @@ -1010,10 +1010,6 @@ else COMPAT_OBJS += compat/win32mmap.o endif endif -ifdef USE_WIN32_MMAP - COMPAT_CFLAGS += -DUSE_WIN32_MMAP - COMPAT_OBJS += compat/win32mmap.o -endif ifdef NO_PREAD COMPAT_CFLAGS += -DNO_PREAD COMPAT_OBJS += compat/pread.o From aa3abfc9852a4d4cfaa7f0042102eb56ed2e0daa Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 20 Apr 2009 21:43:11 +0200 Subject: [PATCH 216/253] Work around a regression in Windows 7, causing erase_in_line() to crash sometimes The function FillConsoleOutputCharacterA() was pretty content in XP to take a NULL pointer if we did not want to store the number of written columns. In Windows 7, it crashes, but only when called from within Git Bash, not from within cmd.exe. Go figure. Signed-off-by: Johannes Schindelin --- compat/winansi.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compat/winansi.c b/compat/winansi.c index 44dc293ad3..4bee335f9e 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -80,6 +80,7 @@ static void set_console_attr(void) static void erase_in_line(void) { CONSOLE_SCREEN_BUFFER_INFO sbi; + long dummy; /* Needed for Windows 7 (or Vista) regression */ if (!console) return; @@ -87,7 +88,7 @@ static void erase_in_line(void) GetConsoleScreenBufferInfo(console, &sbi); FillConsoleOutputCharacterA(console, ' ', sbi.dwSize.X - sbi.dwCursorPosition.X, sbi.dwCursorPosition, - NULL); + &dummy); } From aea0a0601b58268e40e60441567d004f47de72f3 Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Wed, 1 Apr 2009 14:10:24 +0200 Subject: [PATCH 217/253] Add custom memory allocator to MinGW and MacOS builds The standard allocator on Windows is pretty bad prior to Windows Vista, and nedmalloc is better than the modified dlmalloc provided with newer versions of the MinGW libc. NedMalloc stats in Git ---------------------- All results are the best result out of 3 runs. The benchmarks have been done on different hardware, so the repack times are not comparable. These benchmarks are all based on 'git repack -adf' on the Linux kernel. XP ----------------------------------------------- MinGW Threads Total Time Speed ----------------------------------------------- 3.4.2 (1T) 00:12:28.422 3.4.2 + nedmalloc (1T) 00:07:25.437 1.68x 3.4.5 (1T) 00:12:20.718 3.4.5 + nedmalloc (1T) 00:07:24.809 1.67x 4.3.3-tdm (1T) 00:12:01.843 4.3.3-tdm + nedmalloc (1T) 00:07:16.468 1.65x 4.3.3-tdm (2T) 00:07:35.062 4.3.3-tdm + nedmalloc (2T) 00:04:57.874 1.54x Vista ----------------------------------------------- MinGW Threads Total Time Speed ----------------------------------------------- 4.3.3-tdm (1T) 00:07:40.844 4.3.3-tdm + nedmalloc (1T) 00:07:17.548 1.05x 4.3.3-tdm (2T) 00:05:33.746 4.3.3-tdm + nedmalloc (2T) 00:05:27.334 1.02x Mac Mini ----------------------------------------------- GCC Threads Total Time Speed ----------------------------------------------- i686-darwin9-4.0.1 (2T) 00:09:57.346 i686-darwin9-4.0.1+ned (2T) 00:08:51.072 1.12x Signed-off-by: Marius Storm-Olsen --- Makefile | 9 + compat/nedmalloc/License.txt | 23 + compat/nedmalloc/Readme.txt | 136 + compat/nedmalloc/malloc.c.h | 5750 ++++++++++++++++++++++++++++++++++ compat/nedmalloc/nedmalloc.c | 966 ++++++ compat/nedmalloc/nedmalloc.h | 180 ++ 6 files changed, 7064 insertions(+) create mode 100644 compat/nedmalloc/License.txt create mode 100644 compat/nedmalloc/Readme.txt create mode 100644 compat/nedmalloc/malloc.c.h create mode 100644 compat/nedmalloc/nedmalloc.c create mode 100644 compat/nedmalloc/nedmalloc.h diff --git a/Makefile b/Makefile index b851df818a..bec8a4229e 100644 --- a/Makefile +++ b/Makefile @@ -165,6 +165,9 @@ all:: # Define NO_EXTERNAL_GREP if you don't want "git grep" to ever call # your external grep (e.g., if your system lacks grep, if its grep is # broken, or spawning external process is slower than built-in grep git has). +# +# Define USE_NED_ALLOCATOR if you want to replace the platforms default +# memory allocators with the nedmalloc allocator written by Niall Douglas. GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE @$(SHELL_PATH) ./GIT-VERSION-GEN @@ -827,6 +830,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease NO_NSEC = YesPlease USE_WIN32_MMAP = YesPlease + USE_NED_ALLOCATOR = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o @@ -1106,6 +1110,11 @@ ifdef NO_EXTERNAL_GREP BASIC_CFLAGS += -DNO_EXTERNAL_GREP endif +ifdef USE_NED_ALLOCATOR + COMPAT_CFLAGS += -DUSE_NED_ALLOCATOR -DOVERRIDE_STRDUP -DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR -Icompat/nedmalloc + COMPAT_OBJS += compat/nedmalloc/nedmalloc.o +endif + ifeq ($(TCLTK_PATH),) NO_TCLTK=NoThanks endif diff --git a/compat/nedmalloc/License.txt b/compat/nedmalloc/License.txt new file mode 100644 index 0000000000..36b7cd93cd --- /dev/null +++ b/compat/nedmalloc/License.txt @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/compat/nedmalloc/Readme.txt b/compat/nedmalloc/Readme.txt new file mode 100644 index 0000000000..0534be7c20 --- /dev/null +++ b/compat/nedmalloc/Readme.txt @@ -0,0 +1,136 @@ +nedalloc v1.05 15th June 2008: +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +by Niall Douglas (http://www.nedprod.com/programs/portable/nedmalloc/) + +Enclosed is nedalloc, an alternative malloc implementation for multiple +threads without lock contention based on dlmalloc v2.8.4. It is more +or less a newer implementation of ptmalloc2, the standard allocator in +Linux (which is based on dlmalloc v2.7.0) but also contains a per-thread +cache for maximum CPU scalability. + +It is licensed under the Boost Software License which basically means +you can do anything you like with it. This does not apply to the malloc.c.h +file which remains copyright to others. + +It has been tested on win32 (x86), win64 (x64), Linux (x64), FreeBSD (x64) +and Apple MacOS X (x86). It works very well on all of these and is very +significantly faster than the system allocator on all of these platforms. + +By literally dropping in this allocator as a replacement for your system +allocator, you can see real world improvements of up to three times in normal +code! + +To use: +-=-=-=- +Drop in nedmalloc.h, nedmalloc.c and malloc.c.h into your project. +Configure using the instructions in nedmalloc.h. Run and enjoy. + +To test, compile test.c. It will run a comparison between your system +allocator and nedalloc and tell you how much faster nedalloc is. It also +serves as an example of usage. + +Notes: +-=-=-= +If you want the very latest version of this allocator, get it from the +TnFOX SVN repository at svn://svn.berlios.de/viewcvs/tnfox/trunk/src/nedmalloc + +Because of how nedalloc allocates an mspace per thread, it can cause +severe bloating of memory usage under certain allocation patterns. +You can substantially reduce this wastage by setting MAXTHREADSINPOOL +or the threads parameter to nedcreatepool() to a fraction of the number of +threads which would normally be in a pool at once. This will reduce +bloating at the cost of an increase in lock contention. If allocated size +is less than THREADCACHEMAX, locking is avoided 90-99% of the time and +if most of your allocations are below this value, you can safely set +MAXTHREADSINPOOL to one. + +You will suffer memory leakage unless you call neddisablethreadcache() +per pool for every thread which exits. This is because nedalloc cannot +portably know when a thread exits and thus when its thread cache can +be returned for use by other code. Don't forget pool zero, the system pool. + +For C++ type allocation patterns (where the same sizes of memory are +regularly allocated and deallocated as objects are created and destroyed), +the threadcache always benefits performance. If however your allocation +patterns are different, searching the threadcache may significantly slow +down your code - as a rule of thumb, if cache utilisation is below 80% +(see the source for neddisablethreadcache() for how to enable debug +printing in release mode) then you should disable the thread cache for +that thread. You can compile out the threadcache code by setting +THREADCACHEMAX to zero. + +Speed comparisons: +-=-=-=-=-=-=-=-=-= +See Benchmarks.xls for details. + +The enclosed test.c can do two things: it can be a torture test or a speed +test. The speed test is designed to be a representative synthetic +memory allocator test. It works by randomly mixing allocations with frees +with half of the allocation sizes being a two power multiple less than +512 bytes (to mimic C++ stack instantiated objects) and the other half +being a simple random value less than 16Kb. + +The real world code results are from Tn's TestIO benchmark. This is a +heavily multithreaded and memory intensive benchmark with a lot of branching +and other stuff modern processors don't like so much. As you'll note, the +test doesn't show the benefits of the threadcache mostly due to the saturation +of the memory bus being the limiting factor. + +ChangeLog: +-=-=-=-=-= +v1.05 15th June 2008: + * { 1042 } Added error check for TLSSET() and TLSFREE() macros. Thanks to +Markus Elfring for reporting this. + * { 1043 } Fixed a segfault when freeing memory allocated using +nedindependent_comalloc(). Thanks to Pavel Vozenilek for reporting this. + +v1.04 14th July 2007: + * Fixed a bug with the new optimised implementation that failed to lock +on a realloc under certain conditions. + * Fixed lack of thread synchronisation in InitPool() causing pool corruption + * Fixed a memory leak of thread cache contents on disabling. Thanks to Earl +Chew for reporting this. + * Added a sanity check for freed blocks being valid. + * Reworked test.c into being a torture test. + * Fixed GCC assembler optimisation misspecification + +v1.04alpha_svn915 7th October 2006: + * Fixed failure to unlock thread cache list if allocating a new list failed. +Thanks to Dmitry Chichkov for reporting this. Futher thanks to Aleksey Sanin. + * Fixed realloc(0, ) segfaulting. Thanks to Dmitry Chichkov for +reporting this. + * Made config defines #ifndef so they can be overriden by the build system. +Thanks to Aleksey Sanin for suggesting this. + * Fixed deadlock in nedprealloc() due to unnecessary locking of preferred +thread mspace when mspace_realloc() always uses the original block's mspace +anyway. Thanks to Aleksey Sanin for reporting this. + * Made some speed improvements by hacking mspace_malloc() to no longer lock +its mspace, thus allowing the recursive mutex implementation to be removed +with an associated speed increase. Thanks to Aleksey Sanin for suggesting this. + * Fixed a bug where allocating mspaces overran its max limit. Thanks to +Aleksey Sanin for reporting this. + +v1.03 10th July 2006: + * Fixed memory corruption bug in threadcache code which only appeared with >4 +threads and in heavy use of the threadcache. + +v1.02 15th May 2006: + * Integrated dlmalloc v2.8.4, fixing the win32 memory release problem and +improving performance still further. Speed is now up to twice the speed of v1.01 +(average is 67% faster). + * Fixed win32 critical section implementation. Thanks to Pavel Kuznetsov +for reporting this. + * Wasn't locking mspace if all mspaces were locked. Thanks to Pavel Kuznetsov +for reporting this. + * Added Apple Mac OS X support. + +v1.01 24th February 2006: + * Fixed multiprocessor scaling problems by removing sources of cache sloshing + * Earl Chew agilent com> sent patches for the following: + 1. size2binidx() wasn't working for default code path (non x86) + 2. Fixed failure to release mspace lock under certain circumstances which + caused a deadlock + +v1.00 1st January 2006: + * First release diff --git a/compat/nedmalloc/malloc.c.h b/compat/nedmalloc/malloc.c.h new file mode 100644 index 0000000000..1d9bbe0aba --- /dev/null +++ b/compat/nedmalloc/malloc.c.h @@ -0,0 +1,5750 @@ +/* + This is a version (aka dlmalloc) of malloc/free/realloc written by + Doug Lea and released to the public domain, as explained at + http://creativecommons.org/licenses/publicdomain. Send questions, + comments, complaints, performance data, etc to dl@cs.oswego.edu + +* Version pre-2.8.4 Mon Nov 27 11:22:37 2006 (dl at gee) + + Note: There may be an updated version of this malloc obtainable at + ftp://gee.cs.oswego.edu/pub/misc/malloc.c + Check before installing! + +* Quickstart + + This library is all in one file to simplify the most common usage: + ftp it, compile it (-O3), and link it into another program. All of + the compile-time options default to reasonable values for use on + most platforms. You might later want to step through various + compile-time and dynamic tuning options. + + For convenience, an include file for code using this malloc is at: + ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.4.h + You don't really need this .h file unless you call functions not + defined in your system include files. The .h file contains only the + excerpts from this file needed for using this malloc on ANSI C/C++ + systems, so long as you haven't changed compile-time options about + naming and tuning parameters. If you do, then you can create your + own malloc.h that does include all settings by cutting at the point + indicated below. Note that you may already by default be using a C + library containing a malloc that is based on some version of this + malloc (for example in linux). You might still want to use the one + in this file to customize settings or to avoid overheads associated + with library versions. + +* Vital statistics: + + Supported pointer/size_t representation: 4 or 8 bytes + size_t MUST be an unsigned type of the same width as + pointers. (If you are using an ancient system that declares + size_t as a signed type, or need it to be a different width + than pointers, you can use a previous release of this malloc + (e.g. 2.7.2) supporting these.) + + Alignment: 8 bytes (default) + This suffices for nearly all current machines and C compilers. + However, you can define MALLOC_ALIGNMENT to be wider than this + if necessary (up to 128bytes), at the expense of using more space. + + Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes) + 8 or 16 bytes (if 8byte sizes) + Each malloced chunk has a hidden word of overhead holding size + and status information, and additional cross-check word + if FOOTERS is defined. + + Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead) + 8-byte ptrs: 32 bytes (including overhead) + + Even a request for zero bytes (i.e., malloc(0)) returns a + pointer to something of the minimum allocatable size. + The maximum overhead wastage (i.e., number of extra bytes + allocated than were requested in malloc) is less than or equal + to the minimum size, except for requests >= mmap_threshold that + are serviced via mmap(), where the worst case wastage is about + 32 bytes plus the remainder from a system page (the minimal + mmap unit); typically 4096 or 8192 bytes. + + Security: static-safe; optionally more or less + The "security" of malloc refers to the ability of malicious + code to accentuate the effects of errors (for example, freeing + space that is not currently malloc'ed or overwriting past the + ends of chunks) in code that calls malloc. This malloc + guarantees not to modify any memory locations below the base of + heap, i.e., static variables, even in the presence of usage + errors. The routines additionally detect most improper frees + and reallocs. All this holds as long as the static bookkeeping + for malloc itself is not corrupted by some other means. This + is only one aspect of security -- these checks do not, and + cannot, detect all possible programming errors. + + If FOOTERS is defined nonzero, then each allocated chunk + carries an additional check word to verify that it was malloced + from its space. These check words are the same within each + execution of a program using malloc, but differ across + executions, so externally crafted fake chunks cannot be + freed. This improves security by rejecting frees/reallocs that + could corrupt heap memory, in addition to the checks preventing + writes to statics that are always on. This may further improve + security at the expense of time and space overhead. (Note that + FOOTERS may also be worth using with MSPACES.) + + By default detected errors cause the program to abort (calling + "abort()"). You can override this to instead proceed past + errors by defining PROCEED_ON_ERROR. In this case, a bad free + has no effect, and a malloc that encounters a bad address + caused by user overwrites will ignore the bad address by + dropping pointers and indices to all known memory. This may + be appropriate for programs that should continue if at all + possible in the face of programming errors, although they may + run out of memory because dropped memory is never reclaimed. + + If you don't like either of these options, you can define + CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything + else. And if if you are sure that your program using malloc has + no errors or vulnerabilities, you can define INSECURE to 1, + which might (or might not) provide a small performance improvement. + + Thread-safety: NOT thread-safe unless USE_LOCKS defined + When USE_LOCKS is defined, each public call to malloc, free, + etc is surrounded with either a pthread mutex or a win32 + spinlock (depending on WIN32). This is not especially fast, and + can be a major bottleneck. It is designed only to provide + minimal protection in concurrent environments, and to provide a + basis for extensions. If you are using malloc in a concurrent + program, consider instead using nedmalloc + (http://www.nedprod.com/programs/portable/nedmalloc/) or + ptmalloc (See http://www.malloc.de), which are derived + from versions of this malloc. + + System requirements: Any combination of MORECORE and/or MMAP/MUNMAP + This malloc can use unix sbrk or any emulation (invoked using + the CALL_MORECORE macro) and/or mmap/munmap or any emulation + (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system + memory. On most unix systems, it tends to work best if both + MORECORE and MMAP are enabled. On Win32, it uses emulations + based on VirtualAlloc. It also uses common C library functions + like memset. + + Compliance: I believe it is compliant with the Single Unix Specification + (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably + others as well. + +* Overview of algorithms + + This is not the fastest, most space-conserving, most portable, or + most tunable malloc ever written. However it is among the fastest + while also being among the most space-conserving, portable and + tunable. Consistent balance across these factors results in a good + general-purpose allocator for malloc-intensive programs. + + In most ways, this malloc is a best-fit allocator. Generally, it + chooses the best-fitting existing chunk for a request, with ties + broken in approximately least-recently-used order. (This strategy + normally maintains low fragmentation.) However, for requests less + than 256bytes, it deviates from best-fit when there is not an + exactly fitting available chunk by preferring to use space adjacent + to that used for the previous small request, as well as by breaking + ties in approximately most-recently-used order. (These enhance + locality of series of small allocations.) And for very large requests + (>= 256Kb by default), it relies on system memory mapping + facilities, if supported. (This helps avoid carrying around and + possibly fragmenting memory used only for large chunks.) + + All operations (except malloc_stats and mallinfo) have execution + times that are bounded by a constant factor of the number of bits in + a size_t, not counting any clearing in calloc or copying in realloc, + or actions surrounding MORECORE and MMAP that have times + proportional to the number of non-contiguous regions returned by + system allocation routines, which is often just 1. In real-time + applications, you can optionally suppress segment traversals using + NO_SEGMENT_TRAVERSAL, which assures bounded execution even when + system allocators return non-contiguous spaces, at the typical + expense of carrying around more memory and increased fragmentation. + + The implementation is not very modular and seriously overuses + macros. Perhaps someday all C compilers will do as good a job + inlining modular code as can now be done by brute-force expansion, + but now, enough of them seem not to. + + Some compilers issue a lot of warnings about code that is + dead/unreachable only on some platforms, and also about intentional + uses of negation on unsigned types. All known cases of each can be + ignored. + + For a longer but out of date high-level description, see + http://gee.cs.oswego.edu/dl/html/malloc.html + +* MSPACES + If MSPACES is defined, then in addition to malloc, free, etc., + this file also defines mspace_malloc, mspace_free, etc. These + are versions of malloc routines that take an "mspace" argument + obtained using create_mspace, to control all internal bookkeeping. + If ONLY_MSPACES is defined, only these versions are compiled. + So if you would like to use this allocator for only some allocations, + and your system malloc for others, you can compile with + ONLY_MSPACES and then do something like... + static mspace mymspace = create_mspace(0,0); // for example + #define mymalloc(bytes) mspace_malloc(mymspace, bytes) + + (Note: If you only need one instance of an mspace, you can instead + use "USE_DL_PREFIX" to relabel the global malloc.) + + You can similarly create thread-local allocators by storing + mspaces as thread-locals. For example: + static __thread mspace tlms = 0; + void* tlmalloc(size_t bytes) { + if (tlms == 0) tlms = create_mspace(0, 0); + return mspace_malloc(tlms, bytes); + } + void tlfree(void* mem) { mspace_free(tlms, mem); } + + Unless FOOTERS is defined, each mspace is completely independent. + You cannot allocate from one and free to another (although + conformance is only weakly checked, so usage errors are not always + caught). If FOOTERS is defined, then each chunk carries around a tag + indicating its originating mspace, and frees are directed to their + originating spaces. + + ------------------------- Compile-time options --------------------------- + +Be careful in setting #define values for numerical constants of type +size_t. On some systems, literal values are not automatically extended +to size_t precision unless they are explicitly casted. You can also +use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below. + +WIN32 default: defined if _WIN32 defined + Defining WIN32 sets up defaults for MS environment and compilers. + Otherwise defaults are for unix. Beware that there seem to be some + cases where this malloc might not be a pure drop-in replacement for + Win32 malloc: Random-looking failures from Win32 GDI API's (eg; + SetDIBits()) may be due to bugs in some video driver implementations + when pixel buffers are malloc()ed, and the region spans more than + one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb) + default granularity, pixel buffers may straddle virtual allocation + regions more often than when using the Microsoft allocator. You can + avoid this by using VirtualAlloc() and VirtualFree() for all pixel + buffers rather than using malloc(). If this is not possible, + recompile this malloc with a larger DEFAULT_GRANULARITY. + +MALLOC_ALIGNMENT default: (size_t)8 + Controls the minimum alignment for malloc'ed chunks. It must be a + power of two and at least 8, even on machines for which smaller + alignments would suffice. It may be defined as larger than this + though. Note however that code and data structures are optimized for + the case of 8-byte alignment. + +MSPACES default: 0 (false) + If true, compile in support for independent allocation spaces. + This is only supported if HAVE_MMAP is true. + +ONLY_MSPACES default: 0 (false) + If true, only compile in mspace versions, not regular versions. + +USE_LOCKS default: 0 (false) + Causes each call to each public routine to be surrounded with + pthread or WIN32 mutex lock/unlock. (If set true, this can be + overridden on a per-mspace basis for mspace versions.) If set to a + non-zero value other than 1, locks are used, but their + implementation is left out, so lock functions must be supplied manually. + +USE_SPIN_LOCKS default: 1 iff USE_LOCKS and on x86 using gcc or MSC + If true, uses custom spin locks for locking. This is currently + supported only for x86 platforms using gcc or recent MS compilers. + Otherwise, posix locks or win32 critical sections are used. + +FOOTERS default: 0 + If true, provide extra checking and dispatching by placing + information in the footers of allocated chunks. This adds + space and time overhead. + +INSECURE default: 0 + If true, omit checks for usage errors and heap space overwrites. + +USE_DL_PREFIX default: NOT defined + Causes compiler to prefix all public routines with the string 'dl'. + This can be useful when you only want to use this malloc in one part + of a program, using your regular system malloc elsewhere. + +ABORT default: defined as abort() + Defines how to abort on failed checks. On most systems, a failed + check cannot die with an "assert" or even print an informative + message, because the underlying print routines in turn call malloc, + which will fail again. Generally, the best policy is to simply call + abort(). It's not very useful to do more than this because many + errors due to overwriting will show up as address faults (null, odd + addresses etc) rather than malloc-triggered checks, so will also + abort. Also, most compilers know that abort() does not return, so + can better optimize code conditionally calling it. + +PROCEED_ON_ERROR default: defined as 0 (false) + Controls whether detected bad addresses cause them to bypassed + rather than aborting. If set, detected bad arguments to free and + realloc are ignored. And all bookkeeping information is zeroed out + upon a detected overwrite of freed heap space, thus losing the + ability to ever return it from malloc again, but enabling the + application to proceed. If PROCEED_ON_ERROR is defined, the + static variable malloc_corruption_error_count is compiled in + and can be examined to see if errors have occurred. This option + generates slower code than the default abort policy. + +DEBUG default: NOT defined + The DEBUG setting is mainly intended for people trying to modify + this code or diagnose problems when porting to new platforms. + However, it may also be able to better isolate user errors than just + using runtime checks. The assertions in the check routines spell + out in more detail the assumptions and invariants underlying the + algorithms. The checking is fairly extensive, and will slow down + execution noticeably. Calling malloc_stats or mallinfo with DEBUG + set will attempt to check every non-mmapped allocated and free chunk + in the course of computing the summaries. + +ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) + Debugging assertion failures can be nearly impossible if your + version of the assert macro causes malloc to be called, which will + lead to a cascade of further failures, blowing the runtime stack. + ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), + which will usually make debugging easier. + +MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 + The action to take before "return 0" when malloc fails to be able to + return memory because there is none available. + +HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES + True if this system supports sbrk or an emulation of it. + +MORECORE default: sbrk + The name of the sbrk-style system routine to call to obtain more + memory. See below for guidance on writing custom MORECORE + functions. The type of the argument to sbrk/MORECORE varies across + systems. It cannot be size_t, because it supports negative + arguments, so it is normally the signed type of the same width as + size_t (sometimes declared as "intptr_t"). It doesn't much matter + though. Internally, we only call it with arguments less than half + the max value of a size_t, which should work across all reasonable + possibilities, although sometimes generating compiler warnings. + +MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE + If true, take advantage of fact that consecutive calls to MORECORE + with positive arguments always return contiguous increasing + addresses. This is true of unix sbrk. It does not hurt too much to + set it true anyway, since malloc copes with non-contiguities. + Setting it false when definitely non-contiguous saves time + and possibly wasted space it would take to discover this though. + +MORECORE_CANNOT_TRIM default: NOT defined + True if MORECORE cannot release space back to the system when given + negative arguments. This is generally necessary only if you are + using a hand-crafted MORECORE function that cannot handle negative + arguments. + +NO_SEGMENT_TRAVERSAL default: 0 + If non-zero, suppresses traversals of memory segments + returned by either MORECORE or CALL_MMAP. This disables + merging of segments that are contiguous, and selectively + releasing them to the OS if unused, but bounds execution times. + +HAVE_MMAP default: 1 (true) + True if this system supports mmap or an emulation of it. If so, and + HAVE_MORECORE is not true, MMAP is used for all system + allocation. If set and HAVE_MORECORE is true as well, MMAP is + primarily used to directly allocate very large blocks. It is also + used as a backup strategy in cases where MORECORE fails to provide + space from system. Note: A single call to MUNMAP is assumed to be + able to unmap memory that may have be allocated using multiple calls + to MMAP, so long as they are adjacent. + +HAVE_MREMAP default: 1 on linux, else 0 + If true realloc() uses mremap() to re-allocate large blocks and + extend or shrink allocation spaces. + +MMAP_CLEARS default: 1 except on WINCE. + True if mmap clears memory so calloc doesn't need to. This is true + for standard unix mmap using /dev/zero and on WIN32 except for WINCE. + +USE_BUILTIN_FFS default: 0 (i.e., not used) + Causes malloc to use the builtin ffs() function to compute indices. + Some compilers may recognize and intrinsify ffs to be faster than the + supplied C version. Also, the case of x86 using gcc is special-cased + to an asm instruction, so is already as fast as it can be, and so + this setting has no effect. Similarly for Win32 under recent MS compilers. + (On most x86s, the asm version is only slightly faster than the C version.) + +malloc_getpagesize default: derive from system includes, or 4096. + The system page size. To the extent possible, this malloc manages + memory from the system in page-size units. This may be (and + usually is) a function rather than a constant. This is ignored + if WIN32, where page size is determined using getSystemInfo during + initialization. + +USE_DEV_RANDOM default: 0 (i.e., not used) + Causes malloc to use /dev/random to initialize secure magic seed for + stamping footers. Otherwise, the current time is used. + +NO_MALLINFO default: 0 + If defined, don't compile "mallinfo". This can be a simple way + of dealing with mismatches between system declarations and + those in this file. + +MALLINFO_FIELD_TYPE default: size_t + The type of the fields in the mallinfo struct. This was originally + defined as "int" in SVID etc, but is more usefully defined as + size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set + +REALLOC_ZERO_BYTES_FREES default: not defined + This should be set if a call to realloc with zero bytes should + be the same as a call to free. Some people think it should. Otherwise, + since this malloc returns a unique pointer for malloc(0), so does + realloc(p, 0). + +LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H +LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H +LACKS_STDLIB_H default: NOT defined unless on WIN32 + Define these if your system does not have these header files. + You might need to manually insert some of the declarations they provide. + +DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS, + system_info.dwAllocationGranularity in WIN32, + otherwise 64K. + Also settable using mallopt(M_GRANULARITY, x) + The unit for allocating and deallocating memory from the system. On + most systems with contiguous MORECORE, there is no reason to + make this more than a page. However, systems with MMAP tend to + either require or encourage larger granularities. You can increase + this value to prevent system allocation functions to be called so + often, especially if they are slow. The value must be at least one + page and must be a power of two. Setting to 0 causes initialization + to either page size or win32 region size. (Note: In previous + versions of malloc, the equivalent of this option was called + "TOP_PAD") + +DEFAULT_TRIM_THRESHOLD default: 2MB + Also settable using mallopt(M_TRIM_THRESHOLD, x) + The maximum amount of unused top-most memory to keep before + releasing via malloc_trim in free(). Automatic trimming is mainly + useful in long-lived programs using contiguous MORECORE. Because + trimming via sbrk can be slow on some systems, and can sometimes be + wasteful (in cases where programs immediately afterward allocate + more large chunks) the value should be high enough so that your + overall system performance would improve by releasing this much + memory. As a rough guide, you might set to a value close to the + average size of a process (program) running on your system. + Releasing this much memory would allow such a process to run in + memory. Generally, it is worth tuning trim thresholds when a + program undergoes phases where several large chunks are allocated + and released in ways that can reuse each other's storage, perhaps + mixed with phases where there are no such chunks at all. The trim + value must be greater than page size to have any useful effect. To + disable trimming completely, you can set to MAX_SIZE_T. Note that the trick + some people use of mallocing a huge space and then freeing it at + program startup, in an attempt to reserve system memory, doesn't + have the intended effect under automatic trimming, since that memory + will immediately be returned to the system. + +DEFAULT_MMAP_THRESHOLD default: 256K + Also settable using mallopt(M_MMAP_THRESHOLD, x) + The request size threshold for using MMAP to directly service a + request. Requests of at least this size that cannot be allocated + using already-existing space will be serviced via mmap. (If enough + normal freed space already exists it is used instead.) Using mmap + segregates relatively large chunks of memory so that they can be + individually obtained and released from the host system. A request + serviced through mmap is never reused by any other request (at least + not directly; the system may just so happen to remap successive + requests to the same locations). Segregating space in this way has + the benefits that: Mmapped space can always be individually released + back to the system, which helps keep the system level memory demands + of a long-lived program low. Also, mapped memory doesn't become + `locked' between other chunks, as can happen with normally allocated + chunks, which means that even trimming via malloc_trim would not + release them. However, it has the disadvantage that the space + cannot be reclaimed, consolidated, and then used to service later + requests, as happens with normal chunks. The advantages of mmap + nearly always outweigh disadvantages for "large" chunks, but the + value of "large" may vary across systems. The default is an + empirically derived value that works well in most systems. You can + disable mmap by setting to MAX_SIZE_T. + +MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP + The number of consolidated frees between checks to release + unused segments when freeing. When using non-contiguous segments, + especially with multiple mspaces, checking only for topmost space + doesn't always suffice to trigger trimming. To compensate for this, + free() will, with a period of MAX_RELEASE_CHECK_RATE (or the + current number of segments, if greater) try to release unused + segments to the OS when freeing chunks that result in + consolidation. The best value for this parameter is a compromise + between slowing down frees with relatively costly checks that + rarely trigger versus holding on to unused memory. To effectively + disable, set to MAX_SIZE_T. This may lead to a very slight speed + improvement at the expense of carrying around more memory. +*/ + +/* Version identifier to allow people to support multiple versions */ +#ifndef DLMALLOC_VERSION +#define DLMALLOC_VERSION 20804 +#endif /* DLMALLOC_VERSION */ + +#ifndef WIN32 +#ifdef _WIN32 +#define WIN32 1 +#endif /* _WIN32 */ +#ifdef _WIN32_WCE +#define LACKS_FCNTL_H +#define WIN32 1 +#endif /* _WIN32_WCE */ +#endif /* WIN32 */ +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#define _WIN32_WINNT 0x403 +#include +#define HAVE_MMAP 1 +#define HAVE_MORECORE 0 +#define LACKS_UNISTD_H +#define LACKS_SYS_PARAM_H +#define LACKS_SYS_MMAN_H +#define LACKS_STRING_H +#define LACKS_STRINGS_H +#define LACKS_SYS_TYPES_H +#define LACKS_ERRNO_H +#ifndef MALLOC_FAILURE_ACTION +#define MALLOC_FAILURE_ACTION +#endif /* MALLOC_FAILURE_ACTION */ +#ifdef _WIN32_WCE /* WINCE reportedly does not clear */ +#define MMAP_CLEARS 0 +#else +#define MMAP_CLEARS 1 +#endif /* _WIN32_WCE */ +#endif /* WIN32 */ + +#if defined(DARWIN) || defined(_DARWIN) +/* Mac OSX docs advise not to use sbrk; it seems better to use mmap */ +#ifndef HAVE_MORECORE +#define HAVE_MORECORE 0 +#define HAVE_MMAP 1 +/* OSX allocators provide 16 byte alignment */ +#ifndef MALLOC_ALIGNMENT +#define MALLOC_ALIGNMENT ((size_t)16U) +#endif +#endif /* HAVE_MORECORE */ +#endif /* DARWIN */ + +#ifndef LACKS_SYS_TYPES_H +#include /* For size_t */ +#endif /* LACKS_SYS_TYPES_H */ + +/* The maximum possible size_t value has all bits set */ +#define MAX_SIZE_T (~(size_t)0) + +#ifndef ONLY_MSPACES +#define ONLY_MSPACES 0 /* define to a value */ +#else +#define ONLY_MSPACES 1 +#endif /* ONLY_MSPACES */ +#ifndef MSPACES +#if ONLY_MSPACES +#define MSPACES 1 +#else /* ONLY_MSPACES */ +#define MSPACES 0 +#endif /* ONLY_MSPACES */ +#endif /* MSPACES */ +#ifndef MALLOC_ALIGNMENT +#define MALLOC_ALIGNMENT ((size_t)8U) +#endif /* MALLOC_ALIGNMENT */ +#ifndef FOOTERS +#define FOOTERS 0 +#endif /* FOOTERS */ +#ifndef ABORT +#define ABORT abort() +#endif /* ABORT */ +#ifndef ABORT_ON_ASSERT_FAILURE +#define ABORT_ON_ASSERT_FAILURE 1 +#endif /* ABORT_ON_ASSERT_FAILURE */ +#ifndef PROCEED_ON_ERROR +#define PROCEED_ON_ERROR 0 +#endif /* PROCEED_ON_ERROR */ +#ifndef USE_LOCKS +#define USE_LOCKS 0 +#endif /* USE_LOCKS */ +#ifndef USE_SPIN_LOCKS +#if USE_LOCKS && (defined(__GNUC__) && ((defined(__i386__) || defined(__x86_64__)))) || (defined(_MSC_VER) && _MSC_VER>=1310) +#define USE_SPIN_LOCKS 1 +#else +#define USE_SPIN_LOCKS 0 +#endif /* USE_LOCKS && ... */ +#endif /* USE_SPIN_LOCKS */ +#ifndef INSECURE +#define INSECURE 0 +#endif /* INSECURE */ +#ifndef HAVE_MMAP +#define HAVE_MMAP 1 +#endif /* HAVE_MMAP */ +#ifndef MMAP_CLEARS +#define MMAP_CLEARS 1 +#endif /* MMAP_CLEARS */ +#ifndef HAVE_MREMAP +#ifdef linux +#define HAVE_MREMAP 1 +#else /* linux */ +#define HAVE_MREMAP 0 +#endif /* linux */ +#endif /* HAVE_MREMAP */ +#ifndef MALLOC_FAILURE_ACTION +#define MALLOC_FAILURE_ACTION errno = ENOMEM; +#endif /* MALLOC_FAILURE_ACTION */ +#ifndef HAVE_MORECORE +#if ONLY_MSPACES +#define HAVE_MORECORE 0 +#else /* ONLY_MSPACES */ +#define HAVE_MORECORE 1 +#endif /* ONLY_MSPACES */ +#endif /* HAVE_MORECORE */ +#if !HAVE_MORECORE +#define MORECORE_CONTIGUOUS 0 +#else /* !HAVE_MORECORE */ +#define MORECORE_DEFAULT sbrk +#ifndef MORECORE_CONTIGUOUS +#define MORECORE_CONTIGUOUS 1 +#endif /* MORECORE_CONTIGUOUS */ +#endif /* HAVE_MORECORE */ +#ifndef DEFAULT_GRANULARITY +#if (MORECORE_CONTIGUOUS || defined(WIN32)) +#define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */ +#else /* MORECORE_CONTIGUOUS */ +#define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U) +#endif /* MORECORE_CONTIGUOUS */ +#endif /* DEFAULT_GRANULARITY */ +#ifndef DEFAULT_TRIM_THRESHOLD +#ifndef MORECORE_CANNOT_TRIM +#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) +#else /* MORECORE_CANNOT_TRIM */ +#define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T +#endif /* MORECORE_CANNOT_TRIM */ +#endif /* DEFAULT_TRIM_THRESHOLD */ +#ifndef DEFAULT_MMAP_THRESHOLD +#if HAVE_MMAP +#define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U) +#else /* HAVE_MMAP */ +#define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T +#endif /* HAVE_MMAP */ +#endif /* DEFAULT_MMAP_THRESHOLD */ +#ifndef MAX_RELEASE_CHECK_RATE +#if HAVE_MMAP +#define MAX_RELEASE_CHECK_RATE 4095 +#else +#define MAX_RELEASE_CHECK_RATE MAX_SIZE_T +#endif /* HAVE_MMAP */ +#endif /* MAX_RELEASE_CHECK_RATE */ +#ifndef USE_BUILTIN_FFS +#define USE_BUILTIN_FFS 0 +#endif /* USE_BUILTIN_FFS */ +#ifndef USE_DEV_RANDOM +#define USE_DEV_RANDOM 0 +#endif /* USE_DEV_RANDOM */ +#ifndef NO_MALLINFO +#define NO_MALLINFO 0 +#endif /* NO_MALLINFO */ +#ifndef MALLINFO_FIELD_TYPE +#define MALLINFO_FIELD_TYPE size_t +#endif /* MALLINFO_FIELD_TYPE */ +#ifndef NO_SEGMENT_TRAVERSAL +#define NO_SEGMENT_TRAVERSAL 0 +#endif /* NO_SEGMENT_TRAVERSAL */ + +/* + mallopt tuning options. SVID/XPG defines four standard parameter + numbers for mallopt, normally defined in malloc.h. None of these + are used in this malloc, so setting them has no effect. But this + malloc does support the following options. +*/ + +#define M_TRIM_THRESHOLD (-1) +#define M_GRANULARITY (-2) +#define M_MMAP_THRESHOLD (-3) + +/* ------------------------ Mallinfo declarations ------------------------ */ + +#if !NO_MALLINFO +/* + This version of malloc supports the standard SVID/XPG mallinfo + routine that returns a struct containing usage properties and + statistics. It should work on any system that has a + /usr/include/malloc.h defining struct mallinfo. The main + declaration needed is the mallinfo struct that is returned (by-copy) + by mallinfo(). The malloinfo struct contains a bunch of fields that + are not even meaningful in this version of malloc. These fields are + are instead filled by mallinfo() with other numbers that might be of + interest. + + HAVE_USR_INCLUDE_MALLOC_H should be set if you have a + /usr/include/malloc.h file that includes a declaration of struct + mallinfo. If so, it is included; else a compliant version is + declared below. These must be precisely the same for mallinfo() to + work. The original SVID version of this struct, defined on most + systems with mallinfo, declares all fields as ints. But some others + define as unsigned long. If your system defines the fields using a + type of different width than listed here, you MUST #include your + system version and #define HAVE_USR_INCLUDE_MALLOC_H. +*/ + +/* #define HAVE_USR_INCLUDE_MALLOC_H */ + +#ifdef HAVE_USR_INCLUDE_MALLOC_H +#include "/usr/include/malloc.h" +#else /* HAVE_USR_INCLUDE_MALLOC_H */ +#ifndef STRUCT_MALLINFO_DECLARED +#define STRUCT_MALLINFO_DECLARED 1 +struct mallinfo { + MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ + MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ + MALLINFO_FIELD_TYPE smblks; /* always 0 */ + MALLINFO_FIELD_TYPE hblks; /* always 0 */ + MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ + MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ + MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ + MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ + MALLINFO_FIELD_TYPE fordblks; /* total free space */ + MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ +}; +#endif /* STRUCT_MALLINFO_DECLARED */ +#endif /* HAVE_USR_INCLUDE_MALLOC_H */ +#endif /* NO_MALLINFO */ + +/* + Try to persuade compilers to inline. The most critical functions for + inlining are defined as macros, so these aren't used for them. +*/ + +#ifndef FORCEINLINE + #if defined(__GNUC__) +#define FORCEINLINE __inline __attribute__ ((always_inline)) + #elif defined(_MSC_VER) + #define FORCEINLINE __forceinline + #endif +#endif +#ifndef NOINLINE + #if defined(__GNUC__) + #define NOINLINE __attribute__ ((noinline)) + #elif defined(_MSC_VER) + #define NOINLINE __declspec(noinline) + #else + #define NOINLINE + #endif +#endif + +#ifdef __cplusplus +extern "C" { +#ifndef FORCEINLINE + #define FORCEINLINE inline +#endif +#endif /* __cplusplus */ +#ifndef FORCEINLINE + #define FORCEINLINE +#endif + +#if !ONLY_MSPACES + +/* ------------------- Declarations of public routines ------------------- */ + +#ifndef USE_DL_PREFIX +#define dlcalloc calloc +#define dlfree free +#define dlmalloc malloc +#define dlmemalign memalign +#define dlrealloc realloc +#define dlvalloc valloc +#define dlpvalloc pvalloc +#define dlmallinfo mallinfo +#define dlmallopt mallopt +#define dlmalloc_trim malloc_trim +#define dlmalloc_stats malloc_stats +#define dlmalloc_usable_size malloc_usable_size +#define dlmalloc_footprint malloc_footprint +#define dlmalloc_max_footprint malloc_max_footprint +#define dlindependent_calloc independent_calloc +#define dlindependent_comalloc independent_comalloc +#endif /* USE_DL_PREFIX */ + + +/* + malloc(size_t n) + Returns a pointer to a newly allocated chunk of at least n bytes, or + null if no space is available, in which case errno is set to ENOMEM + on ANSI C systems. + + If n is zero, malloc returns a minimum-sized chunk. (The minimum + size is 16 bytes on most 32bit systems, and 32 bytes on 64bit + systems.) Note that size_t is an unsigned type, so calls with + arguments that would be negative if signed are interpreted as + requests for huge amounts of space, which will often fail. The + maximum supported value of n differs across systems, but is in all + cases less than the maximum representable value of a size_t. +*/ +void* dlmalloc(size_t); + +/* + free(void* p) + Releases the chunk of memory pointed to by p, that had been previously + allocated using malloc or a related routine such as realloc. + It has no effect if p is null. If p was not malloced or already + freed, free(p) will by default cause the current program to abort. +*/ +void dlfree(void*); + +/* + calloc(size_t n_elements, size_t element_size); + Returns a pointer to n_elements * element_size bytes, with all locations + set to zero. +*/ +void* dlcalloc(size_t, size_t); + +/* + realloc(void* p, size_t n) + Returns a pointer to a chunk of size n that contains the same data + as does chunk p up to the minimum of (n, p's size) bytes, or null + if no space is available. + + The returned pointer may or may not be the same as p. The algorithm + prefers extending p in most cases when possible, otherwise it + employs the equivalent of a malloc-copy-free sequence. + + If p is null, realloc is equivalent to malloc. + + If space is not available, realloc returns null, errno is set (if on + ANSI) and p is NOT freed. + + if n is for fewer bytes than already held by p, the newly unused + space is lopped off and freed if possible. realloc with a size + argument of zero (re)allocates a minimum-sized chunk. + + The old unix realloc convention of allowing the last-free'd chunk + to be used as an argument to realloc is not supported. +*/ + +void* dlrealloc(void*, size_t); + +/* + memalign(size_t alignment, size_t n); + Returns a pointer to a newly allocated chunk of n bytes, aligned + in accord with the alignment argument. + + The alignment argument should be a power of two. If the argument is + not a power of two, the nearest greater power is used. + 8-byte alignment is guaranteed by normal malloc calls, so don't + bother calling memalign with an argument of 8 or less. + + Overreliance on memalign is a sure way to fragment space. +*/ +void* dlmemalign(size_t, size_t); + +/* + valloc(size_t n); + Equivalent to memalign(pagesize, n), where pagesize is the page + size of the system. If the pagesize is unknown, 4096 is used. +*/ +void* dlvalloc(size_t); + +/* + mallopt(int parameter_number, int parameter_value) + Sets tunable parameters The format is to provide a + (parameter-number, parameter-value) pair. mallopt then sets the + corresponding parameter to the argument value if it can (i.e., so + long as the value is meaningful), and returns 1 if successful else + 0. To workaround the fact that mallopt is specified to use int, + not size_t parameters, the value -1 is specially treated as the + maximum unsigned size_t value. + + SVID/XPG/ANSI defines four standard param numbers for mallopt, + normally defined in malloc.h. None of these are use in this malloc, + so setting them has no effect. But this malloc also supports other + options in mallopt. See below for details. Briefly, supported + parameters are as follows (listed defaults are for "typical" + configurations). + + Symbol param # default allowed param values + M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables) + M_GRANULARITY -2 page size any power of 2 >= page size + M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) +*/ +int dlmallopt(int, int); + +/* + malloc_footprint(); + Returns the number of bytes obtained from the system. The total + number of bytes allocated by malloc, realloc etc., is less than this + value. Unlike mallinfo, this function returns only a precomputed + result, so can be called frequently to monitor memory consumption. + Even if locks are otherwise defined, this function does not use them, + so results might not be up to date. +*/ +size_t dlmalloc_footprint(void); + +/* + malloc_max_footprint(); + Returns the maximum number of bytes obtained from the system. This + value will be greater than current footprint if deallocated space + has been reclaimed by the system. The peak number of bytes allocated + by malloc, realloc etc., is less than this value. Unlike mallinfo, + this function returns only a precomputed result, so can be called + frequently to monitor memory consumption. Even if locks are + otherwise defined, this function does not use them, so results might + not be up to date. +*/ +size_t dlmalloc_max_footprint(void); + +#if !NO_MALLINFO +/* + mallinfo() + Returns (by copy) a struct containing various summary statistics: + + arena: current total non-mmapped bytes allocated from system + ordblks: the number of free chunks + smblks: always zero. + hblks: current number of mmapped regions + hblkhd: total bytes held in mmapped regions + usmblks: the maximum total allocated space. This will be greater + than current total if trimming has occurred. + fsmblks: always zero + uordblks: current total allocated space (normal or mmapped) + fordblks: total free space + keepcost: the maximum number of bytes that could ideally be released + back to system via malloc_trim. ("ideally" means that + it ignores page restrictions etc.) + + Because these fields are ints, but internal bookkeeping may + be kept as longs, the reported values may wrap around zero and + thus be inaccurate. +*/ +struct mallinfo dlmallinfo(void); +#endif /* NO_MALLINFO */ + +/* + independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); + + independent_calloc is similar to calloc, but instead of returning a + single cleared space, it returns an array of pointers to n_elements + independent elements that can hold contents of size elem_size, each + of which starts out cleared, and can be independently freed, + realloc'ed etc. The elements are guaranteed to be adjacently + allocated (this is not guaranteed to occur with multiple callocs or + mallocs), which may also improve cache locality in some + applications. + + The "chunks" argument is optional (i.e., may be null, which is + probably the most typical usage). If it is null, the returned array + is itself dynamically allocated and should also be freed when it is + no longer needed. Otherwise, the chunks array must be of at least + n_elements in length. It is filled in with the pointers to the + chunks. + + In either case, independent_calloc returns this pointer array, or + null if the allocation failed. If n_elements is zero and "chunks" + is null, it returns a chunk representing an array with zero elements + (which should be freed if not wanted). + + Each element must be individually freed when it is no longer + needed. If you'd like to instead be able to free all at once, you + should instead use regular calloc and assign pointers into this + space to represent elements. (In this case though, you cannot + independently free elements.) + + independent_calloc simplifies and speeds up implementations of many + kinds of pools. It may also be useful when constructing large data + structures that initially have a fixed number of fixed-sized nodes, + but the number is not known at compile time, and some of the nodes + may later need to be freed. For example: + + struct Node { int item; struct Node* next; }; + + struct Node* build_list() { + struct Node** pool; + int n = read_number_of_nodes_needed(); + if (n <= 0) return 0; + pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); + if (pool == 0) die(); + // organize into a linked list... + struct Node* first = pool[0]; + for (i = 0; i < n-1; ++i) + pool[i]->next = pool[i+1]; + free(pool); // Can now free the array (or not, if it is needed later) + return first; + } +*/ +void** dlindependent_calloc(size_t, size_t, void**); + +/* + independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); + + independent_comalloc allocates, all at once, a set of n_elements + chunks with sizes indicated in the "sizes" array. It returns + an array of pointers to these elements, each of which can be + independently freed, realloc'ed etc. The elements are guaranteed to + be adjacently allocated (this is not guaranteed to occur with + multiple callocs or mallocs), which may also improve cache locality + in some applications. + + The "chunks" argument is optional (i.e., may be null). If it is null + the returned array is itself dynamically allocated and should also + be freed when it is no longer needed. Otherwise, the chunks array + must be of at least n_elements in length. It is filled in with the + pointers to the chunks. + + In either case, independent_comalloc returns this pointer array, or + null if the allocation failed. If n_elements is zero and chunks is + null, it returns a chunk representing an array with zero elements + (which should be freed if not wanted). + + Each element must be individually freed when it is no longer + needed. If you'd like to instead be able to free all at once, you + should instead use a single regular malloc, and assign pointers at + particular offsets in the aggregate space. (In this case though, you + cannot independently free elements.) + + independent_comallac differs from independent_calloc in that each + element may have a different size, and also that it does not + automatically clear elements. + + independent_comalloc can be used to speed up allocation in cases + where several structs or objects must always be allocated at the + same time. For example: + + struct Head { ... } + struct Foot { ... } + + void send_message(char* msg) { + int msglen = strlen(msg); + size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; + void* chunks[3]; + if (independent_comalloc(3, sizes, chunks) == 0) + die(); + struct Head* head = (struct Head*)(chunks[0]); + char* body = (char*)(chunks[1]); + struct Foot* foot = (struct Foot*)(chunks[2]); + // ... + } + + In general though, independent_comalloc is worth using only for + larger values of n_elements. For small values, you probably won't + detect enough difference from series of malloc calls to bother. + + Overuse of independent_comalloc can increase overall memory usage, + since it cannot reuse existing noncontiguous small chunks that + might be available for some of the elements. +*/ +void** dlindependent_comalloc(size_t, size_t*, void**); + + +/* + pvalloc(size_t n); + Equivalent to valloc(minimum-page-that-holds(n)), that is, + round up n to nearest pagesize. + */ +void* dlpvalloc(size_t); + +/* + malloc_trim(size_t pad); + + If possible, gives memory back to the system (via negative arguments + to sbrk) if there is unused memory at the `high' end of the malloc + pool or in unused MMAP segments. You can call this after freeing + large blocks of memory to potentially reduce the system-level memory + requirements of a program. However, it cannot guarantee to reduce + memory. Under some allocation patterns, some large free blocks of + memory will be locked between two used chunks, so they cannot be + given back to the system. + + The `pad' argument to malloc_trim represents the amount of free + trailing space to leave untrimmed. If this argument is zero, only + the minimum amount of memory to maintain internal data structures + will be left. Non-zero arguments can be supplied to maintain enough + trailing space to service future expected allocations without having + to re-obtain memory from the system. + + Malloc_trim returns 1 if it actually released any memory, else 0. +*/ +int dlmalloc_trim(size_t); + +/* + malloc_stats(); + Prints on stderr the amount of space obtained from the system (both + via sbrk and mmap), the maximum amount (which may be more than + current if malloc_trim and/or munmap got called), and the current + number of bytes allocated via malloc (or realloc, etc) but not yet + freed. Note that this is the number of bytes allocated, not the + number requested. It will be larger than the number requested + because of alignment and bookkeeping overhead. Because it includes + alignment wastage as being in use, this figure may be greater than + zero even when no user-level chunks are allocated. + + The reported current and maximum system memory can be inaccurate if + a program makes other calls to system memory allocation functions + (normally sbrk) outside of malloc. + + malloc_stats prints only the most commonly interesting statistics. + More information can be obtained by calling mallinfo. +*/ +void dlmalloc_stats(void); + +#endif /* ONLY_MSPACES */ + +/* + malloc_usable_size(void* p); + + Returns the number of bytes you can actually use in + an allocated chunk, which may be more than you requested (although + often not) due to alignment and minimum size constraints. + You can use this many bytes without worrying about + overwriting other allocated objects. This is not a particularly great + programming practice. malloc_usable_size can be more useful in + debugging and assertions, for example: + + p = malloc(n); + assert(malloc_usable_size(p) >= 256); +*/ +size_t dlmalloc_usable_size(void*); + + +#if MSPACES + +/* + mspace is an opaque type representing an independent + region of space that supports mspace_malloc, etc. +*/ +typedef void* mspace; + +/* + create_mspace creates and returns a new independent space with the + given initial capacity, or, if 0, the default granularity size. It + returns null if there is no system memory available to create the + space. If argument locked is non-zero, the space uses a separate + lock to control access. The capacity of the space will grow + dynamically as needed to service mspace_malloc requests. You can + control the sizes of incremental increases of this space by + compiling with a different DEFAULT_GRANULARITY or dynamically + setting with mallopt(M_GRANULARITY, value). +*/ +mspace create_mspace(size_t capacity, int locked); + +/* + destroy_mspace destroys the given space, and attempts to return all + of its memory back to the system, returning the total number of + bytes freed. After destruction, the results of access to all memory + used by the space become undefined. +*/ +size_t destroy_mspace(mspace msp); + +/* + create_mspace_with_base uses the memory supplied as the initial base + of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this + space is used for bookkeeping, so the capacity must be at least this + large. (Otherwise 0 is returned.) When this initial space is + exhausted, additional memory will be obtained from the system. + Destroying this space will deallocate all additionally allocated + space (if possible) but not the initial base. +*/ +mspace create_mspace_with_base(void* base, size_t capacity, int locked); + +/* + mspace_mmap_large_chunks controls whether requests for large chunks + are allocated in their own mmapped regions, separate from others in + this mspace. By default this is enabled, which reduces + fragmentation. However, such chunks are not necessarily released to + the system upon destroy_mspace. Disabling by setting to false may + increase fragmentation, but avoids leakage when relying on + destroy_mspace to release all memory allocated using this space. +*/ +int mspace_mmap_large_chunks(mspace msp, int enable); + + +/* + mspace_malloc behaves as malloc, but operates within + the given space. +*/ +void* mspace_malloc(mspace msp, size_t bytes); + +/* + mspace_free behaves as free, but operates within + the given space. + + If compiled with FOOTERS==1, mspace_free is not actually needed. + free may be called instead of mspace_free because freed chunks from + any space are handled by their originating spaces. +*/ +void mspace_free(mspace msp, void* mem); + +/* + mspace_realloc behaves as realloc, but operates within + the given space. + + If compiled with FOOTERS==1, mspace_realloc is not actually + needed. realloc may be called instead of mspace_realloc because + realloced chunks from any space are handled by their originating + spaces. +*/ +void* mspace_realloc(mspace msp, void* mem, size_t newsize); + +/* + mspace_calloc behaves as calloc, but operates within + the given space. +*/ +void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); + +/* + mspace_memalign behaves as memalign, but operates within + the given space. +*/ +void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); + +/* + mspace_independent_calloc behaves as independent_calloc, but + operates within the given space. +*/ +void** mspace_independent_calloc(mspace msp, size_t n_elements, + size_t elem_size, void* chunks[]); + +/* + mspace_independent_comalloc behaves as independent_comalloc, but + operates within the given space. +*/ +void** mspace_independent_comalloc(mspace msp, size_t n_elements, + size_t sizes[], void* chunks[]); + +/* + mspace_footprint() returns the number of bytes obtained from the + system for this space. +*/ +size_t mspace_footprint(mspace msp); + +/* + mspace_max_footprint() returns the peak number of bytes obtained from the + system for this space. +*/ +size_t mspace_max_footprint(mspace msp); + + +#if !NO_MALLINFO +/* + mspace_mallinfo behaves as mallinfo, but reports properties of + the given space. +*/ +struct mallinfo mspace_mallinfo(mspace msp); +#endif /* NO_MALLINFO */ + +/* + malloc_usable_size(void* p) behaves the same as malloc_usable_size; +*/ + size_t mspace_usable_size(void* mem); + +/* + mspace_malloc_stats behaves as malloc_stats, but reports + properties of the given space. +*/ +void mspace_malloc_stats(mspace msp); + +/* + mspace_trim behaves as malloc_trim, but + operates within the given space. +*/ +int mspace_trim(mspace msp, size_t pad); + +/* + An alias for mallopt. +*/ +int mspace_mallopt(int, int); + +#endif /* MSPACES */ + +#ifdef __cplusplus +}; /* end of extern "C" */ +#endif /* __cplusplus */ + +/* + ======================================================================== + To make a fully customizable malloc.h header file, cut everything + above this line, put into file malloc.h, edit to suit, and #include it + on the next line, as well as in programs that use this malloc. + ======================================================================== +*/ + +/* #include "malloc.h" */ + +/*------------------------------ internal #includes ---------------------- */ + +#ifdef WIN32 +#pragma warning( disable : 4146 ) /* no "unsigned" warnings */ +#endif /* WIN32 */ + +#include /* for printing in malloc_stats */ + +#ifndef LACKS_ERRNO_H +#include /* for MALLOC_FAILURE_ACTION */ +#endif /* LACKS_ERRNO_H */ +#if FOOTERS +#include /* for magic initialization */ +#endif /* FOOTERS */ +#ifndef LACKS_STDLIB_H +#include /* for abort() */ +#endif /* LACKS_STDLIB_H */ +#ifdef DEBUG +#if ABORT_ON_ASSERT_FAILURE +#define assert(x) if(!(x)) ABORT +#else /* ABORT_ON_ASSERT_FAILURE */ +#include +#endif /* ABORT_ON_ASSERT_FAILURE */ +#else /* DEBUG */ +#ifndef assert +#define assert(x) +#endif +#define DEBUG 0 +#endif /* DEBUG */ +#ifndef LACKS_STRING_H +#include /* for memset etc */ +#endif /* LACKS_STRING_H */ +#if USE_BUILTIN_FFS +#ifndef LACKS_STRINGS_H +#include /* for ffs */ +#endif /* LACKS_STRINGS_H */ +#endif /* USE_BUILTIN_FFS */ +#if HAVE_MMAP +#ifndef LACKS_SYS_MMAN_H +#include /* for mmap */ +#endif /* LACKS_SYS_MMAN_H */ +#ifndef LACKS_FCNTL_H +#include +#endif /* LACKS_FCNTL_H */ +#endif /* HAVE_MMAP */ +#ifndef LACKS_UNISTD_H +#include /* for sbrk, sysconf */ +#else /* LACKS_UNISTD_H */ +#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) +extern void* sbrk(ptrdiff_t); +#endif /* FreeBSD etc */ +#endif /* LACKS_UNISTD_H */ + +/* Declarations for locking */ +#if USE_LOCKS +#ifndef WIN32 +#include +#if defined (__SVR4) && defined (__sun) /* solaris */ +#include +#endif /* solaris */ +#else +#ifndef _M_AMD64 +/* These are already defined on AMD64 builds */ +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +#ifndef __MINGW32__ +LONG __cdecl _InterlockedCompareExchange(LONG volatile *Dest, LONG Exchange, LONG Comp); +LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value); +#endif +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* _M_AMD64 */ +#ifndef __MINGW32__ +#pragma intrinsic (_InterlockedCompareExchange) +#pragma intrinsic (_InterlockedExchange) +#else + /* --[ start GCC compatibility ]---------------------------------------------- + * Compatibility header for GCC -- GCC equivalents of intrinsic + * Microsoft Visual C++ functions. Originally developed for the ReactOS + * () and TinyKrnl () + * projects. + * + * Copyright (c) 2006 KJK::Hyperion + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + + /*** Atomic operations ***/ + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100 + #define _ReadWriteBarrier() __sync_synchronize() + #else + static __inline__ __attribute__((always_inline)) long __sync_lock_test_and_set(volatile long * const Target, const long Value) + { + long res; + __asm__ __volatile__("xchg%z0 %2, %0" : "=g" (*(Target)), "=r" (res) : "1" (Value)); + return res; + } + static void __inline__ __attribute__((always_inline)) _MemoryBarrier(void) + { + __asm__ __volatile__("" : : : "memory"); + } + #define _ReadWriteBarrier() _MemoryBarrier() + #endif + /* BUGBUG: GCC only supports full barriers */ + static __inline__ __attribute__((always_inline)) long _InterlockedExchange(volatile long * const Target, const long Value) + { + /* NOTE: __sync_lock_test_and_set would be an acquire barrier, so we force a full barrier */ + _ReadWriteBarrier(); + return __sync_lock_test_and_set(Target, Value); + } + /* --[ end GCC compatibility ]---------------------------------------------- */ +#endif +#define interlockedcompareexchange _InterlockedCompareExchange +#define interlockedexchange _InterlockedExchange +#endif /* Win32 */ +#endif /* USE_LOCKS */ + +/* Declarations for bit scanning on win32 */ +#if defined(_MSC_VER) && _MSC_VER>=1300 +#ifndef BitScanForward /* Try to avoid pulling in WinNT.h */ +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +unsigned char _BitScanForward(unsigned long *index, unsigned long mask); +unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#define BitScanForward _BitScanForward +#define BitScanReverse _BitScanReverse +#pragma intrinsic(_BitScanForward) +#pragma intrinsic(_BitScanReverse) +#endif /* BitScanForward */ +#endif /* defined(_MSC_VER) && _MSC_VER>=1300 */ + +#ifndef WIN32 +#ifndef malloc_getpagesize +# ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */ +# ifndef _SC_PAGE_SIZE +# define _SC_PAGE_SIZE _SC_PAGESIZE +# endif +# endif +# ifdef _SC_PAGE_SIZE +# define malloc_getpagesize sysconf(_SC_PAGE_SIZE) +# else +# if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE) + extern size_t getpagesize(); +# define malloc_getpagesize getpagesize() +# else +# ifdef WIN32 /* use supplied emulation of getpagesize */ +# define malloc_getpagesize getpagesize() +# else +# ifndef LACKS_SYS_PARAM_H +# include +# endif +# ifdef EXEC_PAGESIZE +# define malloc_getpagesize EXEC_PAGESIZE +# else +# ifdef NBPG +# ifndef CLSIZE +# define malloc_getpagesize NBPG +# else +# define malloc_getpagesize (NBPG * CLSIZE) +# endif +# else +# ifdef NBPC +# define malloc_getpagesize NBPC +# else +# ifdef PAGESIZE +# define malloc_getpagesize PAGESIZE +# else /* just guess */ +# define malloc_getpagesize ((size_t)4096U) +# endif +# endif +# endif +# endif +# endif +# endif +# endif +#endif +#endif + + + +/* ------------------- size_t and alignment properties -------------------- */ + +/* The byte and bit size of a size_t */ +#define SIZE_T_SIZE (sizeof(size_t)) +#define SIZE_T_BITSIZE (sizeof(size_t) << 3) + +/* Some constants coerced to size_t */ +/* Annoying but necessary to avoid errors on some platforms */ +#define SIZE_T_ZERO ((size_t)0) +#define SIZE_T_ONE ((size_t)1) +#define SIZE_T_TWO ((size_t)2) +#define SIZE_T_FOUR ((size_t)4) +#define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) +#define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) +#define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) +#define HALF_MAX_SIZE_T (MAX_SIZE_T / 2U) + +/* The bit mask value corresponding to MALLOC_ALIGNMENT */ +#define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) + +/* True if address a has acceptable alignment */ +#define is_aligned(A) (((size_t)((A)) & (CHUNK_ALIGN_MASK)) == 0) + +/* the number of bytes to offset an address to align it */ +#define align_offset(A)\ + ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ + ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) + +/* -------------------------- MMAP preliminaries ------------------------- */ + +/* + If HAVE_MORECORE or HAVE_MMAP are false, we just define calls and + checks to fail so compiler optimizer can delete code rather than + using so many "#if"s. +*/ + + +/* MORECORE and MMAP must return MFAIL on failure */ +#define MFAIL ((void*)(MAX_SIZE_T)) +#define CMFAIL ((char*)(MFAIL)) /* defined for convenience */ + +#if HAVE_MMAP + +#ifndef WIN32 +#define MUNMAP_DEFAULT(a, s) munmap((a), (s)) +#define MMAP_PROT (PROT_READ|PROT_WRITE) +#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) +#define MAP_ANONYMOUS MAP_ANON +#endif /* MAP_ANON */ +#ifdef MAP_ANONYMOUS +#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS) +#define MMAP_DEFAULT(s) mmap(0, (s), MMAP_PROT, MMAP_FLAGS, -1, 0) +#else /* MAP_ANONYMOUS */ +/* + Nearly all versions of mmap support MAP_ANONYMOUS, so the following + is unlikely to be needed, but is supplied just in case. +*/ +#define MMAP_FLAGS (MAP_PRIVATE) +static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */ +#define MMAP_DEFAULT(s) ((dev_zero_fd < 0) ? \ + (dev_zero_fd = open("/dev/zero", O_RDWR), \ + mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \ + mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) +#endif /* MAP_ANONYMOUS */ + +#define DIRECT_MMAP_DEFAULT(s) MMAP_DEFAULT(s) + +#else /* WIN32 */ + +/* Win32 MMAP via VirtualAlloc */ +static FORCEINLINE void* win32mmap(size_t size) { + void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); + return (ptr != 0)? ptr: MFAIL; +} + +/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ +static FORCEINLINE void* win32direct_mmap(size_t size) { + void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, + PAGE_READWRITE); + return (ptr != 0)? ptr: MFAIL; +} + +/* This function supports releasing coalesed segments */ +static FORCEINLINE int win32munmap(void* ptr, size_t size) { + MEMORY_BASIC_INFORMATION minfo; + char* cptr = (char*)ptr; + while (size) { + if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) + return -1; + if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || + minfo.State != MEM_COMMIT || minfo.RegionSize > size) + return -1; + if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) + return -1; + cptr += minfo.RegionSize; + size -= minfo.RegionSize; + } + return 0; +} + +#define MMAP_DEFAULT(s) win32mmap(s) +#define MUNMAP_DEFAULT(a, s) win32munmap((a), (s)) +#define DIRECT_MMAP_DEFAULT(s) win32direct_mmap(s) +#endif /* WIN32 */ +#endif /* HAVE_MMAP */ + +#if HAVE_MREMAP +#ifndef WIN32 +#define MREMAP_DEFAULT(addr, osz, nsz, mv) mremap((addr), (osz), (nsz), (mv)) +#endif /* WIN32 */ +#endif /* HAVE_MREMAP */ + + +/** + * Define CALL_MORECORE + */ +#if HAVE_MORECORE + #ifdef MORECORE + #define CALL_MORECORE(S) MORECORE(S) + #else /* MORECORE */ + #define CALL_MORECORE(S) MORECORE_DEFAULT(S) + #endif /* MORECORE */ +#else /* HAVE_MORECORE */ + #define CALL_MORECORE(S) MFAIL +#endif /* HAVE_MORECORE */ + +/** + * Define CALL_MMAP/CALL_MUNMAP/CALL_DIRECT_MMAP + */ +#if HAVE_MMAP + #define IS_MMAPPED_BIT (SIZE_T_ONE) + #define USE_MMAP_BIT (SIZE_T_ONE) + + #ifdef MMAP + #define CALL_MMAP(s) MMAP(s) + #else /* MMAP */ + #define CALL_MMAP(s) MMAP_DEFAULT(s) + #endif /* MMAP */ + #ifdef MUNMAP + #define CALL_MUNMAP(a, s) MUNMAP((a), (s)) + #else /* MUNMAP */ + #define CALL_MUNMAP(a, s) MUNMAP_DEFAULT((a), (s)) + #endif /* MUNMAP */ + #ifdef DIRECT_MMAP + #define CALL_DIRECT_MMAP(s) DIRECT_MMAP(s) + #else /* DIRECT_MMAP */ + #define CALL_DIRECT_MMAP(s) DIRECT_MMAP_DEFAULT(s) + #endif /* DIRECT_MMAP */ +#else /* HAVE_MMAP */ + #define IS_MMAPPED_BIT (SIZE_T_ZERO) + #define USE_MMAP_BIT (SIZE_T_ZERO) + + #define MMAP(s) MFAIL + #define MUNMAP(a, s) (-1) + #define DIRECT_MMAP(s) MFAIL + #define CALL_DIRECT_MMAP(s) DIRECT_MMAP(s) + #define CALL_MMAP(s) MMAP(s) + #define CALL_MUNMAP(a, s) MUNMAP((a), (s)) +#endif /* HAVE_MMAP */ + +/** + * Define CALL_MREMAP + */ +#if HAVE_MMAP && HAVE_MREMAP + #ifdef MREMAP + #define CALL_MREMAP(addr, osz, nsz, mv) MREMAP((addr), (osz), (nsz), (mv)) + #else /* MREMAP */ + #define CALL_MREMAP(addr, osz, nsz, mv) MREMAP_DEFAULT((addr), (osz), (nsz), (mv)) + #endif /* MREMAP */ +#else /* HAVE_MMAP && HAVE_MREMAP */ + #define CALL_MREMAP(addr, osz, nsz, mv) MFAIL +#endif /* HAVE_MMAP && HAVE_MREMAP */ + +/* mstate bit set if continguous morecore disabled or failed */ +#define USE_NONCONTIGUOUS_BIT (4U) + +/* segment bit set in create_mspace_with_base */ +#define EXTERN_BIT (8U) + + +/* --------------------------- Lock preliminaries ------------------------ */ + +/* + When locks are defined, there is one global lock, plus + one per-mspace lock. + + The global lock_ensures that mparams.magic and other unique + mparams values are initialized only once. It also protects + sequences of calls to MORECORE. In many cases sys_alloc requires + two calls, that should not be interleaved with calls by other + threads. This does not protect against direct calls to MORECORE + by other threads not using this lock, so there is still code to + cope the best we can on interference. + + Per-mspace locks surround calls to malloc, free, etc. To enable use + in layered extensions, per-mspace locks are reentrant. + + Because lock-protected regions generally have bounded times, it is + OK to use the supplied simple spinlocks in the custom versions for + x86. + + If USE_LOCKS is > 1, the definitions of lock routines here are + bypassed, in which case you will need to define at least + INITIAL_LOCK, ACQUIRE_LOCK, RELEASE_LOCK and possibly TRY_LOCK + (which is not used in this malloc, but commonly needed in + extensions.) +*/ + +#if USE_LOCKS == 1 + +#if USE_SPIN_LOCKS +#ifndef WIN32 + +/* Custom pthread-style spin locks on x86 and x64 for gcc */ +struct pthread_mlock_t { + volatile unsigned int l; + volatile unsigned int c; + volatile pthread_t threadid; +}; +#define MLOCK_T struct pthread_mlock_t +#define CURRENT_THREAD pthread_self() +#define INITIAL_LOCK(sl) (memset(sl, 0, sizeof(MLOCK_T)), 0) +#define ACQUIRE_LOCK(sl) pthread_acquire_lock(sl) +#define RELEASE_LOCK(sl) pthread_release_lock(sl) +#define TRY_LOCK(sl) pthread_try_lock(sl) +#define SPINS_PER_YIELD 63 + +static MLOCK_T malloc_global_mutex = { 0, 0, 0}; + +static FORCEINLINE int pthread_acquire_lock (MLOCK_T *sl) { + int spins = 0; + volatile unsigned int* lp = &sl->l; + for (;;) { + if (*lp != 0) { + if (sl->threadid == CURRENT_THREAD) { + ++sl->c; + return 0; + } + } + else { + /* place args to cmpxchgl in locals to evade oddities in some gccs */ + int cmp = 0; + int val = 1; + int ret; + __asm__ __volatile__ ("lock; cmpxchgl %1, %2" + : "=a" (ret) + : "r" (val), "m" (*(lp)), "0"(cmp) + : "memory", "cc"); + if (!ret) { + assert(!sl->threadid); + sl->c = 1; + sl->threadid = CURRENT_THREAD; + return 0; + } + if ((++spins & SPINS_PER_YIELD) == 0) { +#if defined (__SVR4) && defined (__sun) /* solaris */ + thr_yield(); +#else +#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) + sched_yield(); +#else /* no-op yield on unknown systems */ + ; +#endif /* __linux__ || __FreeBSD__ || __APPLE__ */ +#endif /* solaris */ + } + } + } +} + +static FORCEINLINE void pthread_release_lock (MLOCK_T *sl) { + assert(sl->l != 0); + assert(sl->threadid == CURRENT_THREAD); + if (--sl->c == 0) { + sl->threadid = 0; + volatile unsigned int* lp = &sl->l; + int prev = 0; + int ret; + __asm__ __volatile__ ("lock; xchgl %0, %1" + : "=r" (ret) + : "m" (*(lp)), "0"(prev) + : "memory"); + } +} + +static FORCEINLINE int pthread_try_lock (MLOCK_T *sl) { + volatile unsigned int* lp = &sl->l; + if (*lp != 0) { + if (sl->threadid == CURRENT_THREAD) { + ++sl->c; + return 1; + } + } + else { + int cmp = 0; + int val = 1; + int ret; + __asm__ __volatile__ ("lock; cmpxchgl %1, %2" + : "=a" (ret) + : "r" (val), "m" (*(lp)), "0"(cmp) + : "memory", "cc"); + if (!ret) { + assert(!sl->threadid); + sl->c = 1; + sl->threadid = CURRENT_THREAD; + return 1; + } + } + return 0; +} + + +#else /* WIN32 */ +/* Custom win32-style spin locks on x86 and x64 for MSC */ +struct win32_mlock_t +{ + volatile long l; + volatile unsigned int c; + volatile long threadid; +}; + +#define MLOCK_T struct win32_mlock_t +#define CURRENT_THREAD win32_getcurrentthreadid() +#define INITIAL_LOCK(sl) (memset(sl, 0, sizeof(MLOCK_T)), 0) +#define ACQUIRE_LOCK(sl) win32_acquire_lock(sl) +#define RELEASE_LOCK(sl) win32_release_lock(sl) +#define TRY_LOCK(sl) win32_try_lock(sl) +#define SPINS_PER_YIELD 63 + +static MLOCK_T malloc_global_mutex = { 0, 0, 0}; + +static FORCEINLINE long win32_getcurrentthreadid() { +#ifdef _MSC_VER +#if defined(_M_IX86) + long *threadstruct=(long *)__readfsdword(0x18); + long threadid=threadstruct[0x24/sizeof(long)]; + return threadid; +#elif defined(_M_X64) + /* todo */ + return GetCurrentThreadId(); +#else + return GetCurrentThreadId(); +#endif +#else + return GetCurrentThreadId(); +#endif +} + +static FORCEINLINE int win32_acquire_lock (MLOCK_T *sl) { + int spins = 0; + for (;;) { + if (sl->l != 0) { + if (sl->threadid == CURRENT_THREAD) { + ++sl->c; + return 0; + } + } + else { + if (!interlockedexchange(&sl->l, 1)) { + assert(!sl->threadid); + sl->c=CURRENT_THREAD; + sl->threadid = CURRENT_THREAD; + sl->c = 1; + return 0; + } + } + if ((++spins & SPINS_PER_YIELD) == 0) + SleepEx(0, FALSE); + } +} + +static FORCEINLINE void win32_release_lock (MLOCK_T *sl) { + assert(sl->threadid == CURRENT_THREAD); + assert(sl->l != 0); + if (--sl->c == 0) { + sl->threadid = 0; + interlockedexchange (&sl->l, 0); + } +} + +static FORCEINLINE int win32_try_lock (MLOCK_T *sl) { + if(sl->l != 0) { + if (sl->threadid == CURRENT_THREAD) { + ++sl->c; + return 1; + } + } + else { + if (!interlockedexchange(&sl->l, 1)){ + assert(!sl->threadid); + sl->threadid = CURRENT_THREAD; + sl->c = 1; + return 1; + } + } + return 0; +} + +#endif /* WIN32 */ +#else /* USE_SPIN_LOCKS */ + +#ifndef WIN32 +/* pthreads-based locks */ + +#define MLOCK_T pthread_mutex_t +#define CURRENT_THREAD pthread_self() +#define INITIAL_LOCK(sl) pthread_init_lock(sl) +#define ACQUIRE_LOCK(sl) pthread_mutex_lock(sl) +#define RELEASE_LOCK(sl) pthread_mutex_unlock(sl) +#define TRY_LOCK(sl) (!pthread_mutex_trylock(sl)) + +static MLOCK_T malloc_global_mutex = PTHREAD_MUTEX_INITIALIZER; + +/* Cope with old-style linux recursive lock initialization by adding */ +/* skipped internal declaration from pthread.h */ +#ifdef linux +#ifndef PTHREAD_MUTEX_RECURSIVE +extern int pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr, + int __kind)); +#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP +#define pthread_mutexattr_settype(x,y) pthread_mutexattr_setkind_np(x,y) +#endif +#endif + +static int pthread_init_lock (MLOCK_T *sl) { + pthread_mutexattr_t attr; + if (pthread_mutexattr_init(&attr)) return 1; + if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) return 1; + if (pthread_mutex_init(sl, &attr)) return 1; + if (pthread_mutexattr_destroy(&attr)) return 1; + return 0; +} + +#else /* WIN32 */ +/* Win32 critical sections */ +#define MLOCK_T CRITICAL_SECTION +#define CURRENT_THREAD GetCurrentThreadId() +#define INITIAL_LOCK(s) (!InitializeCriticalSectionAndSpinCount((s), 0x80000000|4000)) +#define ACQUIRE_LOCK(s) (EnterCriticalSection(s), 0) +#define RELEASE_LOCK(s) LeaveCriticalSection(s) +#define TRY_LOCK(s) TryEnterCriticalSection(s) +#define NEED_GLOBAL_LOCK_INIT + +static MLOCK_T malloc_global_mutex; +static volatile long malloc_global_mutex_status; + +/* Use spin loop to initialize global lock */ +static void init_malloc_global_mutex() { + for (;;) { + long stat = malloc_global_mutex_status; + if (stat > 0) + return; + /* transition to < 0 while initializing, then to > 0) */ + if (stat == 0 && + interlockedcompareexchange(&malloc_global_mutex_status, -1, 0) == 0) { + InitializeCriticalSection(&malloc_global_mutex); + interlockedexchange(&malloc_global_mutex_status,1); + return; + } + SleepEx(0, FALSE); + } +} + +#endif /* WIN32 */ +#endif /* USE_SPIN_LOCKS */ +#endif /* USE_LOCKS == 1 */ + +/* ----------------------- User-defined locks ------------------------ */ + +#if USE_LOCKS > 1 +/* Define your own lock implementation here */ +/* #define INITIAL_LOCK(sl) ... */ +/* #define ACQUIRE_LOCK(sl) ... */ +/* #define RELEASE_LOCK(sl) ... */ +/* #define TRY_LOCK(sl) ... */ +/* static MLOCK_T malloc_global_mutex = ... */ +#endif /* USE_LOCKS > 1 */ + +/* ----------------------- Lock-based state ------------------------ */ + +#if USE_LOCKS +#define USE_LOCK_BIT (2U) +#else /* USE_LOCKS */ +#define USE_LOCK_BIT (0U) +#define INITIAL_LOCK(l) +#endif /* USE_LOCKS */ + +#if USE_LOCKS +#define ACQUIRE_MALLOC_GLOBAL_LOCK() ACQUIRE_LOCK(&malloc_global_mutex); +#define RELEASE_MALLOC_GLOBAL_LOCK() RELEASE_LOCK(&malloc_global_mutex); +#else /* USE_LOCKS */ +#define ACQUIRE_MALLOC_GLOBAL_LOCK() +#define RELEASE_MALLOC_GLOBAL_LOCK() +#endif /* USE_LOCKS */ + + +/* ----------------------- Chunk representations ------------------------ */ + +/* + (The following includes lightly edited explanations by Colin Plumb.) + + The malloc_chunk declaration below is misleading (but accurate and + necessary). It declares a "view" into memory allowing access to + necessary fields at known offsets from a given base. + + Chunks of memory are maintained using a `boundary tag' method as + originally described by Knuth. (See the paper by Paul Wilson + ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such + techniques.) Sizes of free chunks are stored both in the front of + each chunk and at the end. This makes consolidating fragmented + chunks into bigger chunks fast. The head fields also hold bits + representing whether chunks are free or in use. + + Here are some pictures to make it clearer. They are "exploded" to + show that the state of a chunk can be thought of as extending from + the high 31 bits of the head field of its header through the + prev_foot and PINUSE_BIT bit of the following chunk header. + + A chunk that's in use looks like: + + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of previous chunk (if P = 0) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| + | Size of this chunk 1| +-+ + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + +- -+ + | | + +- -+ + | : + +- size - sizeof(size_t) available payload bytes -+ + : | + chunk-> +- -+ + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1| + | Size of next chunk (may or may not be in use) | +-+ + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + And if it's free, it looks like this: + + chunk-> +- -+ + | User payload (must be in use, or we would have merged!) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| + | Size of this chunk 0| +-+ + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Next pointer | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Prev pointer | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | : + +- size - sizeof(struct chunk) unused bytes -+ + : | + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of this chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0| + | Size of next chunk (must be in use, or we would have merged)| +-+ + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | : + +- User payload -+ + : | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |0| + +-+ + Note that since we always merge adjacent free chunks, the chunks + adjacent to a free chunk must be in use. + + Given a pointer to a chunk (which can be derived trivially from the + payload pointer) we can, in O(1) time, find out whether the adjacent + chunks are free, and if so, unlink them from the lists that they + are on and merge them with the current chunk. + + Chunks always begin on even word boundaries, so the mem portion + (which is returned to the user) is also on an even word boundary, and + thus at least double-word aligned. + + The P (PINUSE_BIT) bit, stored in the unused low-order bit of the + chunk size (which is always a multiple of two words), is an in-use + bit for the *previous* chunk. If that bit is *clear*, then the + word before the current chunk size contains the previous chunk + size, and can be used to find the front of the previous chunk. + The very first chunk allocated always has this bit set, preventing + access to non-existent (or non-owned) memory. If pinuse is set for + any given chunk, then you CANNOT determine the size of the + previous chunk, and might even get a memory addressing fault when + trying to do so. + + The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of + the chunk size redundantly records whether the current chunk is + inuse. This redundancy enables usage checks within free and realloc, + and reduces indirection when freeing and consolidating chunks. + + Each freshly allocated chunk must have both cinuse and pinuse set. + That is, each allocated chunk borders either a previously allocated + and still in-use chunk, or the base of its memory arena. This is + ensured by making all allocations from the the `lowest' part of any + found chunk. Further, no free chunk physically borders another one, + so each free chunk is known to be preceded and followed by either + inuse chunks or the ends of memory. + + Note that the `foot' of the current chunk is actually represented + as the prev_foot of the NEXT chunk. This makes it easier to + deal with alignments etc but can be very confusing when trying + to extend or adapt this code. + + The exceptions to all this are + + 1. The special chunk `top' is the top-most available chunk (i.e., + the one bordering the end of available memory). It is treated + specially. Top is never included in any bin, is used only if + no other chunk is available, and is released back to the + system if it is very large (see M_TRIM_THRESHOLD). In effect, + the top chunk is treated as larger (and thus less well + fitting) than any other available chunk. The top chunk + doesn't update its trailing size field since there is no next + contiguous chunk that would have to index off it. However, + space is still allocated for it (TOP_FOOT_SIZE) to enable + separation or merging when space is extended. + + 3. Chunks allocated via mmap, which have the lowest-order bit + (IS_MMAPPED_BIT) set in their prev_foot fields, and do not set + PINUSE_BIT in their head fields. Because they are allocated + one-by-one, each must carry its own prev_foot field, which is + also used to hold the offset this chunk has within its mmapped + region, which is needed to preserve alignment. Each mmapped + chunk is trailed by the first two fields of a fake next-chunk + for sake of usage checks. + +*/ + +struct malloc_chunk { + size_t prev_foot; /* Size of previous chunk (if free). */ + size_t head; /* Size and inuse bits. */ + struct malloc_chunk* fd; /* double links -- used only if free. */ + struct malloc_chunk* bk; +}; + +typedef struct malloc_chunk mchunk; +typedef struct malloc_chunk* mchunkptr; +typedef struct malloc_chunk* sbinptr; /* The type of bins of chunks */ +typedef unsigned int bindex_t; /* Described below */ +typedef unsigned int binmap_t; /* Described below */ +typedef unsigned int flag_t; /* The type of various bit flag sets */ + +/* ------------------- Chunks sizes and alignments ----------------------- */ + +#define MCHUNK_SIZE (sizeof(mchunk)) + +#if FOOTERS +#define CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) +#else /* FOOTERS */ +#define CHUNK_OVERHEAD (SIZE_T_SIZE) +#endif /* FOOTERS */ + +/* MMapped chunks need a second word of overhead ... */ +#define MMAP_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) +/* ... and additional padding for fake next-chunk at foot */ +#define MMAP_FOOT_PAD (FOUR_SIZE_T_SIZES) + +/* The smallest size we can malloc is an aligned minimal chunk */ +#define MIN_CHUNK_SIZE\ + ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) + +/* conversion from malloc headers to user pointers, and back */ +#define chunk2mem(p) ((void*)((char*)(p) + TWO_SIZE_T_SIZES)) +#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - TWO_SIZE_T_SIZES)) +/* chunk associated with aligned address A */ +#define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A))) + +/* Bounds on request (not chunk) sizes. */ +#define MAX_REQUEST ((-MIN_CHUNK_SIZE) << 2) +#define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE) + +/* pad request bytes into a usable size */ +#define pad_request(req) \ + (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) + +/* pad request, checking for minimum (but not maximum) */ +#define request2size(req) \ + (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) + + +/* ------------------ Operations on head and foot fields ----------------- */ + +/* + The head field of a chunk is or'ed with PINUSE_BIT when previous + adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in + use. If the chunk was obtained with mmap, the prev_foot field has + IS_MMAPPED_BIT set, otherwise holding the offset of the base of the + mmapped region to the base of the chunk. + + FLAG4_BIT is not used by this malloc, but might be useful in extensions. +*/ + +#define PINUSE_BIT (SIZE_T_ONE) +#define CINUSE_BIT (SIZE_T_TWO) +#define FLAG4_BIT (SIZE_T_FOUR) +#define INUSE_BITS (PINUSE_BIT|CINUSE_BIT) +#define FLAG_BITS (PINUSE_BIT|CINUSE_BIT|FLAG4_BIT) + +/* Head value for fenceposts */ +#define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE) + +/* extraction of fields from head words */ +#define cinuse(p) ((p)->head & CINUSE_BIT) +#define pinuse(p) ((p)->head & PINUSE_BIT) +#define chunksize(p) ((p)->head & ~(FLAG_BITS)) + +#define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) +#define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT) + +/* Treat space at ptr +/- offset as a chunk */ +#define chunk_plus_offset(p, s) ((mchunkptr)(((char*)(p)) + (s))) +#define chunk_minus_offset(p, s) ((mchunkptr)(((char*)(p)) - (s))) + +/* Ptr to next or previous physical malloc_chunk. */ +#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->head & ~FLAG_BITS))) +#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_foot) )) + +/* extract next chunk's pinuse bit */ +#define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT) + +/* Get/set size at footer */ +#define get_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_foot) +#define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_foot = (s)) + +/* Set size, pinuse bit, and foot */ +#define set_size_and_pinuse_of_free_chunk(p, s)\ + ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) + +/* Set size, pinuse bit, foot, and clear next pinuse */ +#define set_free_with_pinuse(p, s, n)\ + (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) + +#define is_mmapped(p)\ + (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_MMAPPED_BIT)) + +/* Get the internal overhead associated with chunk p */ +#define overhead_for(p)\ + (is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD) + +/* Return true if malloced space is not necessarily cleared */ +#if MMAP_CLEARS +#define calloc_must_clear(p) (!is_mmapped(p)) +#else /* MMAP_CLEARS */ +#define calloc_must_clear(p) (1) +#endif /* MMAP_CLEARS */ + +/* ---------------------- Overlaid data structures ----------------------- */ + +/* + When chunks are not in use, they are treated as nodes of either + lists or trees. + + "Small" chunks are stored in circular doubly-linked lists, and look + like this: + + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of previous chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `head:' | Size of chunk, in bytes |P| + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Forward pointer to next chunk in list | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Back pointer to previous chunk in list | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Unused space (may be 0 bytes long) . + . . + . | +nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `foot:' | Size of chunk, in bytes | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Larger chunks are kept in a form of bitwise digital trees (aka + tries) keyed on chunksizes. Because malloc_tree_chunks are only for + free chunks greater than 256 bytes, their size doesn't impose any + constraints on user chunk sizes. Each node looks like: + + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of previous chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `head:' | Size of chunk, in bytes |P| + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Forward pointer to next chunk of same size | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Back pointer to previous chunk of same size | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Pointer to left child (child[0]) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Pointer to right child (child[1]) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Pointer to parent | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | bin index of this chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Unused space . + . | +nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `foot:' | Size of chunk, in bytes | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Each tree holding treenodes is a tree of unique chunk sizes. Chunks + of the same size are arranged in a circularly-linked list, with only + the oldest chunk (the next to be used, in our FIFO ordering) + actually in the tree. (Tree members are distinguished by a non-null + parent pointer.) If a chunk with the same size an an existing node + is inserted, it is linked off the existing node using pointers that + work in the same way as fd/bk pointers of small chunks. + + Each tree contains a power of 2 sized range of chunk sizes (the + smallest is 0x100 <= x < 0x180), which is is divided in half at each + tree level, with the chunks in the smaller half of the range (0x100 + <= x < 0x140 for the top nose) in the left subtree and the larger + half (0x140 <= x < 0x180) in the right subtree. This is, of course, + done by inspecting individual bits. + + Using these rules, each node's left subtree contains all smaller + sizes than its right subtree. However, the node at the root of each + subtree has no particular ordering relationship to either. (The + dividing line between the subtree sizes is based on trie relation.) + If we remove the last chunk of a given size from the interior of the + tree, we need to replace it with a leaf node. The tree ordering + rules permit a node to be replaced by any leaf below it. + + The smallest chunk in a tree (a common operation in a best-fit + allocator) can be found by walking a path to the leftmost leaf in + the tree. Unlike a usual binary tree, where we follow left child + pointers until we reach a null, here we follow the right child + pointer any time the left one is null, until we reach a leaf with + both child pointers null. The smallest chunk in the tree will be + somewhere along that path. + + The worst case number of steps to add, find, or remove a node is + bounded by the number of bits differentiating chunks within + bins. Under current bin calculations, this ranges from 6 up to 21 + (for 32 bit sizes) or up to 53 (for 64 bit sizes). The typical case + is of course much better. +*/ + +struct malloc_tree_chunk { + /* The first four fields must be compatible with malloc_chunk */ + size_t prev_foot; + size_t head; + struct malloc_tree_chunk* fd; + struct malloc_tree_chunk* bk; + + struct malloc_tree_chunk* child[2]; + struct malloc_tree_chunk* parent; + bindex_t index; +}; + +typedef struct malloc_tree_chunk tchunk; +typedef struct malloc_tree_chunk* tchunkptr; +typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ + +/* A little helper macro for trees */ +#define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1]) + +/* ----------------------------- Segments -------------------------------- */ + +/* + Each malloc space may include non-contiguous segments, held in a + list headed by an embedded malloc_segment record representing the + top-most space. Segments also include flags holding properties of + the space. Large chunks that are directly allocated by mmap are not + included in this list. They are instead independently created and + destroyed without otherwise keeping track of them. + + Segment management mainly comes into play for spaces allocated by + MMAP. Any call to MMAP might or might not return memory that is + adjacent to an existing segment. MORECORE normally contiguously + extends the current space, so this space is almost always adjacent, + which is simpler and faster to deal with. (This is why MORECORE is + used preferentially to MMAP when both are available -- see + sys_alloc.) When allocating using MMAP, we don't use any of the + hinting mechanisms (inconsistently) supported in various + implementations of unix mmap, or distinguish reserving from + committing memory. Instead, we just ask for space, and exploit + contiguity when we get it. It is probably possible to do + better than this on some systems, but no general scheme seems + to be significantly better. + + Management entails a simpler variant of the consolidation scheme + used for chunks to reduce fragmentation -- new adjacent memory is + normally prepended or appended to an existing segment. However, + there are limitations compared to chunk consolidation that mostly + reflect the fact that segment processing is relatively infrequent + (occurring only when getting memory from system) and that we + don't expect to have huge numbers of segments: + + * Segments are not indexed, so traversal requires linear scans. (It + would be possible to index these, but is not worth the extra + overhead and complexity for most programs on most platforms.) + * New segments are only appended to old ones when holding top-most + memory; if they cannot be prepended to others, they are held in + different segments. + + Except for the top-most segment of an mstate, each segment record + is kept at the tail of its segment. Segments are added by pushing + segment records onto the list headed by &mstate.seg for the + containing mstate. + + Segment flags control allocation/merge/deallocation policies: + * If EXTERN_BIT set, then we did not allocate this segment, + and so should not try to deallocate or merge with others. + (This currently holds only for the initial segment passed + into create_mspace_with_base.) + * If IS_MMAPPED_BIT set, the segment may be merged with + other surrounding mmapped segments and trimmed/de-allocated + using munmap. + * If neither bit is set, then the segment was obtained using + MORECORE so can be merged with surrounding MORECORE'd segments + and deallocated/trimmed using MORECORE with negative arguments. +*/ + +struct malloc_segment { + char* base; /* base address */ + size_t size; /* allocated size */ + struct malloc_segment* next; /* ptr to next segment */ + flag_t sflags; /* mmap and extern flag */ +}; + +#define is_mmapped_segment(S) ((S)->sflags & IS_MMAPPED_BIT) +#define is_extern_segment(S) ((S)->sflags & EXTERN_BIT) + +typedef struct malloc_segment msegment; +typedef struct malloc_segment* msegmentptr; + +/* ---------------------------- malloc_state ----------------------------- */ + +/* + A malloc_state holds all of the bookkeeping for a space. + The main fields are: + + Top + The topmost chunk of the currently active segment. Its size is + cached in topsize. The actual size of topmost space is + topsize+TOP_FOOT_SIZE, which includes space reserved for adding + fenceposts and segment records if necessary when getting more + space from the system. The size at which to autotrim top is + cached from mparams in trim_check, except that it is disabled if + an autotrim fails. + + Designated victim (dv) + This is the preferred chunk for servicing small requests that + don't have exact fits. It is normally the chunk split off most + recently to service another small request. Its size is cached in + dvsize. The link fields of this chunk are not maintained since it + is not kept in a bin. + + SmallBins + An array of bin headers for free chunks. These bins hold chunks + with sizes less than MIN_LARGE_SIZE bytes. Each bin contains + chunks of all the same size, spaced 8 bytes apart. To simplify + use in double-linked lists, each bin header acts as a malloc_chunk + pointing to the real first node, if it exists (else pointing to + itself). This avoids special-casing for headers. But to avoid + waste, we allocate only the fd/bk pointers of bins, and then use + repositioning tricks to treat these as the fields of a chunk. + + TreeBins + Treebins are pointers to the roots of trees holding a range of + sizes. There are 2 equally spaced treebins for each power of two + from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything + larger. + + Bin maps + There is one bit map for small bins ("smallmap") and one for + treebins ("treemap). Each bin sets its bit when non-empty, and + clears the bit when empty. Bit operations are then used to avoid + bin-by-bin searching -- nearly all "search" is done without ever + looking at bins that won't be selected. The bit maps + conservatively use 32 bits per map word, even if on 64bit system. + For a good description of some of the bit-based techniques used + here, see Henry S. Warren Jr's book "Hacker's Delight" (and + supplement at http://hackersdelight.org/). Many of these are + intended to reduce the branchiness of paths through malloc etc, as + well as to reduce the number of memory locations read or written. + + Segments + A list of segments headed by an embedded malloc_segment record + representing the initial space. + + Address check support + The least_addr field is the least address ever obtained from + MORECORE or MMAP. Attempted frees and reallocs of any address less + than this are trapped (unless INSECURE is defined). + + Magic tag + A cross-check field that should always hold same value as mparams.magic. + + Flags + Bits recording whether to use MMAP, locks, or contiguous MORECORE + + Statistics + Each space keeps track of current and maximum system memory + obtained via MORECORE or MMAP. + + Trim support + Fields holding the amount of unused topmost memory that should trigger + timming, and a counter to force periodic scanning to release unused + non-topmost segments. + + Locking + If USE_LOCKS is defined, the "mutex" lock is acquired and released + around every public call using this mspace. + + Extension support + A void* pointer and a size_t field that can be used to help implement + extensions to this malloc. +*/ + +/* Bin types, widths and sizes */ +#define NSMALLBINS (32U) +#define NTREEBINS (32U) +#define SMALLBIN_SHIFT (3U) +#define SMALLBIN_WIDTH (SIZE_T_ONE << SMALLBIN_SHIFT) +#define TREEBIN_SHIFT (8U) +#define MIN_LARGE_SIZE (SIZE_T_ONE << TREEBIN_SHIFT) +#define MAX_SMALL_SIZE (MIN_LARGE_SIZE - SIZE_T_ONE) +#define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD) + +struct malloc_state { + binmap_t smallmap; + binmap_t treemap; + size_t dvsize; + size_t topsize; + char* least_addr; + mchunkptr dv; + mchunkptr top; + size_t trim_check; + size_t release_checks; + size_t magic; + mchunkptr smallbins[(NSMALLBINS+1)*2]; + tbinptr treebins[NTREEBINS]; + size_t footprint; + size_t max_footprint; + flag_t mflags; +#if USE_LOCKS + MLOCK_T mutex; /* locate lock among fields that rarely change */ +#endif /* USE_LOCKS */ + msegment seg; + void* extp; /* Unused but available for extensions */ + size_t exts; +}; + +typedef struct malloc_state* mstate; + +/* ------------- Global malloc_state and malloc_params ------------------- */ + +/* + malloc_params holds global properties, including those that can be + dynamically set using mallopt. There is a single instance, mparams, + initialized in init_mparams. Note that the non-zeroness of "magic" + also serves as an initialization flag. +*/ + +struct malloc_params { + volatile size_t magic; + size_t page_size; + size_t granularity; + size_t mmap_threshold; + size_t trim_threshold; + flag_t default_mflags; +}; + +static struct malloc_params mparams; + +/* Ensure mparams initialized */ +#define ensure_initialization() (mparams.magic != 0 || init_mparams()) + +#if !ONLY_MSPACES + +/* The global malloc_state used for all non-"mspace" calls */ +static struct malloc_state _gm_; +#define gm (&_gm_) +#define is_global(M) ((M) == &_gm_) + +#endif /* !ONLY_MSPACES */ + +#define is_initialized(M) ((M)->top != 0) + +/* -------------------------- system alloc setup ------------------------- */ + +/* Operations on mflags */ + +#define use_lock(M) ((M)->mflags & USE_LOCK_BIT) +#define enable_lock(M) ((M)->mflags |= USE_LOCK_BIT) +#define disable_lock(M) ((M)->mflags &= ~USE_LOCK_BIT) + +#define use_mmap(M) ((M)->mflags & USE_MMAP_BIT) +#define enable_mmap(M) ((M)->mflags |= USE_MMAP_BIT) +#define disable_mmap(M) ((M)->mflags &= ~USE_MMAP_BIT) + +#define use_noncontiguous(M) ((M)->mflags & USE_NONCONTIGUOUS_BIT) +#define disable_contiguous(M) ((M)->mflags |= USE_NONCONTIGUOUS_BIT) + +#define set_lock(M,L)\ + ((M)->mflags = (L)?\ + ((M)->mflags | USE_LOCK_BIT) :\ + ((M)->mflags & ~USE_LOCK_BIT)) + +/* page-align a size */ +#define page_align(S)\ + (((S) + (mparams.page_size - SIZE_T_ONE)) & ~(mparams.page_size - SIZE_T_ONE)) + +/* granularity-align a size */ +#define granularity_align(S)\ + (((S) + (mparams.granularity - SIZE_T_ONE))\ + & ~(mparams.granularity - SIZE_T_ONE)) + + +/* For mmap, use granularity alignment on windows, else page-align */ +#ifdef WIN32 +#define mmap_align(S) granularity_align(S) +#else +#define mmap_align(S) page_align(S) +#endif + +/* For sys_alloc, enough padding to ensure can malloc request on success */ +#define SYS_ALLOC_PADDING (TOP_FOOT_SIZE + MALLOC_ALIGNMENT) + +#define is_page_aligned(S)\ + (((size_t)(S) & (mparams.page_size - SIZE_T_ONE)) == 0) +#define is_granularity_aligned(S)\ + (((size_t)(S) & (mparams.granularity - SIZE_T_ONE)) == 0) + +/* True if segment S holds address A */ +#define segment_holds(S, A)\ + ((char*)(A) >= S->base && (char*)(A) < S->base + S->size) + +/* Return segment holding given address */ +static msegmentptr segment_holding(mstate m, char* addr) { + msegmentptr sp = &m->seg; + for (;;) { + if (addr >= sp->base && addr < sp->base + sp->size) + return sp; + if ((sp = sp->next) == 0) + return 0; + } +} + +/* Return true if segment contains a segment link */ +static int has_segment_link(mstate m, msegmentptr ss) { + msegmentptr sp = &m->seg; + for (;;) { + if ((char*)sp >= ss->base && (char*)sp < ss->base + ss->size) + return 1; + if ((sp = sp->next) == 0) + return 0; + } +} + +#ifndef MORECORE_CANNOT_TRIM +#define should_trim(M,s) ((s) > (M)->trim_check) +#else /* MORECORE_CANNOT_TRIM */ +#define should_trim(M,s) (0) +#endif /* MORECORE_CANNOT_TRIM */ + +/* + TOP_FOOT_SIZE is padding at the end of a segment, including space + that may be needed to place segment records and fenceposts when new + noncontiguous segments are added. +*/ +#define TOP_FOOT_SIZE\ + (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) + + +/* ------------------------------- Hooks -------------------------------- */ + +/* + PREACTION should be defined to return 0 on success, and nonzero on + failure. If you are not using locking, you can redefine these to do + anything you like. +*/ + +#if USE_LOCKS + +#define PREACTION(M) ((use_lock(M))? ACQUIRE_LOCK(&(M)->mutex) : 0) +#define POSTACTION(M) { if (use_lock(M)) RELEASE_LOCK(&(M)->mutex); } +#else /* USE_LOCKS */ + +#ifndef PREACTION +#define PREACTION(M) (0) +#endif /* PREACTION */ + +#ifndef POSTACTION +#define POSTACTION(M) +#endif /* POSTACTION */ + +#endif /* USE_LOCKS */ + +/* + CORRUPTION_ERROR_ACTION is triggered upon detected bad addresses. + USAGE_ERROR_ACTION is triggered on detected bad frees and + reallocs. The argument p is an address that might have triggered the + fault. It is ignored by the two predefined actions, but might be + useful in custom actions that try to help diagnose errors. +*/ + +#if PROCEED_ON_ERROR + +/* A count of the number of corruption errors causing resets */ +int malloc_corruption_error_count; + +/* default corruption action */ +static void reset_on_error(mstate m); + +#define CORRUPTION_ERROR_ACTION(m) reset_on_error(m) +#define USAGE_ERROR_ACTION(m, p) + +#else /* PROCEED_ON_ERROR */ + +#ifndef CORRUPTION_ERROR_ACTION +#define CORRUPTION_ERROR_ACTION(m) ABORT +#endif /* CORRUPTION_ERROR_ACTION */ + +#ifndef USAGE_ERROR_ACTION +#define USAGE_ERROR_ACTION(m,p) ABORT +#endif /* USAGE_ERROR_ACTION */ + +#endif /* PROCEED_ON_ERROR */ + +/* -------------------------- Debugging setup ---------------------------- */ + +#if ! DEBUG + +#define check_free_chunk(M,P) +#define check_inuse_chunk(M,P) +#define check_malloced_chunk(M,P,N) +#define check_mmapped_chunk(M,P) +#define check_malloc_state(M) +#define check_top_chunk(M,P) + +#else /* DEBUG */ +#define check_free_chunk(M,P) do_check_free_chunk(M,P) +#define check_inuse_chunk(M,P) do_check_inuse_chunk(M,P) +#define check_top_chunk(M,P) do_check_top_chunk(M,P) +#define check_malloced_chunk(M,P,N) do_check_malloced_chunk(M,P,N) +#define check_mmapped_chunk(M,P) do_check_mmapped_chunk(M,P) +#define check_malloc_state(M) do_check_malloc_state(M) + +static void do_check_any_chunk(mstate m, mchunkptr p); +static void do_check_top_chunk(mstate m, mchunkptr p); +static void do_check_mmapped_chunk(mstate m, mchunkptr p); +static void do_check_inuse_chunk(mstate m, mchunkptr p); +static void do_check_free_chunk(mstate m, mchunkptr p); +static void do_check_malloced_chunk(mstate m, void* mem, size_t s); +static void do_check_tree(mstate m, tchunkptr t); +static void do_check_treebin(mstate m, bindex_t i); +static void do_check_smallbin(mstate m, bindex_t i); +static void do_check_malloc_state(mstate m); +static int bin_find(mstate m, mchunkptr x); +static size_t traverse_and_check(mstate m); +#endif /* DEBUG */ + +/* ---------------------------- Indexing Bins ---------------------------- */ + +#define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS) +#define small_index(s) ((s) >> SMALLBIN_SHIFT) +#define small_index2size(i) ((i) << SMALLBIN_SHIFT) +#define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE)) + +/* addressing by index. See above about smallbin repositioning */ +#define smallbin_at(M, i) ((sbinptr)((char*)&((M)->smallbins[(i)<<1]))) +#define treebin_at(M,i) (&((M)->treebins[i])) + +/* assign tree index for size S to variable I. Use x86 asm if possible */ +#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) +#define compute_tree_index(S, I)\ +{\ + unsigned int X = S >> TREEBIN_SHIFT;\ + if (X == 0)\ + I = 0;\ + else if (X > 0xFFFF)\ + I = NTREEBINS-1;\ + else {\ + unsigned int K;\ + __asm__("bsrl\t%1, %0\n\t" : "=r" (K) : "rm" (X));\ + I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ + }\ +} + +#elif defined (__INTEL_COMPILER) +#define compute_tree_index(S, I)\ +{\ + size_t X = S >> TREEBIN_SHIFT;\ + if (X == 0)\ + I = 0;\ + else if (X > 0xFFFF)\ + I = NTREEBINS-1;\ + else {\ + unsigned int K = _bit_scan_reverse (X); \ + I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ + }\ +} + +#elif defined(_MSC_VER) && _MSC_VER>=1300 +#define compute_tree_index(S, I)\ +{\ + size_t X = S >> TREEBIN_SHIFT;\ + if (X == 0)\ + I = 0;\ + else if (X > 0xFFFF)\ + I = NTREEBINS-1;\ + else {\ + unsigned int K;\ + _BitScanReverse((DWORD *) &K, X);\ + I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ + }\ +} + +#else /* GNUC */ +#define compute_tree_index(S, I)\ +{\ + size_t X = S >> TREEBIN_SHIFT;\ + if (X == 0)\ + I = 0;\ + else if (X > 0xFFFF)\ + I = NTREEBINS-1;\ + else {\ + unsigned int Y = (unsigned int)X;\ + unsigned int N = ((Y - 0x100) >> 16) & 8;\ + unsigned int K = (((Y <<= N) - 0x1000) >> 16) & 4;\ + N += K;\ + N += K = (((Y <<= K) - 0x4000) >> 16) & 2;\ + K = 14 - N + ((Y <<= K) >> 15);\ + I = (K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1));\ + }\ +} +#endif /* GNUC */ + +/* Bit representing maximum resolved size in a treebin at i */ +#define bit_for_tree_index(i) \ + (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) + +/* Shift placing maximum resolved bit in a treebin at i as sign bit */ +#define leftshift_for_tree_index(i) \ + ((i == NTREEBINS-1)? 0 : \ + ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) + +/* The size of the smallest chunk held in bin with index i */ +#define minsize_for_tree_index(i) \ + ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ + (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) + + +/* ------------------------ Operations on bin maps ----------------------- */ + +/* bit corresponding to given index */ +#define idx2bit(i) ((binmap_t)(1) << (i)) + +/* Mark/Clear bits with given index */ +#define mark_smallmap(M,i) ((M)->smallmap |= idx2bit(i)) +#define clear_smallmap(M,i) ((M)->smallmap &= ~idx2bit(i)) +#define smallmap_is_marked(M,i) ((M)->smallmap & idx2bit(i)) + +#define mark_treemap(M,i) ((M)->treemap |= idx2bit(i)) +#define clear_treemap(M,i) ((M)->treemap &= ~idx2bit(i)) +#define treemap_is_marked(M,i) ((M)->treemap & idx2bit(i)) + +/* isolate the least set bit of a bitmap */ +#define least_bit(x) ((x) & -(x)) + +/* mask with all bits to left of least bit of x on */ +#define left_bits(x) ((x<<1) | -(x<<1)) + +/* mask with all bits to left of or equal to least bit of x on */ +#define same_or_left_bits(x) ((x) | -(x)) + +/* index corresponding to given bit. Use x86 asm if possible */ + +#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) +#define compute_bit2idx(X, I)\ +{\ + unsigned int J;\ + __asm__("bsfl\t%1, %0\n\t" : "=r" (J) : "rm" (X));\ + I = (bindex_t)J;\ +} + +#elif defined (__INTEL_COMPILER) +#define compute_bit2idx(X, I)\ +{\ + unsigned int J;\ + J = _bit_scan_forward (X); \ + I = (bindex_t)J;\ +} + +#elif defined(_MSC_VER) && _MSC_VER>=1300 +#define compute_bit2idx(X, I)\ +{\ + unsigned int J;\ + _BitScanForward((DWORD *) &J, X);\ + I = (bindex_t)J;\ +} + +#elif USE_BUILTIN_FFS +#define compute_bit2idx(X, I) I = ffs(X)-1 + +#else +#define compute_bit2idx(X, I)\ +{\ + unsigned int Y = X - 1;\ + unsigned int K = Y >> (16-4) & 16;\ + unsigned int N = K; Y >>= K;\ + N += K = Y >> (8-3) & 8; Y >>= K;\ + N += K = Y >> (4-2) & 4; Y >>= K;\ + N += K = Y >> (2-1) & 2; Y >>= K;\ + N += K = Y >> (1-0) & 1; Y >>= K;\ + I = (bindex_t)(N + Y);\ +} +#endif /* GNUC */ + + +/* ----------------------- Runtime Check Support ------------------------- */ + +/* + For security, the main invariant is that malloc/free/etc never + writes to a static address other than malloc_state, unless static + malloc_state itself has been corrupted, which cannot occur via + malloc (because of these checks). In essence this means that we + believe all pointers, sizes, maps etc held in malloc_state, but + check all of those linked or offsetted from other embedded data + structures. These checks are interspersed with main code in a way + that tends to minimize their run-time cost. + + When FOOTERS is defined, in addition to range checking, we also + verify footer fields of inuse chunks, which can be used guarantee + that the mstate controlling malloc/free is intact. This is a + streamlined version of the approach described by William Robertson + et al in "Run-time Detection of Heap-based Overflows" LISA'03 + http://www.usenix.org/events/lisa03/tech/robertson.html The footer + of an inuse chunk holds the xor of its mstate and a random seed, + that is checked upon calls to free() and realloc(). This is + (probablistically) unguessable from outside the program, but can be + computed by any code successfully malloc'ing any chunk, so does not + itself provide protection against code that has already broken + security through some other means. Unlike Robertson et al, we + always dynamically check addresses of all offset chunks (previous, + next, etc). This turns out to be cheaper than relying on hashes. +*/ + +#if !INSECURE +/* Check if address a is at least as high as any from MORECORE or MMAP */ +#define ok_address(M, a) ((char*)(a) >= (M)->least_addr) +/* Check if address of next chunk n is higher than base chunk p */ +#define ok_next(p, n) ((char*)(p) < (char*)(n)) +/* Check if p has its cinuse bit on */ +#define ok_cinuse(p) cinuse(p) +/* Check if p has its pinuse bit on */ +#define ok_pinuse(p) pinuse(p) + +#else /* !INSECURE */ +#define ok_address(M, a) (1) +#define ok_next(b, n) (1) +#define ok_cinuse(p) (1) +#define ok_pinuse(p) (1) +#endif /* !INSECURE */ + +#if (FOOTERS && !INSECURE) +/* Check if (alleged) mstate m has expected magic field */ +#define ok_magic(M) ((M)->magic == mparams.magic) +#else /* (FOOTERS && !INSECURE) */ +#define ok_magic(M) (1) +#endif /* (FOOTERS && !INSECURE) */ + + +/* In gcc, use __builtin_expect to minimize impact of checks */ +#if !INSECURE +#if defined(__GNUC__) && __GNUC__ >= 3 +#define RTCHECK(e) __builtin_expect(e, 1) +#else /* GNUC */ +#define RTCHECK(e) (e) +#endif /* GNUC */ +#else /* !INSECURE */ +#define RTCHECK(e) (1) +#endif /* !INSECURE */ + +/* macros to set up inuse chunks with or without footers */ + +#if !FOOTERS + +#define mark_inuse_foot(M,p,s) + +/* Set cinuse bit and pinuse bit of next chunk */ +#define set_inuse(M,p,s)\ + ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ + ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) + +/* Set cinuse and pinuse of this chunk and pinuse of next chunk */ +#define set_inuse_and_pinuse(M,p,s)\ + ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ + ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) + +/* Set size, cinuse and pinuse bit of this chunk */ +#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ + ((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) + +#else /* FOOTERS */ + +/* Set foot of inuse chunk to be xor of mstate and seed */ +#define mark_inuse_foot(M,p,s)\ + (((mchunkptr)((char*)(p) + (s)))->prev_foot = ((size_t)(M) ^ mparams.magic)) + +#define get_mstate_for(p)\ + ((mstate)(((mchunkptr)((char*)(p) +\ + (chunksize(p))))->prev_foot ^ mparams.magic)) + +#define set_inuse(M,p,s)\ + ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ + (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT), \ + mark_inuse_foot(M,p,s)) + +#define set_inuse_and_pinuse(M,p,s)\ + ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ + (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT),\ + mark_inuse_foot(M,p,s)) + +#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ + ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ + mark_inuse_foot(M, p, s)) + +#endif /* !FOOTERS */ + +/* ---------------------------- setting mparams -------------------------- */ + +/* Initialize mparams */ +static int init_mparams(void) { +#ifdef NEED_GLOBAL_LOCK_INIT + if (malloc_global_mutex_status <= 0) + init_malloc_global_mutex(); +#endif + + ACQUIRE_MALLOC_GLOBAL_LOCK(); + if (mparams.magic == 0) { + size_t magic; + size_t psize; + size_t gsize; + +#ifndef WIN32 + psize = malloc_getpagesize; + gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize); +#else /* WIN32 */ + { + SYSTEM_INFO system_info; + GetSystemInfo(&system_info); + psize = system_info.dwPageSize; + gsize = ((DEFAULT_GRANULARITY != 0)? + DEFAULT_GRANULARITY : system_info.dwAllocationGranularity); + } +#endif /* WIN32 */ + + /* Sanity-check configuration: + size_t must be unsigned and as wide as pointer type. + ints must be at least 4 bytes. + alignment must be at least 8. + Alignment, min chunk size, and page size must all be powers of 2. + */ + if ((sizeof(size_t) != sizeof(char*)) || + (MAX_SIZE_T < MIN_CHUNK_SIZE) || + (sizeof(int) < 4) || + (MALLOC_ALIGNMENT < (size_t)8U) || + ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-SIZE_T_ONE)) != 0) || + ((MCHUNK_SIZE & (MCHUNK_SIZE-SIZE_T_ONE)) != 0) || + ((gsize & (gsize-SIZE_T_ONE)) != 0) || + ((psize & (psize-SIZE_T_ONE)) != 0)) + ABORT; + + mparams.granularity = gsize; + mparams.page_size = psize; + mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD; + mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD; +#if MORECORE_CONTIGUOUS + mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT; +#else /* MORECORE_CONTIGUOUS */ + mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT; +#endif /* MORECORE_CONTIGUOUS */ + +#if !ONLY_MSPACES + /* Set up lock for main malloc area */ + gm->mflags = mparams.default_mflags; + INITIAL_LOCK(&gm->mutex); +#endif + +#if (FOOTERS && !INSECURE) + { +#if USE_DEV_RANDOM + int fd; + unsigned char buf[sizeof(size_t)]; + /* Try to use /dev/urandom, else fall back on using time */ + if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 && + read(fd, buf, sizeof(buf)) == sizeof(buf)) { + magic = *((size_t *) buf); + close(fd); + } + else +#endif /* USE_DEV_RANDOM */ +#ifdef WIN32 + magic = (size_t)(GetTickCount() ^ (size_t)0x55555555U); +#else + magic = (size_t)(time(0) ^ (size_t)0x55555555U); +#endif + magic |= (size_t)8U; /* ensure nonzero */ + magic &= ~(size_t)7U; /* improve chances of fault for bad values */ + } +#else /* (FOOTERS && !INSECURE) */ + magic = (size_t)0x58585858U; +#endif /* (FOOTERS && !INSECURE) */ + + mparams.magic = magic; + } + + RELEASE_MALLOC_GLOBAL_LOCK(); + return 1; +} + +/* support for mallopt */ +static int change_mparam(int param_number, int value) { + size_t val = (value == -1)? MAX_SIZE_T : (size_t)value; + ensure_initialization(); + switch(param_number) { + case M_TRIM_THRESHOLD: + mparams.trim_threshold = val; + return 1; + case M_GRANULARITY: + if (val >= mparams.page_size && ((val & (val-1)) == 0)) { + mparams.granularity = val; + return 1; + } + else + return 0; + case M_MMAP_THRESHOLD: + mparams.mmap_threshold = val; + return 1; + default: + return 0; + } +} + +#if DEBUG +/* ------------------------- Debugging Support --------------------------- */ + +/* Check properties of any chunk, whether free, inuse, mmapped etc */ +static void do_check_any_chunk(mstate m, mchunkptr p) { + assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); + assert(ok_address(m, p)); +} + +/* Check properties of top chunk */ +static void do_check_top_chunk(mstate m, mchunkptr p) { + msegmentptr sp = segment_holding(m, (char*)p); + size_t sz = p->head & ~INUSE_BITS; /* third-lowest bit can be set! */ + assert(sp != 0); + assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); + assert(ok_address(m, p)); + assert(sz == m->topsize); + assert(sz > 0); + assert(sz == ((sp->base + sp->size) - (char*)p) - TOP_FOOT_SIZE); + assert(pinuse(p)); + assert(!pinuse(chunk_plus_offset(p, sz))); +} + +/* Check properties of (inuse) mmapped chunks */ +static void do_check_mmapped_chunk(mstate m, mchunkptr p) { + size_t sz = chunksize(p); + size_t len = (sz + (p->prev_foot & ~IS_MMAPPED_BIT) + MMAP_FOOT_PAD); + assert(is_mmapped(p)); + assert(use_mmap(m)); + assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); + assert(ok_address(m, p)); + assert(!is_small(sz)); + assert((len & (mparams.page_size-SIZE_T_ONE)) == 0); + assert(chunk_plus_offset(p, sz)->head == FENCEPOST_HEAD); + assert(chunk_plus_offset(p, sz+SIZE_T_SIZE)->head == 0); +} + +/* Check properties of inuse chunks */ +static void do_check_inuse_chunk(mstate m, mchunkptr p) { + do_check_any_chunk(m, p); + assert(cinuse(p)); + assert(next_pinuse(p)); + /* If not pinuse and not mmapped, previous chunk has OK offset */ + assert(is_mmapped(p) || pinuse(p) || next_chunk(prev_chunk(p)) == p); + if (is_mmapped(p)) + do_check_mmapped_chunk(m, p); +} + +/* Check properties of free chunks */ +static void do_check_free_chunk(mstate m, mchunkptr p) { + size_t sz = chunksize(p); + mchunkptr next = chunk_plus_offset(p, sz); + do_check_any_chunk(m, p); + assert(!cinuse(p)); + assert(!next_pinuse(p)); + assert (!is_mmapped(p)); + if (p != m->dv && p != m->top) { + if (sz >= MIN_CHUNK_SIZE) { + assert((sz & CHUNK_ALIGN_MASK) == 0); + assert(is_aligned(chunk2mem(p))); + assert(next->prev_foot == sz); + assert(pinuse(p)); + assert (next == m->top || cinuse(next)); + assert(p->fd->bk == p); + assert(p->bk->fd == p); + } + else /* markers are always of size SIZE_T_SIZE */ + assert(sz == SIZE_T_SIZE); + } +} + +/* Check properties of malloced chunks at the point they are malloced */ +static void do_check_malloced_chunk(mstate m, void* mem, size_t s) { + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + size_t sz = p->head & ~(PINUSE_BIT|CINUSE_BIT); + do_check_inuse_chunk(m, p); + assert((sz & CHUNK_ALIGN_MASK) == 0); + assert(sz >= MIN_CHUNK_SIZE); + assert(sz >= s); + /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */ + assert(is_mmapped(p) || sz < (s + MIN_CHUNK_SIZE)); + } +} + +/* Check a tree and its subtrees. */ +static void do_check_tree(mstate m, tchunkptr t) { + tchunkptr head = 0; + tchunkptr u = t; + bindex_t tindex = t->index; + size_t tsize = chunksize(t); + bindex_t idx; + compute_tree_index(tsize, idx); + assert(tindex == idx); + assert(tsize >= MIN_LARGE_SIZE); + assert(tsize >= minsize_for_tree_index(idx)); + assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1)))); + + do { /* traverse through chain of same-sized nodes */ + do_check_any_chunk(m, ((mchunkptr)u)); + assert(u->index == tindex); + assert(chunksize(u) == tsize); + assert(!cinuse(u)); + assert(!next_pinuse(u)); + assert(u->fd->bk == u); + assert(u->bk->fd == u); + if (u->parent == 0) { + assert(u->child[0] == 0); + assert(u->child[1] == 0); + } + else { + assert(head == 0); /* only one node on chain has parent */ + head = u; + assert(u->parent != u); + assert (u->parent->child[0] == u || + u->parent->child[1] == u || + *((tbinptr*)(u->parent)) == u); + if (u->child[0] != 0) { + assert(u->child[0]->parent == u); + assert(u->child[0] != u); + do_check_tree(m, u->child[0]); + } + if (u->child[1] != 0) { + assert(u->child[1]->parent == u); + assert(u->child[1] != u); + do_check_tree(m, u->child[1]); + } + if (u->child[0] != 0 && u->child[1] != 0) { + assert(chunksize(u->child[0]) < chunksize(u->child[1])); + } + } + u = u->fd; + } while (u != t); + assert(head != 0); +} + +/* Check all the chunks in a treebin. */ +static void do_check_treebin(mstate m, bindex_t i) { + tbinptr* tb = treebin_at(m, i); + tchunkptr t = *tb; + int empty = (m->treemap & (1U << i)) == 0; + if (t == 0) + assert(empty); + if (!empty) + do_check_tree(m, t); +} + +/* Check all the chunks in a smallbin. */ +static void do_check_smallbin(mstate m, bindex_t i) { + sbinptr b = smallbin_at(m, i); + mchunkptr p = b->bk; + unsigned int empty = (m->smallmap & (1U << i)) == 0; + if (p == b) + assert(empty); + if (!empty) { + for (; p != b; p = p->bk) { + size_t size = chunksize(p); + mchunkptr q; + /* each chunk claims to be free */ + do_check_free_chunk(m, p); + /* chunk belongs in bin */ + assert(small_index(size) == i); + assert(p->bk == b || chunksize(p->bk) == chunksize(p)); + /* chunk is followed by an inuse chunk */ + q = next_chunk(p); + if (q->head != FENCEPOST_HEAD) + do_check_inuse_chunk(m, q); + } + } +} + +/* Find x in a bin. Used in other check functions. */ +static int bin_find(mstate m, mchunkptr x) { + size_t size = chunksize(x); + if (is_small(size)) { + bindex_t sidx = small_index(size); + sbinptr b = smallbin_at(m, sidx); + if (smallmap_is_marked(m, sidx)) { + mchunkptr p = b; + do { + if (p == x) + return 1; + } while ((p = p->fd) != b); + } + } + else { + bindex_t tidx; + compute_tree_index(size, tidx); + if (treemap_is_marked(m, tidx)) { + tchunkptr t = *treebin_at(m, tidx); + size_t sizebits = size << leftshift_for_tree_index(tidx); + while (t != 0 && chunksize(t) != size) { + t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; + sizebits <<= 1; + } + if (t != 0) { + tchunkptr u = t; + do { + if (u == (tchunkptr)x) + return 1; + } while ((u = u->fd) != t); + } + } + } + return 0; +} + +/* Traverse each chunk and check it; return total */ +static size_t traverse_and_check(mstate m) { + size_t sum = 0; + if (is_initialized(m)) { + msegmentptr s = &m->seg; + sum += m->topsize + TOP_FOOT_SIZE; + while (s != 0) { + mchunkptr q = align_as_chunk(s->base); + mchunkptr lastq = 0; + assert(pinuse(q)); + while (segment_holds(s, q) && + q != m->top && q->head != FENCEPOST_HEAD) { + sum += chunksize(q); + if (cinuse(q)) { + assert(!bin_find(m, q)); + do_check_inuse_chunk(m, q); + } + else { + assert(q == m->dv || bin_find(m, q)); + assert(lastq == 0 || cinuse(lastq)); /* Not 2 consecutive free */ + do_check_free_chunk(m, q); + } + lastq = q; + q = next_chunk(q); + } + s = s->next; + } + } + return sum; +} + +/* Check all properties of malloc_state. */ +static void do_check_malloc_state(mstate m) { + bindex_t i; + size_t total; + /* check bins */ + for (i = 0; i < NSMALLBINS; ++i) + do_check_smallbin(m, i); + for (i = 0; i < NTREEBINS; ++i) + do_check_treebin(m, i); + + if (m->dvsize != 0) { /* check dv chunk */ + do_check_any_chunk(m, m->dv); + assert(m->dvsize == chunksize(m->dv)); + assert(m->dvsize >= MIN_CHUNK_SIZE); + assert(bin_find(m, m->dv) == 0); + } + + if (m->top != 0) { /* check top chunk */ + do_check_top_chunk(m, m->top); + /*assert(m->topsize == chunksize(m->top)); redundant */ + assert(m->topsize > 0); + assert(bin_find(m, m->top) == 0); + } + + total = traverse_and_check(m); + assert(total <= m->footprint); + assert(m->footprint <= m->max_footprint); +} +#endif /* DEBUG */ + +/* ----------------------------- statistics ------------------------------ */ + +#if !NO_MALLINFO +static struct mallinfo internal_mallinfo(mstate m) { + struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + ensure_initialization(); + if (!PREACTION(m)) { + check_malloc_state(m); + if (is_initialized(m)) { + size_t nfree = SIZE_T_ONE; /* top always free */ + size_t mfree = m->topsize + TOP_FOOT_SIZE; + size_t sum = mfree; + msegmentptr s = &m->seg; + while (s != 0) { + mchunkptr q = align_as_chunk(s->base); + while (segment_holds(s, q) && + q != m->top && q->head != FENCEPOST_HEAD) { + size_t sz = chunksize(q); + sum += sz; + if (!cinuse(q)) { + mfree += sz; + ++nfree; + } + q = next_chunk(q); + } + s = s->next; + } + + nm.arena = sum; + nm.ordblks = nfree; + nm.hblkhd = m->footprint - sum; + nm.usmblks = m->max_footprint; + nm.uordblks = m->footprint - mfree; + nm.fordblks = mfree; + nm.keepcost = m->topsize; + } + + POSTACTION(m); + } + return nm; +} +#endif /* !NO_MALLINFO */ + +static void internal_malloc_stats(mstate m) { + ensure_initialization(); + if (!PREACTION(m)) { + size_t maxfp = 0; + size_t fp = 0; + size_t used = 0; + check_malloc_state(m); + if (is_initialized(m)) { + msegmentptr s = &m->seg; + maxfp = m->max_footprint; + fp = m->footprint; + used = fp - (m->topsize + TOP_FOOT_SIZE); + + while (s != 0) { + mchunkptr q = align_as_chunk(s->base); + while (segment_holds(s, q) && + q != m->top && q->head != FENCEPOST_HEAD) { + if (!cinuse(q)) + used -= chunksize(q); + q = next_chunk(q); + } + s = s->next; + } + } + + fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)(maxfp)); + fprintf(stderr, "system bytes = %10lu\n", (unsigned long)(fp)); + fprintf(stderr, "in use bytes = %10lu\n", (unsigned long)(used)); + + POSTACTION(m); + } +} + +/* ----------------------- Operations on smallbins ----------------------- */ + +/* + Various forms of linking and unlinking are defined as macros. Even + the ones for trees, which are very long but have very short typical + paths. This is ugly but reduces reliance on inlining support of + compilers. +*/ + +/* Link a free chunk into a smallbin */ +#define insert_small_chunk(M, P, S) {\ + bindex_t I = small_index(S);\ + mchunkptr B = smallbin_at(M, I);\ + mchunkptr F = B;\ + assert(S >= MIN_CHUNK_SIZE);\ + if (!smallmap_is_marked(M, I))\ + mark_smallmap(M, I);\ + else if (RTCHECK(ok_address(M, B->fd)))\ + F = B->fd;\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + }\ + B->fd = P;\ + F->bk = P;\ + P->fd = F;\ + P->bk = B;\ +} + +/* Unlink a chunk from a smallbin */ +#define unlink_small_chunk(M, P, S) {\ + mchunkptr F = P->fd;\ + mchunkptr B = P->bk;\ + bindex_t I = small_index(S);\ + assert(P != B);\ + assert(P != F);\ + assert(chunksize(P) == small_index2size(I));\ + if (F == B)\ + clear_smallmap(M, I);\ + else if (RTCHECK((F == smallbin_at(M,I) || ok_address(M, F)) &&\ + (B == smallbin_at(M,I) || ok_address(M, B)))) {\ + F->bk = B;\ + B->fd = F;\ + }\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + }\ +} + +/* Unlink the first chunk from a smallbin */ +#define unlink_first_small_chunk(M, B, P, I) {\ + mchunkptr F = P->fd;\ + assert(P != B);\ + assert(P != F);\ + assert(chunksize(P) == small_index2size(I));\ + if (B == F)\ + clear_smallmap(M, I);\ + else if (RTCHECK(ok_address(M, F))) {\ + B->fd = F;\ + F->bk = B;\ + }\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + }\ +} + + + +/* Replace dv node, binning the old one */ +/* Used only when dvsize known to be small */ +#define replace_dv(M, P, S) {\ + size_t DVS = M->dvsize;\ + if (DVS != 0) {\ + mchunkptr DV = M->dv;\ + assert(is_small(DVS));\ + insert_small_chunk(M, DV, DVS);\ + }\ + M->dvsize = S;\ + M->dv = P;\ +} + +/* ------------------------- Operations on trees ------------------------- */ + +/* Insert chunk into tree */ +#define insert_large_chunk(M, X, S) {\ + tbinptr* H;\ + bindex_t I;\ + compute_tree_index(S, I);\ + H = treebin_at(M, I);\ + X->index = I;\ + X->child[0] = X->child[1] = 0;\ + if (!treemap_is_marked(M, I)) {\ + mark_treemap(M, I);\ + *H = X;\ + X->parent = (tchunkptr)H;\ + X->fd = X->bk = X;\ + }\ + else {\ + tchunkptr T = *H;\ + size_t K = S << leftshift_for_tree_index(I);\ + for (;;) {\ + if (chunksize(T) != S) {\ + tchunkptr* C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ + K <<= 1;\ + if (*C != 0)\ + T = *C;\ + else if (RTCHECK(ok_address(M, C))) {\ + *C = X;\ + X->parent = T;\ + X->fd = X->bk = X;\ + break;\ + }\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + break;\ + }\ + }\ + else {\ + tchunkptr F = T->fd;\ + if (RTCHECK(ok_address(M, T) && ok_address(M, F))) {\ + T->fd = F->bk = X;\ + X->fd = F;\ + X->bk = T;\ + X->parent = 0;\ + break;\ + }\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + break;\ + }\ + }\ + }\ + }\ +} + +/* + Unlink steps: + + 1. If x is a chained node, unlink it from its same-sized fd/bk links + and choose its bk node as its replacement. + 2. If x was the last node of its size, but not a leaf node, it must + be replaced with a leaf node (not merely one with an open left or + right), to make sure that lefts and rights of descendents + correspond properly to bit masks. We use the rightmost descendent + of x. We could use any other leaf, but this is easy to locate and + tends to counteract removal of leftmosts elsewhere, and so keeps + paths shorter than minimally guaranteed. This doesn't loop much + because on average a node in a tree is near the bottom. + 3. If x is the base of a chain (i.e., has parent links) relink + x's parent and children to x's replacement (or null if none). +*/ + +#define unlink_large_chunk(M, X) {\ + tchunkptr XP = X->parent;\ + tchunkptr R;\ + if (X->bk != X) {\ + tchunkptr F = X->fd;\ + R = X->bk;\ + if (RTCHECK(ok_address(M, F))) {\ + F->bk = R;\ + R->fd = F;\ + }\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + }\ + }\ + else {\ + tchunkptr* RP;\ + if (((R = *(RP = &(X->child[1]))) != 0) ||\ + ((R = *(RP = &(X->child[0]))) != 0)) {\ + tchunkptr* CP;\ + while ((*(CP = &(R->child[1])) != 0) ||\ + (*(CP = &(R->child[0])) != 0)) {\ + R = *(RP = CP);\ + }\ + if (RTCHECK(ok_address(M, RP)))\ + *RP = 0;\ + else {\ + CORRUPTION_ERROR_ACTION(M);\ + }\ + }\ + }\ + if (XP != 0) {\ + tbinptr* H = treebin_at(M, X->index);\ + if (X == *H) {\ + if ((*H = R) == 0) \ + clear_treemap(M, X->index);\ + }\ + else if (RTCHECK(ok_address(M, XP))) {\ + if (XP->child[0] == X) \ + XP->child[0] = R;\ + else \ + XP->child[1] = R;\ + }\ + else\ + CORRUPTION_ERROR_ACTION(M);\ + if (R != 0) {\ + if (RTCHECK(ok_address(M, R))) {\ + tchunkptr C0, C1;\ + R->parent = XP;\ + if ((C0 = X->child[0]) != 0) {\ + if (RTCHECK(ok_address(M, C0))) {\ + R->child[0] = C0;\ + C0->parent = R;\ + }\ + else\ + CORRUPTION_ERROR_ACTION(M);\ + }\ + if ((C1 = X->child[1]) != 0) {\ + if (RTCHECK(ok_address(M, C1))) {\ + R->child[1] = C1;\ + C1->parent = R;\ + }\ + else\ + CORRUPTION_ERROR_ACTION(M);\ + }\ + }\ + else\ + CORRUPTION_ERROR_ACTION(M);\ + }\ + }\ +} + +/* Relays to large vs small bin operations */ + +#define insert_chunk(M, P, S)\ + if (is_small(S)) insert_small_chunk(M, P, S)\ + else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } + +#define unlink_chunk(M, P, S)\ + if (is_small(S)) unlink_small_chunk(M, P, S)\ + else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } + + +/* Relays to internal calls to malloc/free from realloc, memalign etc */ + +#if ONLY_MSPACES +#define internal_malloc(m, b) mspace_malloc(m, b) +#define internal_free(m, mem) mspace_free(m,mem); +#else /* ONLY_MSPACES */ +#if MSPACES +#define internal_malloc(m, b)\ + (m == gm)? dlmalloc(b) : mspace_malloc(m, b) +#define internal_free(m, mem)\ + if (m == gm) dlfree(mem); else mspace_free(m,mem); +#else /* MSPACES */ +#define internal_malloc(m, b) dlmalloc(b) +#define internal_free(m, mem) dlfree(mem) +#endif /* MSPACES */ +#endif /* ONLY_MSPACES */ + +/* ----------------------- Direct-mmapping chunks ----------------------- */ + +/* + Directly mmapped chunks are set up with an offset to the start of + the mmapped region stored in the prev_foot field of the chunk. This + allows reconstruction of the required argument to MUNMAP when freed, + and also allows adjustment of the returned chunk to meet alignment + requirements (especially in memalign). There is also enough space + allocated to hold a fake next chunk of size SIZE_T_SIZE to maintain + the PINUSE bit so frees can be checked. +*/ + +/* Malloc using mmap */ +static void* mmap_alloc(mstate m, size_t nb) { + size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + if (mmsize > nb) { /* Check for wrap around 0 */ + char* mm = (char*)(CALL_DIRECT_MMAP(mmsize)); + if (mm != CMFAIL) { + size_t offset = align_offset(chunk2mem(mm)); + size_t psize = mmsize - offset - MMAP_FOOT_PAD; + mchunkptr p = (mchunkptr)(mm + offset); + p->prev_foot = offset | IS_MMAPPED_BIT; + (p)->head = (psize|CINUSE_BIT); + mark_inuse_foot(m, p, psize); + chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; + chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; + + if (mm < m->least_addr) + m->least_addr = mm; + if ((m->footprint += mmsize) > m->max_footprint) + m->max_footprint = m->footprint; + assert(is_aligned(chunk2mem(p))); + check_mmapped_chunk(m, p); + return chunk2mem(p); + } + } + return 0; +} + +/* Realloc using mmap */ +static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb) { + size_t oldsize = chunksize(oldp); + if (is_small(nb)) /* Can't shrink mmap regions below small size */ + return 0; + /* Keep old chunk if big enough but not too big */ + if (oldsize >= nb + SIZE_T_SIZE && + (oldsize - nb) <= (mparams.granularity << 1)) + return oldp; + else { + size_t offset = oldp->prev_foot & ~IS_MMAPPED_BIT; + size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD; + size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + char* cp = (char*)CALL_MREMAP((char*)oldp - offset, + oldmmsize, newmmsize, 1); + if (cp != CMFAIL) { + mchunkptr newp = (mchunkptr)(cp + offset); + size_t psize = newmmsize - offset - MMAP_FOOT_PAD; + newp->head = (psize|CINUSE_BIT); + mark_inuse_foot(m, newp, psize); + chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; + chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; + + if (cp < m->least_addr) + m->least_addr = cp; + if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint) + m->max_footprint = m->footprint; + check_mmapped_chunk(m, newp); + return newp; + } + } + return 0; +} + +/* -------------------------- mspace management -------------------------- */ + +/* Initialize top chunk and its size */ +static void init_top(mstate m, mchunkptr p, size_t psize) { + /* Ensure alignment */ + size_t offset = align_offset(chunk2mem(p)); + p = (mchunkptr)((char*)p + offset); + psize -= offset; + + m->top = p; + m->topsize = psize; + p->head = psize | PINUSE_BIT; + /* set size of fake trailing chunk holding overhead space only once */ + chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; + m->trim_check = mparams.trim_threshold; /* reset on each update */ +} + +/* Initialize bins for a new mstate that is otherwise zeroed out */ +static void init_bins(mstate m) { + /* Establish circular links for smallbins */ + bindex_t i; + for (i = 0; i < NSMALLBINS; ++i) { + sbinptr bin = smallbin_at(m,i); + bin->fd = bin->bk = bin; + } +} + +#if PROCEED_ON_ERROR + +/* default corruption action */ +static void reset_on_error(mstate m) { + int i; + ++malloc_corruption_error_count; + /* Reinitialize fields to forget about all memory */ + m->smallbins = m->treebins = 0; + m->dvsize = m->topsize = 0; + m->seg.base = 0; + m->seg.size = 0; + m->seg.next = 0; + m->top = m->dv = 0; + for (i = 0; i < NTREEBINS; ++i) + *treebin_at(m, i) = 0; + init_bins(m); +} +#endif /* PROCEED_ON_ERROR */ + +/* Allocate chunk and prepend remainder with chunk in successor base. */ +static void* prepend_alloc(mstate m, char* newbase, char* oldbase, + size_t nb) { + mchunkptr p = align_as_chunk(newbase); + mchunkptr oldfirst = align_as_chunk(oldbase); + size_t psize = (char*)oldfirst - (char*)p; + mchunkptr q = chunk_plus_offset(p, nb); + size_t qsize = psize - nb; + set_size_and_pinuse_of_inuse_chunk(m, p, nb); + + assert((char*)oldfirst > (char*)q); + assert(pinuse(oldfirst)); + assert(qsize >= MIN_CHUNK_SIZE); + + /* consolidate remainder with first chunk of old base */ + if (oldfirst == m->top) { + size_t tsize = m->topsize += qsize; + m->top = q; + q->head = tsize | PINUSE_BIT; + check_top_chunk(m, q); + } + else if (oldfirst == m->dv) { + size_t dsize = m->dvsize += qsize; + m->dv = q; + set_size_and_pinuse_of_free_chunk(q, dsize); + } + else { + if (!cinuse(oldfirst)) { + size_t nsize = chunksize(oldfirst); + unlink_chunk(m, oldfirst, nsize); + oldfirst = chunk_plus_offset(oldfirst, nsize); + qsize += nsize; + } + set_free_with_pinuse(q, qsize, oldfirst); + insert_chunk(m, q, qsize); + check_free_chunk(m, q); + } + + check_malloced_chunk(m, chunk2mem(p), nb); + return chunk2mem(p); +} + +/* Add a segment to hold a new noncontiguous region */ +static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { + /* Determine locations and sizes of segment, fenceposts, old top */ + char* old_top = (char*)m->top; + msegmentptr oldsp = segment_holding(m, old_top); + char* old_end = oldsp->base + oldsp->size; + size_t ssize = pad_request(sizeof(struct malloc_segment)); + char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + size_t offset = align_offset(chunk2mem(rawsp)); + char* asp = rawsp + offset; + char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp; + mchunkptr sp = (mchunkptr)csp; + msegmentptr ss = (msegmentptr)(chunk2mem(sp)); + mchunkptr tnext = chunk_plus_offset(sp, ssize); + mchunkptr p = tnext; + int nfences = 0; + + /* reset top to new space */ + init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); + + /* Set up segment record */ + assert(is_aligned(ss)); + set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); + *ss = m->seg; /* Push current record */ + m->seg.base = tbase; + m->seg.size = tsize; + m->seg.sflags = mmapped; + m->seg.next = ss; + + /* Insert trailing fenceposts */ + for (;;) { + mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); + p->head = FENCEPOST_HEAD; + ++nfences; + if ((char*)(&(nextp->head)) < old_end) + p = nextp; + else + break; + } + assert(nfences >= 2); + + /* Insert the rest of old top into a bin as an ordinary free chunk */ + if (csp != old_top) { + mchunkptr q = (mchunkptr)old_top; + size_t psize = csp - old_top; + mchunkptr tn = chunk_plus_offset(q, psize); + set_free_with_pinuse(q, psize, tn); + insert_chunk(m, q, psize); + } + + check_top_chunk(m, m->top); +} + +/* -------------------------- System allocation -------------------------- */ + +/* Get memory from system using MORECORE or MMAP */ +static void* sys_alloc(mstate m, size_t nb) { + char* tbase = CMFAIL; + size_t tsize = 0; + flag_t mmap_flag = 0; + + ensure_initialization(); + + /* Directly map large chunks */ + if (use_mmap(m) && nb >= mparams.mmap_threshold) { + void* mem = mmap_alloc(m, nb); + if (mem != 0) + return mem; + } + + /* + Try getting memory in any of three ways (in most-preferred to + least-preferred order): + 1. A call to MORECORE that can normally contiguously extend memory. + (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or + or main space is mmapped or a previous contiguous call failed) + 2. A call to MMAP new space (disabled if not HAVE_MMAP). + Note that under the default settings, if MORECORE is unable to + fulfill a request, and HAVE_MMAP is true, then mmap is + used as a noncontiguous system allocator. This is a useful backup + strategy for systems with holes in address spaces -- in this case + sbrk cannot contiguously expand the heap, but mmap may be able to + find space. + 3. A call to MORECORE that cannot usually contiguously extend memory. + (disabled if not HAVE_MORECORE) + + In all cases, we need to request enough bytes from system to ensure + we can malloc nb bytes upon success, so pad with enough space for + top_foot, plus alignment-pad to make sure we don't lose bytes if + not on boundary, and round this up to a granularity unit. + */ + + if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { + char* br = CMFAIL; + msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top); + size_t asize = 0; + ACQUIRE_MALLOC_GLOBAL_LOCK(); + + if (ss == 0) { /* First time through or recovery */ + char* base = (char*)CALL_MORECORE(0); + if (base != CMFAIL) { + asize = granularity_align(nb + SYS_ALLOC_PADDING); + /* Adjust to end on a page boundary */ + if (!is_page_aligned(base)) + asize += (page_align((size_t)base) - (size_t)base); + /* Can't call MORECORE if size is negative when treated as signed */ + if (asize < HALF_MAX_SIZE_T && + (br = (char*)(CALL_MORECORE(asize))) == base) { + tbase = base; + tsize = asize; + } + } + } + else { + /* Subtract out existing available top space from MORECORE request. */ + asize = granularity_align(nb - m->topsize + SYS_ALLOC_PADDING); + /* Use mem here only if it did continuously extend old space */ + if (asize < HALF_MAX_SIZE_T && + (br = (char*)(CALL_MORECORE(asize))) == ss->base+ss->size) { + tbase = br; + tsize = asize; + } + } + + if (tbase == CMFAIL) { /* Cope with partial failure */ + if (br != CMFAIL) { /* Try to use/extend the space we did get */ + if (asize < HALF_MAX_SIZE_T && + asize < nb + SYS_ALLOC_PADDING) { + size_t esize = granularity_align(nb + SYS_ALLOC_PADDING - asize); + if (esize < HALF_MAX_SIZE_T) { + char* end = (char*)CALL_MORECORE(esize); + if (end != CMFAIL) + asize += esize; + else { /* Can't use; try to release */ + (void) CALL_MORECORE(-asize); + br = CMFAIL; + } + } + } + } + if (br != CMFAIL) { /* Use the space we did get */ + tbase = br; + tsize = asize; + } + else + disable_contiguous(m); /* Don't try contiguous path in the future */ + } + + RELEASE_MALLOC_GLOBAL_LOCK(); + } + + if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ + size_t rsize = granularity_align(nb + SYS_ALLOC_PADDING); + if (rsize > nb) { /* Fail if wraps around zero */ + char* mp = (char*)(CALL_MMAP(rsize)); + if (mp != CMFAIL) { + tbase = mp; + tsize = rsize; + mmap_flag = IS_MMAPPED_BIT; + } + } + } + + if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ + size_t asize = granularity_align(nb + SYS_ALLOC_PADDING); + if (asize < HALF_MAX_SIZE_T) { + char* br = CMFAIL; + char* end = CMFAIL; + ACQUIRE_MALLOC_GLOBAL_LOCK(); + br = (char*)(CALL_MORECORE(asize)); + end = (char*)(CALL_MORECORE(0)); + RELEASE_MALLOC_GLOBAL_LOCK(); + if (br != CMFAIL && end != CMFAIL && br < end) { + size_t ssize = end - br; + if (ssize > nb + TOP_FOOT_SIZE) { + tbase = br; + tsize = ssize; + } + } + } + } + + if (tbase != CMFAIL) { + + if ((m->footprint += tsize) > m->max_footprint) + m->max_footprint = m->footprint; + + if (!is_initialized(m)) { /* first-time initialization */ + m->seg.base = m->least_addr = tbase; + m->seg.size = tsize; + m->seg.sflags = mmap_flag; + m->magic = mparams.magic; + m->release_checks = MAX_RELEASE_CHECK_RATE; + init_bins(m); +#if !ONLY_MSPACES + if (is_global(m)) + init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); + else +#endif + { + /* Offset top by embedded malloc_state */ + mchunkptr mn = next_chunk(mem2chunk(m)); + init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE); + } + } + + else { + /* Try to merge with an existing segment */ + msegmentptr sp = &m->seg; + /* Only consider most recent segment if traversal suppressed */ + while (sp != 0 && tbase != sp->base + sp->size) + sp = (NO_SEGMENT_TRAVERSAL) ? 0 : sp->next; + if (sp != 0 && + !is_extern_segment(sp) && + (sp->sflags & IS_MMAPPED_BIT) == mmap_flag && + segment_holds(sp, m->top)) { /* append */ + sp->size += tsize; + init_top(m, m->top, m->topsize + tsize); + } + else { + if (tbase < m->least_addr) + m->least_addr = tbase; + sp = &m->seg; + while (sp != 0 && sp->base != tbase + tsize) + sp = (NO_SEGMENT_TRAVERSAL) ? 0 : sp->next; + if (sp != 0 && + !is_extern_segment(sp) && + (sp->sflags & IS_MMAPPED_BIT) == mmap_flag) { + char* oldbase = sp->base; + sp->base = tbase; + sp->size += tsize; + return prepend_alloc(m, tbase, oldbase, nb); + } + else + add_segment(m, tbase, tsize, mmap_flag); + } + } + + if (nb < m->topsize) { /* Allocate from new or extended top space */ + size_t rsize = m->topsize -= nb; + mchunkptr p = m->top; + mchunkptr r = m->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(m, p, nb); + check_top_chunk(m, m->top); + check_malloced_chunk(m, chunk2mem(p), nb); + return chunk2mem(p); + } + } + + MALLOC_FAILURE_ACTION; + return 0; +} + +/* ----------------------- system deallocation -------------------------- */ + +/* Unmap and unlink any mmapped segments that don't contain used chunks */ +static size_t release_unused_segments(mstate m) { + size_t released = 0; + int nsegs = 0; + msegmentptr pred = &m->seg; + msegmentptr sp = pred->next; + while (sp != 0) { + char* base = sp->base; + size_t size = sp->size; + msegmentptr next = sp->next; + ++nsegs; + if (is_mmapped_segment(sp) && !is_extern_segment(sp)) { + mchunkptr p = align_as_chunk(base); + size_t psize = chunksize(p); + /* Can unmap if first chunk holds entire segment and not pinned */ + if (!cinuse(p) && (char*)p + psize >= base + size - TOP_FOOT_SIZE) { + tchunkptr tp = (tchunkptr)p; + assert(segment_holds(sp, (char*)sp)); + if (p == m->dv) { + m->dv = 0; + m->dvsize = 0; + } + else { + unlink_large_chunk(m, tp); + } + if (CALL_MUNMAP(base, size) == 0) { + released += size; + m->footprint -= size; + /* unlink obsoleted record */ + sp = pred; + sp->next = next; + } + else { /* back out if cannot unmap */ + insert_large_chunk(m, tp, psize); + } + } + } + if (NO_SEGMENT_TRAVERSAL) /* scan only first segment */ + break; + pred = sp; + sp = next; + } + /* Reset check counter */ + m->release_checks = ((nsegs > MAX_RELEASE_CHECK_RATE)? + nsegs : MAX_RELEASE_CHECK_RATE); + return released; +} + +static int sys_trim(mstate m, size_t pad) { + size_t released = 0; + ensure_initialization(); + if (pad < MAX_REQUEST && is_initialized(m)) { + pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ + + if (m->topsize > pad) { + /* Shrink top space in granularity-size units, keeping at least one */ + size_t unit = mparams.granularity; + size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - + SIZE_T_ONE) * unit; + msegmentptr sp = segment_holding(m, (char*)m->top); + + if (!is_extern_segment(sp)) { + if (is_mmapped_segment(sp)) { + if (HAVE_MMAP && + sp->size >= extra && + !has_segment_link(m, sp)) { /* can't shrink if pinned */ + size_t newsize = sp->size - extra; + /* Prefer mremap, fall back to munmap */ + if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) || + (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { + released = extra; + } + } + } + else if (HAVE_MORECORE) { + if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */ + extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit; + ACQUIRE_MALLOC_GLOBAL_LOCK(); + { + /* Make sure end of memory is where we last set it. */ + char* old_br = (char*)(CALL_MORECORE(0)); + if (old_br == sp->base + sp->size) { + char* rel_br = (char*)(CALL_MORECORE(-extra)); + char* new_br = (char*)(CALL_MORECORE(0)); + if (rel_br != CMFAIL && new_br < old_br) + released = old_br - new_br; + } + } + RELEASE_MALLOC_GLOBAL_LOCK(); + } + } + + if (released != 0) { + sp->size -= released; + m->footprint -= released; + init_top(m, m->top, m->topsize - released); + check_top_chunk(m, m->top); + } + } + + /* Unmap any unused mmapped segments */ + if (HAVE_MMAP) + released += release_unused_segments(m); + + /* On failure, disable autotrim to avoid repeated failed future calls */ + if (released == 0 && m->topsize > m->trim_check) + m->trim_check = MAX_SIZE_T; + } + + return (released != 0)? 1 : 0; +} + + +/* ---------------------------- malloc support --------------------------- */ + +/* allocate a large request from the best fitting chunk in a treebin */ +static void* tmalloc_large(mstate m, size_t nb) { + tchunkptr v = 0; + size_t rsize = -nb; /* Unsigned negation */ + tchunkptr t; + bindex_t idx; + compute_tree_index(nb, idx); + if ((t = *treebin_at(m, idx)) != 0) { + /* Traverse tree for this bin looking for node with size == nb */ + size_t sizebits = nb << leftshift_for_tree_index(idx); + tchunkptr rst = 0; /* The deepest untaken right subtree */ + for (;;) { + tchunkptr rt; + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + v = t; + if ((rsize = trem) == 0) + break; + } + rt = t->child[1]; + t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; + if (rt != 0 && rt != t) + rst = rt; + if (t == 0) { + t = rst; /* set t to least subtree holding sizes > nb */ + break; + } + sizebits <<= 1; + } + } + if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ + binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; + if (leftbits != 0) { + bindex_t i; + binmap_t leastbit = least_bit(leftbits); + compute_bit2idx(leastbit, i); + t = *treebin_at(m, i); + } + } + + while (t != 0) { /* find smallest of tree or subtree */ + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + rsize = trem; + v = t; + } + t = leftmost_child(t); + } + + /* If dv is a better fit, return 0 so malloc will use it */ + if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { + if (RTCHECK(ok_address(m, v))) { /* split */ + mchunkptr r = chunk_plus_offset(v, nb); + assert(chunksize(v) == rsize + nb); + if (RTCHECK(ok_next(v, r))) { + unlink_large_chunk(m, v); + if (rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(m, v, (rsize + nb)); + else { + set_size_and_pinuse_of_inuse_chunk(m, v, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + insert_chunk(m, r, rsize); + } + return chunk2mem(v); + } + } + CORRUPTION_ERROR_ACTION(m); + } + return 0; +} + +/* allocate a small request from the best fitting chunk in a treebin */ +static void* tmalloc_small(mstate m, size_t nb) { + tchunkptr t, v; + size_t rsize; + bindex_t i; + binmap_t leastbit = least_bit(m->treemap); + compute_bit2idx(leastbit, i); + v = t = *treebin_at(m, i); + rsize = chunksize(t) - nb; + + while ((t = leftmost_child(t)) != 0) { + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + rsize = trem; + v = t; + } + } + + if (RTCHECK(ok_address(m, v))) { + mchunkptr r = chunk_plus_offset(v, nb); + assert(chunksize(v) == rsize + nb); + if (RTCHECK(ok_next(v, r))) { + unlink_large_chunk(m, v); + if (rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(m, v, (rsize + nb)); + else { + set_size_and_pinuse_of_inuse_chunk(m, v, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(m, r, rsize); + } + return chunk2mem(v); + } + } + + CORRUPTION_ERROR_ACTION(m); + return 0; +} + +/* --------------------------- realloc support --------------------------- */ + +static void* internal_realloc(mstate m, void* oldmem, size_t bytes) { + if (bytes >= MAX_REQUEST) { + MALLOC_FAILURE_ACTION; + return 0; + } + if (!PREACTION(m)) { + mchunkptr oldp = mem2chunk(oldmem); + size_t oldsize = chunksize(oldp); + mchunkptr next = chunk_plus_offset(oldp, oldsize); + mchunkptr newp = 0; + void* extra = 0; + + /* Try to either shrink or extend into top. Else malloc-copy-free */ + + if (RTCHECK(ok_address(m, oldp) && ok_cinuse(oldp) && + ok_next(oldp, next) && ok_pinuse(next))) { + size_t nb = request2size(bytes); + if (is_mmapped(oldp)) + newp = mmap_resize(m, oldp, nb); + else if (oldsize >= nb) { /* already big enough */ + size_t rsize = oldsize - nb; + newp = oldp; + if (rsize >= MIN_CHUNK_SIZE) { + mchunkptr remainder = chunk_plus_offset(newp, nb); + set_inuse(m, newp, nb); + set_inuse(m, remainder, rsize); + extra = chunk2mem(remainder); + } + } + else if (next == m->top && oldsize + m->topsize > nb) { + /* Expand into top */ + size_t newsize = oldsize + m->topsize; + size_t newtopsize = newsize - nb; + mchunkptr newtop = chunk_plus_offset(oldp, nb); + set_inuse(m, oldp, nb); + newtop->head = newtopsize |PINUSE_BIT; + m->top = newtop; + m->topsize = newtopsize; + newp = oldp; + } + } + else { + USAGE_ERROR_ACTION(m, oldmem); + POSTACTION(m); + return 0; + } + + POSTACTION(m); + + if (newp != 0) { + if (extra != 0) { + internal_free(m, extra); + } + check_inuse_chunk(m, newp); + return chunk2mem(newp); + } + else { + void* newmem = internal_malloc(m, bytes); + if (newmem != 0) { + size_t oc = oldsize - overhead_for(oldp); + memcpy(newmem, oldmem, (oc < bytes)? oc : bytes); + internal_free(m, oldmem); + } + return newmem; + } + } + return 0; +} + +/* --------------------------- memalign support -------------------------- */ + +static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { + if (alignment <= MALLOC_ALIGNMENT) /* Can just use malloc */ + return internal_malloc(m, bytes); + if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ + alignment = MIN_CHUNK_SIZE; + if ((alignment & (alignment-SIZE_T_ONE)) != 0) {/* Ensure a power of 2 */ + size_t a = MALLOC_ALIGNMENT << 1; + while (a < alignment) a <<= 1; + alignment = a; + } + + if (bytes >= MAX_REQUEST - alignment) { + if (m != 0) { /* Test isn't needed but avoids compiler warning */ + MALLOC_FAILURE_ACTION; + } + } + else { + size_t nb = request2size(bytes); + size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD; + char* mem = (char*)internal_malloc(m, req); + if (mem != 0) { + void* leader = 0; + void* trailer = 0; + mchunkptr p = mem2chunk(mem); + + if (PREACTION(m)) return 0; + if ((((size_t)(mem)) % alignment) != 0) { /* misaligned */ + /* + Find an aligned spot inside chunk. Since we need to give + back leading space in a chunk of at least MIN_CHUNK_SIZE, if + the first calculation places us at a spot with less than + MIN_CHUNK_SIZE leader, we can move to the next aligned spot. + We've allocated enough total room so that this is always + possible. + */ + char* br = (char*)mem2chunk((size_t)(((size_t)(mem + + alignment - + SIZE_T_ONE)) & + -alignment)); + char* pos = ((size_t)(br - (char*)(p)) >= MIN_CHUNK_SIZE)? + br : br+alignment; + mchunkptr newp = (mchunkptr)pos; + size_t leadsize = pos - (char*)(p); + size_t newsize = chunksize(p) - leadsize; + + if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */ + newp->prev_foot = p->prev_foot + leadsize; + newp->head = (newsize|CINUSE_BIT); + } + else { /* Otherwise, give back leader, use the rest */ + set_inuse(m, newp, newsize); + set_inuse(m, p, leadsize); + leader = chunk2mem(p); + } + p = newp; + } + + /* Give back spare room at the end */ + if (!is_mmapped(p)) { + size_t size = chunksize(p); + if (size > nb + MIN_CHUNK_SIZE) { + size_t remainder_size = size - nb; + mchunkptr remainder = chunk_plus_offset(p, nb); + set_inuse(m, p, nb); + set_inuse(m, remainder, remainder_size); + trailer = chunk2mem(remainder); + } + } + + assert (chunksize(p) >= nb); + assert((((size_t)(chunk2mem(p))) % alignment) == 0); + check_inuse_chunk(m, p); + POSTACTION(m); + if (leader != 0) { + internal_free(m, leader); + } + if (trailer != 0) { + internal_free(m, trailer); + } + return chunk2mem(p); + } + } + return 0; +} + +/* ------------------------ comalloc/coalloc support --------------------- */ + +static void** ialloc(mstate m, + size_t n_elements, + size_t* sizes, + int opts, + void* chunks[]) { + /* + This provides common support for independent_X routines, handling + all of the combinations that can result. + + The opts arg has: + bit 0 set if all elements are same size (using sizes[0]) + bit 1 set if elements should be zeroed + */ + + size_t element_size; /* chunksize of each element, if all same */ + size_t contents_size; /* total size of elements */ + size_t array_size; /* request size of pointer array */ + void* mem; /* malloced aggregate space */ + mchunkptr p; /* corresponding chunk */ + size_t remainder_size; /* remaining bytes while splitting */ + void** marray; /* either "chunks" or malloced ptr array */ + mchunkptr array_chunk; /* chunk for malloced ptr array */ + flag_t was_enabled; /* to disable mmap */ + size_t size; + size_t i; + + ensure_initialization(); + /* compute array length, if needed */ + if (chunks != 0) { + if (n_elements == 0) + return chunks; /* nothing to do */ + marray = chunks; + array_size = 0; + } + else { + /* if empty req, must still return chunk representing empty array */ + if (n_elements == 0) + return (void**)internal_malloc(m, 0); + marray = 0; + array_size = request2size(n_elements * (sizeof(void*))); + } + + /* compute total element size */ + if (opts & 0x1) { /* all-same-size */ + element_size = request2size(*sizes); + contents_size = n_elements * element_size; + } + else { /* add up all the sizes */ + element_size = 0; + contents_size = 0; + for (i = 0; i != n_elements; ++i) + contents_size += request2size(sizes[i]); + } + + size = contents_size + array_size; + + /* + Allocate the aggregate chunk. First disable direct-mmapping so + malloc won't use it, since we would not be able to later + free/realloc space internal to a segregated mmap region. + */ + was_enabled = use_mmap(m); + disable_mmap(m); + mem = internal_malloc(m, size - CHUNK_OVERHEAD); + if (was_enabled) + enable_mmap(m); + if (mem == 0) + return 0; + + if (PREACTION(m)) return 0; + p = mem2chunk(mem); + remainder_size = chunksize(p); + + assert(!is_mmapped(p)); + + if (opts & 0x2) { /* optionally clear the elements */ + memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size); + } + + /* If not provided, allocate the pointer array as final part of chunk */ + if (marray == 0) { + size_t array_chunk_size; + array_chunk = chunk_plus_offset(p, contents_size); + array_chunk_size = remainder_size - contents_size; + marray = (void**) (chunk2mem(array_chunk)); + set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size); + remainder_size = contents_size; + } + + /* split out elements */ + for (i = 0; ; ++i) { + marray[i] = chunk2mem(p); + if (i != n_elements-1) { + if (element_size != 0) + size = element_size; + else + size = request2size(sizes[i]); + remainder_size -= size; + set_size_and_pinuse_of_inuse_chunk(m, p, size); + p = chunk_plus_offset(p, size); + } + else { /* the final element absorbs any overallocation slop */ + set_size_and_pinuse_of_inuse_chunk(m, p, remainder_size); + break; + } + } + +#if DEBUG + if (marray != chunks) { + /* final element must have exactly exhausted chunk */ + if (element_size != 0) { + assert(remainder_size == element_size); + } + else { + assert(remainder_size == request2size(sizes[i])); + } + check_inuse_chunk(m, mem2chunk(marray)); + } + for (i = 0; i != n_elements; ++i) + check_inuse_chunk(m, mem2chunk(marray[i])); + +#endif /* DEBUG */ + + POSTACTION(m); + return marray; +} + + +/* -------------------------- public routines ---------------------------- */ + +#if !ONLY_MSPACES + +void* dlmalloc(size_t bytes) { + /* + Basic algorithm: + If a small request (< 256 bytes minus per-chunk overhead): + 1. If one exists, use a remainderless chunk in associated smallbin. + (Remainderless means that there are too few excess bytes to + represent as a chunk.) + 2. If it is big enough, use the dv chunk, which is normally the + chunk adjacent to the one used for the most recent small request. + 3. If one exists, split the smallest available chunk in a bin, + saving remainder in dv. + 4. If it is big enough, use the top chunk. + 5. If available, get memory from system and use it + Otherwise, for a large request: + 1. Find the smallest available binned chunk that fits, and use it + if it is better fitting than dv chunk, splitting if necessary. + 2. If better fitting than any binned chunk, use the dv chunk. + 3. If it is big enough, use the top chunk. + 4. If request size >= mmap threshold, try to directly mmap this chunk. + 5. If available, get memory from system and use it + + The ugly goto's here ensure that postaction occurs along all paths. + */ + +#if USE_LOCKS + ensure_initialization(); /* initialize in sys_alloc if not using locks */ +#endif + + if (!PREACTION(gm)) { + void* mem; + size_t nb; + if (bytes <= MAX_SMALL_REQUEST) { + bindex_t idx; + binmap_t smallbits; + nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); + idx = small_index(nb); + smallbits = gm->smallmap >> idx; + + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ + mchunkptr b, p; + idx += ~smallbits & 1; /* Uses next bin if idx empty */ + b = smallbin_at(gm, idx); + p = b->fd; + assert(chunksize(p) == small_index2size(idx)); + unlink_first_small_chunk(gm, b, p, idx); + set_inuse_and_pinuse(gm, p, small_index2size(idx)); + mem = chunk2mem(p); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + else if (nb > gm->dvsize) { + if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ + mchunkptr b, p, r; + size_t rsize; + bindex_t i; + binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); + binmap_t leastbit = least_bit(leftbits); + compute_bit2idx(leastbit, i); + b = smallbin_at(gm, i); + p = b->fd; + assert(chunksize(p) == small_index2size(i)); + unlink_first_small_chunk(gm, b, p, i); + rsize = small_index2size(i) - nb; + /* Fit here cannot be remainderless if 4byte sizes */ + if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(gm, p, small_index2size(i)); + else { + set_size_and_pinuse_of_inuse_chunk(gm, p, nb); + r = chunk_plus_offset(p, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(gm, r, rsize); + } + mem = chunk2mem(p); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) { + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + } + } + else if (bytes >= MAX_REQUEST) + nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ + else { + nb = pad_request(bytes); + if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) { + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + } + + if (nb <= gm->dvsize) { + size_t rsize = gm->dvsize - nb; + mchunkptr p = gm->dv; + if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ + mchunkptr r = gm->dv = chunk_plus_offset(p, nb); + gm->dvsize = rsize; + set_size_and_pinuse_of_free_chunk(r, rsize); + set_size_and_pinuse_of_inuse_chunk(gm, p, nb); + } + else { /* exhaust dv */ + size_t dvs = gm->dvsize; + gm->dvsize = 0; + gm->dv = 0; + set_inuse_and_pinuse(gm, p, dvs); + } + mem = chunk2mem(p); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + else if (nb < gm->topsize) { /* Split top */ + size_t rsize = gm->topsize -= nb; + mchunkptr p = gm->top; + mchunkptr r = gm->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(gm, p, nb); + mem = chunk2mem(p); + check_top_chunk(gm, gm->top); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + mem = sys_alloc(gm, nb); + + postaction: + POSTACTION(gm); + return mem; + } + + return 0; +} + +void dlfree(void* mem) { + /* + Consolidate freed chunks with preceeding or succeeding bordering + free chunks, if they exist, and then place in a bin. Intermixed + with special cases for top, dv, mmapped chunks, and usage errors. + */ + + if (mem != 0) { + mchunkptr p = mem2chunk(mem); +#if FOOTERS + mstate fm = get_mstate_for(p); + if (!ok_magic(fm)) { + USAGE_ERROR_ACTION(fm, p); + return; + } +#else /* FOOTERS */ +#define fm gm +#endif /* FOOTERS */ + if (!PREACTION(fm)) { + check_inuse_chunk(fm, p); + if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) { + size_t psize = chunksize(p); + mchunkptr next = chunk_plus_offset(p, psize); + if (!pinuse(p)) { + size_t prevsize = p->prev_foot; + if ((prevsize & IS_MMAPPED_BIT) != 0) { + prevsize &= ~IS_MMAPPED_BIT; + psize += prevsize + MMAP_FOOT_PAD; + if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) + fm->footprint -= psize; + goto postaction; + } + else { + mchunkptr prev = chunk_minus_offset(p, prevsize); + psize += prevsize; + p = prev; + if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ + if (p != fm->dv) { + unlink_chunk(fm, p, prevsize); + } + else if ((next->head & INUSE_BITS) == INUSE_BITS) { + fm->dvsize = psize; + set_free_with_pinuse(p, psize, next); + goto postaction; + } + } + else + goto erroraction; + } + } + + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { + if (!cinuse(next)) { /* consolidate forward */ + if (next == fm->top) { + size_t tsize = fm->topsize += psize; + fm->top = p; + p->head = tsize | PINUSE_BIT; + if (p == fm->dv) { + fm->dv = 0; + fm->dvsize = 0; + } + if (should_trim(fm, tsize)) + sys_trim(fm, 0); + goto postaction; + } + else if (next == fm->dv) { + size_t dsize = fm->dvsize += psize; + fm->dv = p; + set_size_and_pinuse_of_free_chunk(p, dsize); + goto postaction; + } + else { + size_t nsize = chunksize(next); + psize += nsize; + unlink_chunk(fm, next, nsize); + set_size_and_pinuse_of_free_chunk(p, psize); + if (p == fm->dv) { + fm->dvsize = psize; + goto postaction; + } + } + } + else + set_free_with_pinuse(p, psize, next); + + if (is_small(psize)) { + insert_small_chunk(fm, p, psize); + check_free_chunk(fm, p); + } + else { + tchunkptr tp = (tchunkptr)p; + insert_large_chunk(fm, tp, psize); + check_free_chunk(fm, p); + if (--fm->release_checks == 0) + release_unused_segments(fm); + } + goto postaction; + } + } + erroraction: + USAGE_ERROR_ACTION(fm, p); + postaction: + POSTACTION(fm); + } + } +#if !FOOTERS +#undef fm +#endif /* FOOTERS */ +} + +void* dlcalloc(size_t n_elements, size_t elem_size) { + void* mem; + size_t req = 0; + if (n_elements != 0) { + req = n_elements * elem_size; + if (((n_elements | elem_size) & ~(size_t)0xffff) && + (req / n_elements != elem_size)) + req = MAX_SIZE_T; /* force downstream failure on overflow */ + } + mem = dlmalloc(req); + if (mem != 0 && calloc_must_clear(mem2chunk(mem))) + memset(mem, 0, req); + return mem; +} + +void* dlrealloc(void* oldmem, size_t bytes) { + if (oldmem == 0) + return dlmalloc(bytes); +#ifdef REALLOC_ZERO_BYTES_FREES + if (bytes == 0) { + dlfree(oldmem); + return 0; + } +#endif /* REALLOC_ZERO_BYTES_FREES */ + else { +#if ! FOOTERS + mstate m = gm; +#else /* FOOTERS */ + mstate m = get_mstate_for(mem2chunk(oldmem)); + if (!ok_magic(m)) { + USAGE_ERROR_ACTION(m, oldmem); + return 0; + } +#endif /* FOOTERS */ + return internal_realloc(m, oldmem, bytes); + } +} + +void* dlmemalign(size_t alignment, size_t bytes) { + return internal_memalign(gm, alignment, bytes); +} + +void** dlindependent_calloc(size_t n_elements, size_t elem_size, + void* chunks[]) { + size_t sz = elem_size; /* serves as 1-element array */ + return ialloc(gm, n_elements, &sz, 3, chunks); +} + +void** dlindependent_comalloc(size_t n_elements, size_t sizes[], + void* chunks[]) { + return ialloc(gm, n_elements, sizes, 0, chunks); +} + +void* dlvalloc(size_t bytes) { + size_t pagesz; + ensure_initialization(); + pagesz = mparams.page_size; + return dlmemalign(pagesz, bytes); +} + +void* dlpvalloc(size_t bytes) { + size_t pagesz; + ensure_initialization(); + pagesz = mparams.page_size; + return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE)); +} + +int dlmalloc_trim(size_t pad) { + ensure_initialization(); + int result = 0; + if (!PREACTION(gm)) { + result = sys_trim(gm, pad); + POSTACTION(gm); + } + return result; +} + +size_t dlmalloc_footprint(void) { + return gm->footprint; +} + +size_t dlmalloc_max_footprint(void) { + return gm->max_footprint; +} + +#if !NO_MALLINFO +struct mallinfo dlmallinfo(void) { + return internal_mallinfo(gm); +} +#endif /* NO_MALLINFO */ + +void dlmalloc_stats() { + internal_malloc_stats(gm); +} + +int dlmallopt(int param_number, int value) { + return change_mparam(param_number, value); +} + +#endif /* !ONLY_MSPACES */ + +size_t dlmalloc_usable_size(void* mem) { + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + if (cinuse(p)) + return chunksize(p) - overhead_for(p); + } + return 0; +} + +/* ----------------------------- user mspaces ---------------------------- */ + +#if MSPACES + +static mstate init_user_mstate(char* tbase, size_t tsize) { + size_t msize = pad_request(sizeof(struct malloc_state)); + mchunkptr mn; + mchunkptr msp = align_as_chunk(tbase); + mstate m = (mstate)(chunk2mem(msp)); + memset(m, 0, msize); + INITIAL_LOCK(&m->mutex); + msp->head = (msize|PINUSE_BIT|CINUSE_BIT); + m->seg.base = m->least_addr = tbase; + m->seg.size = m->footprint = m->max_footprint = tsize; + m->magic = mparams.magic; + m->release_checks = MAX_RELEASE_CHECK_RATE; + m->mflags = mparams.default_mflags; + m->extp = 0; + m->exts = 0; + disable_contiguous(m); + init_bins(m); + mn = next_chunk(mem2chunk(m)); + init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) - TOP_FOOT_SIZE); + check_top_chunk(m, m->top); + return m; +} + +mspace create_mspace(size_t capacity, int locked) { + mstate m = 0; + size_t msize; + ensure_initialization(); + msize = pad_request(sizeof(struct malloc_state)); + if (capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) { + size_t rs = ((capacity == 0)? mparams.granularity : + (capacity + TOP_FOOT_SIZE + msize)); + size_t tsize = granularity_align(rs); + char* tbase = (char*)(CALL_MMAP(tsize)); + if (tbase != CMFAIL) { + m = init_user_mstate(tbase, tsize); + m->seg.sflags = IS_MMAPPED_BIT; + set_lock(m, locked); + } + } + return (mspace)m; +} + +mspace create_mspace_with_base(void* base, size_t capacity, int locked) { + mstate m = 0; + size_t msize; + ensure_initialization(); + msize = pad_request(sizeof(struct malloc_state)); + if (capacity > msize + TOP_FOOT_SIZE && + capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) { + m = init_user_mstate((char*)base, capacity); + m->seg.sflags = EXTERN_BIT; + set_lock(m, locked); + } + return (mspace)m; +} + +int mspace_mmap_large_chunks(mspace msp, int enable) { + int ret = 0; + mstate ms = (mstate)msp; + if (!PREACTION(ms)) { + if (use_mmap(ms)) + ret = 1; + if (enable) + enable_mmap(ms); + else + disable_mmap(ms); + POSTACTION(ms); + } + return ret; +} + +size_t destroy_mspace(mspace msp) { + size_t freed = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + msegmentptr sp = &ms->seg; + while (sp != 0) { + char* base = sp->base; + size_t size = sp->size; + flag_t flag = sp->sflags; + sp = sp->next; + if ((flag & IS_MMAPPED_BIT) && !(flag & EXTERN_BIT) && + CALL_MUNMAP(base, size) == 0) + freed += size; + } + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return freed; +} + +/* + mspace versions of routines are near-clones of the global + versions. This is not so nice but better than the alternatives. +*/ + + +void* mspace_malloc(mspace msp, size_t bytes) { + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + if (!PREACTION(ms)) { + void* mem; + size_t nb; + if (bytes <= MAX_SMALL_REQUEST) { + bindex_t idx; + binmap_t smallbits; + nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); + idx = small_index(nb); + smallbits = ms->smallmap >> idx; + + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ + mchunkptr b, p; + idx += ~smallbits & 1; /* Uses next bin if idx empty */ + b = smallbin_at(ms, idx); + p = b->fd; + assert(chunksize(p) == small_index2size(idx)); + unlink_first_small_chunk(ms, b, p, idx); + set_inuse_and_pinuse(ms, p, small_index2size(idx)); + mem = chunk2mem(p); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + else if (nb > ms->dvsize) { + if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ + mchunkptr b, p, r; + size_t rsize; + bindex_t i; + binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); + binmap_t leastbit = least_bit(leftbits); + compute_bit2idx(leastbit, i); + b = smallbin_at(ms, i); + p = b->fd; + assert(chunksize(p) == small_index2size(i)); + unlink_first_small_chunk(ms, b, p, i); + rsize = small_index2size(i) - nb; + /* Fit here cannot be remainderless if 4byte sizes */ + if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(ms, p, small_index2size(i)); + else { + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + r = chunk_plus_offset(p, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(ms, r, rsize); + } + mem = chunk2mem(p); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + } + } + else if (bytes >= MAX_REQUEST) + nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ + else { + nb = pad_request(bytes); + if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + } + + if (nb <= ms->dvsize) { + size_t rsize = ms->dvsize - nb; + mchunkptr p = ms->dv; + if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ + mchunkptr r = ms->dv = chunk_plus_offset(p, nb); + ms->dvsize = rsize; + set_size_and_pinuse_of_free_chunk(r, rsize); + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + } + else { /* exhaust dv */ + size_t dvs = ms->dvsize; + ms->dvsize = 0; + ms->dv = 0; + set_inuse_and_pinuse(ms, p, dvs); + } + mem = chunk2mem(p); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + else if (nb < ms->topsize) { /* Split top */ + size_t rsize = ms->topsize -= nb; + mchunkptr p = ms->top; + mchunkptr r = ms->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + mem = chunk2mem(p); + check_top_chunk(ms, ms->top); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + mem = sys_alloc(ms, nb); + + postaction: + POSTACTION(ms); + return mem; + } + + return 0; +} + +void mspace_free(mspace msp, void* mem) { + if (mem != 0) { + mchunkptr p = mem2chunk(mem); +#if FOOTERS + mstate fm = get_mstate_for(p); +#else /* FOOTERS */ + mstate fm = (mstate)msp; +#endif /* FOOTERS */ + if (!ok_magic(fm)) { + USAGE_ERROR_ACTION(fm, p); + return; + } + if (!PREACTION(fm)) { + check_inuse_chunk(fm, p); + if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) { + size_t psize = chunksize(p); + mchunkptr next = chunk_plus_offset(p, psize); + if (!pinuse(p)) { + size_t prevsize = p->prev_foot; + if ((prevsize & IS_MMAPPED_BIT) != 0) { + prevsize &= ~IS_MMAPPED_BIT; + psize += prevsize + MMAP_FOOT_PAD; + if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) + fm->footprint -= psize; + goto postaction; + } + else { + mchunkptr prev = chunk_minus_offset(p, prevsize); + psize += prevsize; + p = prev; + if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ + if (p != fm->dv) { + unlink_chunk(fm, p, prevsize); + } + else if ((next->head & INUSE_BITS) == INUSE_BITS) { + fm->dvsize = psize; + set_free_with_pinuse(p, psize, next); + goto postaction; + } + } + else + goto erroraction; + } + } + + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { + if (!cinuse(next)) { /* consolidate forward */ + if (next == fm->top) { + size_t tsize = fm->topsize += psize; + fm->top = p; + p->head = tsize | PINUSE_BIT; + if (p == fm->dv) { + fm->dv = 0; + fm->dvsize = 0; + } + if (should_trim(fm, tsize)) + sys_trim(fm, 0); + goto postaction; + } + else if (next == fm->dv) { + size_t dsize = fm->dvsize += psize; + fm->dv = p; + set_size_and_pinuse_of_free_chunk(p, dsize); + goto postaction; + } + else { + size_t nsize = chunksize(next); + psize += nsize; + unlink_chunk(fm, next, nsize); + set_size_and_pinuse_of_free_chunk(p, psize); + if (p == fm->dv) { + fm->dvsize = psize; + goto postaction; + } + } + } + else + set_free_with_pinuse(p, psize, next); + + if (is_small(psize)) { + insert_small_chunk(fm, p, psize); + check_free_chunk(fm, p); + } + else { + tchunkptr tp = (tchunkptr)p; + insert_large_chunk(fm, tp, psize); + check_free_chunk(fm, p); + if (--fm->release_checks == 0) + release_unused_segments(fm); + } + goto postaction; + } + } + erroraction: + USAGE_ERROR_ACTION(fm, p); + postaction: + POSTACTION(fm); + } + } +} + +void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size) { + void* mem; + size_t req = 0; + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + if (n_elements != 0) { + req = n_elements * elem_size; + if (((n_elements | elem_size) & ~(size_t)0xffff) && + (req / n_elements != elem_size)) + req = MAX_SIZE_T; /* force downstream failure on overflow */ + } + mem = internal_malloc(ms, req); + if (mem != 0 && calloc_must_clear(mem2chunk(mem))) + memset(mem, 0, req); + return mem; +} + +void* mspace_realloc(mspace msp, void* oldmem, size_t bytes) { + if (oldmem == 0) + return mspace_malloc(msp, bytes); +#ifdef REALLOC_ZERO_BYTES_FREES + if (bytes == 0) { + mspace_free(msp, oldmem); + return 0; + } +#endif /* REALLOC_ZERO_BYTES_FREES */ + else { +#if FOOTERS + mchunkptr p = mem2chunk(oldmem); + mstate ms = get_mstate_for(p); +#else /* FOOTERS */ + mstate ms = (mstate)msp; +#endif /* FOOTERS */ + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + return internal_realloc(ms, oldmem, bytes); + } +} + +void* mspace_memalign(mspace msp, size_t alignment, size_t bytes) { + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + return internal_memalign(ms, alignment, bytes); +} + +void** mspace_independent_calloc(mspace msp, size_t n_elements, + size_t elem_size, void* chunks[]) { + size_t sz = elem_size; /* serves as 1-element array */ + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + return ialloc(ms, n_elements, &sz, 3, chunks); +} + +void** mspace_independent_comalloc(mspace msp, size_t n_elements, + size_t sizes[], void* chunks[]) { + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + return ialloc(ms, n_elements, sizes, 0, chunks); +} + +int mspace_trim(mspace msp, size_t pad) { + int result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + if (!PREACTION(ms)) { + result = sys_trim(ms, pad); + POSTACTION(ms); + } + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; +} + +void mspace_malloc_stats(mspace msp) { + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + internal_malloc_stats(ms); + } + else { + USAGE_ERROR_ACTION(ms,ms); + } +} + +size_t mspace_footprint(mspace msp) { + size_t result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + result = ms->footprint; + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; +} + + +size_t mspace_max_footprint(mspace msp) { + size_t result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + result = ms->max_footprint; + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; +} + + +#if !NO_MALLINFO +struct mallinfo mspace_mallinfo(mspace msp) { + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + } + return internal_mallinfo(ms); +} +#endif /* NO_MALLINFO */ + +size_t mspace_usable_size(void* mem) { + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + if (cinuse(p)) + return chunksize(p) - overhead_for(p); + } + return 0; +} + +int mspace_mallopt(int param_number, int value) { + return change_mparam(param_number, value); +} + +#endif /* MSPACES */ + +/* -------------------- Alternative MORECORE functions ------------------- */ + +/* + Guidelines for creating a custom version of MORECORE: + + * For best performance, MORECORE should allocate in multiples of pagesize. + * MORECORE may allocate more memory than requested. (Or even less, + but this will usually result in a malloc failure.) + * MORECORE must not allocate memory when given argument zero, but + instead return one past the end address of memory from previous + nonzero call. + * For best performance, consecutive calls to MORECORE with positive + arguments should return increasing addresses, indicating that + space has been contiguously extended. + * Even though consecutive calls to MORECORE need not return contiguous + addresses, it must be OK for malloc'ed chunks to span multiple + regions in those cases where they do happen to be contiguous. + * MORECORE need not handle negative arguments -- it may instead + just return MFAIL when given negative arguments. + Negative arguments are always multiples of pagesize. MORECORE + must not misinterpret negative args as large positive unsigned + args. You can suppress all such calls from even occurring by defining + MORECORE_CANNOT_TRIM, + + As an example alternative MORECORE, here is a custom allocator + kindly contributed for pre-OSX macOS. It uses virtually but not + necessarily physically contiguous non-paged memory (locked in, + present and won't get swapped out). You can use it by uncommenting + this section, adding some #includes, and setting up the appropriate + defines above: + + #define MORECORE osMoreCore + + There is also a shutdown routine that should somehow be called for + cleanup upon program exit. + + #define MAX_POOL_ENTRIES 100 + #define MINIMUM_MORECORE_SIZE (64 * 1024U) + static int next_os_pool; + void *our_os_pools[MAX_POOL_ENTRIES]; + + void *osMoreCore(int size) + { + void *ptr = 0; + static void *sbrk_top = 0; + + if (size > 0) + { + if (size < MINIMUM_MORECORE_SIZE) + size = MINIMUM_MORECORE_SIZE; + if (CurrentExecutionLevel() == kTaskLevel) + ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0); + if (ptr == 0) + { + return (void *) MFAIL; + } + // save ptrs so they can be freed during cleanup + our_os_pools[next_os_pool] = ptr; + next_os_pool++; + ptr = (void *) ((((size_t) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK); + sbrk_top = (char *) ptr + size; + return ptr; + } + else if (size < 0) + { + // we don't currently support shrink behavior + return (void *) MFAIL; + } + else + { + return sbrk_top; + } + } + + // cleanup any allocated memory pools + // called as last thing before shutting down driver + + void osCleanupMem(void) + { + void **ptr; + + for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) + if (*ptr) + { + PoolDeallocate(*ptr); + *ptr = 0; + } + } + +*/ + + +/* ----------------------------------------------------------------------- +History: + V2.8.4 (not yet released) + * Add mspace_mmap_large_chunks; thanks to Jean Brouwers + * Fix insufficient sys_alloc padding when using 16byte alignment + * Fix bad error check in mspace_footprint + * Adaptations for ptmalloc, courtesy of Wolfram Gloger. + * Reentrant spin locks, courtesy of Earl Chew and others + * Win32 improvements, courtesy of Niall Douglas and Earl Chew + * Add NO_SEGMENT_TRAVERSAL and MAX_RELEASE_CHECK_RATE options + * Extension hook in malloc_state + * Various small adjustments to reduce warnings on some compilers + * Various configuration extensions/changes for more platforms. Thanks + to all who contributed these. + + V2.8.3 Thu Sep 22 11:16:32 2005 Doug Lea (dl at gee) + * Add max_footprint functions + * Ensure all appropriate literals are size_t + * Fix conditional compilation problem for some #define settings + * Avoid concatenating segments with the one provided + in create_mspace_with_base + * Rename some variables to avoid compiler shadowing warnings + * Use explicit lock initialization. + * Better handling of sbrk interference. + * Simplify and fix segment insertion, trimming and mspace_destroy + * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x + * Thanks especially to Dennis Flanagan for help on these. + + V2.8.2 Sun Jun 12 16:01:10 2005 Doug Lea (dl at gee) + * Fix memalign brace error. + + V2.8.1 Wed Jun 8 16:11:46 2005 Doug Lea (dl at gee) + * Fix improper #endif nesting in C++ + * Add explicit casts needed for C++ + + V2.8.0 Mon May 30 14:09:02 2005 Doug Lea (dl at gee) + * Use trees for large bins + * Support mspaces + * Use segments to unify sbrk-based and mmap-based system allocation, + removing need for emulation on most platforms without sbrk. + * Default safety checks + * Optional footer checks. Thanks to William Robertson for the idea. + * Internal code refactoring + * Incorporate suggestions and platform-specific changes. + Thanks to Dennis Flanagan, Colin Plumb, Niall Douglas, + Aaron Bachmann, Emery Berger, and others. + * Speed up non-fastbin processing enough to remove fastbins. + * Remove useless cfree() to avoid conflicts with other apps. + * Remove internal memcpy, memset. Compilers handle builtins better. + * Remove some options that no one ever used and rename others. + + V2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee) + * Fix malloc_state bitmap array misdeclaration + + V2.7.1 Thu Jul 25 10:58:03 2002 Doug Lea (dl at gee) + * Allow tuning of FIRST_SORTED_BIN_SIZE + * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte. + * Better detection and support for non-contiguousness of MORECORE. + Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger + * Bypass most of malloc if no frees. Thanks To Emery Berger. + * Fix freeing of old top non-contiguous chunk im sysmalloc. + * Raised default trim and map thresholds to 256K. + * Fix mmap-related #defines. Thanks to Lubos Lunak. + * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield. + * Branch-free bin calculation + * Default trim and mmap thresholds now 256K. + + V2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) + * Introduce independent_comalloc and independent_calloc. + Thanks to Michael Pachos for motivation and help. + * Make optional .h file available + * Allow > 2GB requests on 32bit systems. + * new WIN32 sbrk, mmap, munmap, lock code from . + Thanks also to Andreas Mueller , + and Anonymous. + * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for + helping test this.) + * memalign: check alignment arg + * realloc: don't try to shift chunks backwards, since this + leads to more fragmentation in some programs and doesn't + seem to help in any others. + * Collect all cases in malloc requiring system memory into sysmalloc + * Use mmap as backup to sbrk + * Place all internal state in malloc_state + * Introduce fastbins (although similar to 2.5.1) + * Many minor tunings and cosmetic improvements + * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK + * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS + Thanks to Tony E. Bennett and others. + * Include errno.h to support default failure action. + + V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) + * return null for negative arguments + * Added Several WIN32 cleanups from Martin C. Fong + * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h' + (e.g. WIN32 platforms) + * Cleanup header file inclusion for WIN32 platforms + * Cleanup code to avoid Microsoft Visual C++ compiler complaints + * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing + memory allocation routines + * Set 'malloc_getpagesize' for WIN32 platforms (needs more work) + * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to + usage of 'assert' in non-WIN32 code + * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to + avoid infinite loop + * Always call 'fREe()' rather than 'free()' + + V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) + * Fixed ordering problem with boundary-stamping + + V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) + * Added pvalloc, as recommended by H.J. Liu + * Added 64bit pointer support mainly from Wolfram Gloger + * Added anonymously donated WIN32 sbrk emulation + * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen + * malloc_extend_top: fix mask error that caused wastage after + foreign sbrks + * Add linux mremap support code from HJ Liu + + V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) + * Integrated most documentation with the code. + * Add support for mmap, with help from + Wolfram Gloger (Gloger@lrz.uni-muenchen.de). + * Use last_remainder in more cases. + * Pack bins using idea from colin@nyx10.cs.du.edu + * Use ordered bins instead of best-fit threshhold + * Eliminate block-local decls to simplify tracing and debugging. + * Support another case of realloc via move into top + * Fix error occuring when initial sbrk_base not word-aligned. + * Rely on page size for units instead of SBRK_UNIT to + avoid surprises about sbrk alignment conventions. + * Add mallinfo, mallopt. Thanks to Raymond Nijssen + (raymond@es.ele.tue.nl) for the suggestion. + * Add `pad' argument to malloc_trim and top_pad mallopt parameter. + * More precautions for cases where other routines call sbrk, + courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de). + * Added macros etc., allowing use in linux libc from + H.J. Lu (hjl@gnu.ai.mit.edu) + * Inverted this history list + + V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) + * Re-tuned and fixed to behave more nicely with V2.6.0 changes. + * Removed all preallocation code since under current scheme + the work required to undo bad preallocations exceeds + the work saved in good cases for most test programs. + * No longer use return list or unconsolidated bins since + no scheme using them consistently outperforms those that don't + given above changes. + * Use best fit for very large chunks to prevent some worst-cases. + * Added some support for debugging + + V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) + * Removed footers when chunks are in use. Thanks to + Paul Wilson (wilson@cs.texas.edu) for the suggestion. + + V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) + * Added malloc_trim, with help from Wolfram Gloger + (wmglo@Dent.MED.Uni-Muenchen.DE). + + V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) + + V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) + * realloc: try to expand in both directions + * malloc: swap order of clean-bin strategy; + * realloc: only conditionally expand backwards + * Try not to scavenge used bins + * Use bin counts as a guide to preallocation + * Occasionally bin return list chunks in first scan + * Add a few optimizations from colin@nyx10.cs.du.edu + + V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) + * faster bin computation & slightly different binning + * merged all consolidations to one part of malloc proper + (eliminating old malloc_find_space & malloc_clean_bin) + * Scan 2 returns chunks (not just 1) + * Propagate failure in realloc if malloc returns 0 + * Add stuff to allow compilation on non-ANSI compilers + from kpv@research.att.com + + V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) + * removed potential for odd address access in prev_chunk + * removed dependency on getpagesize.h + * misc cosmetics and a bit more internal documentation + * anticosmetics: mangled names in macros to evade debugger strangeness + * tested on sparc, hp-700, dec-mips, rs6000 + with gcc & native cc (hp, dec only) allowing + Detlefs & Zorn comparison study (in SIGPLAN Notices.) + + Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) + * Based loosely on libg++-1.2X malloc. (It retains some of the overall + structure of old version, but most details differ.) + +*/ + + diff --git a/compat/nedmalloc/nedmalloc.c b/compat/nedmalloc/nedmalloc.c new file mode 100644 index 0000000000..a381a7df36 --- /dev/null +++ b/compat/nedmalloc/nedmalloc.c @@ -0,0 +1,966 @@ +/* Alternative malloc implementation for multiple threads without +lock contention based on dlmalloc. (C) 2005-2006 Niall Douglas + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifdef _MSC_VER +/* Enable full aliasing on MSVC */ +/*#pragma optimize("a", on)*/ +#endif + +/*#define FULLSANITYCHECKS*/ + +#include "nedmalloc.h" +#if defined(WIN32) && !defined(__MINGW32__) + #include +#endif +#define MSPACES 1 +#define ONLY_MSPACES 1 +#ifndef USE_LOCKS + #define USE_LOCKS 1 +#endif +#define FOOTERS 1 /* Need to enable footers so frees lock the right mspace */ +#undef DEBUG /* dlmalloc wants DEBUG either 0 or 1 */ +#ifdef _DEBUG + #define DEBUG 1 +#else + #define DEBUG 0 +#endif +#ifdef NDEBUG /* Disable assert checking on release builds */ + #undef DEBUG +#endif +/* The default of 64Kb means we spend too much time kernel-side */ +#ifndef DEFAULT_GRANULARITY +#define DEFAULT_GRANULARITY (1*1024*1024) +#endif +/*#define USE_SPIN_LOCKS 0*/ + + +/*#define FORCEINLINE*/ +#include "malloc.c.h" +#ifdef NDEBUG /* Disable assert checking on release builds */ + #undef DEBUG +#endif + +/* The maximum concurrent threads in a pool possible */ +#ifndef MAXTHREADSINPOOL +#define MAXTHREADSINPOOL 16 +#endif +/* The maximum number of threadcaches which can be allocated */ +#ifndef THREADCACHEMAXCACHES +#define THREADCACHEMAXCACHES 256 +#endif +/* The maximum size to be allocated from the thread cache */ +#ifndef THREADCACHEMAX +#define THREADCACHEMAX 8192 +#endif +#if 0 +/* The number of cache entries for finer grained bins. This is (topbitpos(THREADCACHEMAX)-4)*2 */ +#define THREADCACHEMAXBINS ((13-4)*2) +#else +/* The number of cache entries. This is (topbitpos(THREADCACHEMAX)-4) */ +#define THREADCACHEMAXBINS (13-4) +#endif +/* Point at which the free space in a thread cache is garbage collected */ +#ifndef THREADCACHEMAXFREESPACE +#define THREADCACHEMAXFREESPACE (512*1024) +#endif + + +#ifdef WIN32 + #define TLSVAR DWORD + #define TLSALLOC(k) (*(k)=TlsAlloc(), TLS_OUT_OF_INDEXES==*(k)) + #define TLSFREE(k) (!TlsFree(k)) + #define TLSGET(k) TlsGetValue(k) + #define TLSSET(k, a) (!TlsSetValue(k, a)) + #ifdef DEBUG +static LPVOID ChkedTlsGetValue(DWORD idx) +{ + LPVOID ret=TlsGetValue(idx); + assert(S_OK==GetLastError()); + return ret; +} + #undef TLSGET + #define TLSGET(k) ChkedTlsGetValue(k) + #endif +#else + #define TLSVAR pthread_key_t + #define TLSALLOC(k) pthread_key_create(k, 0) + #define TLSFREE(k) pthread_key_delete(k) + #define TLSGET(k) pthread_getspecific(k) + #define TLSSET(k, a) pthread_setspecific(k, a) +#endif + +#if 0 +/* Only enable if testing with valgrind. Causes misoperation */ +#define mspace_malloc(p, s) malloc(s) +#define mspace_realloc(p, m, s) realloc(m, s) +#define mspace_calloc(p, n, s) calloc(n, s) +#define mspace_free(p, m) free(m) +#endif + + +#if defined(__cplusplus) +#if !defined(NO_NED_NAMESPACE) +namespace nedalloc { +#else +extern "C" { +#endif +#endif + +size_t nedblksize(void *mem) THROWSPEC +{ +#if 0 + /* Only enable if testing with valgrind. Causes misoperation */ + return THREADCACHEMAX; +#else + if(mem) + { + mchunkptr p=mem2chunk(mem); + assert(cinuse(p)); /* If this fails, someone tried to free a block twice */ + if(cinuse(p)) + return chunksize(p)-overhead_for(p); + } + return 0; +#endif +} + +void nedsetvalue(void *v) THROWSPEC { nedpsetvalue(0, v); } +void * nedmalloc(size_t size) THROWSPEC { return nedpmalloc(0, size); } +void * nedcalloc(size_t no, size_t size) THROWSPEC { return nedpcalloc(0, no, size); } +void * nedrealloc(void *mem, size_t size) THROWSPEC { return nedprealloc(0, mem, size); } +void nedfree(void *mem) THROWSPEC { nedpfree(0, mem); } +void * nedmemalign(size_t alignment, size_t bytes) THROWSPEC { return nedpmemalign(0, alignment, bytes); } +#if !NO_MALLINFO +struct mallinfo nedmallinfo(void) THROWSPEC { return nedpmallinfo(0); } +#endif +int nedmallopt(int parno, int value) THROWSPEC { return nedpmallopt(0, parno, value); } +int nedmalloc_trim(size_t pad) THROWSPEC { return nedpmalloc_trim(0, pad); } +void nedmalloc_stats() THROWSPEC { nedpmalloc_stats(0); } +size_t nedmalloc_footprint() THROWSPEC { return nedpmalloc_footprint(0); } +void **nedindependent_calloc(size_t elemsno, size_t elemsize, void **chunks) THROWSPEC { return nedpindependent_calloc(0, elemsno, elemsize, chunks); } +void **nedindependent_comalloc(size_t elems, size_t *sizes, void **chunks) THROWSPEC { return nedpindependent_comalloc(0, elems, sizes, chunks); } + +struct threadcacheblk_t; +typedef struct threadcacheblk_t threadcacheblk; +struct threadcacheblk_t +{ /* Keep less than 16 bytes on 32 bit systems and 32 bytes on 64 bit systems */ +#ifdef FULLSANITYCHECKS + unsigned int magic; +#endif + unsigned int lastUsed, size; + threadcacheblk *next, *prev; +}; +typedef struct threadcache_t +{ +#ifdef FULLSANITYCHECKS + unsigned int magic1; +#endif + int mymspace; /* Last mspace entry this thread used */ + long threadid; + unsigned int mallocs, frees, successes; + size_t freeInCache; /* How much free space is stored in this cache */ + threadcacheblk *bins[(THREADCACHEMAXBINS+1)*2]; +#ifdef FULLSANITYCHECKS + unsigned int magic2; +#endif +} threadcache; +struct nedpool_t +{ + MLOCK_T mutex; + void *uservalue; + int threads; /* Max entries in m to use */ + threadcache *caches[THREADCACHEMAXCACHES]; + TLSVAR mycache; /* Thread cache for this thread. 0 for unset, negative for use mspace-1 directly, otherwise is cache-1 */ + mstate m[MAXTHREADSINPOOL+1]; /* mspace entries for this pool */ +}; +static nedpool syspool; + +static FORCEINLINE unsigned int size2binidx(size_t _size) THROWSPEC +{ /* 8=1000 16=10000 20=10100 24=11000 32=100000 48=110000 4096=1000000000000 */ + unsigned int topbit, size=(unsigned int)(_size>>4); + /* 16=1 20=1 24=1 32=10 48=11 64=100 96=110 128=1000 4096=100000000 */ + +#if defined(__GNUC__) + topbit = sizeof(size)*__CHAR_BIT__ - 1 - __builtin_clz(size); +#elif defined(_MSC_VER) && _MSC_VER>=1300 + { + unsigned long bsrTopBit; + + _BitScanReverse(&bsrTopBit, size); + + topbit = bsrTopBit; + } +#else +#if 0 + union { + unsigned asInt[2]; + double asDouble; + }; + int n; + + asDouble = (double)size + 0.5; + topbit = (asInt[!FOX_BIGENDIAN] >> 20) - 1023; +#else + { + unsigned int x=size; + x = x | (x >> 1); + x = x | (x >> 2); + x = x | (x >> 4); + x = x | (x >> 8); + x = x | (x >>16); + x = ~x; + x = x - ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x + (x >> 4)) & 0x0F0F0F0F; + x = x + (x << 8); + x = x + (x << 16); + topbit=31 - (x >> 24); + } +#endif +#endif + return topbit; +} + + +#ifdef FULLSANITYCHECKS +static void tcsanitycheck(threadcacheblk **ptr) THROWSPEC +{ + assert((ptr[0] && ptr[1]) || (!ptr[0] && !ptr[1])); + if(ptr[0] && ptr[1]) + { + assert(nedblksize(ptr[0])>=sizeof(threadcacheblk)); + assert(nedblksize(ptr[1])>=sizeof(threadcacheblk)); + assert(*(unsigned int *) "NEDN"==ptr[0]->magic); + assert(*(unsigned int *) "NEDN"==ptr[1]->magic); + assert(!ptr[0]->prev); + assert(!ptr[1]->next); + if(ptr[0]==ptr[1]) + { + assert(!ptr[0]->next); + assert(!ptr[1]->prev); + } + } +} +static void tcfullsanitycheck(threadcache *tc) THROWSPEC +{ + threadcacheblk **tcbptr=tc->bins; + int n; + for(n=0; n<=THREADCACHEMAXBINS; n++, tcbptr+=2) + { + threadcacheblk *b, *ob=0; + tcsanitycheck(tcbptr); + for(b=tcbptr[0]; b; ob=b, b=b->next) + { + assert(*(unsigned int *) "NEDN"==b->magic); + assert(!ob || ob->next==b); + assert(!ob || b->prev==ob); + } + } +} +#endif + +static NOINLINE void RemoveCacheEntries(nedpool *p, threadcache *tc, unsigned int age) THROWSPEC +{ +#ifdef FULLSANITYCHECKS + tcfullsanitycheck(tc); +#endif + if(tc->freeInCache) + { + threadcacheblk **tcbptr=tc->bins; + int n; + for(n=0; n<=THREADCACHEMAXBINS; n++, tcbptr+=2) + { + threadcacheblk **tcb=tcbptr+1; /* come from oldest end of list */ + /*tcsanitycheck(tcbptr);*/ + for(; *tcb && tc->frees-(*tcb)->lastUsed>=age; ) + { + threadcacheblk *f=*tcb; + size_t blksize=f->size; /*nedblksize(f);*/ + assert(blksize<=nedblksize(f)); + assert(blksize); +#ifdef FULLSANITYCHECKS + assert(*(unsigned int *) "NEDN"==(*tcb)->magic); +#endif + *tcb=(*tcb)->prev; + if(*tcb) + (*tcb)->next=0; + else + *tcbptr=0; + tc->freeInCache-=blksize; + assert((long) tc->freeInCache>=0); + mspace_free(0, f); + /*tcsanitycheck(tcbptr);*/ + } + } + } +#ifdef FULLSANITYCHECKS + tcfullsanitycheck(tc); +#endif +} +static void DestroyCaches(nedpool *p) THROWSPEC +{ + if(p->caches) + { + threadcache *tc; + int n; + for(n=0; ncaches[n])) + { + tc->frees++; + RemoveCacheEntries(p, tc, 0); + assert(!tc->freeInCache); + tc->mymspace=-1; + tc->threadid=0; + mspace_free(0, tc); + p->caches[n]=0; + } + } + } +} + +static NOINLINE threadcache *AllocCache(nedpool *p) THROWSPEC +{ + threadcache *tc=0; + int n, end; + ACQUIRE_LOCK(&p->mutex); + for(n=0; ncaches[n]; n++); + if(THREADCACHEMAXCACHES==n) + { /* List exhausted, so disable for this thread */ + RELEASE_LOCK(&p->mutex); + return 0; + } + tc=p->caches[n]=(threadcache *) mspace_calloc(p->m[0], 1, sizeof(threadcache)); + if(!tc) + { + RELEASE_LOCK(&p->mutex); + return 0; + } +#ifdef FULLSANITYCHECKS + tc->magic1=*(unsigned int *)"NEDMALC1"; + tc->magic2=*(unsigned int *)"NEDMALC2"; +#endif + tc->threadid=(long)(size_t)CURRENT_THREAD; + for(end=0; p->m[end]; end++); + tc->mymspace=tc->threadid % end; + RELEASE_LOCK(&p->mutex); + if(TLSSET(p->mycache, (void *)(size_t)(n+1))) abort(); + return tc; +} + +static void *threadcache_malloc(nedpool *p, threadcache *tc, size_t *size) THROWSPEC +{ + void *ret=0; + unsigned int bestsize; + unsigned int idx=size2binidx(*size); + size_t blksize=0; + threadcacheblk *blk, **binsptr; +#ifdef FULLSANITYCHECKS + tcfullsanitycheck(tc); +#endif + /* Calculate best fit bin size */ + bestsize=1<<(idx+4); +#if 0 + /* Finer grained bin fit */ + idx<<=1; + if(*size>bestsize) + { + idx++; + bestsize+=bestsize>>1; + } + if(*size>bestsize) + { + idx++; + bestsize=1<<(4+(idx>>1)); + } +#else + if(*size>bestsize) + { + idx++; + bestsize<<=1; + } +#endif + assert(bestsize>=*size); + if(*sizebins[idx*2]; + /* Try to match close, but move up a bin if necessary */ + blk=*binsptr; + if(!blk || blk->size<*size) + { /* Bump it up a bin */ + if(idxsize; /*nedblksize(blk);*/ + assert(nedblksize(blk)>=blksize); + assert(blksize>=*size); + if(blk->next) + blk->next->prev=0; + *binsptr=blk->next; + if(!*binsptr) + binsptr[1]=0; +#ifdef FULLSANITYCHECKS + blk->magic=0; +#endif + assert(binsptr[0]!=blk && binsptr[1]!=blk); + assert(nedblksize(blk)>=sizeof(threadcacheblk) && nedblksize(blk)<=THREADCACHEMAX+CHUNK_OVERHEAD); + /*printf("malloc: %p, %p, %p, %lu\n", p, tc, blk, (long) size);*/ + ret=(void *) blk; + } + ++tc->mallocs; + if(ret) + { + assert(blksize>=*size); + ++tc->successes; + tc->freeInCache-=blksize; + assert((long) tc->freeInCache>=0); + } +#if defined(DEBUG) && 0 + if(!(tc->mallocs & 0xfff)) + { + printf("*** threadcache=%u, mallocs=%u (%f), free=%u (%f), freeInCache=%u\n", (unsigned int) tc->threadid, tc->mallocs, + (float) tc->successes/tc->mallocs, tc->frees, (float) tc->successes/tc->frees, (unsigned int) tc->freeInCache); + } +#endif +#ifdef FULLSANITYCHECKS + tcfullsanitycheck(tc); +#endif + return ret; +} +static NOINLINE void ReleaseFreeInCache(nedpool *p, threadcache *tc, int mymspace) THROWSPEC +{ + unsigned int age=THREADCACHEMAXFREESPACE/8192; + /*ACQUIRE_LOCK(&p->m[mymspace]->mutex);*/ + while(age && tc->freeInCache>=THREADCACHEMAXFREESPACE) + { + RemoveCacheEntries(p, tc, age); + /*printf("*** Removing cache entries older than %u (%u)\n", age, (unsigned int) tc->freeInCache);*/ + age>>=1; + } + /*RELEASE_LOCK(&p->m[mymspace]->mutex);*/ +} +static void threadcache_free(nedpool *p, threadcache *tc, int mymspace, void *mem, size_t size) THROWSPEC +{ + unsigned int bestsize; + unsigned int idx=size2binidx(size); + threadcacheblk **binsptr, *tck=(threadcacheblk *) mem; + assert(size>=sizeof(threadcacheblk) && size<=THREADCACHEMAX+CHUNK_OVERHEAD); +#ifdef DEBUG + { /* Make sure this is a valid memory block */ + mchunkptr p = mem2chunk(mem); + mstate fm = get_mstate_for(p); + if (!ok_magic(fm)) { + USAGE_ERROR_ACTION(fm, p); + return; + } + } +#endif +#ifdef FULLSANITYCHECKS + tcfullsanitycheck(tc); +#endif + /* Calculate best fit bin size */ + bestsize=1<<(idx+4); +#if 0 + /* Finer grained bin fit */ + idx<<=1; + if(size>bestsize) + { + unsigned int biggerbestsize=bestsize+bestsize<<1; + if(size>=biggerbestsize) + { + idx++; + bestsize=biggerbestsize; + } + } +#endif + if(bestsize!=size) /* dlmalloc can round up, so we round down to preserve indexing */ + size=bestsize; + binsptr=&tc->bins[idx*2]; + assert(idx<=THREADCACHEMAXBINS); + if(tck==*binsptr) + { + fprintf(stderr, "Attempt to free already freed memory block %p - aborting!\n", tck); + abort(); + } +#ifdef FULLSANITYCHECKS + tck->magic=*(unsigned int *) "NEDN"; +#endif + tck->lastUsed=++tc->frees; + tck->size=(unsigned int) size; + tck->next=*binsptr; + tck->prev=0; + if(tck->next) + tck->next->prev=tck; + else + binsptr[1]=tck; + assert(!*binsptr || (*binsptr)->size==tck->size); + *binsptr=tck; + assert(tck==tc->bins[idx*2]); + assert(tc->bins[idx*2+1]==tck || binsptr[0]->next->prev==tck); + /*printf("free: %p, %p, %p, %lu\n", p, tc, mem, (long) size);*/ + tc->freeInCache+=size; +#ifdef FULLSANITYCHECKS + tcfullsanitycheck(tc); +#endif +#if 1 + if(tc->freeInCache>=THREADCACHEMAXFREESPACE) + ReleaseFreeInCache(p, tc, mymspace); +#endif +} + + + + +static NOINLINE int InitPool(nedpool *p, size_t capacity, int threads) THROWSPEC +{ /* threads is -1 for system pool */ + ensure_initialization(); + ACQUIRE_MALLOC_GLOBAL_LOCK(); + if(p->threads) goto done; + if(INITIAL_LOCK(&p->mutex)) goto err; + if(TLSALLOC(&p->mycache)) goto err; + if(!(p->m[0]=(mstate) create_mspace(capacity, 1))) goto err; + p->m[0]->extp=p; + p->threads=(threads<1 || threads>MAXTHREADSINPOOL) ? MAXTHREADSINPOOL : threads; +done: + RELEASE_MALLOC_GLOBAL_LOCK(); + return 1; +err: + if(threads<0) + abort(); /* If you can't allocate for system pool, we're screwed */ + DestroyCaches(p); + if(p->m[0]) + { + destroy_mspace(p->m[0]); + p->m[0]=0; + } + if(p->mycache) + { + if(TLSFREE(p->mycache)) abort(); + p->mycache=0; + } + RELEASE_MALLOC_GLOBAL_LOCK(); + return 0; +} +static NOINLINE mstate FindMSpace(nedpool *p, threadcache *tc, int *lastUsed, size_t size) THROWSPEC +{ /* Gets called when thread's last used mspace is in use. The strategy + is to run through the list of all available mspaces looking for an + unlocked one and if we fail, we create a new one so long as we don't + exceed p->threads */ + int n, end; + for(n=end=*lastUsed+1; p->m[n]; end=++n) + { + if(TRY_LOCK(&p->m[n]->mutex)) goto found; + } + for(n=0; n<*lastUsed && p->m[n]; n++) + { + if(TRY_LOCK(&p->m[n]->mutex)) goto found; + } + if(endthreads) + { + mstate temp; + if(!(temp=(mstate) create_mspace(size, 1))) + goto badexit; + /* Now we're ready to modify the lists, we lock */ + ACQUIRE_LOCK(&p->mutex); + while(p->m[end] && endthreads) + end++; + if(end>=p->threads) + { /* Drat, must destroy it now */ + RELEASE_LOCK(&p->mutex); + destroy_mspace((mspace) temp); + goto badexit; + } + /* We really want to make sure this goes into memory now but we + have to be careful of breaking aliasing rules, so write it twice */ + *((volatile struct malloc_state **) &p->m[end])=p->m[end]=temp; + ACQUIRE_LOCK(&p->m[end]->mutex); + /*printf("Created mspace idx %d\n", end);*/ + RELEASE_LOCK(&p->mutex); + n=end; + goto found; + } + /* Let it lock on the last one it used */ +badexit: + ACQUIRE_LOCK(&p->m[*lastUsed]->mutex); + return p->m[*lastUsed]; +found: + *lastUsed=n; + if(tc) + tc->mymspace=n; + else + { + if(TLSSET(p->mycache, (void *)(size_t)(-(n+1)))) abort(); + } + return p->m[n]; +} + +nedpool *nedcreatepool(size_t capacity, int threads) THROWSPEC +{ + nedpool *ret; + if(!(ret=(nedpool *) nedpcalloc(0, 1, sizeof(nedpool)))) return 0; + if(!InitPool(ret, capacity, threads)) + { + nedpfree(0, ret); + return 0; + } + return ret; +} +void neddestroypool(nedpool *p) THROWSPEC +{ + int n; + ACQUIRE_LOCK(&p->mutex); + DestroyCaches(p); + for(n=0; p->m[n]; n++) + { + destroy_mspace(p->m[n]); + p->m[n]=0; + } + RELEASE_LOCK(&p->mutex); + if(TLSFREE(p->mycache)) abort(); + nedpfree(0, p); +} + +void nedpsetvalue(nedpool *p, void *v) THROWSPEC +{ + if(!p) { p=&syspool; if(!syspool.threads) InitPool(&syspool, 0, -1); } + p->uservalue=v; +} +void *nedgetvalue(nedpool **p, void *mem) THROWSPEC +{ + nedpool *np=0; + mchunkptr mcp=mem2chunk(mem); + mstate fm; + if(!(is_aligned(chunk2mem(mcp))) && mcp->head != FENCEPOST_HEAD) return 0; + if(!cinuse(mcp)) return 0; + if(!next_pinuse(mcp)) return 0; + if(!is_mmapped(mcp) && !pinuse(mcp)) + { + if(next_chunk(prev_chunk(mcp))!=mcp) return 0; + } + fm=get_mstate_for(mcp); + if(!ok_magic(fm)) return 0; + if(!ok_address(fm, mcp)) return 0; + if(!fm->extp) return 0; + np=(nedpool *) fm->extp; + if(p) *p=np; + return np->uservalue; +} + +void neddisablethreadcache(nedpool *p) THROWSPEC +{ + int mycache; + if(!p) + { + p=&syspool; + if(!syspool.threads) InitPool(&syspool, 0, -1); + } + mycache=(int)(size_t) TLSGET(p->mycache); + if(!mycache) + { /* Set to mspace 0 */ + if(TLSSET(p->mycache, (void *)-1)) abort(); + } + else if(mycache>0) + { /* Set to last used mspace */ + threadcache *tc=p->caches[mycache-1]; +#if defined(DEBUG) + printf("Threadcache utilisation: %lf%% in cache with %lf%% lost to other threads\n", + 100.0*tc->successes/tc->mallocs, 100.0*((double) tc->mallocs-tc->frees)/tc->mallocs); +#endif + if(TLSSET(p->mycache, (void *)(size_t)(-tc->mymspace))) abort(); + tc->frees++; + RemoveCacheEntries(p, tc, 0); + assert(!tc->freeInCache); + tc->mymspace=-1; + tc->threadid=0; + mspace_free(0, p->caches[mycache-1]); + p->caches[mycache-1]=0; + } +} + +#define GETMSPACE(m,p,tc,ms,s,action) \ + do \ + { \ + mstate m = GetMSpace((p),(tc),(ms),(s)); \ + action; \ + RELEASE_LOCK(&m->mutex); \ + } while (0) + +static FORCEINLINE mstate GetMSpace(nedpool *p, threadcache *tc, int mymspace, size_t size) THROWSPEC +{ /* Returns a locked and ready for use mspace */ + mstate m=p->m[mymspace]; + assert(m); + if(!TRY_LOCK(&p->m[mymspace]->mutex)) m=FindMSpace(p, tc, &mymspace, size);\ + /*assert(IS_LOCKED(&p->m[mymspace]->mutex));*/ + return m; +} +static FORCEINLINE void GetThreadCache(nedpool **p, threadcache **tc, int *mymspace, size_t *size) THROWSPEC +{ + int mycache; + if(size && *sizemycache); + if(mycache>0) + { + *tc=(*p)->caches[mycache-1]; + *mymspace=(*tc)->mymspace; + } + else if(!mycache) + { + *tc=AllocCache(*p); + if(!*tc) + { /* Disable */ + if(TLSSET((*p)->mycache, (void *)-1)) abort(); + *mymspace=0; + } + else + *mymspace=(*tc)->mymspace; + } + else + { + *tc=0; + *mymspace=-mycache-1; + } + assert(*mymspace>=0); + assert((long)(size_t)CURRENT_THREAD==(*tc)->threadid); +#ifdef FULLSANITYCHECKS + if(*tc) + { + if(*(unsigned int *)"NEDMALC1"!=(*tc)->magic1 || *(unsigned int *)"NEDMALC2"!=(*tc)->magic2) + { + abort(); + } + } +#endif +} + +void * nedpmalloc(nedpool *p, size_t size) THROWSPEC +{ + void *ret=0; + threadcache *tc; + int mymspace; + GetThreadCache(&p, &tc, &mymspace, &size); +#if THREADCACHEMAX + if(tc && size<=THREADCACHEMAX) + { /* Use the thread cache */ + ret=threadcache_malloc(p, tc, &size); + } +#endif + if(!ret) + { /* Use this thread's mspace */ + GETMSPACE(m, p, tc, mymspace, size, + ret=mspace_malloc(m, size)); + } + return ret; +} +void * nedpcalloc(nedpool *p, size_t no, size_t size) THROWSPEC +{ + size_t rsize=size*no; + void *ret=0; + threadcache *tc; + int mymspace; + GetThreadCache(&p, &tc, &mymspace, &rsize); +#if THREADCACHEMAX + if(tc && rsize<=THREADCACHEMAX) + { /* Use the thread cache */ + if((ret=threadcache_malloc(p, tc, &rsize))) + memset(ret, 0, rsize); + } +#endif + if(!ret) + { /* Use this thread's mspace */ + GETMSPACE(m, p, tc, mymspace, rsize, + ret=mspace_calloc(m, 1, rsize)); + } + return ret; +} +void * nedprealloc(nedpool *p, void *mem, size_t size) THROWSPEC +{ + void *ret=0; + threadcache *tc; + int mymspace; + if(!mem) return nedpmalloc(p, size); + GetThreadCache(&p, &tc, &mymspace, &size); +#if THREADCACHEMAX + if(tc && size && size<=THREADCACHEMAX) + { /* Use the thread cache */ + size_t memsize=nedblksize(mem); + assert(memsize); + if((ret=threadcache_malloc(p, tc, &size))) + { + memcpy(ret, mem, memsizem[n]; n++) + { + struct mallinfo t=mspace_mallinfo(p->m[n]); + ret.arena+=t.arena; + ret.ordblks+=t.ordblks; + ret.hblkhd+=t.hblkhd; + ret.usmblks+=t.usmblks; + ret.uordblks+=t.uordblks; + ret.fordblks+=t.fordblks; + ret.keepcost+=t.keepcost; + } + return ret; +} +#endif +int nedpmallopt(nedpool *p, int parno, int value) THROWSPEC +{ + return mspace_mallopt(parno, value); +} +int nedpmalloc_trim(nedpool *p, size_t pad) THROWSPEC +{ + int n, ret=0; + if(!p) { p=&syspool; if(!syspool.threads) InitPool(&syspool, 0, -1); } + for(n=0; p->m[n]; n++) + { + ret+=mspace_trim(p->m[n], pad); + } + return ret; +} +void nedpmalloc_stats(nedpool *p) THROWSPEC +{ + int n; + if(!p) { p=&syspool; if(!syspool.threads) InitPool(&syspool, 0, -1); } + for(n=0; p->m[n]; n++) + { + mspace_malloc_stats(p->m[n]); + } +} +size_t nedpmalloc_footprint(nedpool *p) THROWSPEC +{ + size_t ret=0; + int n; + if(!p) { p=&syspool; if(!syspool.threads) InitPool(&syspool, 0, -1); } + for(n=0; p->m[n]; n++) + { + ret+=mspace_footprint(p->m[n]); + } + return ret; +} +void **nedpindependent_calloc(nedpool *p, size_t elemsno, size_t elemsize, void **chunks) THROWSPEC +{ + void **ret; + threadcache *tc; + int mymspace; + GetThreadCache(&p, &tc, &mymspace, &elemsize); + GETMSPACE(m, p, tc, mymspace, elemsno*elemsize, + ret=mspace_independent_calloc(m, elemsno, elemsize, chunks)); + return ret; +} +void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **chunks) THROWSPEC +{ + void **ret; + threadcache *tc; + int mymspace; + size_t i, *adjustedsizes=(size_t *) alloca(elems*sizeof(size_t)); + if(!adjustedsizes) return 0; + for(i=0; i /* for size_t */ + +#ifndef EXTSPEC + #define EXTSPEC extern +#endif + +#if defined(_MSC_VER) && _MSC_VER>=1400 + #define MALLOCATTR __declspec(restrict) +#endif +#ifdef __GNUC__ + #define MALLOCATTR __attribute__ ((malloc)) +#endif +#ifndef MALLOCATTR + #define MALLOCATTR +#endif + +#ifdef REPLACE_SYSTEM_ALLOCATOR + #define nedmalloc malloc + #define nedcalloc calloc + #define nedrealloc realloc + #define nedfree free + #define nedmemalign memalign + #define nedmallinfo mallinfo + #define nedmallopt mallopt + #define nedmalloc_trim malloc_trim + #define nedmalloc_stats malloc_stats + #define nedmalloc_footprint malloc_footprint + #define nedindependent_calloc independent_calloc + #define nedindependent_comalloc independent_comalloc + #ifdef _MSC_VER + #define nedblksize _msize + #endif +#endif + +#ifndef NO_MALLINFO +#define NO_MALLINFO 0 +#endif + +#if !NO_MALLINFO +struct mallinfo; +#endif + +#if defined(__cplusplus) + #if !defined(NO_NED_NAMESPACE) +namespace nedalloc { + #else +extern "C" { + #endif + #define THROWSPEC throw() +#else + #define THROWSPEC +#endif + +/* These are the global functions */ + +/* Gets the usable size of an allocated block. Note this will always be bigger than what was +asked for due to rounding etc. +*/ +EXTSPEC size_t nedblksize(void *mem) THROWSPEC; + +EXTSPEC void nedsetvalue(void *v) THROWSPEC; + +EXTSPEC MALLOCATTR void * nedmalloc(size_t size) THROWSPEC; +EXTSPEC MALLOCATTR void * nedcalloc(size_t no, size_t size) THROWSPEC; +EXTSPEC MALLOCATTR void * nedrealloc(void *mem, size_t size) THROWSPEC; +EXTSPEC void nedfree(void *mem) THROWSPEC; +EXTSPEC MALLOCATTR void * nedmemalign(size_t alignment, size_t bytes) THROWSPEC; +#if !NO_MALLINFO +EXTSPEC struct mallinfo nedmallinfo(void) THROWSPEC; +#endif +EXTSPEC int nedmallopt(int parno, int value) THROWSPEC; +EXTSPEC int nedmalloc_trim(size_t pad) THROWSPEC; +EXTSPEC void nedmalloc_stats(void) THROWSPEC; +EXTSPEC size_t nedmalloc_footprint(void) THROWSPEC; +EXTSPEC MALLOCATTR void **nedindependent_calloc(size_t elemsno, size_t elemsize, void **chunks) THROWSPEC; +EXTSPEC MALLOCATTR void **nedindependent_comalloc(size_t elems, size_t *sizes, void **chunks) THROWSPEC; + +/* These are the pool functions */ +struct nedpool_t; +typedef struct nedpool_t nedpool; + +/* Creates a memory pool for use with the nedp* functions below. +Capacity is how much to allocate immediately (if you know you'll be allocating a lot +of memory very soon) which you can leave at zero. Threads specifies how many threads +will *normally* be accessing the pool concurrently. Setting this to zero means it +extends on demand, but be careful of this as it can rapidly consume system resources +where bursts of concurrent threads use a pool at once. +*/ +EXTSPEC MALLOCATTR nedpool *nedcreatepool(size_t capacity, int threads) THROWSPEC; + +/* Destroys a memory pool previously created by nedcreatepool(). +*/ +EXTSPEC void neddestroypool(nedpool *p) THROWSPEC; + +/* Sets a value to be associated with a pool. You can retrieve this value by passing +any memory block allocated from that pool. +*/ +EXTSPEC void nedpsetvalue(nedpool *p, void *v) THROWSPEC; +/* Gets a previously set value using nedpsetvalue() or zero if memory is unknown. +Optionally can also retrieve pool. +*/ +EXTSPEC void *nedgetvalue(nedpool **p, void *mem) THROWSPEC; + +/* Disables the thread cache for the calling thread, returning any existing cache +data to the central pool. +*/ +EXTSPEC void neddisablethreadcache(nedpool *p) THROWSPEC; + +EXTSPEC MALLOCATTR void * nedpmalloc(nedpool *p, size_t size) THROWSPEC; +EXTSPEC MALLOCATTR void * nedpcalloc(nedpool *p, size_t no, size_t size) THROWSPEC; +EXTSPEC MALLOCATTR void * nedprealloc(nedpool *p, void *mem, size_t size) THROWSPEC; +EXTSPEC void nedpfree(nedpool *p, void *mem) THROWSPEC; +EXTSPEC MALLOCATTR void * nedpmemalign(nedpool *p, size_t alignment, size_t bytes) THROWSPEC; +#if !NO_MALLINFO +EXTSPEC struct mallinfo nedpmallinfo(nedpool *p) THROWSPEC; +#endif +EXTSPEC int nedpmallopt(nedpool *p, int parno, int value) THROWSPEC; +EXTSPEC int nedpmalloc_trim(nedpool *p, size_t pad) THROWSPEC; +EXTSPEC void nedpmalloc_stats(nedpool *p) THROWSPEC; +EXTSPEC size_t nedpmalloc_footprint(nedpool *p) THROWSPEC; +EXTSPEC MALLOCATTR void **nedpindependent_calloc(nedpool *p, size_t elemsno, size_t elemsize, void **chunks) THROWSPEC; +EXTSPEC MALLOCATTR void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **chunks) THROWSPEC; + +#if defined(__cplusplus) +} +#endif + +#undef MALLOCATTR +#undef EXTSPEC + +#endif From 1afce7945356c2b23caa346ef2e5d9aefb43f458 Mon Sep 17 00:00:00 2001 From: Marius Storm-Olsen Date: Wed, 8 Apr 2009 23:01:47 +0200 Subject: [PATCH 218/253] MinGW readdir reimplementation to support d_type The original readdir implementation was fast, but didn't support the d_type. This means that git would do additional lstats for each entry, to figure out if the entry was a directory or not. This unneedingly slowed down many operations, since Windows API provides this information directly when walking the directories. By running this implementation on Moe's repo structure: mkdir bummer && cd bummer; for ((i=0;i<100;i++)); do mkdir $i && pushd $i; for ((j=0;j<1000;j++)); do echo "$j" >$j; done; popd; done We see the following speedups: git add . ------------------- old: 00:00:23(.087) new: 00:00:21(.512) 1.07x git status ------------------- old: 00:00:03(.306) new: 00:00:01(.684) 1.96x git clean -dxf ------------------- old: 00:00:01(.918) new: 00:00:00(.295) 6.50x Signed-off-by: Marius Storm-Olsen --- compat/mingw.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ compat/mingw.h | 29 +++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 2ab5bbec1f..2951f33d43 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1172,3 +1172,62 @@ char *getpass(const char *prompt) fputs("\n", stderr); return strbuf_detach(&buf, NULL); } + +#ifndef NO_MINGW_REPLACE_READDIR +/* MinGW readdir implementation to avoid extra lstats for Git */ +struct mingw_DIR +{ + struct _finddata_t dd_dta; /* disk transfer area for this dir */ + struct mingw_dirent dd_dir; /* Our own implementation, including d_type */ + long dd_handle; /* _findnext handle */ + int dd_stat; /* 0 = next entry to read is first entry, -1 = off the end, positive = 0 based index of next entry */ + char dd_name[1]; /* given path for dir with search pattern (struct is extended) */ +}; + +struct dirent *mingw_readdir(DIR *dir) +{ + WIN32_FIND_DATAA buf; + HANDLE handle; + struct mingw_DIR *mdir = (struct mingw_DIR*)dir; + + if (!dir->dd_handle) { + errno = EBADF; /* No set_errno for mingw */ + return NULL; + } + + if (dir->dd_handle == (long)INVALID_HANDLE_VALUE && dir->dd_stat == 0) + { + handle = FindFirstFileA(dir->dd_name, &buf); + DWORD lasterr = GetLastError(); + dir->dd_handle = (long)handle; + if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES)) { + errno = err_win_to_posix(lasterr); + return NULL; + } + } else if (dir->dd_handle == (long)INVALID_HANDLE_VALUE) { + return NULL; + } else if (!FindNextFileA((HANDLE)dir->dd_handle, &buf)) { + DWORD lasterr = GetLastError(); + FindClose((HANDLE)dir->dd_handle); + dir->dd_handle = (long)INVALID_HANDLE_VALUE; + /* POSIX says you shouldn't set errno when readdir can't + find any more files; so, if another error we leave it set. */ + if (lasterr != ERROR_NO_MORE_FILES) + errno = err_win_to_posix(lasterr); + return NULL; + } + + /* We get here if `buf' contains valid data. */ + strcpy(dir->dd_dir.d_name, buf.cFileName); + ++dir->dd_stat; + + /* Set file type, based on WIN32_FIND_DATA */ + mdir->dd_dir.d_type = 0; + if (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + mdir->dd_dir.d_type |= DT_DIR; + else + mdir->dd_dir.d_type |= DT_REG; + + return (struct dirent*)&dir->dd_dir; +} +#endif // !NO_MINGW_REPLACE_READDIR diff --git a/compat/mingw.h b/compat/mingw.h index b1156b865e..1b91399f40 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -233,3 +233,32 @@ int main(int argc, const char **argv) \ return mingw_main(argc, argv); \ } \ static int mingw_main(c,v) + +#ifndef NO_MINGW_REPLACE_READDIR +/* + * A replacement of readdir, to ensure that it reads the file type at + * the same time. This avoid extra unneeded lstats in git on MinGW + */ +#undef DT_UNKNOWN +#undef DT_DIR +#undef DT_REG +#undef DT_LNK +#define DT_UNKNOWN 0 +#define DT_DIR 1 +#define DT_REG 2 +#define DT_LNK 3 + +struct mingw_dirent +{ + long d_ino; /* Always zero. */ + union { + unsigned short d_reclen; /* Always zero. */ + unsigned char d_type; /* Reimplementation adds this */ + }; + unsigned short d_namlen; /* Length of name in d_name. */ + char d_name[FILENAME_MAX]; /* File name. */ +}; +#define dirent mingw_dirent +#define readdir(x) mingw_readdir(x) +struct dirent *mingw_readdir(DIR *dir); +#endif // !NO_MINGW_REPLACE_READDIR From c893961d488d8e3ecb7dbcfdb3591bc151b4c9bf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 4 May 2009 16:45:46 +0200 Subject: [PATCH 219/253] test-chmtime: work around Windows limitation Windows has problems changing the mtime when the file is write protected, even by the owner of said file. Add a Windows-only workaround to change the mode if necessary before trying to change the mtime. Signed-off-by: Johannes Schindelin --- test-chmtime.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test-chmtime.c b/test-chmtime.c index d5358cbaac..fe476cb618 100644 --- a/test-chmtime.c +++ b/test-chmtime.c @@ -87,6 +87,15 @@ int main(int argc, const char *argv[]) return -1; } +#ifdef WIN32 + if (!(sb.st_mode & S_IWUSR) && + chmod(argv[i], sb.st_mode | S_IWUSR)) { + fprintf(stderr, "Could not make user-writable %s: %s", + argv[i], strerror(errno)); + return -1; + } +#endif + utb.actime = sb.st_atime; utb.modtime = set_eq ? set_time : sb.st_mtime + set_time; From 4f372b5e306c8b0745d8fced630d073d2acb4c74 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 7 May 2009 14:30:33 +0200 Subject: [PATCH 220/253] Fix warnings in nedmalloc when compiling with GCC 4.4.0 Signed-off-by: Johannes Schindelin --- compat/nedmalloc/malloc.c.h | 4 +++- compat/nedmalloc/nedmalloc.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compat/nedmalloc/malloc.c.h b/compat/nedmalloc/malloc.c.h index 1d9bbe0aba..678beb821a 100644 --- a/compat/nedmalloc/malloc.c.h +++ b/compat/nedmalloc/malloc.c.h @@ -1270,7 +1270,9 @@ int mspace_mallopt(int, int); /*------------------------------ internal #includes ---------------------- */ #ifdef WIN32 +#ifndef __GNUC__ #pragma warning( disable : 4146 ) /* no "unsigned" warnings */ +#endif #endif /* WIN32 */ #include /* for printing in malloc_stats */ @@ -2541,7 +2543,7 @@ struct malloc_params { static struct malloc_params mparams; /* Ensure mparams initialized */ -#define ensure_initialization() (mparams.magic != 0 || init_mparams()) +#define ensure_initialization() if (mparams.magic == 0) init_mparams() #if !ONLY_MSPACES diff --git a/compat/nedmalloc/nedmalloc.c b/compat/nedmalloc/nedmalloc.c index a381a7df36..60a40934af 100644 --- a/compat/nedmalloc/nedmalloc.c +++ b/compat/nedmalloc/nedmalloc.c @@ -34,7 +34,7 @@ DEALINGS IN THE SOFTWARE. /*#define FULLSANITYCHECKS*/ #include "nedmalloc.h" -#if defined(WIN32) && !defined(__MINGW32__) +#if defined(WIN32) #include #endif #define MSPACES 1 From 5dae887ff1468189d8c900440e30db97d03d8f12 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 7 May 2009 14:31:17 +0200 Subject: [PATCH 221/253] MinGW: Fix compiler warning in merge-recursive GCC 4.4.0 on Windows does not like the format %zu. It is quite unlikely, though, that we need more merge bases than a %d can display, so replace the %zu by a %d. Signed-off-by: Johannes Schindelin --- builtin-merge-recursive.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/builtin-merge-recursive.c b/builtin-merge-recursive.c index 703045bfc8..d26a96e486 100644 --- a/builtin-merge-recursive.c +++ b/builtin-merge-recursive.c @@ -45,8 +45,9 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix) bases[bases_count++] = sha; } else - warning("Cannot handle more than %zu bases. " - "Ignoring %s.", ARRAY_SIZE(bases)-1, argv[i]); + warning("Cannot handle more than %d bases. " + "Ignoring %s.", + (int)ARRAY_SIZE(bases)-1, argv[i]); } if (argc - i != 3) /* "--" "" "" */ die("Not handling anything other than two heads merge."); From 3c0d7263c5d0fd4c7fd7db50b189a2bc1959ac0f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 7 May 2009 14:45:59 +0200 Subject: [PATCH 222/253] MinGW: declare getpass() Signed-off-by: Johannes Schindelin --- compat/mingw.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compat/mingw.h b/compat/mingw.h index 1b91399f40..4f7ba4c13f 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -38,6 +38,8 @@ struct passwd { char *pw_dir; }; +extern char *getpass(const char *prompt); + struct pollfd { int fd; /* file descriptor */ short events; /* requested events */ From d9215c962be9e94721f6a5565da46891a157f249 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 7 May 2009 14:46:48 +0200 Subject: [PATCH 223/253] winansi: fix compile warnings Signed-off-by: Johannes Schindelin --- compat/win32.h | 1 + compat/winansi.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/compat/win32.h b/compat/win32.h index c26384e595..d531130b12 100644 --- a/compat/win32.h +++ b/compat/win32.h @@ -1,5 +1,6 @@ /* common Win32 functions for MinGW and Cygwin */ #include +#include static inline int file_attr_to_st_mode (DWORD attr) { diff --git a/compat/winansi.c b/compat/winansi.c index 4bee335f9e..9217c24b43 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -80,7 +80,7 @@ static void set_console_attr(void) static void erase_in_line(void) { CONSOLE_SCREEN_BUFFER_INFO sbi; - long dummy; /* Needed for Windows 7 (or Vista) regression */ + DWORD dummy; /* Needed for Windows 7 (or Vista) regression */ if (!console) return; From 9b69cf132266161bbb96a705df15808f3ae3e56f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 7 May 2009 14:47:21 +0200 Subject: [PATCH 224/253] Shut up GCC 4.4.0 GCC babbles something about "type-punned" pointers. Just add an extra layer of casting to avoid the warning. Signed-off-by: Johannes Schindelin --- decorate.c | 3 ++- object.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/decorate.c b/decorate.c index 82d9e221ea..a216001dd8 100644 --- a/decorate.c +++ b/decorate.c @@ -8,7 +8,8 @@ static unsigned int hash_obj(const struct object *obj, unsigned int n) { - unsigned int hash = *(unsigned int *)obj->sha1; + const void *p = obj->sha1; + unsigned int hash = *(const unsigned int *)p; return hash % n; } diff --git a/object.c b/object.c index 7e6a92c88e..ce294241d3 100644 --- a/object.c +++ b/object.c @@ -45,7 +45,8 @@ int type_from_string(const char *str) static unsigned int hash_obj(struct object *obj, unsigned int n) { - unsigned int hash = *(unsigned int *)obj->sha1; + const void *p = obj->sha1; + unsigned int hash = *(const unsigned int *)p; return hash % n; } From fdffdb2ddbf05442dd2662937e62258991e4b28c Mon Sep 17 00:00:00 2001 From: Pat Thoyts Date: Mon, 18 May 2009 12:42:41 +0100 Subject: [PATCH 225/253] git: browsing paths with spaces when using the start command msysGit issue 258 tracks a problem opening a browser onto file paths that contain spaces or parentheses when calling the web--browse script. This path modifies how the start command is called to solve this. Signed-off-by: Pat Thoyts --- git-web--browse.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/git-web--browse.sh b/git-web--browse.sh index 7ed0faddcd..4f5c740df5 100755 --- a/git-web--browse.sh +++ b/git-web--browse.sh @@ -161,9 +161,12 @@ case "$browser" in ;; esac ;; - w3m|links|lynx|open|start) + w3m|links|lynx|open) eval "$browser_path" "$@" ;; + start) + exec "$browser_path" '"web-browse"' "$@" + ;; dillo) "$browser_path" "$@" & ;; From b078c82afda34beb2fa33f7227cc3e10ac3db226 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 18 May 2009 14:53:34 +0200 Subject: [PATCH 226/253] Error out rather than hang when plink does not know the host yet As reported in msysGit issue 96, plink wants to interact with the user asking if a host key should be accepted, but this just blocks the terminal, as plink tries to get the answer from stdin. However, stdin is already connected to Git that wants to send input to the remote command. Rather than blocking, just call plink with -batch so that it will error out with an error message. Signed-off-by: Johannes Schindelin --- connect.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/connect.c b/connect.c index b3c4d22050..db7020939a 100644 --- a/connect.c +++ b/connect.c @@ -602,13 +602,15 @@ struct child_process *git_connect(int fd[2], const char *url_orig, die("command line too long"); conn->in = conn->out = -1; - conn->argv = arg = xcalloc(6, sizeof(*arg)); + conn->argv = arg = xcalloc(7, sizeof(*arg)); if (protocol == PROTO_SSH) { const char *ssh = getenv("GIT_SSH"); int putty = ssh && strstr(ssh, "plink"); if (!ssh) ssh = "ssh"; *arg++ = ssh; + if (putty) + *arg++ = "-batch"; if (port) { /* P is for PuTTY, p is for OpenSSH */ *arg++ = putty ? "-P" : "-p"; From ac3f43986a2a5948d474adb21cd5fde78178d367 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 20 May 2009 13:42:04 +0200 Subject: [PATCH 227/253] Correctly detect Plink.exe, and avoid passing -batch to TortoisePlink On Windows, file names are case-insensitive, and some versions of TortoisePlink do not handle -batch well (i.e. they do not understand, neither ignore it). Signed-off-by: Johannes Schindelin --- connect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connect.c b/connect.c index db7020939a..692d476578 100644 --- a/connect.c +++ b/connect.c @@ -605,11 +605,11 @@ struct child_process *git_connect(int fd[2], const char *url_orig, conn->argv = arg = xcalloc(7, sizeof(*arg)); if (protocol == PROTO_SSH) { const char *ssh = getenv("GIT_SSH"); - int putty = ssh && strstr(ssh, "plink"); + int putty = ssh && strcasestr(ssh, "plink"); if (!ssh) ssh = "ssh"; *arg++ = ssh; - if (putty) + if (putty && !strcasestr(ssh, "tortoiseplink")) *arg++ = "-batch"; if (port) { /* P is for PuTTY, p is for OpenSSH */ From 5fb08d69b146757b7bfbfb20c7c002b496e867d4 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sat, 23 May 2009 10:49:17 +0200 Subject: [PATCH 228/253] MinGW: Teach Makefile to detect msysgit This commit changes handling of the msysgit specific settings, so that they can be applied to Junio's git.git. Some msysgit settings differ from the standard MinGW settings. We move them into a ifndef block that is only evaluated if a file THIS_IS_MSYSGIT is present in the parent directory, which will be the case for an msysgit working environment, and we assume that such a file is not present accidentally. Signed-off-by: Steffen Prohaska --- Makefile | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 52e190be35..e4fa9a4ee2 100644 --- a/Makefile +++ b/Makefile @@ -817,9 +817,6 @@ ifneq (,$(findstring CYGWIN,$(uname_S))) endif ifneq (,$(findstring MINGW,$(uname_S))) pathsep = ; - prefix = - INSTALL = /bin/install - EXTLIBS += /mingw/lib/libz.a NO_PREAD = YesPlease NO_OPENSSL = YesPlease NO_SYMLINK_HEAD = YesPlease @@ -838,8 +835,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) SNPRINTF_RETURNS_BOGUS = YesPlease NO_SVN_TESTS = YesPlease NO_PERL_MAKEMAKER = YesPlease - NO_R_TO_GCC_LINKER = YesPlease - INTERNAL_QSORT = YesPlease RUNTIME_PREFIX = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease @@ -853,7 +848,16 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o EXTLIBS += -lws2_32 X = .exe +ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) htmldir=doc/git/html/ + prefix = + INSTALL = /bin/install + EXTLIBS += /mingw/lib/libz.a + NO_R_TO_GCC_LINKER = YesPlease + INTERNAL_QSORT = YesPlease +else + NO_CURL = YesPlease +endif endif ifneq (,$(findstring arm,$(uname_M))) ARM_SHA1 = YesPlease From e564daea9217d848b5869f5e47723890934ae8a7 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 24 May 2009 08:38:21 +0200 Subject: [PATCH 229/253] Fix "MinGW: GCC >= 4 does not need SNPRINTF_SIZE_CORR anymore" The commit message of 3a76fdaf98efbb1dc2f71352c811ab6d2710b74f explains that GCC >= 4 does not need SNPRINTF_SIZE_CORR, but the ifdef tested for < 3, instead of < 4. This is fixed. Thanks to Johannes Sixt for spotting the bug. --- compat/snprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/snprintf.c b/compat/snprintf.c index 7d780508a7..6c0fb056a5 100644 --- a/compat/snprintf.c +++ b/compat/snprintf.c @@ -6,7 +6,7 @@ * number of characters to write without the trailing NUL. */ #ifndef SNPRINTF_SIZE_CORR -#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ < 3 +#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ < 4 #define SNPRINTF_SIZE_CORR 1 #else #define SNPRINTF_SIZE_CORR 0 From b2bab8d91b346abf4a4603d443df2dce7b62301f Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 25 May 2009 06:44:26 +0200 Subject: [PATCH 230/253] Revert "Renaming .gitk-new to .gitk fails if there is already a .gitk." This reverts commit 3a8b35c3e37369014dc06c767e8fa569150a1e59 A quick test showed that this is unnecessary. Unfortunately, the commit message does not explain the specific reason for the patch. It could be issues with FAT or Windows shares. We hope that this is fixed since we switched to Tcl/Tk 8.5.5. --- gitk-git/gitk | 1 - 1 file changed, 1 deletion(-) diff --git a/gitk-git/gitk b/gitk-git/gitk index a5f77e2f87..1a7887b252 100644 --- a/gitk-git/gitk +++ b/gitk-git/gitk @@ -2568,7 +2568,6 @@ proc savestuff {w} { } puts $f "}" close $f - catch {file delete "~/.gitk"} file rename -force "~/.gitk-new" "~/.gitk" } set stuffsaved 1 From ef407fda650d652d554e385ec4604c88d39949d4 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 25 May 2009 06:56:11 +0200 Subject: [PATCH 231/253] Enable THREADED_DELTA_SEARCH only for msysgit, but not mingw in general --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e4fa9a4ee2..1f034e3a91 100644 --- a/Makefile +++ b/Makefile @@ -826,7 +826,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease NO_MEMMEM = YesPlease - THREADED_DELTA_SEARCH = YesPlease NEEDS_LIBICONV = YesPlease OLD_ICONV = YesPlease NO_C99_FORMAT = YesPlease @@ -855,8 +854,10 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) EXTLIBS += /mingw/lib/libz.a NO_R_TO_GCC_LINKER = YesPlease INTERNAL_QSORT = YesPlease + THREADED_DELTA_SEARCH = YesPlease else NO_CURL = YesPlease + NO_PTHREADS = YesPlease endif endif ifneq (,$(findstring arm,$(uname_M))) From a464b3e17abc16b3ece8fa8c4b4e98a499873b45 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 1 Jun 2009 08:15:12 +0200 Subject: [PATCH 232/253] Move conio.h from win32.h to mingw.c We need conio.h to avoid a warning about the implicit declaration of _getch(). Including it in mingw.c is sufficient. --- compat/mingw.c | 1 + compat/win32.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index d85d68065e..bed417875e 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1,5 +1,6 @@ #include "../git-compat-util.h" #include "win32.h" +#include #include "../strbuf.h" unsigned int _CRT_fmode = _O_BINARY; diff --git a/compat/win32.h b/compat/win32.h index d531130b12..c26384e595 100644 --- a/compat/win32.h +++ b/compat/win32.h @@ -1,6 +1,5 @@ /* common Win32 functions for MinGW and Cygwin */ #include -#include static inline int file_attr_to_st_mode (DWORD attr) { From 65deb66a2d92ef38d2cdee71328b48a22605ad1b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 17 Jun 2009 14:49:39 +0200 Subject: [PATCH 233/253] import-tars: support symlinks Without this patch, symbolic links are turned into empty files. Signed-off-by: Johannes Schindelin --- contrib/fast-import/import-tars.perl | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/contrib/fast-import/import-tars.perl b/contrib/fast-import/import-tars.perl index 6309d146e7..78e40d2a13 100755 --- a/contrib/fast-import/import-tars.perl +++ b/contrib/fast-import/import-tars.perl @@ -82,10 +82,16 @@ foreach my $tar_file (@ARGV) $mtime = oct $mtime; next if $typeflag == 5; # directory - print FI "blob\n", "mark :$next_mark\n", "data $size\n"; - while ($size > 0 && read(I, $_, 512) == 512) { - print FI substr($_, 0, $size); - $size -= 512; + print FI "blob\n", "mark :$next_mark\n"; + if ($typeflag == 2) { # symbolic link + print FI "data ", length($linkname), "\n", $linkname; + $mode = 0120000; + } else { + print FI "data $size\n"; + while ($size > 0 && read(I, $_, 512) == 512) { + print FI substr($_, 0, $size); + $size -= 512; + } } print FI "\n"; @@ -118,7 +124,8 @@ EOF { my ($mark, $mode) = @{$files{$path}}; $path =~ s,^([^/]+)/,, if $have_top_dir; - printf FI "M %o :%i %s\n", $mode & 0111 ? 0755 : 0644, $mark, $path; + $mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000; + printf FI "M %o :%i %s\n", $mode, $mark, $path; } print FI "\n"; From 7336516a6f2ac781b4a78a73f99e41e9e8b66e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Wed, 17 Jun 2009 22:37:31 +0300 Subject: [PATCH 234/253] Refactor winsock initialization into a separate function Signed-off-by: Martin Storsjo --- compat/mingw.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index bed417875e..f570d115c9 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -886,16 +886,25 @@ char **env_setenv(char **env, const char *name) return env; } -/* this is the first function to call into WS_32; initialize it */ -#undef gethostbyname -struct hostent *mingw_gethostbyname(const char *host) +static void ensure_socket_initialization(void) { WSADATA wsa; + static int initialized = 0; + + if (initialized) + return; if (WSAStartup(MAKEWORD(2,2), &wsa)) die("unable to initialize winsock subsystem, error %d", WSAGetLastError()); atexit((void(*)(void)) WSACleanup); + initialized = 1; +} + +#undef gethostbyname +struct hostent *mingw_gethostbyname(const char *host) +{ + ensure_socket_initialization(); return gethostbyname(host); } From bc611ebb9212f5a88446e1b9971e30d7e872feeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Thu, 18 Jun 2009 09:40:22 +0300 Subject: [PATCH 235/253] Enable support for IPv6 on MinGW The IPv6 support functions are loaded dynamically, to maintain backwards compatibility with versions of Windows prior to XP, and fallback wrappers are provided, implemented in terms of gethostbyname and gethostbyaddr. Signed-off-by: Martin Storsjo --- Makefile | 1 - compat/mingw.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++- compat/mingw.h | 13 ++++ 3 files changed, 181 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7a53bb7da1..81d4c8950e 100644 --- a/Makefile +++ b/Makefile @@ -824,7 +824,6 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_PREAD = YesPlease NO_OPENSSL = YesPlease NO_SYMLINK_HEAD = YesPlease - NO_IPV6 = YesPlease NO_SETENV = YesPlease NO_UNSETENV = YesPlease NO_STRCASESTR = YesPlease diff --git a/compat/mingw.c b/compat/mingw.c index f570d115c9..5cda95d084 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -886,10 +886,129 @@ char **env_setenv(char **env, const char *name) return env; } +/* + * Note, this isn't a complete replacement for getaddrinfo. It assumes + * that service contains a numerical port, or that it it is null. It + * does a simple search using gethostbyname, and returns one IPv4 host + * if one was found. + */ +static int WSAAPI getaddrinfo_stub(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) +{ + struct hostent *h = gethostbyname(node); + struct addrinfo *ai; + struct sockaddr_in *sin; + + if (!h) + return WSAGetLastError(); + + ai = xmalloc(sizeof(struct addrinfo)); + *res = ai; + ai->ai_flags = 0; + ai->ai_family = AF_INET; + ai->ai_socktype = hints->ai_socktype; + switch (hints->ai_socktype) { + case SOCK_STREAM: + ai->ai_protocol = IPPROTO_TCP; + break; + case SOCK_DGRAM: + ai->ai_protocol = IPPROTO_UDP; + break; + default: + ai->ai_protocol = 0; + break; + } + ai->ai_addrlen = sizeof(struct sockaddr_in); + ai->ai_canonname = strdup(h->h_name); + + sin = xmalloc(ai->ai_addrlen); + memset(sin, 0, ai->ai_addrlen); + sin->sin_family = AF_INET; + if (service) + sin->sin_port = htons(atoi(service)); + sin->sin_addr = *(struct in_addr *)h->h_addr; + ai->ai_addr = (struct sockaddr *)sin; + ai->ai_next = 0; + return 0; +} + +static void WSAAPI freeaddrinfo_stub(struct addrinfo *res) +{ + free(res->ai_canonname); + free(res->ai_addr); + free(res); +} + +static int WSAAPI getnameinfo_stub(const struct sockaddr *sa, socklen_t salen, + char *host, DWORD hostlen, + char *serv, DWORD servlen, int flags) +{ + const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; + if (sa->sa_family != AF_INET) + return EAI_FAMILY; + if (!host && !serv) + return EAI_NONAME; + + if (host && hostlen > 0) { + struct hostent *ent = NULL; + if (!(flags & NI_NUMERICHOST)) + ent = gethostbyaddr((const char *)&sin->sin_addr, + sizeof(sin->sin_addr), AF_INET); + + if (ent) + snprintf(host, hostlen, "%s", ent->h_name); + else if (flags & NI_NAMEREQD) + return EAI_NONAME; + else + snprintf(host, hostlen, "%s", inet_ntoa(sin->sin_addr)); + } + + if (serv && servlen > 0) { + struct servent *ent = NULL; + if (!(flags & NI_NUMERICSERV)) + ent = getservbyport(sin->sin_port, + flags & NI_DGRAM ? "udp" : "tcp"); + + if (ent) + snprintf(serv, servlen, "%s", ent->s_name); + else + snprintf(serv, servlen, "%d", ntohs(sin->sin_port)); + } + + return 0; +} + +static HMODULE ipv6_dll = NULL; +static void (WSAAPI *ipv6_freeaddrinfo)(struct addrinfo *res); +static int (WSAAPI *ipv6_getaddrinfo)(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res); +static int (WSAAPI *ipv6_getnameinfo)(const struct sockaddr *sa, socklen_t salen, + char *host, DWORD hostlen, + char *serv, DWORD servlen, int flags); +/* + * gai_strerror is an inline function in the ws2tcpip.h header, so we + * don't need to try to load that one dynamically. + */ + +static void socket_cleanup(void) +{ + WSACleanup(); + if (ipv6_dll) + FreeLibrary(ipv6_dll); + ipv6_dll = NULL; + ipv6_freeaddrinfo = freeaddrinfo_stub; + ipv6_getaddrinfo = getaddrinfo_stub; + ipv6_getnameinfo = getnameinfo_stub; +} + static void ensure_socket_initialization(void) { WSADATA wsa; static int initialized = 0; + const char *libraries[] = { "ws2_32.dll", "wship6.dll", NULL }; + const char **name; if (initialized) return; @@ -897,7 +1016,35 @@ static void ensure_socket_initialization(void) if (WSAStartup(MAKEWORD(2,2), &wsa)) die("unable to initialize winsock subsystem, error %d", WSAGetLastError()); - atexit((void(*)(void)) WSACleanup); + + for (name = libraries; *name; name++) { + ipv6_dll = LoadLibrary(*name); + if (!ipv6_dll) + continue; + + ipv6_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *)) + GetProcAddress(ipv6_dll, "freeaddrinfo"); + ipv6_getaddrinfo = (int (WSAAPI *)(const char *, const char *, + const struct addrinfo *, + struct addrinfo **)) + GetProcAddress(ipv6_dll, "getaddrinfo"); + ipv6_getnameinfo = (int (WSAAPI *)(const struct sockaddr *, + socklen_t, char *, DWORD, + char *, DWORD, int)) + GetProcAddress(ipv6_dll, "getnameinfo"); + if (!ipv6_freeaddrinfo || !ipv6_getaddrinfo || !ipv6_getnameinfo) { + FreeLibrary(ipv6_dll); + ipv6_dll = NULL; + } else + break; + } + if (!ipv6_freeaddrinfo || !ipv6_getaddrinfo || !ipv6_getnameinfo) { + ipv6_freeaddrinfo = freeaddrinfo_stub; + ipv6_getaddrinfo = getaddrinfo_stub; + ipv6_getnameinfo = getnameinfo_stub; + } + + atexit(socket_cleanup); initialized = 1; } @@ -908,6 +1055,26 @@ struct hostent *mingw_gethostbyname(const char *host) return gethostbyname(host); } +void mingw_freeaddrinfo(struct addrinfo *res) +{ + ipv6_freeaddrinfo(res); +} + +int mingw_getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, struct addrinfo **res) +{ + ensure_socket_initialization(); + return ipv6_getaddrinfo(node, service, hints, res); +} + +int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen, + char *host, DWORD hostlen, char *serv, DWORD servlen, + int flags) +{ + ensure_socket_initialization(); + return ipv6_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); +} + int mingw_socket(int domain, int type, int protocol) { int sockfd; diff --git a/compat/mingw.h b/compat/mingw.h index 4f7ba4c13f..244ace82e1 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -1,4 +1,5 @@ #include +#include /* * things that are not available in header files @@ -152,6 +153,18 @@ char *mingw_getenv(const char *name); struct hostent *mingw_gethostbyname(const char *host); #define gethostbyname mingw_gethostbyname +void mingw_freeaddrinfo(struct addrinfo *res); +#define freeaddrinfo mingw_freeaddrinfo + +int mingw_getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, struct addrinfo **res); +#define getaddrinfo mingw_getaddrinfo + +int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen, + char *host, DWORD hostlen, char *serv, DWORD servlen, + int flags); +#define getnameinfo mingw_getnameinfo + int mingw_socket(int domain, int type, int protocol); #define socket mingw_socket From 198cb7c3f0b8420d8ba21fc8005b3ce6091e75b6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 26 Jul 2009 05:08:42 +0200 Subject: [PATCH 236/253] Handle http.* config variables pointing to files gracefully on Windows On Windows, we would like to be able to have a default http.sslCAinfo that points to an MSys path (i.e. relative to the installation root of Git). As Git is a MinGW program, it has to handle the conversion of the MSys path into a MinGW32 path itself. Signed-off-by: Johannes Schindelin --- http.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/http.c b/http.c index a2720d576d..a7ebbaf94e 100644 --- a/http.c +++ b/http.c @@ -1,5 +1,6 @@ #include "http.h" #include "pack.h" +#include "exec_cmd.h" int data_received; int active_requests; @@ -129,6 +130,15 @@ static void process_curl_messages(void) } #endif +static int git_config_path(const char **result, + const char *var, const char *value) +{ + if (git_config_string(result, var, value)) + return 1; + *result = system_path(*result); + return 0; +} + static int http_options(const char *var, const char *value, void *cb) { if (!strcmp("http.sslverify", var)) { @@ -136,17 +146,17 @@ static int http_options(const char *var, const char *value, void *cb) return 0; } if (!strcmp("http.sslcert", var)) - return git_config_string(&ssl_cert, var, value); + return git_config_path(&ssl_cert, var, value); #if LIBCURL_VERSION_NUM >= 0x070903 if (!strcmp("http.sslkey", var)) - return git_config_string(&ssl_key, var, value); + return git_config_path(&ssl_key, var, value); #endif #if LIBCURL_VERSION_NUM >= 0x070908 if (!strcmp("http.sslcapath", var)) - return git_config_string(&ssl_capath, var, value); + return git_config_path(&ssl_capath, var, value); #endif if (!strcmp("http.sslcainfo", var)) - return git_config_string(&ssl_cainfo, var, value); + return git_config_path(&ssl_cainfo, var, value); if (!strcmp("http.sslcertpasswordprotected", var)) { if (git_config_bool(var, value)) ssl_cert_password_required = 1; From ed04b5ce213a39f9f0379640d8e4078c0a2635f8 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Mon, 27 Jul 2009 21:05:40 +0200 Subject: [PATCH 237/253] send-email: accept absolute path even on Windows Signed-off-by: Johannes Schindelin --- git-send-email.perl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-send-email.perl b/git-send-email.perl index d508f83349..7a6a82c810 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -898,7 +898,7 @@ X-Mailer: git-send-email $gitversion if ($dry_run) { # We don't want to send the email. - } elsif ($smtp_server =~ m#^/#) { + } elsif ($smtp_server =~ m#^/# || $smtp_server =~ m#[a-zA-Z]\:#) { my $pid = open my $sm, '|-'; defined $pid or die $!; if (!$pid) { From 069b288d1b44ad729eac9a31193e0b17a6ab5f6b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 29 Jul 2009 22:21:55 +0200 Subject: [PATCH 238/253] Skip tests requiring 'iconv' when that is not found Signed-off-by: Johannes Schindelin --- t/t3900-i18n-commit.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/t/t3900-i18n-commit.sh b/t/t3900-i18n-commit.sh index 256c4c9701..a0fb178a67 100755 --- a/t/t3900-i18n-commit.sh +++ b/t/t3900-i18n-commit.sh @@ -64,6 +64,13 @@ test_expect_success 'config to remove customization' ' git config i18n.commitencoding UTF-8 ' +iconv --help 2>/dev/null >/dev/null +test $? = 127 && { + say "No iconv found; skip rest" + test_done + exit +} + test_expect_success 'ISO8859-1 should be shown in UTF-8 now' ' compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt ' From ea20f4081910213be44e2f38360099f961091c2c Mon Sep 17 00:00:00 2001 From: Thorvald Natvig Date: Mon, 10 Aug 2009 12:47:37 +0200 Subject: [PATCH 239/253] Symlink support Add symlink support; with UAC, you may need administrator's privileges to create symlinks, but you can at least read them. As reparse points on Windows differentiate between file and directory links, we just assume that file links are meant for the time being; it might be very hard to determine the type before the target exists, but it is thinkable to change the type on-the-fly. A bridge to cross when we arrive there (read: something for somebody to fix who actually has that problem). Signed-off-by: Johannes Schindelin --- compat/mingw.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++- compat/mingw.h | 6 ++--- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 5cda95d084..20f370b73b 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1,6 +1,7 @@ #include "../git-compat-util.h" #include "win32.h" #include +#include #include "../strbuf.h" unsigned int _CRT_fmode = _O_BINARY; @@ -166,6 +167,21 @@ static int do_lstat(const char *file_name, struct stat *buf) buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { + WIN32_FIND_DATAA findbuf; + HANDLE handle = FindFirstFileA(file_name, &findbuf); + if (handle != INVALID_HANDLE_VALUE) { + if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && + (findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { + char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; + buf->st_mode = S_IREAD | S_IFLNK; + if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) + buf->st_mode |= S_IWRITE; + buf->st_size = readlink(file_name, buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE); + } + FindClose(handle); + } + } return 0; } return -1; @@ -1334,6 +1350,58 @@ int link(const char *oldpath, const char *newpath) return 0; } +int symlink(const char *oldpath, const char *newpath) +{ + typedef BOOL WINAPI (*symlink_fn)(const char*, const char*, DWORD); + static symlink_fn create_symbolic_link = NULL; + if (!create_symbolic_link) { + create_symbolic_link = (symlink_fn) GetProcAddress( + GetModuleHandle("kernel32.dll"), "CreateSymbolicLinkA"); + if (!create_symbolic_link) + create_symbolic_link = (symlink_fn)-1; + } + if (create_symbolic_link == (symlink_fn)-1) { + errno = ENOSYS; + return -1; + } + + if (!create_symbolic_link(newpath, oldpath, 0)) { + errno = err_win_to_posix(GetLastError()); + return -1; + } + return 0; +} + +int readlink(const char *path, char *buf, size_t bufsiz) +{ + HANDLE handle = CreateFile(path, GENERIC_READ, + FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, + NULL, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, + NULL); + + if (handle != INVALID_HANDLE_VALUE) { + unsigned char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; + DWORD dummy = 0; + if (DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { + REPARSE_DATA_BUFFER *b = (REPARSE_DATA_BUFFER *) buffer; + if (b->ReparseTag == IO_REPARSE_TAG_SYMLINK) { + int len = b->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); + int offset = b->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(wchar_t); + snprintf(buf, bufsiz, "%*ls", len, & b->SymbolicLinkReparseBuffer.PathBuffer[offset]); + CloseHandle(handle); + return len; + } + } + + CloseHandle(handle); + } + + errno = EINVAL; + return -1; +} + char *getpass(const char *prompt) { struct strbuf buf = STRBUF_INIT; @@ -1399,7 +1467,10 @@ struct dirent *mingw_readdir(DIR *dir) /* Set file type, based on WIN32_FIND_DATA */ mdir->dd_dir.d_type = 0; - if (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + if ((buf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && + (buf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) + mdir->dd_dir.d_type |= DT_LNK; + else if (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) mdir->dd_dir.d_type |= DT_DIR; else mdir->dd_dir.d_type |= DT_REG; diff --git a/compat/mingw.h b/compat/mingw.h index c76af61617..4f9f7f4887 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -66,10 +66,6 @@ struct itimerval { * trivial stubs */ -static inline int readlink(const char *path, char *buf, size_t bufsiz) -{ errno = ENOSYS; return -1; } -static inline int symlink(const char *oldpath, const char *newpath) -{ errno = ENOSYS; return -1; } static inline int fchmod(int fildes, mode_t mode) { errno = ENOSYS; return -1; } static inline int fork(void) @@ -138,6 +134,8 @@ struct passwd *getpwuid(int uid); int setitimer(int type, struct itimerval *in, struct itimerval *out); int sigaction(int sig, struct sigaction *in, struct sigaction *out); int link(const char *oldpath, const char *newpath); +int symlink(const char *oldpath, const char *newpath); +int readlink(const char *path, char *buf, size_t bufsiz); /* * replacements of existing functions From afb9111bacf3da37d87186057739b3e3fbf044b0 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 11 Aug 2009 18:08:44 +0200 Subject: [PATCH 240/253] Fix style of multi-line comments In git.git, we want multi-line comments between single /* and */ in a line. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 20f370b73b..ed2664dc00 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -147,7 +147,8 @@ static inline time_t filetime_to_time_t(const FILETIME *ft) return (time_t)winTime; } -/* We keep the do_lstat code in a separate function to avoid recursion. +/* + * We keep the do_lstat code in a separate function to avoid recursion. * When a path ends with a slash, the stat will fail with ENOENT. In * this case, we strip the trailing slashes and stat again. */ @@ -187,7 +188,8 @@ static int do_lstat(const char *file_name, struct stat *buf) return -1; } -/* We provide our own lstat/fstat functions, since the provided +/* + * We provide our own lstat/fstat functions, since the provided * lstat/fstat functions are so slow. These stat functions are * tailored for Git's usage (read: fast), and are not meant to be * complete. Note that Git stat()s are redirected to mingw_lstat() @@ -201,7 +203,8 @@ int mingw_lstat(const char *file_name, struct stat *buf) if (!do_lstat(file_name, buf)) return 0; - /* if file_name ended in a '/', Windows returned ENOENT; + /* + * if file_name ended in a '/', Windows returned ENOENT; * try again without trailing slashes */ if (errno != ENOENT) @@ -368,7 +371,8 @@ int poll(struct pollfd *ufds, unsigned int nfds, int timeout) return errno = EINVAL, error("poll timeout not supported"); } - /* When there is only one fd to wait for, then we pretend that + /* + * When there is only one fd to wait for, then we pretend that * input is available and let the actual wait happen when the * caller invokes read(). */ @@ -408,7 +412,8 @@ repeat: ufds[i].revents = 0; } if (!pending) { - /* The only times that we spin here is when the process + /* + * The only times that we spin here is when the process * that is connected through the pipes is waiting for * its own input data to become available. But since * the process (pack-objects) is itself CPU intensive, @@ -663,7 +668,8 @@ static pid_t mingw_spawnve(const char *cmd, const char **argv, char **env, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (cons == INVALID_HANDLE_VALUE) { - /* There is no console associated with this process. + /* + * There is no console associated with this process. * Since the child is a console process, Windows * would normally create a console window. But * since we'll be redirecting std streams, we do @@ -674,7 +680,8 @@ static pid_t mingw_spawnve(const char *cmd, const char **argv, char **env, */ flags = DETACHED_PROCESS; } else { - /* There is already a console. If we specified + /* + * There is already a console. If we specified * DETACHED_PROCESS here, too, Windows would * disassociate the child from the console. * The same is true for CREATE_NO_WINDOW. @@ -874,9 +881,7 @@ static int lookup_env(char **env, const char *name, size_t nmln) return -1; } -/* - * If name contains '=', then sets the variable, otherwise it unsets it - */ +/* If name contains '=', then sets the variable, otherwise it unsets it */ char **env_setenv(char **env, const char *name) { char *eq = strchrnul(name, '='); @@ -1195,7 +1200,8 @@ static int timer_interval; static int one_shot; static sig_handler_t timer_fn = SIG_DFL; -/* The timer works like this: +/* + * The timer works like this: * The thread, ticktack(), is a trivial routine that most of the time * only waits to receive the signal to terminate. The main thread tells * the thread to terminate by setting the timer_event to the signalled @@ -1454,8 +1460,10 @@ struct dirent *mingw_readdir(DIR *dir) DWORD lasterr = GetLastError(); FindClose((HANDLE)dir->dd_handle); dir->dd_handle = (long)INVALID_HANDLE_VALUE; - /* POSIX says you shouldn't set errno when readdir can't - find any more files; so, if another error we leave it set. */ + /* + * POSIX says you shouldn't set errno when readdir can't + * find any more files; so, if another error we leave it set. + */ if (lasterr != ERROR_NO_MORE_FILES) errno = err_win_to_posix(lasterr); return NULL; From 8653dacef71c86e5a95d32eda5240bbafc40d1a6 Mon Sep 17 00:00:00 2001 From: Johan 't Hart Date: Sat, 8 Aug 2009 22:44:01 +0200 Subject: [PATCH 241/253] On Windows, mark directory hidden when starting with dot. In Windows a file or directory starting with a dot is not automatically hidden. So lets mark it as hidden when such a directory is created. This fixes msysGit issue 288. Signed-off-by: Johannes Schindelin Acked-by: Johannes Sixt --- compat/mingw.c | 26 ++++++++++++++++++++++++++ compat/mingw.h | 9 +++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 5cda95d084..c3f5cef22c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -118,6 +118,32 @@ static int err_win_to_posix(DWORD winerr) return error; } +static int make_hidden(const char *path) +{ + DWORD attribs = GetFileAttributes(path); + if (SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN | attribs)) + return 0; + errno = err_win_to_posix(GetLastError()); + return -1; +} + +#undef mkdir +int mingw_mkdir(const char *path, int mode) +{ + int ret = mkdir(path); + if (!ret) { + /* + * In Windows a file or dir starting with a dot is not + * automatically hidden. So lets mark it as hidden when + * such a directory is created. + */ + const char *start = basename((char*)path); + if (*start == '.') + return make_hidden(path); + } + return ret; +} + #undef open int mingw_open (const char *filename, int oflags, ...) { diff --git a/compat/mingw.h b/compat/mingw.h index c76af61617..19a0205f86 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -100,12 +100,6 @@ static inline int fcntl(int fd, int cmd, long arg) * simple adaptors */ -static inline int mingw_mkdir(const char *path, int mode) -{ - return mkdir(path); -} -#define mkdir mingw_mkdir - static inline int mingw_unlink(const char *pathname) { /* read-only files cannot be removed */ @@ -143,6 +137,9 @@ int link(const char *oldpath, const char *newpath); * replacements of existing functions */ +int mingw_mkdir(const char *path, int mode); +#define mkdir mingw_mkdir + int mingw_open (const char *filename, int oflags, ...); #define open mingw_open From 996ff4a9ae8d4db3b5fedea3f1cf03ea0f689035 Mon Sep 17 00:00:00 2001 From: Johan 't Hart Date: Sat, 8 Aug 2009 22:59:27 +0200 Subject: [PATCH 242/253] Make auto-hiding dot-files optional on Windows. Although a file starting with a dot usually ought to be hidden, there could be reasons users do not want it to happen automatically. Original patch by Erik Faye-Lund. Signed-off-by: Johannes Schindelin Acked-by: Johannes Sixt --- cache.h | 1 + compat/mingw.c | 3 ++- config.c | 5 +++++ environment.c | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cache.h b/cache.h index e6c7f3307d..f591437a50 100644 --- a/cache.h +++ b/cache.h @@ -518,6 +518,7 @@ extern size_t delta_base_cache_limit; extern int auto_crlf; extern int fsync_object_files; extern int core_preload_index; +extern int hide_dotfiles; enum safe_crlf { SAFE_CRLF_FALSE = 0, diff --git a/compat/mingw.c b/compat/mingw.c index c3f5cef22c..665d861389 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -130,8 +130,9 @@ static int make_hidden(const char *path) #undef mkdir int mingw_mkdir(const char *path, int mode) { + extern int hide_dotfiles; int ret = mkdir(path); - if (!ret) { + if (!ret && hide_dotfiles) { /* * In Windows a file or dir starting with a dot is not * automatically hidden. So lets mark it as hidden when diff --git a/config.c b/config.c index 738b24419d..fc45ea2198 100644 --- a/config.c +++ b/config.c @@ -505,6 +505,11 @@ static int git_default_core_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.hidedotfiles")) { + hide_dotfiles = git_config_bool(var, value); + return 0; + } + /* Add other config variables here and to Documentation/config.txt. */ return 0; } diff --git a/environment.c b/environment.c index 8f5eaa7dd8..940f3cfa3a 100644 --- a/environment.c +++ b/environment.c @@ -48,6 +48,7 @@ enum push_default_type push_default = PUSH_DEFAULT_MATCHING; #endif enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE; int grafts_replace_parents = 1; +int hide_dotfiles = 1; /* Parallel index stat data preload? */ int core_preload_index = 0; From 5d098f4e5f1e21d802769f16058118ec12cdda25 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 9 Aug 2009 19:08:05 +0200 Subject: [PATCH 243/253] Document the core.hideDotFiles variable Signed-off-by: Johannes Schindelin Acked-by: Johannes Sixt --- Documentation/config.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/config.txt b/Documentation/config.txt index c6f09f801a..7c6f7fcb87 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -118,6 +118,10 @@ core.fileMode:: the working copy are ignored; useful on broken filesystems like FAT. See linkgit:git-update-index[1]. True by default. +core.hideDotFiles:: + (Windows-only) If true (which is the default), mark newly-created + directories whose name starts with a dot as hidden. + core.ignoreCygwinFSTricks:: This option is only used by Cygwin implementation of Git. If false, the Cygwin stat() and lstat() functions are used. This may be useful From 2328ceef54b7b2e40ad66ecd74a3451bf7c3c6d5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 9 Aug 2009 19:12:17 +0200 Subject: [PATCH 244/253] Windows: mark newly-created files whose name starts with a dot as hidden Signed-off-by: Johannes Schindelin Acked-by: Johannes Sixt --- Documentation/config.txt | 2 +- compat/mingw.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 7c6f7fcb87..30ab22b840 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -120,7 +120,7 @@ core.fileMode:: core.hideDotFiles:: (Windows-only) If true (which is the default), mark newly-created - directories whose name starts with a dot as hidden. + directories and files whose name starts with a dot as hidden. core.ignoreCygwinFSTricks:: This option is only used by Cygwin implementation of Git. If false, diff --git a/compat/mingw.c b/compat/mingw.c index 665d861389..a68ef11696 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3,6 +3,7 @@ #include #include "../strbuf.h" +extern int hide_dotfiles; unsigned int _CRT_fmode = _O_BINARY; static int err_win_to_posix(DWORD winerr) @@ -130,7 +131,6 @@ static int make_hidden(const char *path) #undef mkdir int mingw_mkdir(const char *path, int mode) { - extern int hide_dotfiles; int ret = mkdir(path); if (!ret && hide_dotfiles) { /* @@ -162,6 +162,16 @@ int mingw_open (const char *filename, int oflags, ...) if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) errno = EISDIR; } + if ((oflags & O_CREAT) && fd >= 0 && hide_dotfiles) { + /* + * In Windows a file or dir starting with a dot is not + * automatically hidden. So lets mark it as hidden when + * such a file is created. + */ + const char *start = basename((char*)filename); + if (*start == '.' && make_hidden(filename)) + warning("Could not mark '%s' as hidden.", filename); + } return fd; } From 4863902f4f89d00a7fa916d11cf24fe1cb9b8160 Mon Sep 17 00:00:00 2001 From: Johan 't Hart Date: Wed, 12 Aug 2009 23:40:50 +0200 Subject: [PATCH 245/253] Windows: mark newly-created dot files by fopen() as hidden Files starting with a dot are considered "hidden", i.e. you have to ask explicitely to see them. On Windows, this is not done automatically, as Windows has its own mechanism. We already mark dot directories created by mkdir() and dot files created by open() as hidden, so let's do that with fopen(), too (but only if the file was just created, not when it already exists). Naturally, we make this behavior optional on core.hideDotFiles. Signed-off-by: Johannes Schindelin Acked-by: Johannes Sixt --- compat/mingw.c | 18 ++++++++++++++++++ compat/mingw.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index a68ef11696..293bd5999d 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -175,6 +175,24 @@ int mingw_open (const char *filename, int oflags, ...) return fd; } +#undef fopen +FILE *mingw_fopen (const char *filename, const char *mode) +{ + int hide = 0; + if (hide_dotfiles && basename((char*)filename)[0] == '.') + hide = access(filename, F_OK); + + FILE *file = fopen(filename, mode); + /* + * In Windows a file or dir starting with a dot is not + * automatically hidden. So lets mark it as hidden when + * such a file is created. + */ + if (file && hide && make_hidden(filename)) + warning("Could not mark '%s' as hidden.", filename); + return file; +} + static inline time_t filetime_to_time_t(const FILETIME *ft) { long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; diff --git a/compat/mingw.h b/compat/mingw.h index 19a0205f86..8fe1773974 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -143,6 +143,9 @@ int mingw_mkdir(const char *path, int mode); int mingw_open (const char *filename, int oflags, ...); #define open mingw_open +FILE *mingw_fopen (const char *filename, const char *mode); +#define fopen mingw_fopen + char *mingw_getcwd(char *pointer, int len); #define getcwd mingw_getcwd From 3c150abb1016cef67dfa2644578dbca8700e280c Mon Sep 17 00:00:00 2001 From: Frank Li Date: Sun, 16 Aug 2009 13:34:03 +0800 Subject: [PATCH 246/253] Avoid declaration after instruction Microsoft Visual C++ does not understand this C99 style. Signed-off-by: Frank Li Signed-off-by: Johannes Schindelin --- compat/mingw.c | 16 ++++++++++++---- help.c | 3 ++- run-command.c | 2 ++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 62983d0e32..d262e1c226 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -151,13 +151,17 @@ int mingw_open (const char *filename, int oflags, ...) { va_list args; unsigned mode; + int fd; + va_start(args, oflags); mode = va_arg(args, int); va_end(args); if (!strcmp(filename, "/dev/null")) filename = "nul"; - int fd = open(filename, oflags, mode); + + fd = open(filename, oflags, mode); + if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) { DWORD attrs = GetFileAttributes(filename); if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) @@ -656,10 +660,11 @@ static char **get_path_split(void) static void free_path_split(char **path) { + char **p = path; + if (!path) return; - char **p = path; while (*p) free(*p++); free(path); @@ -1361,9 +1366,11 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out) #undef signal sig_handler_t mingw_signal(int sig, sig_handler_t handler) { + sig_handler_t old; + if (sig != SIGALRM) return signal(sig, handler); - sig_handler_t old = timer_fn; + old = timer_fn; timer_fn = handler; return old; } @@ -1502,8 +1509,9 @@ struct dirent *mingw_readdir(DIR *dir) if (dir->dd_handle == (long)INVALID_HANDLE_VALUE && dir->dd_stat == 0) { + DWORD lasterr; handle = FindFirstFileA(dir->dd_name, &buf); - DWORD lasterr = GetLastError(); + lasterr = GetLastError(); dir->dd_handle = (long)handle; if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES)) { errno = err_win_to_posix(lasterr); diff --git a/help.c b/help.c index 6c46d8b494..399b0b40f4 100644 --- a/help.c +++ b/help.c @@ -127,7 +127,7 @@ static int is_executable(const char *name) return 0; #ifdef __MINGW32__ - /* cannot trust the executable bit, peek into the file instead */ +{ /* cannot trust the executable bit, peek into the file instead */ char buf[3] = { 0 }; int n; int fd = open(name, O_RDONLY); @@ -140,6 +140,7 @@ static int is_executable(const char *name) st.st_mode |= S_IXUSR; close(fd); } +} #endif return st.st_mode & S_IXUSR; } diff --git a/run-command.c b/run-command.c index ff3d8e2d8b..d1df7abe9a 100644 --- a/run-command.c +++ b/run-command.c @@ -123,6 +123,7 @@ int start_command(struct child_process *cmd) exit(127); } #else +{ int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */ const char **sargv = cmd->argv; char **env = environ; @@ -186,6 +187,7 @@ int start_command(struct child_process *cmd) dup2(s1, 1), close(s1); if (s2 >= 0) dup2(s2, 2), close(s2); +} #endif if (cmd->pid < 0) { From 0b79d7e97888f2f26820ac7c7d5b911e4f84821e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 19 Aug 2009 10:30:55 +0200 Subject: [PATCH 247/253] Fix a few old-style function declarations in compat/ Signed-off-by: Johannes Schindelin --- compat/fnmatch/fnmatch.c | 20 +-- compat/mingw.c | 2 +- compat/mingw.h | 2 +- compat/nedmalloc/malloc.c.h | 2 +- compat/nedmalloc/nedmalloc.c | 4 +- compat/regex/regex.c | 240 +++++++++++------------------------ 6 files changed, 84 insertions(+), 186 deletions(-) diff --git a/compat/fnmatch/fnmatch.c b/compat/fnmatch/fnmatch.c index 14feac7fe1..1180fc793f 100644 --- a/compat/fnmatch/fnmatch.c +++ b/compat/fnmatch/fnmatch.c @@ -130,10 +130,7 @@ extern int errno; /* This function doesn't exist on most systems. */ # if !defined HAVE___STRCHRNUL && !defined _LIBC -static char * -__strchrnul (s, c) - const char *s; - int c; +static char * __strchrnul (const char *s, int c) { char *result = strchr (s, c); if (result == NULL) @@ -153,13 +150,8 @@ __strchrnul (s, c) static int internal_fnmatch __P ((const char *pattern, const char *string, int no_leading_period, int flags)) internal_function; -static int -internal_function -internal_fnmatch (pattern, string, no_leading_period, flags) - const char *pattern; - const char *string; - int no_leading_period; - int flags; +static int internal_function internal_fnmatch (const char *pattern, + const char *string, int no_leading_period, int flags) { register const char *p = pattern, *n = string; register unsigned char c; @@ -476,11 +468,7 @@ internal_fnmatch (pattern, string, no_leading_period, flags) } -int -fnmatch (pattern, string, flags) - const char *pattern; - const char *string; - int flags; +int fnmatch (const char *pattern, const char *string, int flags) { return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags); } diff --git a/compat/mingw.c b/compat/mingw.c index d262e1c226..d70fd21573 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -907,7 +907,7 @@ void mingw_execvp(const char *cmd, char *const *argv) free_path_split(path); } -char **copy_environ() +char **copy_environ(void) { char **env; int i = 0; diff --git a/compat/mingw.h b/compat/mingw.h index 201e79c741..cfbcc0e0cd 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -78,7 +78,7 @@ static inline int getppid(void) { return 1; } static inline void sync(void) {} -static inline int getuid() +static inline int getuid(void) { return 1; } static inline struct passwd *getpwnam(const char *name) { return NULL; } diff --git a/compat/nedmalloc/malloc.c.h b/compat/nedmalloc/malloc.c.h index 74c42e3162..41b8698ab3 100644 --- a/compat/nedmalloc/malloc.c.h +++ b/compat/nedmalloc/malloc.c.h @@ -1802,7 +1802,7 @@ struct win32_mlock_t static MLOCK_T malloc_global_mutex = { 0, 0, 0}; -static FORCEINLINE long win32_getcurrentthreadid() { +static FORCEINLINE long win32_getcurrentthreadid(void) { #ifdef _MSC_VER #if defined(_M_IX86) long *threadstruct=(long *)__readfsdword(0x18); diff --git a/compat/nedmalloc/nedmalloc.c b/compat/nedmalloc/nedmalloc.c index d9a17a8057..229f942d10 100644 --- a/compat/nedmalloc/nedmalloc.c +++ b/compat/nedmalloc/nedmalloc.c @@ -159,8 +159,8 @@ struct mallinfo nedmallinfo(void) THROWSPEC { return nedpmallinfo(0); } #endif int nedmallopt(int parno, int value) THROWSPEC { return nedpmallopt(0, parno, value); } int nedmalloc_trim(size_t pad) THROWSPEC { return nedpmalloc_trim(0, pad); } -void nedmalloc_stats() THROWSPEC { nedpmalloc_stats(0); } -size_t nedmalloc_footprint() THROWSPEC { return nedpmalloc_footprint(0); } +void nedmalloc_stats(void) THROWSPEC { nedpmalloc_stats(0); } +size_t nedmalloc_footprint(void) THROWSPEC { return nedpmalloc_footprint(0); } void **nedindependent_calloc(size_t elemsno, size_t elemsize, void **chunks) THROWSPEC { return nedpindependent_calloc(0, elemsno, elemsize, chunks); } void **nedindependent_comalloc(size_t elems, size_t *sizes, void **chunks) THROWSPEC { return nedpindependent_comalloc(0, elems, sizes, chunks); } diff --git a/compat/regex/regex.c b/compat/regex/regex.c index 5ea007567d..9e8a203857 100644 --- a/compat/regex/regex.c +++ b/compat/regex/regex.c @@ -64,8 +64,7 @@ extern char *re_syntax_table; static char re_syntax_table[CHAR_SET_SIZE]; -static void -init_syntax_once () +static void init_syntax_once(void) { register int c; static int done = 0; @@ -380,10 +379,7 @@ typedef enum } while (0) #ifdef DEBUG -static void -extract_number (dest, source) - int *dest; - unsigned char *source; +static void extract_number(int *dest, unsigned char *source) { int temp = SIGN_EXTEND_CHAR (*(source + 1)); *dest = *source & 0377; @@ -407,10 +403,8 @@ extract_number (dest, source) } while (0) #ifdef DEBUG -static void -extract_number_and_incr (destination, source) - int *destination; - unsigned char **source; +static void extract_number_and_incr(int *destination, + unsigned char **source) { extract_number (destination, *source); *source += 2; @@ -455,9 +449,7 @@ extern void printchar (); /* Print the fastmap in human-readable form. */ -void -print_fastmap (fastmap) - char *fastmap; +void print_fastmap(char *fastmap) { unsigned was_a_range = 0; unsigned i = 0; @@ -487,10 +479,8 @@ print_fastmap (fastmap) /* Print a compiled pattern string in human-readable form, starting at the START pointer into it and ending just before the pointer END. */ -void -print_partial_compiled_pattern (start, end) - unsigned char *start; - unsigned char *end; +void print_partial_compiled_pattern(unsigned char *start, + unsigned char *end) { int mcnt, mcnt2; unsigned char *p = start; @@ -695,9 +685,7 @@ print_partial_compiled_pattern (start, end) } -void -print_compiled_pattern (bufp) - struct re_pattern_buffer *bufp; +void print_compiled_pattern(struct re_pattern_buffer *bufp) { unsigned char *buffer = bufp->buffer; @@ -722,13 +710,9 @@ print_compiled_pattern (bufp) } -void -print_double_string (where, string1, size1, string2, size2) - const char *where; - const char *string1; - const char *string2; - int size1; - int size2; +void print_double_string(const char *where, + const char *string1, int size1, + const char *string2, int size2) { unsigned this_char; @@ -777,9 +761,7 @@ reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS; The argument SYNTAX is a bit mask comprised of the various bits defined in regex.h. We return the old syntax. */ -reg_syntax_t -re_set_syntax (syntax) - reg_syntax_t syntax; +reg_syntax_t re_set_syntax(reg_syntax_t syntax) { reg_syntax_t ret = re_syntax_options; @@ -1031,12 +1013,9 @@ typedef struct The `fastmap' and `newline_anchor' fields are neither examined nor set. */ -static reg_errcode_t -regex_compile (pattern, size, syntax, bufp) - const char *pattern; - int size; - reg_syntax_t syntax; - struct re_pattern_buffer *bufp; +static reg_errcode_t regex_compile(const char *pattern, + int size, reg_syntax_t syntax, + struct re_pattern_buffer *bufp) { /* We fetch characters from PATTERN here. Even though PATTERN is `char *' (i.e., signed), we declare these variables as unsigned, so @@ -2036,11 +2015,8 @@ regex_compile (pattern, size, syntax, bufp) /* Store OP at LOC followed by two-byte integer parameter ARG. */ -static void -store_op1 (op, loc, arg) - re_opcode_t op; - unsigned char *loc; - int arg; +static void store_op1(re_opcode_t op, unsigned char *loc, + int arg) { *loc = (unsigned char) op; STORE_NUMBER (loc + 1, arg); @@ -2049,11 +2025,8 @@ store_op1 (op, loc, arg) /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */ -static void -store_op2 (op, loc, arg1, arg2) - re_opcode_t op; - unsigned char *loc; - int arg1, arg2; +static void store_op2(re_opcode_t op, unsigned char *loc, + int arg1, int arg2) { *loc = (unsigned char) op; STORE_NUMBER (loc + 1, arg1); @@ -2064,12 +2037,8 @@ store_op2 (op, loc, arg1, arg2) /* Copy the bytes from LOC to END to open up three bytes of space at LOC for OP followed by two-byte integer parameter ARG. */ -static void -insert_op1 (op, loc, arg, end) - re_opcode_t op; - unsigned char *loc; - int arg; - unsigned char *end; +static void insert_op1(re_opcode_t op, unsigned char *loc, + int arg, unsigned char *end) { register unsigned char *pfrom = end; register unsigned char *pto = end + 3; @@ -2083,12 +2052,8 @@ insert_op1 (op, loc, arg, end) /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */ -static void -insert_op2 (op, loc, arg1, arg2, end) - re_opcode_t op; - unsigned char *loc; - int arg1, arg2; - unsigned char *end; +static void insert_op2(re_opcode_t op, unsigned char *loc, + int arg1, int arg2, unsigned char *end) { register unsigned char *pfrom = end; register unsigned char *pto = end + 5; @@ -2104,10 +2069,8 @@ insert_op2 (op, loc, arg1, arg2, end) after an alternative or a begin-subexpression. We assume there is at least one character before the ^. */ -static boolean -at_begline_loc_p (pattern, p, syntax) - const char *pattern, *p; - reg_syntax_t syntax; +static boolean at_begline_loc_p(const char *pattern, + const char *p, reg_syntax_t syntax) { const char *prev = p - 2; boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\'; @@ -2123,10 +2086,8 @@ at_begline_loc_p (pattern, p, syntax) /* The dual of at_begline_loc_p. This one is for $. We assume there is at least one character after the $, i.e., `P < PEND'. */ -static boolean -at_endline_loc_p (p, pend, syntax) - const char *p, *pend; - int syntax; +static boolean at_endline_loc_p(const char *p, + const char *pend, int syntax) { const char *next = p; boolean next_backslash = *next == '\\'; @@ -2145,10 +2106,8 @@ at_endline_loc_p (p, pend, syntax) /* Returns true if REGNUM is in one of COMPILE_STACK's elements and false if it's not. */ -static boolean -group_in_compile_stack (compile_stack, regnum) - compile_stack_type compile_stack; - regnum_t regnum; +static boolean group_in_compile_stack(compile_stack_type + compile_stack, regnum_t regnum) { int this_element; @@ -2173,12 +2132,9 @@ group_in_compile_stack (compile_stack, regnum) We use these short variable names so we can use the same macros as `regex_compile' itself. */ -static reg_errcode_t -compile_range (p_ptr, pend, translate, syntax, b) - const char **p_ptr, *pend; - char *translate; - reg_syntax_t syntax; - unsigned char *b; +static reg_errcode_t compile_range(const char **p_ptr, + const char *pend, char *translate, + reg_syntax_t syntax, unsigned char *b) { unsigned this_char; @@ -2505,9 +2461,7 @@ typedef struct Returns 0 if we succeed, -2 if an internal error. */ -int -re_compile_fastmap (bufp) - struct re_pattern_buffer *bufp; +int re_compile_fastmap(struct re_pattern_buffer *bufp) { int j, k; fail_stack_type fail_stack; @@ -2790,12 +2744,10 @@ re_compile_fastmap (bufp) PATTERN_BUFFER will allocate its own register data, without freeing the old data. */ -void -re_set_registers (bufp, regs, num_regs, starts, ends) - struct re_pattern_buffer *bufp; - struct re_registers *regs; - unsigned num_regs; - regoff_t *starts, *ends; +void re_set_registers(struct re_pattern_buffer *bufp, + struct re_registers *regs, + unsigned num_regs, + regoff_t *starts, regoff_t *ends) { if (num_regs) { @@ -2817,12 +2769,10 @@ re_set_registers (bufp, regs, num_regs, starts, ends) /* Like re_search_2, below, but only one string is specified, and doesn't let you say where to stop matching. */ -int -re_search (bufp, string, size, startpos, range, regs) - struct re_pattern_buffer *bufp; - const char *string; - int size, startpos, range; - struct re_registers *regs; +int re_search(struct re_pattern_buffer *bufp, + const char *string, int size, + int startpos, int range, + struct re_registers *regs) { return re_search_2 (bufp, NULL, 0, string, size, startpos, range, regs, size); @@ -2850,15 +2800,11 @@ re_search (bufp, string, size, startpos, range, regs) found, -1 if no match, or -2 if error (such as failure stack overflow). */ -int -re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop) - struct re_pattern_buffer *bufp; - const char *string1, *string2; - int size1, size2; - int startpos; - int range; - struct re_registers *regs; - int stop; +int re_search_2(struct re_pattern_buffer *bufp, + const char *string1, int size1, + const char *string2, int size2, + int startpos, int range, + struct re_registers *regs, int stop) { int val; register char *fastmap = bufp->fastmap; @@ -3109,12 +3055,9 @@ typedef union #ifndef emacs /* Emacs never uses this. */ /* re_match is like re_match_2 except it takes only a single string. */ -int -re_match (bufp, string, size, pos, regs) - struct re_pattern_buffer *bufp; - const char *string; - int size, pos; - struct re_registers *regs; +int re_match(struct re_pattern_buffer *bufp, + const char *string, int size, int pos, + struct re_registers *regs) { return re_match_2 (bufp, NULL, 0, string, size, pos, regs, size); } @@ -3134,14 +3077,10 @@ re_match (bufp, string, size, pos, regs) failure stack overflowing). Otherwise, we return the length of the matched substring. */ -int -re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) - struct re_pattern_buffer *bufp; - const char *string1, *string2; - int size1, size2; - int pos; - struct re_registers *regs; - int stop; +int re_match_2(struct re_pattern_buffer *bufp, + const char *string1, int size1, + const char *string2, int size2, + int pos, struct re_registers *regs, int stop) { /* General temporaries. */ int mcnt; @@ -4330,10 +4269,9 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop) We don't handle duplicates properly (yet). */ -static boolean -group_match_null_string_p (p, end, reg_info) - unsigned char **p, *end; - register_info_type *reg_info; +static boolean group_match_null_string_p(unsigned char **p, + unsigned char *end, + register_info_type *reg_info) { int mcnt; /* Point to after the args to the start_memory. */ @@ -4439,10 +4377,9 @@ group_match_null_string_p (p, end, reg_info) It expects P to be the first byte of a single alternative and END one byte past the last. The alternative can contain groups. */ -static boolean -alt_match_null_string_p (p, end, reg_info) - unsigned char *p, *end; - register_info_type *reg_info; +static boolean alt_match_null_string_p(unsigned char *p, + unsigned char *end, + register_info_type *reg_info) { int mcnt; unsigned char *p1 = p; @@ -4476,10 +4413,9 @@ alt_match_null_string_p (p, end, reg_info) Sets P to one after the op and its arguments, if any. */ -static boolean -common_op_match_null_string_p (p, end, reg_info) - unsigned char **p, *end; - register_info_type *reg_info; +static boolean common_op_match_null_string_p(unsigned char **p, + unsigned char *end, + register_info_type *reg_info) { int mcnt; boolean ret; @@ -4564,13 +4500,8 @@ common_op_match_null_string_p (p, end, reg_info) /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN bytes; nonzero otherwise. */ -static int -bcmp_translate( - unsigned char *s1, - unsigned char *s2, - int len, - char *translate -) +static int bcmp_translate(unsigned char *s1, unsigned char *s2, + int len, char *translate) { register unsigned char *p1 = s1, *p2 = s2; while (len) @@ -4592,11 +4523,8 @@ bcmp_translate( We call regex_compile to do the actual compilation. */ -const char * -re_compile_pattern (pattern, length, bufp) - const char *pattern; - int length; - struct re_pattern_buffer *bufp; +const char *re_compile_pattern(const char *pattern, + int length, struct re_pattern_buffer *bufp) { reg_errcode_t ret; @@ -4625,9 +4553,7 @@ re_compile_pattern (pattern, length, bufp) /* BSD has one and only one pattern buffer. */ static struct re_pattern_buffer re_comp_buf; -char * -re_comp (s) - const char *s; +char *re_comp(const char *s) { reg_errcode_t ret; @@ -4663,9 +4589,7 @@ re_comp (s) } -int -re_exec (s) - const char *s; +int re_exec(const char *s) { const int len = strlen (s); return @@ -4711,11 +4635,7 @@ re_exec (s) It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for the return codes and their meanings.) */ -int -regcomp (preg, pattern, cflags) - regex_t *preg; - const char *pattern; - int cflags; +int regcomp(regex_t *preg, const char *pattern, int cflags) { reg_errcode_t ret; unsigned syntax @@ -4786,13 +4706,9 @@ regcomp (preg, pattern, cflags) We return 0 if we find a match and REG_NOMATCH if not. */ -int -regexec (preg, string, nmatch, pmatch, eflags) - const regex_t *preg; - const char *string; - size_t nmatch; - regmatch_t pmatch[]; - int eflags; +int regexec(const regex_t *preg, const char *string, + size_t nmatch, regmatch_t pmatch[], + int eflags) { int ret; struct re_registers regs; @@ -4851,12 +4767,8 @@ regexec (preg, string, nmatch, pmatch, eflags) /* Returns a message corresponding to an error code, ERRCODE, returned from either regcomp or regexec. We don't use PREG here. */ -size_t -regerror (errcode, preg, errbuf, errbuf_size) - int errcode; - const regex_t *preg; - char *errbuf; - size_t errbuf_size; +size_t regerror(int errcode, const regex_t *preg, + char *errbuf, size_t errbuf_size) { const char *msg; size_t msg_size; @@ -4895,9 +4807,7 @@ regerror (errcode, preg, errbuf, errbuf_size) /* Free dynamically allocated space used by PREG. */ -void -regfree (preg) - regex_t *preg; +void regfree(regex_t *preg) { if (preg->buffer != NULL) free (preg->buffer); From 95971f14a81d531b8fcb34e66b82c7fe537003dc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 19 Aug 2009 10:31:29 +0200 Subject: [PATCH 248/253] compat/mingw.c: Fix declaration after instruction Signed-off-by: Johannes Schindelin --- compat/mingw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index d70fd21573..449bea140a 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -184,10 +184,11 @@ int mingw_open (const char *filename, int oflags, ...) FILE *mingw_fopen (const char *filename, const char *mode) { int hide = 0; + FILE *file; if (hide_dotfiles && basename((char*)filename)[0] == '.') hide = access(filename, F_OK); - FILE *file = fopen(filename, mode); + file = fopen(filename, mode); /* * In Windows a file or dir starting with a dot is not * automatically hidden. So lets mark it as hidden when From 1edac2172dd74fa656dc6e4a9e450751f9a5d1a9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 19 Aug 2009 09:59:58 +0200 Subject: [PATCH 249/253] MinGW means that we have gcc, so we can add gcc-specific compiler options These were suggested by Junio at some stage. Signed-off-by: Johannes Schindelin --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 690ac55b7d..e0f9a631c2 100644 --- a/Makefile +++ b/Makefile @@ -905,6 +905,9 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_REGEX = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/fnmatch COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" + # We have GCC, so let's make use of those nice options + COMPAT_CFLAGS += -Werror -Wno-pointer-to-int-cast \ + -Wold-style-definition -Wdeclaration-after-statement COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/winansi.o EXTLIBS += -lws2_32 X = .exe From 8d7c8c95b6d995555d514b43ab6969a1cf7e3044 Mon Sep 17 00:00:00 2001 From: Frank Li Date: Sun, 16 Aug 2009 13:53:30 +0800 Subject: [PATCH 250/253] Define SNPRINTF_SIZE_CORR=1 for Microsoft Visual C++ The Microsoft C runtime's vsnprintf function does not add NUL at the end of the buffer. Further, Microsoft deprecated vsnprintf in favor of _vsnprintf, so add a #define to that end. Signed-off-by: Frank Li Signed-off-by: Johannes Schindelin --- compat/snprintf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compat/snprintf.c b/compat/snprintf.c index 6c0fb056a5..47b2b8a55e 100644 --- a/compat/snprintf.c +++ b/compat/snprintf.c @@ -6,7 +6,7 @@ * number of characters to write without the trailing NUL. */ #ifndef SNPRINTF_SIZE_CORR -#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ < 4 +#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ < 4 || defined(_MSC_VER) #define SNPRINTF_SIZE_CORR 1 #else #define SNPRINTF_SIZE_CORR 0 @@ -14,6 +14,11 @@ #endif #undef vsnprintf + +#if defined(_MSC_VER) +#define vsnprintf _vsnprintf +#endif + int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) { char *s; From d84f77b0cc2381c59d39011d92bd0ad6740f96ab Mon Sep 17 00:00:00 2001 From: Frank Li Date: Tue, 18 Aug 2009 23:39:54 +0800 Subject: [PATCH 251/253] Test for WIN32 instead of __MINGW32_ The code which is conditional on MinGW32 is actually conditional on Windows. Use the WIN32 symbol, which is defined by the MINGW32 and MSVC environments, but not by Cygwin. Signed-off-by: Frank Li Signed-off-by: Johannes Schindelin --- help.c | 2 +- run-command.c | 8 ++++---- run-command.h | 2 +- setup.c | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/help.c b/help.c index 399b0b40f4..9dae4e7551 100644 --- a/help.c +++ b/help.c @@ -126,7 +126,7 @@ static int is_executable(const char *name) !S_ISREG(st.st_mode)) return 0; -#ifdef __MINGW32__ +#ifdef WIN32 { /* cannot trust the executable bit, peek into the file instead */ char buf[3] = { 0 }; int n; diff --git a/run-command.c b/run-command.c index d1df7abe9a..d0833f846e 100644 --- a/run-command.c +++ b/run-command.c @@ -67,7 +67,7 @@ int start_command(struct child_process *cmd) trace_argv_printf(cmd->argv, "trace: run_command:"); -#ifndef __MINGW32__ +#ifndef WIN32 fflush(NULL); cmd->pid = fork(); if (!cmd->pid) { @@ -294,7 +294,7 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const return run_command(&cmd); } -#ifdef __MINGW32__ +#ifdef WIN32 static __stdcall unsigned run_thread(void *data) { struct async *async = data; @@ -310,7 +310,7 @@ int start_async(struct async *async) return error("cannot create pipe: %s", strerror(errno)); async->out = pipe_out[0]; -#ifndef __MINGW32__ +#ifndef WIN32 /* Flush stdio before fork() to avoid cloning buffers */ fflush(NULL); @@ -339,7 +339,7 @@ int start_async(struct async *async) int finish_async(struct async *async) { -#ifndef __MINGW32__ +#ifndef WIN32 int ret = 0; if (wait_or_whine(async->pid)) diff --git a/run-command.h b/run-command.h index e345502843..09a4cb25b2 100644 --- a/run-command.h +++ b/run-command.h @@ -79,7 +79,7 @@ struct async { int (*proc)(int fd, void *data); void *data; int out; /* caller reads from here and closes it */ -#ifndef __MINGW32__ +#ifndef WIN32 pid_t pid; #else HANDLE tid; diff --git a/setup.c b/setup.c index e3781b656d..029371e584 100644 --- a/setup.c +++ b/setup.c @@ -41,7 +41,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) { static char path[PATH_MAX]; -#ifndef __MINGW32__ +#ifndef WIN32 if (!pfx || !*pfx || is_absolute_path(arg)) return arg; memcpy(path, pfx, pfx_len); From e76177f62a158b71c22cc3f7fa3aaa0ffa646860 Mon Sep 17 00:00:00 2001 From: Frank Li Date: Mon, 17 Aug 2009 14:52:01 +0800 Subject: [PATCH 252/253] mingw.c: Use the O_BINARY flag to open files On Windows, non-text files must be opened using the O_BINARY flag. MinGW does this for us automatically, but Microsoft Visual C++ does not. So let's be explicit. Signed-off-by: Frank Li Signed-off-by: Johannes Schindelin --- compat/mingw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 449bea140a..5d3d24617e 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -160,7 +160,7 @@ int mingw_open (const char *filename, int oflags, ...) if (!strcmp(filename, "/dev/null")) filename = "nul"; - fd = open(filename, oflags, mode); + fd = open(filename, oflags | O_BINARY, mode); if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) { DWORD attrs = GetFileAttributes(filename); @@ -353,7 +353,7 @@ int mkstemp(char *template) char *filename = mktemp(template); if (filename == NULL) return -1; - return open(filename, O_RDWR | O_CREAT, 0600); + return open(filename, O_RDWR | O_CREAT | O_BINARY, 0600); } int gettimeofday(struct timeval *tv, void *tz) From d92d40ecd04798b7a31b9a5f38052d43c86e2ae0 Mon Sep 17 00:00:00 2001 From: Frank Li Date: Tue, 18 Aug 2009 23:59:53 +0800 Subject: [PATCH 253/253] Place __stdcall between return value and function name MSVC requires that __stdcall be between return value and function name. Further, all Win32 API definitions look like this: TYPE WINAPI function_name... Signed-off-by: Frank Li Signed-off-by: Johannes Schindelin --- compat/mingw.c | 4 ++-- run-command.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 5d3d24617e..fd642e4309 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1271,7 +1271,7 @@ static sig_handler_t timer_fn = SIG_DFL; * length to call the signal handler. */ -static __stdcall unsigned ticktack(void *dummy) +static unsigned __stdcall ticktack(void *dummy) { while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) { if (timer_fn == SIG_DFL) @@ -1400,7 +1400,7 @@ void mingw_open_html(const char *unixpath) int link(const char *oldpath, const char *newpath) { - typedef BOOL WINAPI (*T)(const char*, const char*, LPSECURITY_ATTRIBUTES); + typedef BOOL (WINAPI *T)(const char*, const char*, LPSECURITY_ATTRIBUTES); static T create_hard_link = NULL; if (!create_hard_link) { create_hard_link = (T) GetProcAddress( diff --git a/run-command.c b/run-command.c index d0833f846e..62bea191d4 100644 --- a/run-command.c +++ b/run-command.c @@ -295,7 +295,7 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const } #ifdef WIN32 -static __stdcall unsigned run_thread(void *data) +static unsigned __stdcall run_thread(void *data) { struct async *async = data; return async->proc(async->fd_for_proc, async->data);