[FFmpeg-cvslog] mem: add av_strndup() for duplicating substrings

Anton Khirnov git at videolan.org
Thu Aug 14 00:37:52 CEST 2014


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Tue Aug 12 16:24:19 2014 +0000| [8ddc32629a6d6be77256694c9e322dde134609f3] | committer: Anton Khirnov

mem: add av_strndup() for duplicating substrings

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8ddc32629a6d6be77256694c9e322dde134609f3
---

 doc/APIchanges      |    3 +++
 libavutil/mem.c     |   20 ++++++++++++++++++++
 libavutil/mem.h     |   10 ++++++++++
 libavutil/version.h |    2 +-
 4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 66fd1d1..6d1f061 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,9 @@ libavutil:     2014-08-09
 
 API changes, most recent first:
 
+2014-08-xx - xxxxxxx - lavu 54.03.0 - mem.h
+  Add av_strndup().
+
 2014-xx-xx - xxxxxxx - lavu 54.02.0 - opt.h
   Add av_opt_get_dict_val/set_dict_val with AV_OPT_TYPE_DICT to support
   dictionary types being set as options.
diff --git a/libavutil/mem.c b/libavutil/mem.c
index be42342..b7bb65c 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -222,6 +222,26 @@ char *av_strdup(const char *s)
     return ptr;
 }
 
+char *av_strndup(const char *s, size_t len)
+{
+    char *ret = NULL, *end;
+
+    if (!s)
+        return NULL;
+
+    end = memchr(s, 0, len);
+    if (end)
+        len = end - s;
+
+    ret = av_realloc(NULL, len + 1);
+    if (!ret)
+        return NULL;
+
+    memcpy(ret, s, len);
+    ret[len] = 0;
+    return ret;
+}
+
 static void fill16(uint8_t *dst, int len)
 {
     uint32_t v = AV_RN16(dst - 2);
diff --git a/libavutil/mem.h b/libavutil/mem.h
index 4a5e362..9f667c2 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -218,6 +218,16 @@ av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t si
 char *av_strdup(const char *s) av_malloc_attrib;
 
 /**
+ * Duplicate a substring of the string s.
+ * @param s string to be duplicated
+ * @param len the maximum length of the resulting string (not counting the
+ *            terminating byte).
+ * @return Pointer to a newly-allocated string containing a
+ * copy of s or NULL if the string cannot be allocated.
+ */
+char *av_strndup(const char *s, size_t len) av_malloc_attrib;
+
+/**
  * Free a memory block which has been allocated with av_malloc(z)() or
  * av_realloc() and set the pointer pointing to it to NULL.
  * @param ptr Pointer to the pointer to the memory block which should
diff --git a/libavutil/version.h b/libavutil/version.h
index 4a4154d..c22d0c5 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -54,7 +54,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 54
-#define LIBAVUTIL_VERSION_MINOR  2
+#define LIBAVUTIL_VERSION_MINOR  3
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list