msgbuf: Add pldm_msgbuf_destroy_used()

Used when inserting int a msgbuf, returns the total length that has been
used.

Change-Id: I981c6b8a1fd194387fdf8a6a3299c3811d33c622
Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
diff --git a/src/msgbuf.h b/src/msgbuf.h
index d34c4a4..a176891 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -1131,6 +1131,38 @@
 }
 
 /**
+ * Return the number of bytes used in a msgbuf instance.
+ *
+ * @param ctx - The msgbuf.
+ * @param orig_len - The original size of the msgbuf, the `len` argument passed to
+ * 		pldm_msgbuf_init_errno().
+ * @param ret_used_len - The number of bytes that have been used from the msgbuf instance.
+ *
+ * This can be called after a number of pldm_msgbuf_insert...() calls to
+ * determine the total size that was written.
+ *
+ */
+LIBPLDM_CC_NONNULL
+LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_destroy_used(struct pldm_msgbuf *ctx,
+						      size_t orig_len,
+						      size_t *ret_used_len)
+{
+	int rc;
+	rc = pldm_msgbuf_validate(ctx);
+	if (rc) {
+		return rc;
+	}
+
+	if ((size_t)ctx->remaining > orig_len) {
+		/* Caller passed incorrect orig_len */
+		return -EOVERFLOW;
+	}
+
+	*ret_used_len = orig_len - ctx->remaining;
+	return 0;
+}
+
+/**
  * @brief pldm_msgbuf copy data between two msg buffers
  *
  * @param[in,out] src - pldm_msgbuf for source from where value should be copied