Andrew's git
/
gitweb.git
/ diff
summary
|
log
|
commit
| diff |
tree
commit
grep
author
committer
pickaxe
?
re
Add xmemdupz() that duplicates a block of memory, and NUL terminates it.
author
Pierre Habouzit
<madcoder@debian.org>
Sat, 15 Sep 2007 21:53:05 +0000
(23:53 +0200)
committer
Junio C Hamano
<gitster@pobox.com>
Tue, 18 Sep 2007 10:07:58 +0000
(
03:07
-0700)
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>
git-compat-util.h
patch
|
blob
|
history
raw
|
patch
|
inline
| side by side (parent:
0557656
)
diff --git
a/git-compat-util.h
b/git-compat-util.h
index 1bfbdeb94f55d57b429b91aa8762618153c34f7f..f23d934f667cc2b10ee668a3bfef8492aadbbe7d 100644
(file)
--- a/
git-compat-util.h
+++ b/
git-compat-util.h
@@
-211,19
+211,20
@@
static inline void *xmalloc(size_t size)
return ret;
}
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;
}
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);
static inline void *xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);