[Asterisk-code-review] taskprocessor.c: New API for human friendly taskprocessor na... (asterisk[master])

Joshua Colp asteriskteam at digium.com
Tue Jan 12 13:25:49 CST 2016


Joshua Colp has submitted this change and it was merged.

Change subject: taskprocessor.c: New API for human friendly taskprocessor names.
......................................................................


taskprocessor.c: New API for human friendly taskprocessor names.

* Add new API call to get a sequence number for use in human friendly
taskprocessor names.

* Add new API call to create a taskprocessor name in a given buffer and
append a sequence number.

Change-Id: Iac458f05b45232315ed64aa31b1df05b875537a9
---
M include/asterisk/taskprocessor.h
M main/taskprocessor.c
2 files changed, 61 insertions(+), 0 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Anonymous Coward #1000019: Verified
  Joshua Colp: Looks good to me, approved



diff --git a/include/asterisk/taskprocessor.h b/include/asterisk/taskprocessor.h
index 6ebf072..af3ce74 100644
--- a/include/asterisk/taskprocessor.h
+++ b/include/asterisk/taskprocessor.h
@@ -56,6 +56,9 @@
 
 struct ast_taskprocessor;
 
+/*! \brief Suggested maximum taskprocessor name length (less null terminator). */
+#define AST_TASKPROCESSOR_MAX_NAME	45
+
 #define AST_TASKPROCESSOR_HIGH_WATER_LEVEL 500
 
 /*!
@@ -259,6 +262,30 @@
 int ast_taskprocessor_is_task(struct ast_taskprocessor *tps);
 
 /*!
+ * \brief Get the next sequence number to create a human friendly taskprocessor name.
+ * \since 13.8.0
+ *
+ * \return Sequence number for use in creating human friendly taskprocessor names.
+ */
+unsigned int ast_taskprocessor_seq_num(void);
+
+/*!
+ * \brief Build a taskprocessor name with a sequence number on the end.
+ * \since 13.8.0
+ *
+ * \param buf Where to put the built taskprocessor name.
+ * \param size How large is buf including null terminator.
+ * \param format printf format to create the non-sequenced part of the name.
+ *
+ * \note The user supplied part of the taskprocessor name is truncated
+ * to allow the full sequence number to be appended within the supplied
+ * buffer size.
+ *
+ * \return Nothing
+ */
+void __attribute__((format(printf, 3, 4))) ast_taskprocessor_build_name(char *buf, unsigned int size, const char *format, ...);
+
+/*!
  * \brief Return the name of the taskprocessor singleton
  * \since 1.6.1
  */
diff --git a/main/taskprocessor.c b/main/taskprocessor.c
index 0b67c0f..9a4424e 100644
--- a/main/taskprocessor.c
+++ b/main/taskprocessor.c
@@ -879,3 +879,37 @@
 	ao2_unlock(tps);
 	return is_task;
 }
+
+unsigned int ast_taskprocessor_seq_num(void)
+{
+	static int seq_num;
+
+	return (unsigned int) ast_atomic_fetchadd_int(&seq_num, +1);
+}
+
+void ast_taskprocessor_build_name(char *buf, unsigned int size, const char *format, ...)
+{
+	va_list ap;
+	int user_size;
+#define SEQ_STR_SIZE (1 + 8 + 1)	/* Dash plus 8 hex digits plus null terminator */
+
+	ast_assert(buf != NULL);
+	ast_assert(SEQ_STR_SIZE <= size);
+
+	va_start(ap, format);
+	user_size = vsnprintf(buf, size - (SEQ_STR_SIZE - 1), format, ap);
+	va_end(ap);
+	if (user_size < 0) {
+		/*
+		 * Wow!  We got an output error to a memory buffer.
+		 * Assume no user part of name written.
+		 */
+		user_size = 0;
+	} else if (size < user_size + SEQ_STR_SIZE) {
+		/* Truncate user part of name to make sequence number fit. */
+		user_size = size - SEQ_STR_SIZE;
+	}
+
+	/* Append sequence number to end of user name. */
+	snprintf(buf + user_size, SEQ_STR_SIZE, "-%08x", ast_taskprocessor_seq_num());
+}

-- 
To view, visit https://gerrit.asterisk.org/1955
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iac458f05b45232315ed64aa31b1df05b875537a9
Gerrit-PatchSet: 2
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>



More information about the asterisk-code-review mailing list