istdup() performs the equivalent of strdup() on a `struct ist`.
---
 include/common/ist.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/common/ist.h b/include/common/ist.h
index 4d75d8607..9614f957d 100644
--- a/include/common/ist.h
+++ b/include/common/ist.h
@@ -420,6 +420,23 @@ static inline ssize_t istscpy(struct ist *dst, const 
struct ist src, size_t coun
        return -1;
 }
 
+/* This function performs the equivalent of strdup() on the given <src>.
+ * If this function fails to allocate memory an `ist` with `.ptr = NULL` and 
`.len = 0`
+ * is returned.
+ */
+static inline struct ist istdup(const struct ist src)
+{
+       const size_t alloc_size = src.len;
+       const char *alloc = malloc(alloc_size);
+       struct ist dst = ist2(alloc, 0);
+       
+       if (dst.ptr != NULL) {
+               istcpy(&dst, src, alloc_size);
+       }
+       
+       return dst;
+}
+
 /* appends <src> after <dst> for a maximum of <count> total bytes in <dst> 
after
  * the copy. <dst> is assumed to be <count> or less before the call. The new
  * string's length is returned, or -1 if a truncation happened. In all cases,
-- 
2.25.1


Reply via email to