[sheepdog] [PATCH] corosync: avoid useless allocations in cdrv_cpg_deliver

Christoph Hellwig hch at infradead.org
Thu May 17 08:16:17 CEST 2012


Only two of the five cases in cdrv_cpg_deliver need to allocate a new
cevent structure.  Move the allocation of it to the places that actually
need it.

Signed-off-by: Christoph Hellwig <hch at lst.de>

Index: sheepdog/sheep/cluster/corosync.c
===================================================================
--- sheepdog.orig/sheep/cluster/corosync.c	2012-05-16 07:32:11.371931352 +0200
+++ sheepdog/sheep/cluster/corosync.c	2012-05-16 07:34:48.363929855 +0200
@@ -443,14 +443,8 @@ static void cdrv_cpg_deliver(cpg_handle_
 
 	dprintf("%d\n", cmsg->type);
 
-	cevent = zalloc(sizeof(*cevent));
-	if (!cevent)
-		panic("failed to allocate memory\n");
-
 	switch (cmsg->type) {
 	case COROSYNC_MSG_TYPE_JOIN_REQUEST:
-		free(cevent); /* we don't add a new cluster event in this case */
-
 		cevent = update_block_event(COROSYNC_EVENT_TYPE_JOIN, &cmsg->sender,
 					    cmsg->msg, cmsg->msg_len);
 		if (!cevent)
@@ -460,11 +454,12 @@ static void cdrv_cpg_deliver(cpg_handle_
 		cevent->msg_len = cmsg->msg_len;
 		break;
 	case COROSYNC_MSG_TYPE_BLOCK:
-		cevent->blocked = 1;
-		/* fall through */
 	case COROSYNC_MSG_TYPE_NOTIFY:
-		cevent->type = COROSYNC_EVENT_TYPE_NOTIFY;
+		cevent = zalloc(sizeof(*cevent));
+		if (!cevent)
+			panic("failed to allocate memory\n");
 
+		cevent->type = COROSYNC_EVENT_TYPE_NOTIFY;
 		cevent->sender = cmsg->sender;
 		cevent->msg_len = cmsg->msg_len;
 		if (cmsg->msg_len) {
@@ -475,9 +470,15 @@ static void cdrv_cpg_deliver(cpg_handle_
 		} else
 			cevent->msg = NULL;
 
+		if (cmsg->type == COROSYNC_MSG_TYPE_BLOCK)
+			cevent->blocked = 1;
+
 		list_add_tail(&cevent->list, &corosync_event_list);
 		break;
 	case COROSYNC_MSG_TYPE_LEAVE:
+		cevent = zalloc(sizeof(*cevent));
+		if (!cevent)
+			panic("failed to allocate memory\n");
 		cevent->type = COROSYNC_EVENT_TYPE_LEAVE;
 
 		master = is_master(&cmsg->sender);
@@ -498,8 +499,6 @@ static void cdrv_cpg_deliver(cpg_handle_
 		list_add_tail(&cevent->list, &corosync_event_list);
 		break;
 	case COROSYNC_MSG_TYPE_JOIN_RESPONSE:
-		free(cevent); /* we don't add a new cluster event in this case */
-
 		cevent = update_block_event(COROSYNC_EVENT_TYPE_JOIN, &cmsg->sender,
 					    cmsg->msg, cmsg->msg_len);
 		if (!cevent)
@@ -514,8 +513,6 @@ static void cdrv_cpg_deliver(cpg_handle_
 
 		break;
 	case COROSYNC_MSG_TYPE_UNBLOCK:
-		free(cevent); /* we don't add a new cluster event in this case */
-
 		cevent = update_block_event(COROSYNC_EVENT_TYPE_NOTIFY,
 					    &cmsg->sender, cmsg->msg, cmsg->msg_len);
 		if (!cevent)



More information about the sheepdog mailing list