[sr-dev] git:master:c2886552: core: add two new string handling functions to copy chars and str

Henning Westerholt hw at skalatan.de
Sat Nov 23 23:23:31 CET 2019


Module: kamailio
Branch: master
Commit: c288655278036e5125010ba3b818d2e9ddf5d1a4
URL: https://github.com/kamailio/kamailio/commit/c288655278036e5125010ba3b818d2e9ddf5d1a4

Author: Henning Westerholt <hw at skalatan.de>
Committer: Henning Westerholt <hw at skalatan.de>
Date: 2019-11-23T22:52:13+01:00

core: add two new string handling functions to copy chars and str

- add two new string handling functions to copy chars and str
- shm_str2char_dup: Make a copy from str structure to a char pointer using shm_malloc
- shm_char_dup: Make a copy of a char pointer to a char pointer using shm_malloc

---

Modified: src/core/ut.h

---

Diff:  https://github.com/kamailio/kamailio/commit/c288655278036e5125010ba3b818d2e9ddf5d1a4.diff
Patch: https://github.com/kamailio/kamailio/commit/c288655278036e5125010ba3b818d2e9ddf5d1a4.patch

---

diff --git a/src/core/ut.h b/src/core/ut.h
index 2259aea973..8b64070947 100644
--- a/src/core/ut.h
+++ b/src/core/ut.h
@@ -691,7 +691,7 @@ static inline int str2sint(str* _s, int* _r)
 
 
 /**
- * \brief Make a copy of a str structure using shm_malloc
+ * \brief Make a copy of a str structure to a str using shm_malloc
  * \param dst destination
  * \param src source
  * \return 0 on success, -1 on failure
@@ -736,6 +736,58 @@ static inline int shm_str_dup(str* dst, const str* src)
 	return 0;
 }
 
+/**
+ * \brief Make a copy of a char pointer to a char pointer using shm_malloc
+ * \param src source
+ * \return a pointer to the new allocated char on success, 0 on failure
+ */
+static inline char* shm_char_dup(const char *src)
+{
+	char *rval;
+	int len;
+
+	if (!src) {
+		LM_ERR("NULL src or dst\n");
+		return NULL;
+	}
+
+	len = strlen(src) + 1;
+	rval = shm_malloc(len);
+	if (!rval) {
+		SHM_MEM_ERROR;
+		return NULL;
+	}
+
+	memcpy(rval, src, len);
+
+	return rval;
+}
+
+
+/**
+ * \brief Make a copy from str structure to a char pointer using shm_malloc
+ * \param src source
+ * \return a pointer to the new allocated char on success, 0 on failure
+ */
+static inline char* shm_str2char_dup(str *src)
+{
+	char *res;
+
+	if (!src || !src->s) {
+		LM_ERR("NULL src\n");
+		return NULL;
+	}
+
+	if (!(res = (char *) shm_malloc(src->len + 1))) {
+		SHM_MEM_ERROR;
+		return NULL;
+	}
+
+	strncpy(res, src->s, src->len);
+	res[src->len] = 0;
+
+	return res;
+}
 
 
 /**




More information about the sr-dev mailing list