summaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-15 21:53:05 (GMT)
committerJunio C Hamano <gitster@pobox.com>2007-09-18 10:07:58 (GMT)
commit68d3025a805097ec148ec6e9b0b54a5db1ef138e (patch)
treef6e8d1a5fdb89dd52a7f90d34bb271a2c4048cc4 /git-compat-util.h
parent0557656930d41f10c90bccf90e3c5bd87bd53661 (diff)
downloadgit-68d3025a805097ec148ec6e9b0b54a5db1ef138e.zip
git-68d3025a805097ec148ec6e9b0b54a5db1ef138e.tar.gz
git-68d3025a805097ec148ec6e9b0b54a5db1ef138e.tar.bz2
Add xmemdupz() that duplicates a block of memory, and NUL terminates it.
A lot of places in git's code use code like: char *res; len = ... find length of an interesting segment in src ...; res = xmalloc(len + 1); memcpy(res, src, len); res[len] = '\0'; return res; A new function xmemdupz() captures the allocation, copy and NUL termination. Existing xstrndup() is reimplemented in terms of this new function. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h17
1 files changed, 9 insertions, 8 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index 1bfbdeb..f23d934 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -211,19 +211,20 @@ static inline void *xmalloc(size_t size)
return ret;
}
-static inline char *xstrndup(const char *str, size_t len)
+static inline void *xmemdupz(const void *data, size_t len)
{
- char *p;
-
- p = memchr(str, '\0', len);
- if (p)
- len = p - str;
- p = xmalloc(len + 1);
- memcpy(p, str, len);
+ char *p = xmalloc(len + 1);
+ memcpy(p, data, len);
p[len] = '\0';
return p;
}
+static inline char *xstrndup(const char *str, size_t len)
+{
+ char *p = memchr(str, '\0', len);
+ return xmemdupz(str, p ? p - str : len);
+}
+
static inline void *xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);