[asterisk-commits] dlee: branch dlee/jansson r378908 - /team/dlee/jansson/res/res_json.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jan 10 09:33:17 CST 2013


Author: dlee
Date: Thu Jan 10 09:33:14 2013
New Revision: 378908

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=378908
Log:
Optimized ast_json_dump_str

Modified:
    team/dlee/jansson/res/res_json.c

Modified: team/dlee/jansson/res/res_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/res/res_json.c?view=diff&rev=378908&r1=378907&r2=378908
==============================================================================
--- team/dlee/jansson/res/res_json.c (original)
+++ team/dlee/jansson/res/res_json.c Thu Jan 10 09:33:14 2013
@@ -350,23 +350,30 @@
 static int write_to_ast_str(const char *buffer, size_t size, void *data)
 {
 	struct ast_str **dst = data;
+	size_t str_size = ast_str_size(*dst);
+	size_t remaining = str_size - ast_str_strlen(*dst);
+	int ret;
+
 	/* While ast_str_append will grow the ast_str, it won't report
 	 * allocation errors. Fortunately, it's not that hard.
 	 */
-	size_t remaining = ast_str_size(*dst) - ast_str_strlen(*dst);
+
 	/* Remaining needs to be big enough for buffer, plus null char */
 	while (remaining < size + 1) {
 		/* doubling the size of the buffer gives us 'amortized
 		 * constant' time.
 		 * See http://stackoverflow.com/a/249695/115478 for info.
 		 */
-		int ret = ast_str_make_space(dst, 2 * ast_str_size(*dst));
-		if (ret == -1) {
-			/* Could not alloc; fail */
-			return -1;
-		}
-		remaining = ast_str_size(*dst) - ast_str_strlen(*dst);
-	}
+		str_size *= 2;
+		remaining = str_size - ast_str_strlen(*dst);
+	}
+
+	ret = ast_str_make_space(dst, str_size);
+	if (ret == -1) {
+		/* Could not alloc; fail */
+		return -1;
+	}
+
 	ast_str_append_substr(dst, -1, buffer, size);
 	return 0;
 }




More information about the asterisk-commits mailing list