mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-24 20:33:31 +01:00
Add a memdup function to util
svn:r6635
This commit is contained in:
parent
78428dccdb
commit
af8096815e
@ -190,6 +190,22 @@ _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
|
|||||||
return dup;
|
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
|
* String manipulation
|
||||||
* ===== */
|
* ===== */
|
||||||
|
@ -67,6 +67,7 @@ void *_tor_malloc_zero(size_t size DMALLOC_PARAMS);
|
|||||||
void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
|
void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
|
||||||
char *_tor_strdup(const char *s DMALLOC_PARAMS);
|
char *_tor_strdup(const char *s DMALLOC_PARAMS);
|
||||||
char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS);
|
char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS);
|
||||||
|
void *_tor_memdup(const void *mem, size_t len DMALLOC_PARAMS);
|
||||||
#ifdef USE_DMALLOC
|
#ifdef USE_DMALLOC
|
||||||
extern int dmalloc_free(const char *file, const int line, void *pnt,
|
extern int dmalloc_free(const char *file, const int line, void *pnt,
|
||||||
const int func_id);
|
const int func_id);
|
||||||
@ -85,6 +86,7 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
|
|||||||
#define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
|
#define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
|
||||||
#define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
|
#define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
|
||||||
#define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
|
#define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
|
||||||
|
#define tor_memdup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
|
||||||
|
|
||||||
/* String manipulation */
|
/* String manipulation */
|
||||||
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
||||||
|
Loading…
Reference in New Issue
Block a user