Index: exec/totempg.c
===================================================================
--- exec/totempg.c	(revision 1493)
+++ exec/totempg.c	(revision 1496)
@@ -91,7 +91,6 @@
 #include <assert.h>
 #include <pthread.h>
 #include <errno.h>
-#include <unistd.h>
 
 #include "../include/hdb.h"
 #include "../include/list.h"
@@ -280,7 +279,7 @@
 	list_add (&assembly->list, &assembly_list_free);
 }
 
-static void app_confchg_fn (
+static inline void app_confchg_fn (
 	enum totem_configuration_type configuration_type,
 	unsigned int *member_list, int member_list_entries,
 	unsigned int *left_list, int left_list_entries,
@@ -313,7 +312,7 @@
 	}
 }
 
-static void group_endian_convert (
+static inline void group_endian_convert (
 	struct iovec *iovec)
 {
 	unsigned short *group_len;
@@ -324,9 +323,10 @@
 	for (i = 1; i < group_len[0] + 1; i++) {
 		group_len[i] = swab16(group_len[i]);
 	}
+
 }
 
-static int group_matches (
+static inline int group_matches (
 	struct iovec *iovec,
 	unsigned int iov_len,
 	struct totempg_group *groups_b,
@@ -346,8 +346,7 @@
 	 */
 	if ((size_t)iovec->iov_base % 4 != 0) {
 		iovec_aligned.iov_base = alloca(iovec->iov_len);
-		memcpy(iovec_aligned.iov_base, iovec->iov_base, iovec->iov_len);
-        iovec_aligned.iov_len = iovec->iov_len;
+		memcpy(iovec_aligned.iov_base, iovec->iov_base, iovec->iov_len);                iovec_aligned.iov_len = iovec->iov_len;
 		iovec = &iovec_aligned;
 	}
 
@@ -381,7 +380,7 @@
 }
 	
 
-static void app_deliver_fn (
+static inline void app_deliver_fn (
 	unsigned int nodeid,
 	struct iovec *iovec,
 	unsigned int iov_len,
@@ -394,17 +393,17 @@
 	unsigned int res;
         struct iovec aligned_iovec = { NULL, 0 };
 
+	if (endian_conversion_required) {
+		group_endian_convert (iovec);
+	}
+
 	/*
 	 * Align data structure for sparc and ia64
 	 */
 	aligned_iovec.iov_base = alloca(iovec->iov_len);
 	aligned_iovec.iov_len = iovec->iov_len;
-	assert (aligned_iovec.iov_base != NULL);
 	memcpy(aligned_iovec.iov_base, iovec->iov_base, iovec->iov_len);
 	iovec = &aligned_iovec;
-	if (endian_conversion_required) {
-		group_endian_convert (&aligned_iovec);
-	}
 
 	for (i = 0; i <= totempg_max_handle; i++) {
 		res = hdb_handle_get (&totempg_groups_instance_database,
@@ -412,9 +411,9 @@
 
 		if (res == 0) {
 			assert (iov_len == 1);
-			if (group_matches (&aligned_iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
-				stripped_iovec.iov_len = aligned_iovec.iov_len - adjust_iovec;
-//				stripped_iovec.iov_base = (char *)aligned_iovec.iov_base + adjust_iovec;
+			if (group_matches (iovec, iov_len, instance->groups, instance->groups_cnt, &adjust_iovec)) {
+				stripped_iovec.iov_len = iovec->iov_len - adjust_iovec;
+//				stripped_iovec.iov_base = (char *)iovec->iov_base + adjust_iovec;
 
 				/*
 				 * Align data structure for sparc and ia64
@@ -464,11 +463,11 @@
 	int endian_conversion_required)
 {
 	struct totempg_mcast *mcast;
-	unsigned short *msg_lens_from_msg;
-	unsigned short msg_lens[1500];
-	unsigned short mcast_msg_count;
+	unsigned short *msg_lens;
 	int i;
 	struct assembly *assembly;
+	char header[FRAME_SIZE_MAX];
+	int h_index;
 	int a_i = 0;
 	int msg_count;
 	int continuation;
@@ -477,15 +476,8 @@
 	assembly = assembly_ref (nodeid);
 	assert (assembly);
 
-	mcast = (struct totempg_mcast *)iovec[0].iov_base;
-	if (endian_conversion_required) {
-		mcast_msg_count = swab16 (mcast->msg_count);
-	} else {
-		mcast_msg_count = mcast->msg_count;
-	}
-	msg_count = mcast_msg_count;
-
 	/*
+	 * Assemble the header into one block of data and
 	 * assemble the packet contents into one block of data to simplify delivery
 	 */
 	if (iov_len == 1) {
@@ -496,36 +488,44 @@
 		char *data;
 		int datasize;
 
-		msg_lens_from_msg = (unsigned short *) (iovec[0].iov_base +
-			sizeof (struct totempg_mcast));
-		for (i = 0; i < mcast_msg_count; i++) {
-			msg_lens[i] = swab16 (msg_lens_from_msg[i]);
+		mcast = (struct totempg_mcast *)iovec[0].iov_base;
+		if (endian_conversion_required) {
+			mcast->msg_count = swab16 (mcast->msg_count);
 		}
 
+		msg_count = mcast->msg_count;
 		datasize = sizeof (struct totempg_mcast) +
-			mcast_msg_count * sizeof (unsigned short);
+			msg_count * sizeof (unsigned short);
 		
+		memcpy (header, iovec[0].iov_base, datasize);
 		assert(iovec);
 		data = iovec[0].iov_base;
 
-		memcpy (&assembly->data[assembly->index], &data[datasize],
-			iovec[0].iov_len - datasize);
-	} else {
-		msg_lens_from_msg = (unsigned short *)iovec[1].iov_base;
+		msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
 		if (endian_conversion_required) {
-			for (i = 0; i < mcast_msg_count; i++) {
-				msg_lens[i] = swab16 (msg_lens_from_msg[i]);
+			for (i = 0; i < mcast->msg_count; i++) {
+				msg_lens[i] = swab16 (msg_lens[i]);
 			}
-		} else {
-			for (i = 0; i < mcast_msg_count; i++) {
-				msg_lens[i] = msg_lens_from_msg[i];
-			}
 		}
 
+		memcpy (&assembly->data[assembly->index], &data[datasize],
+			iovec[0].iov_len - datasize);
+	} else {
 		/* 
 		 * The message originated from local processor  
-		 * because there is greater than one iovec for the full msg.
+		 * becasue there is greater than one iovec for then full msg.
 		 */
+		h_index = 0;
+		for (i = 0; i < 2; i++) {
+			memcpy (&header[h_index], iovec[i].iov_base, iovec[i].iov_len);
+			h_index += iovec[i].iov_len;
+		}
+
+		mcast = (struct totempg_mcast *)header;
+// TODO make sure we are using a copy of mcast not the actual data itself
+
+		msg_lens = (unsigned short *) (header + sizeof (struct totempg_mcast));
+
 		for (i = 2; i < iov_len; i++) {
 			a_i = assembly->index;
 			assert (iovec[i].iov_len + a_i <= MESSAGE_SIZE_MAX);
@@ -541,7 +541,7 @@
 	 * then adjust the assembly buffer so we can add the rest of the 
 	 * fragment when it arrives.
 	 */
-	msg_count = mcast->fragmented ? mcast_msg_count - 1 : mcast_msg_count;
+	msg_count = mcast->fragmented ? mcast->msg_count - 1 : mcast->msg_count;
 	continuation = mcast->continuation;
 	iov_delv.iov_base = &assembly->data[0];
 	iov_delv.iov_len = assembly->index + msg_lens[0];
@@ -567,8 +567,7 @@
 		}
 
 		if ((assembly->index == 0) ||
-			(!continuation && assembly->index)) {
-
+							(!continuation && assembly->index)) {
 			log_printf (totempg_log_level_error,
 				"Throwing away broken message: continuation %u, index %u\n",
 				continuation, assembly->index);
@@ -610,7 +609,7 @@
 		 * Message is fragmented, keep around assembly list
 		 */
 		assembly->last_frag_num = mcast->fragmented;
-		if (mcast_msg_count > 1) {
+		if (mcast->msg_count > 1) {
 			memmove (&assembly->data[0],
 				&assembly->data[assembly->index],
 				msg_lens[msg_count]);
