[maemo-commits] [maemo-commits] r15415 - in projects/haf/trunk/maemo-launcher: . launcher

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Wed Apr 16 01:35:35 EEST 2008
Author: guillem
Date: 2008-04-16 01:35:34 +0300 (Wed, 16 Apr 2008)
New Revision: 15415

Modified:
   projects/haf/trunk/maemo-launcher/ChangeLog
   projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
   projects/haf/trunk/maemo-launcher/launcher/comm_msg.h
   projects/haf/trunk/maemo-launcher/launcher/launcher.c
   projects/haf/trunk/maemo-launcher/launcher/test_msg.c
Log:
Add support for capped comm_msg


Modified: projects/haf/trunk/maemo-launcher/ChangeLog
===================================================================
--- projects/haf/trunk/maemo-launcher/ChangeLog	2008-04-15 22:35:32 UTC (rev 15414)
+++ projects/haf/trunk/maemo-launcher/ChangeLog	2008-04-15 22:35:34 UTC (rev 15415)
@@ -1,5 +1,18 @@
 2008-02-21  Guillem Jover  <guillem.jover at nokia.com>
 
+	* launcher/comm_msg.h (struct comm_msg): Add a size_max member.
+	(comm_msg_new): Add a size_max argument.
+	* launcher/comm_msg.c: Include <assert.h>.
+	(comm_msg_new): Assert that size is <= size_max when size_max is
+	in use. Initialize size_max.
+	(comm_msg_destroy): Reset size_max.
+	(comm_msg_grow): Check for size_max cap.
+	* launcher/launcher.c (store_state): Initialize comm_msg cap to 0.
+	* launcher/test_msg.c (main): Likewise. Add a test for a capped
+	comm_msg.
+
+2008-02-21  Guillem Jover  <guillem.jover at nokia.com>
+
 	* launcher/launcher.c (invoked_send_pid): Return status value from
 	invoked_send_action.
 	(invoked_send_exit): Likewise.

Modified: projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-04-15 22:35:32 UTC (rev 15414)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-04-15 22:35:34 UTC (rev 15415)
@@ -20,6 +20,7 @@
  *
  */
 
+#include <assert.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -34,10 +35,13 @@
 #define WORD_ALIGN(x)	(((x) + WORD_SIZE - 1) & WORD_MASK)
 
 comm_msg_t *
-comm_msg_new(uint32_t size)
+comm_msg_new(uint32_t size, size_t size_max)
 {
   comm_msg_t *msg;
 
+  if (size_max)
+    assert(size <= size_max);
+
   msg = malloc(sizeof(*msg));
   if (!msg)
   {
@@ -47,6 +51,7 @@
 
   msg->used = msg->read = 0;
   msg->size = WORD_ALIGN(size);
+  msg->size_max = WORD_ALIGN(size_max);
   msg->buf = malloc(msg->size);
   if (!msg->buf)
   {
@@ -63,7 +68,7 @@
 {
   if (msg->buf)
   {
-    msg->size = msg->used = msg->read = 0;
+    msg->size = msg->size_max = msg->used = msg->read = 0;
     free(msg->buf);
     msg->buf = NULL;
   }
@@ -82,9 +87,17 @@
 
   end_size = msg->size + need_size;
 
-  /* Add some more space. */
-  end_size += msg->size;
+  if (msg->size_max)
+  {
+    if (end_size > msg->size_max)
+      return false;
 
+    if (end_size + msg->size <= msg->size_max)
+      end_size += msg->size;
+  }
+  else
+    end_size += msg->size;
+
   p = realloc(msg->buf, end_size);
   if (!p)
     return false;

Modified: projects/haf/trunk/maemo-launcher/launcher/comm_msg.h
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.h	2008-04-15 22:35:32 UTC (rev 15414)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.h	2008-04-15 22:35:34 UTC (rev 15415)
@@ -28,12 +28,13 @@
 
 typedef struct comm_msg {
   uint32_t size;
+  uint32_t size_max;
   uint32_t used;
   uint32_t read;
   char *buf;
 } comm_msg_t;
 
-comm_msg_t *comm_msg_new(uint32_t size);
+comm_msg_t *comm_msg_new(uint32_t size, uint32_t size_max);
 bool comm_msg_destroy(comm_msg_t *msg);
 bool comm_msg_grow(comm_msg_t *msg, uint32_t need_size);
 bool comm_msg_reset(comm_msg_t *msg);

Modified: projects/haf/trunk/maemo-launcher/launcher/launcher.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/launcher.c	2008-04-15 22:35:32 UTC (rev 15414)
+++ projects/haf/trunk/maemo-launcher/launcher/launcher.c	2008-04-15 22:35:34 UTC (rev 15415)
@@ -564,7 +564,7 @@
     return false;
   }
 
-  msg = comm_msg_new(512);
+  msg = comm_msg_new(512, 0);
 
   comm_msg_pack_str(msg, LAUNCHER_STATE_SIG);
   comm_msg_pack_int(msg, invoker_fd);
@@ -603,7 +603,7 @@
     return NULL;
   }
 
-  msg = comm_msg_new(512);
+  msg = comm_msg_new(512, 0);
   comm_msg_recv(fd, msg);
 
   close(fd);

Modified: projects/haf/trunk/maemo-launcher/launcher/test_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/test_msg.c	2008-04-15 22:35:32 UTC (rev 15414)
+++ projects/haf/trunk/maemo-launcher/launcher/test_msg.c	2008-04-15 22:35:34 UTC (rev 15415)
@@ -44,7 +44,7 @@
 
 	/* Serializing. */
 
-	msg = comm_msg_new(20);
+	msg = comm_msg_new(20, 0);
 
 	comm_msg_pack_int(msg, w1);
 	comm_msg_pack_int(msg, w2);
@@ -64,7 +64,7 @@
 
 	/* Deserializing. */
 
-	msg = comm_msg_new(5);
+	msg = comm_msg_new(5, 0);
 
 	fd = open(test_file, O_RDONLY, 0644);
 	if (fd < 0)
@@ -72,7 +72,7 @@
 
 	comm_msg_recv(fd, msg);
 
-	tests_init(6);
+	tests_init(7);
 
 	if (comm_msg_unpack_int(msg, &w))
 		test_cmp_int(w1, w);
@@ -93,6 +93,16 @@
 
 	unlink(test_file);
 
+	/* Capped buffer. */
+
+	msg = comm_msg_new(4, 8);
+
+	comm_msg_pack_int(msg, w1);
+	comm_msg_pack_int(msg, w2);
+	test_failure(comm_msg_pack_int(msg, w3));
+
+	comm_msg_destroy(msg);
+
 	return tests_summary() ? 0 : 1;
 }
 


More information about the maemo-commits mailing list