summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-10 11:22:32 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-10 11:22:32 +0000
commit15e4e5ef56c35935b57f2b1efc40548c54234d3e (patch)
treedf43f1d6d61317915bc7a78a1f5de208e7a89cb4
parent52e04dad6eab39408a35da45462c2ddf8a2f6a10 (diff)
downloadgnunet-15e4e5ef56c35935b57f2b1efc40548c54234d3e.tar.gz
gnunet-15e4e5ef56c35935b57f2b1efc40548c54234d3e.zip
strndup
-rw-r--r--src/include/gnunet_common.h22
-rw-r--r--src/util/common_allocation.c24
2 files changed, 45 insertions, 1 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h
index 503fda5c1..7e4f82ad4 100644
--- a/src/include/gnunet_common.h
+++ b/src/include/gnunet_common.h
@@ -392,7 +392,7 @@ unsigned long long GNUNET_htonll (unsigned long long n);
392 * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free. 392 * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free.
393 * 393 *
394 * @param ptr location where to free the memory. ptr must have 394 * @param ptr location where to free the memory. ptr must have
395 * been returned by GNUNET_strdup, GNUNET_malloc or GNUNET_array_grow earlier. 395 * been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier.
396 */ 396 */
397#define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__) 397#define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
398 398
@@ -414,6 +414,16 @@ unsigned long long GNUNET_htonll (unsigned long long n);
414#define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__) 414#define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
415 415
416/** 416/**
417 * Wrapper around GNUNET_strndup. Makes a partial copy of the string
418 * pointed to by a.
419 *
420 * @param a pointer to a string
421 * @param length of the string to duplicate
422 * @return a partial copy of the string including zero-termination
423 */
424#define GNUNET_strndup(a,b) GNUNET_xstrndup_(a,b,__FILE__,__LINE__)
425
426/**
417 * Grow a well-typed (!) array. This is a convenience 427 * Grow a well-typed (!) array. This is a convenience
418 * method to grow a vector <tt>arr</tt> of size <tt>size</tt> 428 * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
419 * to the new (target) size <tt>tsize</tt>. 429 * to the new (target) size <tt>tsize</tt>.
@@ -552,6 +562,16 @@ void GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
552char *GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber); 562char *GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
553 563
554/** 564/**
565 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the GNUNET_strndup macro.
566 * @param str string to duplicate
567 * @param len lenght of the string to duplicate
568 * @param filename where is this call being made (for debugging)
569 * @param linenumber line where this call is being made (for debugging)
570 * @return the duplicated string
571 */
572char *GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber);
573
574/**
555 * Grow an array, the new elements are zeroed out. 575 * Grow an array, the new elements are zeroed out.
556 * Grows old by (*oldCount-newCount)*elementSize 576 * Grows old by (*oldCount-newCount)*elementSize
557 * bytes and sets *oldCount to newCount. 577 * bytes and sets *oldCount to newCount.
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index 59fa2dc05..1ae01ca49 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -215,6 +215,30 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
215 return res; 215 return res;
216} 216}
217 217
218
219/**
220 * Dup partially a string (same semantics as strndup).
221 *
222 * @param str the string to dup
223 * @param len the lenght of the string to dup
224 * @param filename where in the code was the call to GNUNET_strndup
225 * @param linenumber where in the code was the call to GNUNET_strndup
226 * @return strndup(str,len)
227 */
228char *
229GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber)
230{
231 char *res;
232
233 GNUNET_assert_at (str != NULL, filename, linenumber);
234 len = GNUNET_MIN(len,strlen(str));
235 res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
236 memcpy (res, str, len);
237 res[len] = '\0';
238 return res;
239}
240
241
218/** 242/**
219 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes 243 * Grow an array. Grows old by (*oldCount-newCount)*elementSize bytes
220 * and sets *oldCount to newCount. 244 * and sets *oldCount to newCount.