gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r16436 - in gnunet/src: include util


From: gnunet
Subject: [GNUnet-SVN] r16436 - in gnunet/src: include util
Date: Wed, 10 Aug 2011 13:22:32 +0200

Author: grothoff
Date: 2011-08-10 13:22:32 +0200 (Wed, 10 Aug 2011)
New Revision: 16436

Modified:
   gnunet/src/include/gnunet_common.h
   gnunet/src/util/common_allocation.c
Log:
strndup

Modified: gnunet/src/include/gnunet_common.h
===================================================================
--- gnunet/src/include/gnunet_common.h  2011-08-10 07:57:11 UTC (rev 16435)
+++ gnunet/src/include/gnunet_common.h  2011-08-10 11:22:32 UTC (rev 16436)
@@ -392,7 +392,7 @@
  * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) 
instead of GNUNET_free.
  *
  * @param ptr location where to free the memory. ptr must have
- *     been returned by GNUNET_strdup, GNUNET_malloc or GNUNET_array_grow 
earlier.
+ *     been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or 
GNUNET_array_grow earlier.
  */
 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
 
@@ -414,6 +414,16 @@
 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
 
 /**
+ * Wrapper around GNUNET_strndup.  Makes a partial copy of the string
+ * pointed to by a.
+ *
+ * @param a pointer to a string
+ * @param length of the string to duplicate
+ * @return a partial copy of the string including zero-termination
+ */
+#define GNUNET_strndup(a,b) GNUNET_xstrndup_(a,b,__FILE__,__LINE__)
+
+/**
  * Grow a well-typed (!) array.  This is a convenience
  * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
  * to the new (target) size <tt>tsize</tt>.
@@ -552,6 +562,16 @@
 char *GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
 
 /**
+ * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the 
GNUNET_strndup macro.
+ * @param str string to duplicate
+ * @param len lenght of the string to duplicate
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return the duplicated string
+ */
+char *GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int 
linenumber);
+
+/**
  * Grow an array, the new elements are zeroed out.
  * Grows old by (*oldCount-newCount)*elementSize
  * bytes and sets *oldCount to newCount.

Modified: gnunet/src/util/common_allocation.c
===================================================================
--- gnunet/src/util/common_allocation.c 2011-08-10 07:57:11 UTC (rev 16435)
+++ gnunet/src/util/common_allocation.c 2011-08-10 11:22:32 UTC (rev 16436)
@@ -215,7 +215,31 @@
   return res;
 }
 
+
 /**
+ * Dup partially a string (same semantics as strndup).
+ *
+ * @param str the string to dup
+ * @param len the lenght of the string to dup
+ * @param filename where in the code was the call to GNUNET_strndup
+ * @param linenumber where in the code was the call to GNUNET_strndup
+ * @return strndup(str,len)
+ */
+char *
+GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int 
linenumber)
+{
+  char *res;
+
+  GNUNET_assert_at (str != NULL, filename, linenumber);
+  len = GNUNET_MIN(len,strlen(str));
+  res = GNUNET_xmalloc_ (len + 1, filename, linenumber);
+  memcpy (res, str, len);
+  res[len] = '\0';
+  return res;
+}
+
+
+/**
  * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
  * and sets *oldCount to newCount.
  *




reply via email to

[Prev in Thread] Current Thread [Next in Thread]