[OpenWrt-Devel] [PATCH 3/7] libubus: add logic to reduce msgbuf data size after 16 small messages

Alexandru Ardelean ardeleanalex at gmail.com
Fri Jun 27 12:11:41 EDT 2014


---
 libubus-io.c | 13 +++++++++++++
 libubus.h    |  1 +
 2 files changed, 14 insertions(+)

diff --git a/libubus-io.c b/libubus-io.c
index 48bb72d..f38403f 100644
--- a/libubus-io.c
+++ b/libubus-io.c
@@ -28,6 +28,8 @@
 
 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
 
+#define UBUS_MSGBUF_REDUCTION_INTERVAL	16
+
 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
 	[UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
 	[UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
@@ -250,6 +252,17 @@ static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
 
 	len = blob_raw_len(&hdrbuf.data);
 	if (len > ctx->msgbuf_data_len) {
+		ctx->msgbuf_reduction_counter = UBUS_MSGBUF_REDUCTION_INTERVAL;
+	} else if (ctx->msgbuf_data_len > UBUS_MSG_CHUNK_SIZE) {
+		if (ctx->msgbuf_reduction_counter > 0) {
+			len = -1;
+			--ctx->msgbuf_reduction_counter;
+		} else
+			len = UBUS_MSG_CHUNK_SIZE;
+	} else
+		len = -1;
+
+	if (len > -1) {
 		ctx->msgbuf.data = realloc(ctx->msgbuf.data, len * sizeof(char));
 		if (ctx->msgbuf.data)
 			ctx->msgbuf_data_len = len;
diff --git a/libubus.h b/libubus.h
index 78ffa38..a63ce31 100644
--- a/libubus.h
+++ b/libubus.h
@@ -156,6 +156,7 @@ struct ubus_context {
 
 	struct ubus_msghdr_buf msgbuf;
 	uint32_t msgbuf_data_len;
+	int msgbuf_reduction_counter;
 };
 
 struct ubus_object_data {
-- 
1.8.4.5
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list