summaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2006-06-18 07:22:36 +0000
committerNick Mathewson <nickm@torproject.org>2006-06-18 07:22:36 +0000
commitaf8096815e126dcab718b2eef6e500ed2aa83912 (patch)
tree1c9f9c8c26f4fc6d47712f702b529ad1ab6f3c73 /src/common/util.c
parent78428dccdbd47af0ca8d39b40c8a3e512d1c1036 (diff)
downloadtor-af8096815e126dcab718b2eef6e500ed2aa83912.tar.gz
tor-af8096815e126dcab718b2eef6e500ed2aa83912.zip
Add a memdup function to util
svn:r6635
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 2af0cf7b40..00888aa72f 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -190,6 +190,22 @@ _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
return dup;
}
+/** Allocate a chunk of <b>len</b> bytes, with the same contents starting at
+ * <b>mem</b>. */
+void *
+_tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
+{
+ char *dup;
+ tor_assert(mem);
+ dup = _tor_malloc(len DMALLOC_FN_ARGS);
+ /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
+ * this function gets called a whole lot, and platform strncpy is
+ * much faster than strlcpy when strlen(s) is much longer than n.
+ */
+ memcpy(dup, mem, len);
+ return dup;
+}
+
/* =====
* String manipulation
* ===== */