From: Pierre Habouzit <madcoder@debian.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH 1/2] Add xmemdup that duplicates a block of memory, and NUL terminates it.
Date: Sat, 15 Sep 2007 23:53:05 +0200 [thread overview]
Message-ID: <20070917161141.CC8AA344A49@madism.org> (raw)
In-Reply-To: <20070917161113.GB460@artemis.corp>
Implement xstrndup using it.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
git-compat-util.h | 17 +++++++++--------
1 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 1bfbdeb..429b0a3 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 *xmemdup(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 xmemdup(str, p ? p - str : len);
+}
+
static inline void *xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
--
1.5.3.1
next prev parent reply other threads:[~2007-09-17 16:11 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-17 16:11 xmemdup patches Pierre Habouzit
2007-09-15 21:53 ` Pierre Habouzit [this message]
2007-09-15 22:32 ` [PATCH 2/2] Use xmemdup in many places Pierre Habouzit
2007-09-19 8:08 ` Junio C Hamano
2007-09-19 8:26 ` Pierre Habouzit
2007-09-17 16:14 ` xmemdup patches Pierre Habouzit
2007-09-17 16:39 ` Johannes Schindelin
2007-09-17 17:43 ` Pierre Habouzit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20070917161141.CC8AA344A49@madism.org \
--to=madcoder@debian.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).