Index: /mpich2/branches/dev/NemesisOnWindows/src/mpi/errhan/errnames.txt
===================================================================
--- /mpich2/branches/dev/NemesisOnWindows/src/mpi/errhan/errnames.txt (revision 3487)
+++ /mpich2/branches/dev/NemesisOnWindows/src/mpi/errhan/errnames.txt (revision 3727)
@@ -580,4 +580,5 @@
 **sock_read:read from socket failed
 **sock_read %s %d:read from socket failed, %s (errno %d)
+# FIXME: ambiguous name for sock write failure
 **write %s:write to socket failed - %s
 **writev %s:writev to socket failed - %s
@@ -623,4 +624,10 @@
 **sock_iocp:unable to create an I/O completion port
 **sock_iocp %d:unable to create an I/O completion port (errno %d)
+**sockw_badtvalhnd: Invalid handle to timeval
+**sockw_badwsethnd: Invalid handle to waitSet
+**sockw_badwsetshnd: Invalid handle to waitSet sock handle
+**shmw_badhnd: Invalid handle to shared memory
+**shmw_gethnd: Unable to get shared memory handle
+**shmw_sethnd: Unable to set shared memory handle
 #
@@ -968,4 +975,6 @@
 **mkstemp:mkstemp failed
 **mkstemp %s:mkstemp failed %s
+**lseek:lseek failed
+**lseek %s:lseek failed - %s
 **fdopen:fdopen failed
 **fdopen %s:fdopen failed %s
Index: /mpich2/branches/dev/NemesisOnWindows/src/mpid/ch3/channels/nemesis/nemesis/src/mpid_nem_alloc.c
===================================================================
--- /mpich2/branches/dev/NemesisOnWindows/src/mpid/ch3/channels/nemesis/nemesis/src/mpid_nem_alloc.c (revision 3601)
+++ /mpich2/branches/dev/NemesisOnWindows/src/mpid/ch3/channels/nemesis/nemesis/src/mpid_nem_alloc.c (revision 3727)
@@ -195,5 +195,5 @@
             MPIU_ERR_CHKANDJUMP1 (pmi_errno != PMI_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**pmi_kvs_get", "**pmi_kvs_get %d", pmi_errno);
-            mpi_errno = MPIU_SHMW_shmHandle_deserialize(memory->handle, val, val_max_sz);
+            mpi_errno = MPIU_SHMW_shmHandle_deserialize(memory->handle, val, strlen(val));
             if(mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
Index: /mpich2/branches/dev/NemesisOnWindows/src/mpid/ch3/channels/nemesis/nemesis/src/mpid_nem_lmt_shm.c
===================================================================
--- /mpich2/branches/dev/NemesisOnWindows/src/mpid/ch3/channels/nemesis/nemesis/src/mpid_nem_lmt_shm.c (revision 3601)
+++ /mpich2/branches/dev/NemesisOnWindows/src/mpid/ch3/channels/nemesis/nemesis/src/mpid_nem_lmt_shm.c (revision 3727)
@@ -255,5 +255,5 @@
     if (vc_ch->lmt_copy_buf == NULL){
-        mpi_errno = MPIU_SHMW_shmHandle_deserialize(vc_ch->lmt_copy_buf_handle, r_cookie.MPID_IOV_BUF, r_cookie.MPID_IOV_LEN);
+        mpi_errno = MPIU_SHMW_shmHandle_deserialize(vc_ch->lmt_copy_buf_handle, r_cookie.MPID_IOV_BUF, strlen(r_cookie.MPID_IOV_BUF));
         if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
@@ -277,5 +277,5 @@
             if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
-            mpi_errno = MPIU_SHMW_shmHandle_deserialize(vc_ch->lmt_copy_buf_handle, r_cookie.MPID_IOV_BUF, r_cookie.MPID_IOV_LEN);
+            mpi_errno = MPIU_SHMW_shmHandle_deserialize(vc_ch->lmt_copy_buf_handle, r_cookie.MPID_IOV_BUF, strlen(r_cookie.MPID_IOV_BUF));
             if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
Index: /mpich2/branches/dev/NemesisOnWindows/src/util/wrappers/mpiu_sock_wrappers.h
===================================================================
--- /mpich2/branches/dev/NemesisOnWindows/src/util/wrappers/mpiu_sock_wrappers.h (revision 3688)
+++ /mpich2/branches/dev/NemesisOnWindows/src/util/wrappers/mpiu_sock_wrappers.h (revision 3727)
@@ -15,4 +15,5 @@
 #include "mpiiov.h"
 #include "mpierror.h"
+#include "mpierrs.h"
 #include "mpimem.h"
 #include "mpidbg.h"
@@ -49,16 +50,16 @@
     static inline int MPIU_SOCKW_init(){
         WSADATA wsaData;
-        int mpi_errno = MPI_SUCCESS;
         /* Init winsock */
         if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
             int err;
             err = MPIU_OSW_getErrno();
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
+            return MPIR_Err_create_code(MPI_SUCCESS,
                 MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
                 "**wsastartup", "**wsastartup %s %d",
                 MPIU_OSW_strerror(err), err);
-            return mpi_errno;
         }
-        return mpi_errno;
+        else{
+            return MPI_SUCCESS;
+        }
     }
@@ -138,31 +139,32 @@
     unsigned short int highP){
     unsigned short int curPort=0;
-    if(!(pSin)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "pointer to sockaddr_in");
-    }
+    int mpi_errno = MPI_SUCCESS;
+    int isBindSuccess = 0;
+
+    MPIU_ERR_CHKANDJUMP1(!pSin, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to sockaddr_in");
+
     for(curPort = lowP; curPort <= highP; curPort++){
         (pSin)->sin_port = htons(curPort);
         if(bind(sock, (struct sockaddr *)(pSin),
             sizeof(struct sockaddr_in)) != SOCKET_ERROR){
-            return MPI_SUCCESS;
+            isBindSuccess = 1;
+            break;
         }
         else{
             int err;
             err = MPIU_OSW_getErrno();
-            if( (err != WSAEADDRINUSE) &&
-                (err != WSAEADDRNOTAVAIL)){
-                return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                        FCNAME, __LINE__, MPI_ERR_OTHER, "**bind",
-                        "**bind %s %d",
-                        MPIU_OSW_strerror(MPIU_OSW_getErrno()),
-                        MPIU_OSW_getErrno());
-            }
+            MPIU_ERR_CHKANDJUMP2( ((err != WSAEADDRINUSE) &&
+                (err != WSAEADDRNOTAVAIL)), mpi_errno, MPI_ERR_OTHER,
+                "**bind", "**bind %s %d", MPIU_OSW_strerror(err), err);
         }
     }
-    return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, FCNAME,
-            __LINE__, MPI_ERR_OTHER, "**bindPortRange",
-            "**bindPortRange %d %d", lowP, highP);
+    MPIU_ERR_CHKANDJUMP2(!isBindSuccess, mpi_errno, MPI_ERR_OTHER,
+        "**bindPortRange", "**bindPortRange %d %d", lowP, highP);
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -208,28 +210,24 @@
     MPIU_SOCKW_sockfd_t sockfd, struct sockaddr *pAddr,
     int addrLen, int *pIsPendingFlag){
-    if(connect(sockfd, pAddr, addrLen) != SOCKET_ERROR){
-        return MPI_SUCCESS;
-    }
-    else{
-        if(MPIU_OSW_getErrno() == WSAEWOULDBLOCK){
-            if(pIsPendingFlag){
-                *pIsPendingFlag = 1;
-                return MPI_SUCCESS;
-            }
-            else{
-                return MPIR_Err_create_code(MPI_SUCCESS,
-                    MPIR_ERR_FATAL, FCNAME, __LINE__,
-                    MPI_ERR_OTHER, "**arg", "**arg %s",
-                    "pointer to connection pending flag");
-            }
-        }
-        else{
-            return MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**sock_connect", "**sock_connect %s %d",
-                MPIU_OSW_strerror(MPIU_OSW_getErrno()),
-                MPIU_OSW_getErrno());
-        }
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    if(connect(sockfd, pAddr, addrLen) == SOCKET_ERROR){
+        int err;
+        err = MPIU_OSW_getErrno();
+
+        MPIU_ERR_CHKANDJUMP2((err != WSAEWOULDBLOCK), mpi_errno,
+            MPI_ERR_OTHER, "**sock_connect", "**sock_connect %s %d",
+            MPIU_OSW_strerror(err), err);
+
+        MPIU_ERR_CHKANDJUMP1(!pIsPendingFlag, mpi_errno,
+            MPI_ERR_OTHER, "**arg", "**arg %s",
+            "pointer to connection pending flag");
+
+        *pIsPendingFlag = 1;
+    }
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -262,22 +260,21 @@
     MPID_IOV *iov, int iovcnt, int *pBytesRead){
     DWORD flags = 0;
+    int mpi_errno = MPI_SUCCESS;
     if(WSARecv(sock, iov, iovcnt,  pBytesRead, &flags, NULL, NULL)
-        != SOCKET_ERROR){
-        return MPI_SUCCESS;
-    }
-    else{
+        == SOCKET_ERROR){
         int err;
         err = MPIU_OSW_getErrno();
-        if((err == WSAEWOULDBLOCK) || (err == WSAEINTR)){
-            *pBytesRead = -1;
-            return MPI_SUCCESS;
-        }
-        else{
-            return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**sock_read",
-                "**sock_read %s %d",
-                MPIU_OSW_strerror(err), err);
-        }
-    }
+        MPIU_ERR_CHKANDJUMP2(!( (err == WSAEWOULDBLOCK) ||
+            (err == WSAEINTR) ), mpi_errno, MPI_ERR_OTHER,
+            "**sock_read", "**sock_read %s %d",
+            MPIU_OSW_strerror(err), err);
+
+        *pBytesRead = -1;
+    }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -381,4 +378,5 @@
     if(getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&hasError,
             &optlen) != SOCKET_ERROR){
+        return MPI_SUCCESS;
     }
     else{
@@ -462,5 +460,5 @@
 typedef MPIU_SOCKW_timeval_t_ *MPIU_SOCKW_timevalHnd_t;
-#   define MPIU_SOCKW_timeValHnd_isInit_(hnd)  ((hnd) ? 1 : 0)
+#   define MPIU_SOCKW_timevalHnd_isInit_(hnd)  ((hnd) ? 1 : 0)
 #   define MPIU_SOCKW_waitSetHnd_isInit_(hnd)  ((hnd) ? 1 : 0)
 #   define MPIU_SOCKW_waitSetSockHnd_isInit_(hnd)   ((hnd) ? 1 : 0)
@@ -472,17 +470,18 @@
 static inline int MPIU_SOCKW_timevalHnd_init(
     MPIU_SOCKW_timevalHnd_t *pHnd){
-    if(!pHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "pointer to timeval handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP1(!pHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to timeval handle");
+
     *pHnd = (MPIU_SOCKW_timevalHnd_t)
                 MPIU_Malloc(sizeof(MPIU_SOCKW_timeval_t_));
-    if(!(*pHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**nomem",
-                "**nomem %s", "handle to timeval");
-    }
-    return MPI_SUCCESS;
+    MPIU_ERR_CHKANDJUMP1( !(*pHnd), mpi_errno, MPI_ERR_OTHER,
+        "**nomem", "**nomem %s", "handle to timeval");
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -496,12 +495,16 @@
 static inline int MPIU_SOCKW_timevalHnd_set(MPIU_SOCKW_timevalHnd_t hnd,
     int timeInMS){
-    if(!MPIU_SOCKW_timeValHnd_isInit_(hnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "timeval handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_timevalHnd_isInit_(hnd), mpi_errno,
+        MPI_ERR_OTHER, "**sockw_badtvalhnd");
+
     hnd->tv_sec = timeInMS/1000;
     hnd->tv_usec = (timeInMS % 1000) * 1000;
-    return MPI_SUCCESS;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -512,14 +515,18 @@
 static inline int MPIU_SOCKW_timevalHnd_finalize(
     MPIU_SOCKW_timevalHnd_t *pHnd){
-    if(!pHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "pointer to timeval handle");
-    }
-    if(MPIU_SOCKW_timeValHnd_isInit_(*pHnd)){
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP1(!pHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to timeval handle");
+
+    if(MPIU_SOCKW_timevalHnd_isInit_(*pHnd)){
         MPIU_Free(*pHnd);
     }
     *pHnd = NULL;
-    return MPI_SUCCESS;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -545,49 +552,43 @@
 static inline int MPIU_SOCKW_waitSetHnd_init(
             MPIU_SOCKW_waitSetHnd_t *pHnd, int nfds){
-    if(!pHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "pointer to waitSet handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP1(!pHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to waitset Handle");
+    MPIU_ERR_CHKANDJUMP1((nfds > FD_SETSIZE), mpi_errno,
+        MPI_ERR_OTHER, "**nomem", "**nomem %s", "select fdset");
+
     if(nfds <= 0){
         nfds = FD_SETSIZE;
     }
-    if(nfds > FD_SETSIZE){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**nomem",
-                "**nomem %s", "select fdset");
-    }
-
     *pHnd = (MPIU_SOCKW_waitSetHnd_t) MPIU_Malloc(
             sizeof(MPIU_SOCKW_waitSet_));
-    if(*pHnd){
-        (*pHnd)->nEvents = 0;
-        FD_ZERO(&((*pHnd)->readfds));
-        (*pHnd)->nReadfds = 0;
-        FD_ZERO(&((*pHnd)->writefds));
-        (*pHnd)->nWritefds = 0;
-        FD_ZERO(&((*pHnd)->exceptfds));
-        (*pHnd)->nExceptfds = 0;
-        (*pHnd)->nfds = nfds;
-        (*pHnd)->fdSetIndex = 0;
-        MPIU_SOCKW_WAITSET_CURINDEX_RESET_(*pHnd);
-
-        /* FIXME: Cheating - Expand dynamically */
-        (*pHnd)->fdSet = (MPIU_SOCKW_waitSetSockHnd_ *)
-            MPIU_Malloc(FD_SETSIZE * sizeof(MPIU_SOCKW_waitSetSockHnd_));
-
-        if(!((*pHnd)->fdSet)){
-            return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**nomem",
-                "**nomem %s", "fdSet array in waitSet");
-        }
-    }
-    else{
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**nomem",
-                "**nomem %s", "handle to waitSet");
-    }
-    return MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP1(!(*pHnd), mpi_errno, MPI_ERR_OTHER,
+        "**nomem", "**nomem %s", "handle to waitset");
+
+    (*pHnd)->nEvents = 0;
+    FD_ZERO(&((*pHnd)->readfds));
+    (*pHnd)->nReadfds = 0;
+    FD_ZERO(&((*pHnd)->writefds));
+    (*pHnd)->nWritefds = 0;
+    FD_ZERO(&((*pHnd)->exceptfds));
+    (*pHnd)->nExceptfds = 0;
+    (*pHnd)->nfds = nfds;
+    (*pHnd)->fdSetIndex = 0;
+    MPIU_SOCKW_WAITSET_CURINDEX_RESET_(*pHnd);
+
+    /* FIXME: Cheating - Expand dynamically */
+    (*pHnd)->fdSet = (MPIU_SOCKW_waitSetSockHnd_ *)
+        MPIU_Malloc(FD_SETSIZE * sizeof(MPIU_SOCKW_waitSetSockHnd_));
+
+    MPIU_ERR_CHKANDJUMP1(!((*pHnd)->fdSet), mpi_errno, MPI_ERR_OTHER,
+        "**nomem", "**nomem %s", "fdSet array in waitSet");
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -598,15 +599,20 @@
 static inline int MPIU_SOCKW_waitSetHnd_finalize(
             MPIU_SOCKW_waitSetHnd_t *pHnd){
-    if(!pHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "pointer to waitSet handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP1(!pHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to waitSet handle");
+
     if(MPIU_SOCKW_waitSetHnd_isInit_(*pHnd)){
         MPIU_Free((*pHnd)->fdSet);
         MPIU_Free(*pHnd);
     }
+
     *pHnd = NULL;
-    return MPI_SUCCESS;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -626,15 +632,10 @@
     MPIU_SOCKW_waitSetHnd_t hnd, MPIU_SOCKW_timevalHnd_t timeout){
     int nfds=0;
-
-    if(!MPIU_SOCKW_waitSetHnd_isInit_(hnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet handle");
-    }
-    if(!MPIU_SOCKW_timeValHnd_isInit_(timeout)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "timeVal handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetHnd_isInit_(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsethnd");
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_timevalHnd_isInit_(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badtvalhnd");
     MPIU_SOCKW_WAITSET_CURINDEX_RESET_(hnd);
@@ -652,16 +653,14 @@
     }
     nfds = 0; /* ignored in windows */
-    if((hnd->nEvents = select(nfds, &(hnd->tmpReadfds),
+    MPIU_ERR_CHKANDJUMP2(((hnd->nEvents=select(nfds,&(hnd->tmpReadfds),
         &(hnd->tmpWritefds), &(hnd->tmpExceptfds), timeout))
-        != SOCKET_ERROR){
-        return MPI_SUCCESS;
-    }
-    else{
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**select",
-                "**select %s %d",
-                MPIU_OSW_strerror(MPIU_OSW_getErrno()),
-                MPIU_OSW_getErrno());
-    }
+        == SOCKET_ERROR), mpi_errno, MPI_ERR_OTHER, "**select",
+        "**select %s %d", MPIU_OSW_strerror(MPIU_OSW_getErrno()),
+        MPIU_OSW_getErrno());
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -715,15 +714,10 @@
     MPIU_SOCKW_waitSetSockHnd_t *pSockHnd){
     int i;
-    if(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet handle");
-    }
-
-    if(!pSockHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "ptr to sock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsethnd");
+    MPIU_ERR_CHKANDJUMP1(!pSockHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "ptr to sock handle");
     *pSockHnd = NULL;
@@ -742,5 +736,9 @@
         MPIU_SOCKW_WAITSET_CURINDEX_INC_(waitSetHnd);
     }
-    return MPI_SUCCESS;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -753,21 +751,18 @@
     int flag, void *user_ptr, MPIU_SOCKW_waitSetSockHnd_t *pSockHnd){
     int index= 0;
-    if(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet handle");
-    }
-    if(!pSockHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet sock handle");
-    }
-    else{
-        *pSockHnd = NULL;
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsethnd");
+    MPIU_ERR_CHKANDJUMP1(!pSockHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "waitSet sock handle");
+
+    *pSockHnd = NULL;
     index = waitSetHnd->fdSetIndex;
     if(index >= waitSetHnd->nfds){
         MPIU_SOCKW_WAITSET_FREEINDEX_GET_(waitSetHnd, &index);
+        /* FIXME: Try not to use MPIU_Assert(). Utils should not depend
+         * on a device impl of assert */
         MPIU_Assert(index != MPIU_SOCKW_WAITSET_CURINDEX_INVALID_);
     }
@@ -794,5 +789,8 @@
     *pSockHnd = &(waitSetHnd->fdSet[index]);
-    return MPI_SUCCESS;
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -805,14 +803,11 @@
     MPIU_SOCKW_waitSetSockHnd_t sockHnd, int flag){
     MPIU_SOCKW_sockfd_t sock;
-    if(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet handle");
-    }
-    if(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSetSock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsethnd");
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsetshnd");
+
     sock = sockHnd->sockfd;
     if((flag & MPIU_SOCKW_FLAG_WAITON_RD) &&
@@ -831,5 +826,9 @@
         waitSetHnd->nExceptfds++;
     }
-    return MPI_SUCCESS;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -842,14 +841,11 @@
     MPIU_SOCKW_waitSetSockHnd_t sockHnd, int flag){
     MPIU_SOCKW_sockfd_t sock;
-    if(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet handle");
-    }
-    if(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSetSock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsethnd");
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsetshnd");
+
     sock = sockHnd->sockfd;
     if((flag & MPIU_SOCKW_FLAG_WAITON_RD) &&
@@ -868,5 +864,9 @@
         waitSetHnd->nExceptfds--;
     }
-    return MPI_SUCCESS;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -880,20 +880,14 @@
     MPIU_SOCKW_waitSetSockHnd_t *pSockHnd){
     MPIU_SOCKW_sockfd_t sock;
-    if(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSet handle");
-    }
-    if(!pSockHnd){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "pointer to waitSetSock handle");
-    }
-
-    if(!MPIU_SOCKW_waitSetSockHnd_isInit_(*pSockHnd)){
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSetSock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetHnd_isInit_(waitSetHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsethnd");
+    MPIU_ERR_CHKANDJUMP1(!pSockHnd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to waitSet sock handle");
+    MPIU_ERR_CHKANDJUMP(
+        !MPIU_SOCKW_waitSetSockHnd_isInit_(*pSockHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsetshnd");
+
     sock = (*pSockHnd)->sockfd;
     if(FD_ISSET(sock, &(waitSetHnd->readfds))){
@@ -915,5 +909,8 @@
     *pSockHnd = NULL;
-    return MPI_SUCCESS;
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -925,20 +922,17 @@
     MPIU_SOCKW_waitSetSockHnd_t sockHnd,
     MPIU_SOCKW_sockfd_t *pSockfd){
-    if(MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd)){
-        if(pSockfd){
-            *pSockfd = sockHnd->sockfd;
-            return MPI_SUCCESS;
-        }
-        else{
-            return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                    FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                    "**arg %s", "pointer to sock fd");
-        }
-    }
-    else{
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSetSock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsetshnd");
+    MPIU_ERR_CHKANDJUMP1(!pSockfd, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to sock fd");
+
+    *pSockfd = sockHnd->sockfd;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -949,20 +943,17 @@
 static inline int MPIU_SOCKW_waitSetSockHnd_getUserPtr(
     MPIU_SOCKW_waitSetSockHnd_t sockHnd, void **pUserPtr){
-    if(MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd)){
-        if(pUserPtr){
-            *pUserPtr = sockHnd->user_ptr;
-            return MPI_SUCCESS;
-        }
-        else{
-            return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                    FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                    "**arg %s", "pointer to user ptr");
-        }
-    }
-    else{
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSetSock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsetshnd");
+    MPIU_ERR_CHKANDJUMP1(!pUserPtr, mpi_errno, MPI_ERR_OTHER,
+        "**arg", "**arg %s", "pointer to user ptr");
+
+    *pUserPtr = sockHnd->user_ptr;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -973,13 +964,15 @@
 static inline int MPIU_SOCKW_waitSetSockHnd_setUserPtr(
     MPIU_SOCKW_waitSetSockHnd_t sockHnd, void *userPtr){
-    if(MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd)){
-        sockHnd->user_ptr = userPtr;
-        return MPI_SUCCESS;
-    }
-    else{
-        return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**arg",
-                "**arg %s", "waitSetSock handle");
-    }
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SOCKW_waitSetSockHnd_isInit_(sockHnd),
+        mpi_errno, MPI_ERR_OTHER, "**sockw_badwsetshnd");
+
+    sockHnd->user_ptr = userPtr;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
Index: /mpich2/branches/dev/NemesisOnWindows/src/util/wrappers/mpiu_shm_wrappers.h
===================================================================
--- /mpich2/branches/dev/NemesisOnWindows/src/util/wrappers/mpiu_shm_wrappers.h (revision 3696)
+++ /mpich2/branches/dev/NemesisOnWindows/src/util/wrappers/mpiu_shm_wrappers.h (revision 3727)
@@ -35,8 +35,11 @@
     #include<winsock2.h>
     #include<windows.h>
+#else
+#   error "No shared memory mechanism specified"
 #endif
 #include "mpi.h"
 #include "mpierror.h"
+#include "mpierrs.h"
 #include "mpimem.h"
 #include "mpiimplthread.h"
@@ -45,11 +48,4 @@
 #include "mpiu_util_wrappers.h"
-/* FIXME: Move the code related to global handle to a common blk */
-
-#ifdef  USE_SYSV_SHM
-/************************** Sys V shm **************************/
-typedef int MPIU_SHMW_shmLHandle_t;
-typedef char * MPIU_SHMW_shmGHandle_t;
-
 #define MPIU_SHMW_FLAG_CLEAR             0x0
 #define MPIU_SHMW_FLAG_SHMCREATENEW      0x1
@@ -57,4 +53,13 @@
 #define MPIU_SHMW_FLAG_SHMGHANDLESTATIC  0x100
+#ifdef USE_SYSV_SHM
+typedef int MPIU_SHMW_shmLHandle_t;
+#elif defined USE_MMAP_SHM
+typedef int MPIU_SHMW_shmLHandle_t;
+#elif defined USE_NT_SHM
+typedef HANDLE MPIU_SHMW_shmLHandle_t;
+#endif
+
+typedef char * MPIU_SHMW_shmGHandle_t;
 /* The local handle, lHnd, is valid only for the current process,
  * The global handle, gHnd, is valid across multiple processes
@@ -70,36 +75,98 @@
 typedef MPIU_SHMW_shmLGHandle_t * MPIU_SHMW_shmHandle_t;
+#define MPIU_SHMW_SHMHANDLE_INVALID     NULL
+#define MPIU_SHMW_SHMGHANDLE_INVALID    NULL
+#define MPIU_SHMW_SHMGHANDLE_INITVAL    '\0'
+
+#ifdef  USE_SYSV_SHM
+/************************** Sys V shm **************************/
+#define MPIU_SHMW_MAX_INT_STR_LEN   20
+#define MPIU_SHMW_SHMSEGKEY_LEN    MPIU_SHMW_MAX_INT_STR_LEN
+#define MPIU_SHMW_SHMGHANDLE_SIZE MPIU_SHMW_SHMSEGKEY_LEN
+#define MPIU_SHMW_SHMLHANDLE_INVALID    -1
+#define MPIU_SHMW_SHMLHANDLE_INITVAL    -1
+
+#define MPIU_SHMW_SHMSEG_ALREADY_EXISTS EEXIST
+
 /* These macros are the setters/getters for the shm handle */
 #define MPIU_SHMW_shmLHandle_get(hnd)   ((hnd)->lHnd)
 #define MPIU_SHMW_shmLHandle_set(hnd, val)  ((hnd)->lHnd=val)
-#define MPIU_SHMW_shmGHandle_getByRef(hnd)   ((hnd)->gHnd)
-#define MPIU_SHMW_shmGHandle_setByRef(hnd, val) ((hnd)->gHnd = val)
-
-#define MPIU_SHMW_MAX_INT_STR_LEN   20
-#define MPIU_SHMW_SHMSEGKEY_LEN    MPIU_SHMW_MAX_INT_STR_LEN
-#define MPIU_SHMW_SHMGHANDLE_SIZE MPIU_SHMW_SHMSEGKEY_LEN
+
+#define MPIU_SHMW_shmLHandle_isValid(hnd) (                         \
+    ((hnd)->lHnd == MPIU_SHMW_SHMLHANDLE_INVALID) ? 0 : 1           \
+)
+#define MPIU_SHMW_shmLHandle_isInit(hnd) 1
+/* Nothing to be done at close */
+#define MPIU_SHMW_shmLHandle_close(hnd) 1
+
+#elif defined USE_MMAP_SHM
+/************************** MMAP shm **************************/
+#define MPIU_SHMW_SHMFNAME_LEN    50
+#define MPIU_SHMW_SHMGHANDLE_SIZE MPIU_SHMW_SHMFNAME_LEN
+#define MPIU_SHMW_SHMLHANDLE_INVALID    -1
+#define MPIU_SHMW_SHMLHANDLE_INITVAL    -1
+
+#define MPIU_SHMW_SHMSEG_ALREADY_EXISTS EEXIST
+
+/* These macros are the setters/getters for the shm handle */
+#define MPIU_SHMW_shmLHandle_get(hnd)   ((hnd)->lHnd)
+#define MPIU_SHMW_shmLHandle_set(hnd, val)  ((hnd)->lHnd=val)
+
+#define MPIU_SHMW_shmLHandle_isValid(hnd) (                         \
+    ((hnd)->lHnd == MPIU_SHMW_SHMLHANDLE_INVALID) ? 0 : 1           \
+)
+#define MPIU_SHMW_shmLHandle_isInit(hnd) 1
+/* Returns 0 on error, non-zero on success */
+#define MPIU_SHMW_shmLHandle_close(hnd) (                           \
+    (!close(MPIU_SHMW_shmLHandle_get(hnd)))                         \
+    ? (MPIU_SHMW_shmLHandle_set(hnd, MPIU_SHMW_SHMLHANDLE_INITVAL)) \
+    : 0                                                             \
+)
+
+#elif defined USE_NT_SHM
+/************************** NT shm **************************/
+#define MPIU_SHMW_SHMSEGNAME_LEN    70
+#define MPIU_SHMW_SHMGHANDLE_SIZE MPIU_SHMW_SHMSEGNAME_LEN
+#define MPIU_SHMW_SHMLHANDLE_INVALID    INVALID_HANDLE_VALUE
+#define MPIU_SHMW_SHMLHANDLE_INITVAL    INVALID_HANDLE_VALUE
+
+#define MPIU_SHMW_SHMSEG_ALREADY_EXISTS ERROR_ALREADY_EXISTS
+
+/* These macros are the setters/getters for the shm handle */
+#define MPIU_SHMW_shmLHandle_get(hnd)   ((hnd)->lHnd)
+#define MPIU_SHMW_shmLHandle_set(hnd, val)  ((hnd)->lHnd=val)
+
+#define MPIU_SHMW_shmLHandle_isValid(hnd) (                         \
+    ((hnd)->lHnd == MPIU_SHMW_SHMLHANDLE_INVALID) ? 0 : 1           \
+)
+#define MPIU_SHMW_shmLHandle_isInit(hnd) 1
+/* Returns 0 on error, non-zero on success */
+#define MPIU_SHMW_shmLHandle_close(hnd)(                            \
+    (MPIU_SHMW_shmLHandle_isValid(hnd))?CloseHandle((hnd)->lHnd) : 1\
+)
+
+#define MPIU_SHMW_shmGHandle_setUniq(hnd) (                         \
+    (MPIU_SHMW_shmHandle_refAlloc(hnd)) ?                           \
+    MPIU_OSW_getUniqStr((hnd)->gHnd, MPIU_SHMW_SHMGHANDLE_SIZE) :   \
+    0                                                               \
+)
+#endif
+
 #define MPIU_SHMW_SHMHANDLE_SIZE    (sizeof(MPIU_SHMW_shmLGHandle_t))
 #define MPIU_SHMW_SHMSERIALIZEDHANDLE_SIZE MPIU_SHMW_SHMGHANDLE_SIZE
-#define MPIU_SHMW_SHMHANDLE_INVALID     NULL
-#define MPIU_SHMW_SHMLHANDLE_INVALID    -1
-#define MPIU_SHMW_SHMLHANDLE_INITVAL    -1
-#define MPIU_SHMW_SHMGHANDLE_INVALID    NULL
-#define MPIU_SHMW_SHMGHANDLE_INITVAL    '\0'
-
-#define MPIU_SHMW_SHMSEG_ALREADY_EXISTS EEXIST
-
-#define MPIU_SHMW_shmHandle_resetVal(hnd) do{                       \
-    (hnd)->lHnd = MPIU_SHMW_SHMLHANDLE_INITVAL;                     \
-    if((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC){              \
-        (hnd)->gHnd = MPIU_SHMW_SHMGHANDLE_INVALID;                 \
-    }                                                               \
-    else{                                                           \
-        ((hnd)->gHnd)[0] = MPIU_SHMW_SHMGHANDLE_INITVAL;            \
-    }                                                               \
-}while(0)
-
-#define MPIU_SHMW_shmLHandle_isValid(hnd) (                         \
-    ((hnd)->lHnd == MPIU_SHMW_SHMLHANDLE_INVALID) ? 0 : 1           \
+/* These macros are the setters/getters for the shm handle */
+#define MPIU_SHMW_shmGHandle_getByRef(hnd)   ((hnd)->gHnd)
+/* Returns a negative val on error, non-negative val on success */
+#define MPIU_SHMW_shmGHandle_getByVal(hnd, str, strlen)  (          \
+    MPIU_Snprintf(str, strlen, "%s",                                \
+        MPIU_SHMW_shmGHandle_getByRef(hnd))                         \
+)
+#define MPIU_SHMW_shmGHandle_setByRef(hnd, val) ((hnd)->gHnd = val)
+/* Returns a negative val on error, non-negative val on success */
+/* FIXME: What if val is a non-null terminated string ? */
+#define MPIU_SHMW_shmGHandle_setByVal(hnd, fmt, val) (              \
+    MPIU_Snprintf(MPIU_SHMW_shmGHandle_getByRef(hnd),               \
+        MPIU_SHMW_SHMGHANDLE_SIZE, fmt, val)                        \
 )
@@ -108,4 +175,23 @@
         (strlen((hnd)->gHnd) == 0)) ? 0 : 1                         \
 )
+#define MPIU_SHMW_shmGHandle_isInit(hnd) (                          \
+    ((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC) ?               \
+    1 :                                                             \
+    (((hnd)->gHnd != MPIU_SHMW_SHMGHANDLE_INVALID) ? 1 : 0)         \
+)
+/* Allocate mem for global handle.
+ * Returns non-zero on success, 0 on failure
+ */
+static inline int MPIU_SHMW_shmGHandle_alloc(
+    MPIU_SHMW_shmHandle_t hnd){
+    if(!(hnd->gHnd)){
+        hnd->gHnd = (MPIU_SHMW_shmGHandle_t )
+                        MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE);
+        if(!(hnd->gHnd)){ return 0; }
+    }
+    /* Global handle is no longer static */
+    hnd->flag &= ~MPIU_SHMW_FLAG_SHMGHANDLESTATIC;
+    return 1;
+}
 /* A Handle is valid if it is initialized/init and has a value
@@ -117,17 +203,8 @@
         !MPIU_SHMW_shmGHandle_isValid(hnd)) ? 0 : 1                 \
 )
-
-#define MPIU_SHMW_shmLHandle_isInit(hnd) 1
-#define MPIU_SHMW_shmGHandle_isInit(hnd) (                          \
-    ((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC) ?               \
-    1 :                                                             \
-    (((hnd)->gHnd != MPIU_SHMW_SHMGHANDLE_INVALID) ? 1 : 0)         \
-)
-
 #define MPIU_SHMW_shmHandle_isInit(hnd) (                           \
     (hnd && MPIU_SHMW_shmLHandle_isInit(hnd) &&                     \
         MPIU_SHMW_shmGHandle_isInit(hnd)) ? 1 : 0                   \
 )
-
 /* Allocate mem for references within the handle */
 #define MPIU_SHMW_shmHandle_refAlloc(hnd)(                          \
@@ -135,34 +212,36 @@
                     MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE)          \
 )
-
-/* FIXME : Handle malloc error */
-#define MPIU_SHMW_shmGHandle_alloc(hnd) do{                         \
-    (hnd)->flag &= ~MPIU_SHMW_FLAG_SHMGHANDLESTATIC;                \
-    if(!(hnd)->gHnd){                                               \
-        (hnd)->gHnd = (MPIU_SHMW_shmGHandle_t)                      \
-                    MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE);         \
-    }                                                               \
-}while(0)
-
 /* Allocate mem for handle. Lazy allocation for global handle */
 #define MPIU_SHMW_shmHandle_alloc(hnd) (                            \
     (hnd = (MPIU_SHMW_shmHandle_t)                                  \
                 MPIU_Malloc(MPIU_SHMW_SHMHANDLE_SIZE)) ?            \
-    ((hnd)->flag = MPIU_SHMW_FLAG_SHMGHANDLESTATIC) : 0             \
-)
-
-/* Nothing to be done at handle close */
-#define MPIU_SHMW_shmHandle_close(hnd) 1
-
-#define MPIU_SHMW_shmHandle_free(hnd) do{                           \
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){                            \
-        if(!((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC)){       \
-            MPIU_Free((hnd)->gHnd);                                 \
-        }                                                           \
-        MPIU_Free(hnd);                                             \
-    }                                                               \
-}while(0)
-
-static inline int MPIU_SHMW_shmSeg_open(MPIU_SHMW_shmHandle_t hnd, int shmSegSize);
+    ((hnd)->flag |= MPIU_SHMW_FLAG_SHMGHANDLESTATIC) : 0            \
+)
+/* Close Handle */
+#define MPIU_SHMW_shmHandle_close(hnd)                              \
+    MPIU_SHMW_shmLHandle_close(hnd)
+
+static inline void MPIU_SHMW_shmHandle_resetVal(
+    MPIU_SHMW_shmHandle_t hnd) {
+    MPIU_SHMW_shmLHandle_set(hnd, MPIU_SHMW_SHMLHANDLE_INITVAL);
+    if(hnd->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC){
+        hnd->gHnd = MPIU_SHMW_SHMGHANDLE_INVALID;
+    }
+    else{
+        (hnd->gHnd)[0] = MPIU_SHMW_SHMGHANDLE_INITVAL;
+    }
+}
+
+static inline void MPIU_SHMW_shmHandle_free(MPIU_SHMW_shmHandle_t hnd){
+    if(MPIU_SHMW_shmHandle_isInit(hnd)){
+        if(!(hnd->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC)){
+            MPIU_Free(hnd->gHnd);
+        }
+        MPIU_Free(hnd);
+    }
+}
+
+static inline int MPIU_SHMW_shmSeg_open(MPIU_SHMW_shmHandle_t hnd,
+    int shmSegSize);
 /* FIXME : Don't print ENGLISH strings on error. Define the error
@@ -185,20 +264,26 @@
     MPIU_SHMW_shmHandle_t hnd, int strHndLen){
     int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_Strncpy(strHnd, MPIU_SHMW_shmGHandle_getByRef(hnd),
-            strHndLen);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!strHnd, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "strHnd");
+    MPIU_ERR_CHKANDJUMP1(strHndLen<MPIU_SHMW_SHMGHANDLE_SIZE,
+        mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s",
+        "shared mem global handle");
+
+    MPIU_ERR_CHKANDJUMP(
+        (MPIU_SHMW_shmGHandle_getByVal(hnd, strHnd, strHndLen)<0),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_gethnd");
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
 /* Deserialize a handle.
- * strHnd   : A pointer to a string which contains the serialized
- *           handle.
+ * strHnd   : A pointer to a null terminated string which
+ *           contains the serialized handle.
  * hnd      : If the call succeeds the user gets back a handle to
  *           shared mem - deserialized from strHnd. This handle
@@ -213,19 +298,30 @@
     MPIU_SHMW_shmHandle_t hnd, const char *strHnd, int strHndLen){
     int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        MPIU_SHMW_shmGHandle_alloc(hnd);
-        MPIU_Strncpy(MPIU_SHMW_shmGHandle_getByRef(hnd), strHnd,
-            ((strHndLen < MPIU_SHMW_SHMGHANDLE_SIZE) ? strHndLen :
-            MPIU_SHMW_SHMGHANDLE_SIZE));
-        mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!strHnd, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "strHnd");
+    MPIU_ERR_CHKANDJUMP1(strHndLen>=MPIU_SHMW_SHMGHANDLE_SIZE,
+        mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s",
+        "shared mem global handle");
+
+    MPIU_SHMW_shmHandle_resetVal(hnd);
+
+    MPIU_ERR_CHKANDJUMP1(!MPIU_SHMW_shmGHandle_alloc(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s",
+        "shared mem global handle");
+
+    MPIU_ERR_CHKANDJUMP(
+        (MPIU_SHMW_shmGHandle_setByVal(hnd, "%s", strHnd)<0),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_sethnd");
+
+    mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
+    if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -246,23 +342,16 @@
     MPIU_SHMW_shmHandle_t hnd, char **pSerHnd){
     int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        if(pSerHnd){
-            *pSerHnd = (char *)MPIU_SHMW_shmGHandle_getByRef(hnd);
-        }
-        else{
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**arg", "**arg %s",
-                "pointer to serialized shared mem Handle");
-            return mpi_errno;
-        }
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!pSerHnd, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "pSerHnd");
+
+    *pSerHnd = (char *)MPIU_SHMW_shmGHandle_getByRef(hnd);
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -283,25 +372,19 @@
     MPIU_SHMW_shmHandle_t hnd, char **pSerHnd){
     int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        if(pSerHnd){
-            MPIU_SHMW_shmGHandle_setByRef(hnd, *pSerHnd);
-        }
-        else{
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**arg", "**arg %s",
-                "pointer to serialized shared mem Handle");
-            return mpi_errno;
-        }
-        mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!pSerHnd, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "pSerHnd");
+
+    MPIU_SHMW_shmHandle_resetVal(hnd);
+    MPIU_SHMW_shmGHandle_setByRef(hnd, *pSerHnd);
+    mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
+    if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -313,19 +396,17 @@
                     MPIU_SHMW_shmHandle_t *pHnd){
     int mpi_errno = MPI_SUCCESS;
-    if(!pHnd){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nullptr", "**nullptr %s",
-            "to shared mem handle");
-        return mpi_errno;
-    }
-    if(!MPIU_SHMW_shmHandle_alloc(*pHnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nomem", 0);
-        return mpi_errno;
-    }
+
+    MPIU_ERR_CHKANDJUMP1(!pHnd, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "pHnd");
+    MPIU_ERR_CHKANDJUMP1(!MPIU_SHMW_shmHandle_alloc(*pHnd),
+        mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s",
+        "shared mem handle");
+
     MPIU_SHMW_shmHandle_resetVal(*pHnd);
-    return mpi_errno;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -337,17 +418,20 @@
                     MPIU_SHMW_shmHandle_t *pHnd){
     int mpi_errno = MPI_SUCCESS;
-    if(!pHnd){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nullptr", "**nullptr %s",
-            "to shared mem handle");
-        return mpi_errno;
-    }
+
+    MPIU_ERR_CHKANDJUMP1(!pHnd, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "pHnd");
+
     MPIU_SHMW_shmHandle_close(*pHnd);
     MPIU_SHMW_shmHandle_free(*pHnd);
     *pHnd = MPIU_SHMW_SHMHANDLE_INVALID;
-    return mpi_errno;
-}
-
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+#ifdef  USE_SYSV_SHM
+/************************** Sys V shm **************************/
 /* A template function which creates/attaches shm seg handle
  * to the shared memory. Used by user-exposed functions below
@@ -363,54 +447,32 @@
     int lhnd = -1;
-    if(!MPIU_SHMW_shmHandle_isInit(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-
     if(flag & MPIU_SHMW_FLAG_SHMCREATENEW){
         lhnd = shmget(IPC_PRIVATE, shmSegSize, IPC_CREAT | S_IRWXU);
         /* Return error if SHM seg already exists or create fails */
-        if((lhnd == -1) ||
-           (MPIU_OSW_getErrno() == MPIU_SHMW_SHMSEG_ALREADY_EXISTS)){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                "shmget",MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
+        MPIU_ERR_CHKANDJUMP2((lhnd == -1) ||
+            (MPIU_OSW_getErrno() == MPIU_SHMW_SHMSEG_ALREADY_EXISTS),
+            mpi_errno, MPI_ERR_OTHER, "**alloc_shar_mem",
+            "**alloc_shar_mem %s %s", "shmget",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
         MPIU_SHMW_shmLHandle_set(hnd, lhnd);
-        MPIU_SHMW_shmGHandle_alloc(hnd);
-        if(!MPIU_SHMW_shmGHandle_isInit(hnd)){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                            FCNAME, __LINE__, MPI_ERR_OTHER, "**alloc_shar_mem",
-                            "**alloc_shar_mem %s %s",
-                            "", "Unable to init global shm Handle");
-                            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
-        MPIU_Snprintf(MPIU_SHMW_shmGHandle_getByRef(hnd),
-                        MPIU_SHMW_SHMGHANDLE_SIZE, "%d", lhnd);
+        MPIU_ERR_CHKANDJUMP1(!MPIU_SHMW_shmGHandle_alloc(hnd),
+            mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s",
+            "shared mem global handle");
+
+        MPIU_ERR_CHKANDJUMP(
+            (MPIU_SHMW_shmGHandle_setByVal(hnd, "%d", lhnd)<0),
+            mpi_errno, MPI_ERR_OTHER, "**shmw_sethnd");
     }
     else{
         /* Open an existing shared memory seg */
-        if(!MPIU_SHMW_shmGHandle_isValid(hnd)){
-            return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                    FCNAME, __LINE__, MPI_ERR_OTHER, "**alloc_shar_mem",
-                    "**alloc_shar_mem %s %s",
-                    "", "Invalid global shm Handle");
-        }
+        MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmGHandle_isValid(hnd),
+            mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+
         if(!MPIU_SHMW_shmLHandle_isValid(hnd)){
             lhnd = atoi(MPIU_SHMW_shmGHandle_getByRef(hnd));
-            if(lhnd == -1){
-                mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                    MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                    "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                    "atoi", MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-                MPIU_SHMW_shmHandle_resetVal(hnd);
-                return mpi_errno;
-            }
+            MPIU_ERR_CHKANDJUMP((lhnd == -1), mpi_errno,
+                MPI_ERR_OTHER, "**shmw_badhnd");
+
             MPIU_SHMW_shmLHandle_set(hnd, lhnd);
         }
@@ -418,70 +480,19 @@
     if(flag & MPIU_SHMW_FLAG_SHMATTACH){
-        if(!ppShm){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**nullptr", "**nullptr %s",
-                "to ptr to shared mem");
-            return mpi_errno;
-        }
+        /* Attach to shared mem seg */
+        MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+            "**nullptr", "**nullptr %s", "ppShm");
+
         *ppShm = shmat(MPIU_SHMW_shmLHandle_get(hnd), NULL, 0x0);
-        if(!(*ppShm)){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**attach_shar_mem",
-                "**attach_shar_mem %s %s","shmat",
-                MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            return mpi_errno;
-        }
+
+        MPIU_ERR_CHKANDJUMP2(!(*ppShm), mpi_errno, MPI_ERR_OTHER,
+            "**attach_shar_mem", "**attach_shar_mem %s %s",
+            "shmat", MPIU_OSW_strerror(MPIU_OSW_getErrno()));
     }
-    return mpi_errno;
-}
-
-/* Create new SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_create
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_create(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            NULL, 0, MPIU_SHMW_FLAG_SHMCREATENEW));
-}
-
-/* Open an existing SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_open
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_open(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            NULL, 0, MPIU_SHMW_FLAG_CLEAR));
-}
-
-/* Create new SHM segment and attach to it */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_createAndAttach
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_createAndAttach(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
-    int attachOffset){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            ppShm, attachOffset, MPIU_SHMW_FLAG_SHMCREATENEW |
-            MPIU_SHMW_FLAG_SHMATTACH));
-}
-
-/* Attach to an existing SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_attach
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_attach(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
-    int attachOffset){
-    MPIU_UNREFERENCED_ARG(shmSegSize);
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, 0, ppShm,
-                attachOffset, MPIU_SHMW_FLAG_SHMATTACH));
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -494,27 +505,21 @@
     MPIU_SHMW_shmHandle_t hnd, char **ppShm, int shmSegSize){
     int mpi_errno = MPI_SUCCESS;
-    MPIU_UNREFERENCED_ARG(shmSegSize);
-    if(!ppShm || !(*ppShm)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem ptr not valid");
-        return mpi_errno;
-    }
-    if(!MPIU_SHMW_shmHandle_isValid(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle not valid");
-        return mpi_errno;
-    }
-
-    if(shmdt(*ppShm) != 0){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**detach_shar_mem",
-            "**detach_shar_mem %s %s","shmdt",
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isValid(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "ppShm");
+    MPIU_ERR_CHKANDJUMP1(!(*ppShm), mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "*ppShm");
+
+    MPIU_ERR_CHKANDJUMP2((shmdt(*ppShm) != 0), mpi_errno, MPI_ERR_OTHER,
+            "**detach_shar_mem", "**detach_shar_mem %s %s","shmdt",
             MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-        return mpi_errno;
-    }
     *ppShm = NULL;
-    return mpi_errno;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -528,714 +533,24 @@
     struct shmid_ds ds;
-    if(!MPIU_SHMW_shmHandle_isValid(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle not valid");
-        return mpi_errno;
-    }
-
-    if(shmctl(MPIU_SHMW_shmLHandle_get(hnd), IPC_RMID, &ds) != 0){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**remove_shar_mem",
-            "**remove_shar_mem %s %s","shmctl",
-            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#elif defined (USE_NT_SHM)
-/**************************** NT shm **************************/
-typedef HANDLE MPIU_SHMW_shmLHandle_t;
-typedef char * MPIU_SHMW_shmGHandle_t;
-
-#define MPIU_SHMW_FLAG_CLEAR             0x0
-#define MPIU_SHMW_FLAG_SHMCREATENEW      0x1
-#define MPIU_SHMW_FLAG_SHMATTACH         0x10
-#define MPIU_SHMW_FLAG_SHMGHANDLESTATIC  0x100
-
-typedef struct{
-    MPIU_SHMW_shmLHandle_t lHnd;
-    MPIU_SHMW_shmGHandle_t gHnd;
-    int flag;
-} MPIU_SHMW_shmLGHandle_t;
-
-typedef MPIU_SHMW_shmLGHandle_t * MPIU_SHMW_shmHandle_t;
-
-#define MPIU_SHMW_shmLHandle_get(hnd)   ((hnd)->lHnd)
-#define MPIU_SHMW_shmLHandle_set(hnd, val)  ((hnd)->lHnd=val)
-#define MPIU_SHMW_shmGHandle_getByRef(hnd)   ((hnd)->gHnd)
-#define MPIU_SHMW_shmGHandle_setByRef(hnd, val) ((hnd)->gHnd = val)
-
-#define MPIU_SHMW_SHMSEGNAME_LEN    70
-#define MPIU_SHMW_SHMGHANDLE_SIZE MPIU_SHMW_SHMSEGNAME_LEN
-#define MPIU_SHMW_SHMHANDLE_SIZE    (sizeof(MPIU_SHMW_shmLGHandle_t))
-#define MPIU_SHMW_SHMSERIALIZEDHANDLE_SIZE MPIU_SHMW_SHMGHANDLE_SIZE
-
-#define MPIU_SHMW_SHMHANDLE_INVALID     NULL
-#define MPIU_SHMW_SHMLHANDLE_INVALID    INVALID_HANDLE_VALUE
-#define MPIU_SHMW_SHMLHANDLE_INITVAL    INVALID_HANDLE_VALUE
-#define MPIU_SHMW_SHMGHANDLE_INVALID    NULL
-#define MPIU_SHMW_SHMGHANDLE_INITVAL    '\0'
-
-#define MPIU_SHMW_SHMSEG_ALREADY_EXISTS ERROR_ALREADY_EXISTS
-
-#define MPIU_SHMW_shmHandle_resetVal(hnd) do{                       \
-    (hnd)->lHnd = MPIU_SHMW_SHMLHANDLE_INITVAL;                     \
-    if((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC){              \
-        (hnd)->gHnd = MPIU_SHMW_SHMGHANDLE_INVALID;                 \
-    }                                                               \
-    else{                                                           \
-        ((hnd)->gHnd)[0] = MPIU_SHMW_SHMGHANDLE_INITVAL;            \
-    }                                                               \
-}while(0)
-
-#define MPIU_SHMW_shmLHandle_isValid(hnd) (                         \
-    ((hnd)->lHnd == MPIU_SHMW_SHMLHANDLE_INVALID) ? 0 : 1           \
-)
-
-#define MPIU_SHMW_shmGHandle_isValid(hnd) (                         \
-    (((hnd)->gHnd == MPIU_SHMW_SHMGHANDLE_INVALID) ||               \
-        (strlen((hnd)->gHnd) == 0)) ? 0 : 1                         \
-)
-
-#define MPIU_SHMW_shmHandle_isValid(hnd) (                          \
-    (!(hnd) ||                                                      \
-        !MPIU_SHMW_shmLHandle_isValid(hnd) ||                       \
-        !MPIU_SHMW_shmGHandle_isValid(hnd)) ? 0 : 1                 \
-)
-
-#define MPIU_SHMW_shmLHandle_isInit(hnd) 1
-#define MPIU_SHMW_shmGHandle_isInit(hnd) (                          \
-    ((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC) ?               \
-    1 :                                                             \
-    (((hnd)->gHnd != MPIU_SHMW_SHMGHANDLE_INVALID) ? 1 : 0)         \
-)
-
-#define MPIU_SHMW_shmHandle_isInit(hnd) (                           \
-    (hnd && MPIU_SHMW_shmLHandle_isInit(hnd) &&                     \
-        MPIU_SHMW_shmGHandle_isInit(hnd)) ? 1 : 0                   \
-)
-
-#define MPIU_SHMW_shmHandle_refAlloc(hnd)(                          \
-    (hnd)->gHnd = (MPIU_SHMW_shmGHandle_t)                          \
-                    MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE)          \
-)
-
-/* FIXME : Handle malloc error */
-#define MPIU_SHMW_shmGHandle_alloc(hnd) do{                         \
-    (hnd)->flag &= ~MPIU_SHMW_FLAG_SHMGHANDLESTATIC;                \
-    if(!(hnd)->gHnd){                                               \
-        (hnd)->gHnd = (MPIU_SHMW_shmGHandle_t)                      \
-                    MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE);         \
-    }                                                               \
-}while(0)
-
-#define MPIU_SHMW_shmHandle_alloc(hnd) (                            \
-    (hnd = (MPIU_SHMW_shmHandle_t)                                  \
-                MPIU_Malloc(MPIU_SHMW_SHMHANDLE_SIZE)) ?            \
-    ((hnd)->flag = MPIU_SHMW_FLAG_SHMGHANDLESTATIC) : 0             \
-)
-
-#define MPIU_SHMW_shmHandle_close(hnd) (                            \
-    (MPIU_SHMW_shmLHandle_isValid(hnd))?CloseHandle((hnd)->lHnd) : 1\
-)
-
-#define MPIU_SHMW_shmHandle_free(hnd) do{                           \
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){                            \
-        if(!((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC)){       \
-            MPIU_Free((hnd)->gHnd);                                 \
-        }                                                           \
-        MPIU_Free(hnd);                                             \
-    }                                                               \
-}while(0)
-
-#define MPIU_SHMW_shmGHandle_setUniq(hnd) (                         \
-    (MPIU_SHMW_shmHandle_refAlloc(hnd)) ?                           \
-    MPIU_OSW_getUniqStr((hnd)->gHnd, MPIU_SHMW_SHMGHANDLE_SIZE) :   \
-    0                                                               \
-)
-
-static inline int MPIU_SHMW_shmSeg_open(MPIU_SHMW_shmHandle_t hnd, int shmSegSize);
-
-/* FIXME : Don't print ENGLISH strings on error. Define the error
- * strings in errnames.txt
- */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_serialize
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_serialize(char *strHnd,
-    MPIU_SHMW_shmHandle_t hnd, int strHndLen){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_Strncpy(strHnd, MPIU_SHMW_shmGHandle_getByRef(hnd),
-            strHndLen);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_deserialize
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_deserialize(
-    MPIU_SHMW_shmHandle_t hnd, const char *strHnd, int strHndLen){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        MPIU_SHMW_shmGHandle_alloc(hnd);
-        MPIU_Strncpy(MPIU_SHMW_shmGHandle_getByRef(hnd), strHnd,
-            ((strHndLen < MPIU_SHMW_SHMGHANDLE_SIZE) ? strHndLen :
-            MPIU_SHMW_SHMGHANDLE_SIZE));
-        mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_getSerializedByRef
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_getSerializedByRef(
-    MPIU_SHMW_shmHandle_t hnd, char **pSerHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        if(pSerHnd){
-            *pSerHnd = (char *)MPIU_SHMW_shmGHandle_getByRef(hnd);
-        }
-        else{
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**arg", "**arg %s",
-                "pointer to serialized shared mem Handle");
-            return mpi_errno;
-        }
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_deserializeByRef
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_deserializeByRef(
-    MPIU_SHMW_shmHandle_t hnd, char **pSerHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        if(pSerHnd){
-            MPIU_SHMW_shmGHandle_setByRef(hnd, *pSerHnd);
-        }
-        else{
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**arg", "**arg %s",
-                "pointer to serialized shared mem Handle");
-            return mpi_errno;
-        }
-        mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_init
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_init(
-                    MPIU_SHMW_shmHandle_t *pHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(!pHnd){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nullptr", "**nullptr %s",
-            "to shared mem handle");
-        return mpi_errno;
-    }
-    if(!MPIU_SHMW_shmHandle_alloc(*pHnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nomem", 0);
-        return mpi_errno;
-    }
-    MPIU_SHMW_shmHandle_resetVal(*pHnd);
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_finalize
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_finalize(
-                    MPIU_SHMW_shmHandle_t *pHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(!pHnd){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nullptr", "**nullptr %s",
-            "to shared mem handle");
-        return mpi_errno;
-    }
-    MPIU_SHMW_shmHandle_close(*pHnd);
-    MPIU_SHMW_shmHandle_free(*pHnd);
-    *pHnd = MPIU_SHMW_SHMHANDLE_INVALID;
-    return mpi_errno;
-}
-
-static inline int MPIU_SHMW_shmSeg_createAttachTempl(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
-    int attachOffset, int flag){
-    int mpi_errno = MPI_SUCCESS;
-    HANDLE lhnd = INVALID_HANDLE_VALUE;
-
-    if(!MPIU_SHMW_shmHandle_isInit(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-
-    if(!MPIU_SHMW_shmGHandle_isValid(hnd)){
-        MPIU_Assert(flag & MPIU_SHMW_FLAG_SHMCREATENEW);
-        if(!MPIU_SHMW_shmGHandle_setUniq(hnd)){
-        /* FIXME : Change error code */
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**alloc_shar_mem",
-            "**alloc_shar_mem %s %s",
-            "", "Unable to get unique shm Handle");
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        return mpi_errno;
-        }
-    }
-
-    if(flag & MPIU_SHMW_FLAG_SHMCREATENEW){
-        lhnd = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
-                PAGE_READWRITE, 0, shmSegSize,
-                MPIU_SHMW_shmGHandle_getByRef(hnd));
-        /* Return error if SHM seg already exists or create fails */
-        if((lhnd == INVALID_HANDLE_VALUE) ||
-           (MPIU_OSW_getErrno() == MPIU_SHMW_SHMSEG_ALREADY_EXISTS)){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                "CreateFileMapping",MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            CloseHandle(lhnd);
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
-        MPIU_SHMW_shmLHandle_set(hnd, lhnd);
-    }
-    else{
-        if(!MPIU_SHMW_shmLHandle_isValid(hnd)){
-            /* Strangely OpenFileMapping() returns NULL on error! */
-            if((lhnd = OpenFileMapping(FILE_MAP_WRITE, FALSE,
-                    MPIU_SHMW_shmGHandle_getByRef(hnd))) == NULL){
-                mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                    MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                    "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                    "OpenFileMapping", "shm seg not found");
-                MPIU_SHMW_shmHandle_resetVal(hnd);
-                return mpi_errno;
-            }
-            MPIU_SHMW_shmLHandle_set(hnd, lhnd);
-        }
-    }
-
-    if(flag & MPIU_SHMW_FLAG_SHMATTACH){
-        if(!ppShm){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**nullptr", "**nullptr %s",
-                "to ptr to shared mem");
-            return mpi_errno;
-        }
-        *ppShm = (char *)MapViewOfFile(MPIU_SHMW_shmLHandle_get(hnd),
-                            FILE_MAP_WRITE, 0, attachOffset, 0);
-        if(!(*ppShm)){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                FCNAME, __LINE__, MPI_ERR_OTHER, "**attach_shar_mem",
-                "**attach_shar_mem %s %s","MapViewOfFile",
-                MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            return mpi_errno;
-        }
-    }
-
-    return mpi_errno;
-}
-
-/* Create new SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_create
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_create(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            NULL, 0, MPIU_SHMW_FLAG_SHMCREATENEW));
-}
-
-/* Open existing SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_open
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_open(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            NULL, 0, MPIU_SHMW_FLAG_CLEAR));
-}
-
-/* Create new SHM segment and attach to it */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_createAndAttach
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_createAndAttach(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
-    int attachOffset){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            ppShm, attachOffset, MPIU_SHMW_FLAG_SHMCREATENEW |
-            MPIU_SHMW_FLAG_SHMATTACH));
-}
-
-/* Attach to an existing SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_attach
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_attach(
-    MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
-    int attachOffset){
-    MPIU_UNREFERENCED_ARG(shmSegSize);
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, 0, ppShm,
-                attachOffset, MPIU_SHMW_FLAG_SHMATTACH));
-}
-
-/* Detach from an attached SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_detach
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_detach(
-    MPIU_SHMW_shmHandle_t hnd, char **ppShm, int shmSegSize){
-    int mpi_errno = MPI_SUCCESS;
-    MPIU_UNREFERENCED_ARG(shmSegSize);
-    if(!ppShm || !(*ppShm)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem ptr not valid");
-        return mpi_errno;
-    }
-    if(!MPIU_SHMW_shmHandle_isValid(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle not valid");
-        return mpi_errno;
-    }
-
-    if(!UnmapViewOfFile(*ppShm)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**detach_shar_mem",
-            "**detach_shar_mem %s %s","UnmapViewOfFile",
-            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-        return mpi_errno;
-    }
-    *ppShm = NULL;
-    return mpi_errno;
-}
-
-/* Remove an existing SHM segment */
-#define  MPIU_SHMW_shmSeg_remove(hnd) MPI_SUCCESS
-
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isValid(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+
+    MPIU_ERR_CHKANDJUMP2(
+        (shmctl(MPIU_SHMW_shmLHandle_get(hnd), IPC_RMID, &ds) != 0),
+        mpi_errno, MPI_ERR_OTHER, "**remove_shar_mem",
+        "**remove_shar_mem %s %s","shmctl",
+        MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
 #elif USE_MMAP_SHM
-/************************** mmap() for shm ***********************/
-typedef int MPIU_SHMW_shmLHandle_t;
-typedef char * MPIU_SHMW_shmGHandle_t;
-
-#define MPIU_SHMW_FLAG_CLEAR             0x0
-#define MPIU_SHMW_FLAG_SHMCREATENEW      0x1
-#define MPIU_SHMW_FLAG_SHMATTACH         0x10
-#define MPIU_SHMW_FLAG_SHMGHANDLESTATIC  0x100
-
-typedef struct{
-    MPIU_SHMW_shmLHandle_t lHnd;
-    MPIU_SHMW_shmGHandle_t gHnd;
-    int flag;
-} MPIU_SHMW_shmLGHandle_t;
-
-typedef MPIU_SHMW_shmLGHandle_t * MPIU_SHMW_shmHandle_t;
-
-#define MPIU_SHMW_shmLHandle_get(hnd)   ((hnd)->lHnd)
-#define MPIU_SHMW_shmLHandle_set(hnd, val)  ((hnd)->lHnd=val)
-#define MPIU_SHMW_shmGHandle_getByRef(hnd)   ((hnd)->gHnd)
-#define MPIU_SHMW_shmGHandle_setByRef(hnd, val) ((hnd)->gHnd = val)
-
-#define MPIU_SHMW_SHMFNAME_LEN    50
-#define MPIU_SHMW_SHMGHANDLE_SIZE MPIU_SHMW_SHMFNAME_LEN
-#define MPIU_SHMW_SHMHANDLE_SIZE    (sizeof(MPIU_SHMW_shmLGHandle_t))
-#define MPIU_SHMW_SHMSERIALIZEDHANDLE_SIZE MPIU_SHMW_SHMGHANDLE_SIZE
-
-#define MPIU_SHMW_SHMHANDLE_INVALID     NULL
-#define MPIU_SHMW_SHMLHANDLE_INVALID    -1
-#define MPIU_SHMW_SHMLHANDLE_INITVAL    -1
-#define MPIU_SHMW_SHMGHANDLE_INVALID    NULL
-#define MPIU_SHMW_SHMGHANDLE_INITVAL    '\0'
-
-#define MPIU_SHMW_SHMSEG_ALREADY_EXISTS EEXIST
-
-#define MPIU_SHMW_shmLHandle_resetVal(hnd) do{                      \
-    (hnd)->lHnd = MPIU_SHMW_SHMLHANDLE_INITVAL;                     \
-}while(0)
-
-#define MPIU_SHMW_shmHandle_resetVal(hnd) do{                       \
-    MPIU_SHMW_shmLHandle_resetVal(hnd);                             \
-    if((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC){              \
-        (hnd)->gHnd = MPIU_SHMW_SHMGHANDLE_INVALID;                 \
-    }                                                               \
-    else{                                                           \
-        ((hnd)->gHnd)[0] = MPIU_SHMW_SHMGHANDLE_INITVAL;            \
-    }                                                               \
-}while(0)
-
-#define MPIU_SHMW_shmLHandle_isValid(hnd) (                         \
-    ((hnd)->lHnd == MPIU_SHMW_SHMLHANDLE_INVALID) ? 0 : 1           \
-)
-
-#define MPIU_SHMW_shmGHandle_isValid(hnd) (                         \
-    (((hnd)->gHnd == MPIU_SHMW_SHMGHANDLE_INVALID) ||               \
-        (strlen((hnd)->gHnd) == 0)) ? 0 : 1                         \
-)
-
-#define MPIU_SHMW_shmHandle_isValid(hnd) (                          \
-    (!(hnd) ||                                                      \
-        !MPIU_SHMW_shmLHandle_isValid(hnd) ||                       \
-        !MPIU_SHMW_shmGHandle_isValid(hnd)) ? 0 : 1                 \
-)
-
-#define MPIU_SHMW_shmLHandle_isInit(hnd) 1
-#define MPIU_SHMW_shmGHandle_isInit(hnd) (                          \
-    ((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC) ?               \
-    1 :                                                             \
-    (((hnd)->gHnd != MPIU_SHMW_SHMGHANDLE_INVALID) ? 1 : 0)         \
-)
-
-#define MPIU_SHMW_shmHandle_isInit(hnd) (                           \
-    (hnd && MPIU_SHMW_shmLHandle_isInit(hnd) &&                     \
-        MPIU_SHMW_shmGHandle_isInit(hnd)) ? 1 : 0                   \
-)
-
-#define MPIU_SHMW_shmHandle_refAlloc(hnd)(                          \
-    (hnd)->gHnd = (MPIU_SHMW_shmGHandle_t)                          \
-                    MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE)          \
-)
-
-/* FIXME : Handle malloc error */
-#define MPIU_SHMW_shmGHandle_alloc(hnd) do{                         \
-    (hnd)->flag &= ~MPIU_SHMW_FLAG_SHMGHANDLESTATIC;                \
-    if(!(hnd)->gHnd){                                               \
-        (hnd)->gHnd = (MPIU_SHMW_shmGHandle_t)                      \
-                    MPIU_Malloc(MPIU_SHMW_SHMGHANDLE_SIZE);         \
-    }                                                               \
-}while(0)
-
-#define MPIU_SHMW_shmHandle_alloc(hnd) (                            \
-    (hnd = (MPIU_SHMW_shmHandle_t)                                  \
-                MPIU_Malloc(MPIU_SHMW_SHMHANDLE_SIZE)) ?            \
-    ((hnd)->flag = MPIU_SHMW_FLAG_SHMGHANDLESTATIC) : 0             \
-)
-
-#define MPIU_SHMW_shmLHandle_close(hnd) do{                         \
-    close(MPIU_SHMW_shmLHandle_get(hnd));                           \
-    MPIU_SHMW_shmLHandle_resetVal(hnd);                             \
-}while(0)
-
-/* FIXME: Close the local handle here */
-#define MPIU_SHMW_shmHandle_close(hnd)
-
-#define MPIU_SHMW_shmHandle_free(hnd) do{                           \
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){                            \
-        if(!((hnd)->flag & MPIU_SHMW_FLAG_SHMGHANDLESTATIC)){       \
-            MPIU_Free((hnd)->gHnd);                                 \
-        }                                                           \
-        MPIU_Free(hnd);                                             \
-    }                                                               \
-}while(0)
-
-static inline int MPIU_SHMW_shmSeg_open(MPIU_SHMW_shmHandle_t hnd, int shmSegSize);
-
-/* FIXME : Don't print ENGLISH strings on error. Define the error
- * strings in errnames.txt
- */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_serialize
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_serialize(char *strHnd,
-    MPIU_SHMW_shmHandle_t hnd, int strHndLen){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_Strncpy(strHnd, MPIU_SHMW_shmGHandle_getByRef(hnd),
-            strHndLen);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_deserialize
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_deserialize(
-    MPIU_SHMW_shmHandle_t hnd, const char *strHnd, int strHndLen){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        MPIU_SHMW_shmGHandle_alloc(hnd);
-        MPIU_Strncpy(MPIU_SHMW_shmGHandle_getByRef(hnd), strHnd,
-            ((strHndLen < MPIU_SHMW_SHMGHANDLE_SIZE) ? strHndLen :
-            MPIU_SHMW_SHMGHANDLE_SIZE));
-        mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_getSerializedByRef
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_getSerializedByRef(
-    MPIU_SHMW_shmHandle_t hnd, char **pSerHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        if(pSerHnd){
-            *pSerHnd = (char *)MPIU_SHMW_shmGHandle_getByRef(hnd);
-        }
-        else{
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**arg", "**arg %s",
-                "pointer to serialized shared mem Handle");
-            return mpi_errno;
-        }
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_deserializeByRef
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_deserializeByRef(
-    MPIU_SHMW_shmHandle_t hnd, char **pSerHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(MPIU_SHMW_shmHandle_isInit(hnd)){
-        MPIU_SHMW_shmHandle_resetVal(hnd);
-        if(pSerHnd){
-            MPIU_SHMW_shmGHandle_setByRef(hnd, *pSerHnd);
-        }
-        else{
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**arg", "**arg %s",
-                "pointer to serialized shared mem Handle");
-            return mpi_errno;
-        }
-        mpi_errno = MPIU_SHMW_shmSeg_open(hnd, 0);
-    }
-    else{
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_init
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_init(
-                    MPIU_SHMW_shmHandle_t *pHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(!pHnd){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nullptr", "**nullptr %s",
-            "to shared mem handle");
-        return mpi_errno;
-    }
-    if(!MPIU_SHMW_shmHandle_alloc(*pHnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nomem", 0);
-        return mpi_errno;
-    }
-    MPIU_SHMW_shmHandle_resetVal(*pHnd);
-    return mpi_errno;
-}
-
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmHandle_finalize
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmHandle_finalize(
-                    MPIU_SHMW_shmHandle_t *pHnd){
-    int mpi_errno = MPI_SUCCESS;
-    if(!pHnd){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-            MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-            "**nullptr", "**nullptr %s",
-            "to shared mem handle");
-        return mpi_errno;
-    }
-    MPIU_SHMW_shmHandle_close(*pHnd);
-    MPIU_SHMW_shmHandle_free(*pHnd);
-    *pHnd = MPIU_SHMW_SHMHANDLE_INVALID;
-    return mpi_errno;
-}
+/************************** MMAP shm **************************/
+/* A template function which creates/attaches shm seg handle
+ * to the shared memory. Used by user-exposed functions below
+ */
 /* FIXME: Pass (void **)ppShm instead of (char **) ppShm
  *  since a util func should be generic
@@ -1244,4 +559,8 @@
  */
+#undef FUNCNAME
+#define FUNCNAME MPIU_SHMW_shmSeg_createAttachTempl
+#undef FCNAME
+#define FCNAME MPIU_QUOTE(FUNCNAME)
 static inline int MPIU_SHMW_shmSeg_createAttachTempl(
     MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
@@ -1249,11 +568,4 @@
     int mpi_errno = MPI_SUCCESS;
     int lhnd = -1, retVal;
-
-    if(!MPIU_SHMW_shmHandle_isInit(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle");
-        return mpi_errno;
-    }
     if(flag & MPIU_SHMW_FLAG_SHMCREATENEW){
@@ -1268,65 +580,37 @@
             lhnd = mkstemp(chosenFName);
         }
-        if(lhnd == -1){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                "mkstemp",MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
+        MPIU_ERR_CHKANDJUMP1((lhnd == -1), mpi_errno,
+            MPI_ERR_OTHER,"**mkstemp","**mkstemp %s",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
         MPIU_SHMW_shmLHandle_set(hnd, lhnd);
         retVal = lseek(lhnd, shmSegSize - 1, SEEK_SET);
-        if(retVal == -1){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                "lseek",MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            MPIU_SHMW_shmLHandle_close(hnd);
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
+        MPIU_ERR_CHKANDJUMP1((retVal == -1), mpi_errno,
+            MPI_ERR_OTHER, "**lseek", "**lseek %s",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
         MPIU_OSW_RETRYON_INTR((retVal == -1), (retVal = write(lhnd, "", 1)));
-        if(retVal == -1){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                "write",MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            MPIU_SHMW_shmLHandle_close(hnd);
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
-
-        MPIU_SHMW_shmGHandle_alloc(hnd);
-        if(!MPIU_SHMW_shmGHandle_isInit(hnd)){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                            FCNAME, __LINE__, MPI_ERR_OTHER, "**alloc_shar_mem",
-                            "**alloc_shar_mem %s %s",
-                            "", "Unable to init global shm Handle");
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
-        }
-        MPIU_Strncpy(MPIU_SHMW_shmGHandle_getByRef(hnd), chosenFName,
-                        MPIU_SHMW_SHMGHANDLE_SIZE);
+        MPIU_ERR_CHKANDJUMP((retVal == -1), mpi_errno, MPI_ERR_OTHER,
+            "**write");
+
+        MPIU_ERR_CHKANDJUMP1(!MPIU_SHMW_shmGHandle_alloc(hnd),mpi_errno,
+            MPI_ERR_OTHER, "**nomem", "**nomem %s",
+            "shared memory global handle");
+
+        MPIU_ERR_CHKANDJUMP(
+            (MPIU_SHMW_shmGHandle_setByVal(hnd, "%s", chosenFName)<0),
+            mpi_errno, MPI_ERR_OTHER, "**shmw_sethnd");
     }
     else{
         /* Open an existing shared memory seg */
-        if(!MPIU_SHMW_shmGHandle_isValid(hnd)){
-            return MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-                    FCNAME, __LINE__, MPI_ERR_OTHER, "**alloc_shar_mem",
-                    "**alloc_shar_mem %s %s",
-                    "", "Invalid global shm Handle");
-        }
+        MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmGHandle_isValid(hnd),
+            mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+
         if(!MPIU_SHMW_shmLHandle_isValid(hnd)){
             lhnd = open(MPIU_SHMW_shmGHandle_getByRef(hnd), O_RDWR);
-            if(lhnd == -1){
-                mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                    MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                    "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                    "atoi", MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-                MPIU_SHMW_shmHandle_resetVal(hnd);
-                return mpi_errno;
-            }
+            MPIU_ERR_CHKANDJUMP1((lhnd == -1), mpi_errno,
+                MPI_ERR_OTHER, "**open", "**open %s",
+                MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
             MPIU_SHMW_shmLHandle_set(hnd, lhnd);
         }
@@ -1335,27 +619,172 @@
     if(flag & MPIU_SHMW_FLAG_SHMATTACH){
         void *bufp = NULL;
-        if(!ppShm){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**nullptr", "**nullptr %s",
-                "to ptr to shared mem");
-            return mpi_errno;
-        }
+
+        MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+            "**nullptr", "**nullptr %s", "ppShm");
+
         bufp = mmap(NULL, shmSegSize, PROT_READ | PROT_WRITE,
                         MAP_SHARED, MPIU_SHMW_shmLHandle_get(hnd), 0);
-        if(bufp == MAP_FAILED){
-            mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
-                MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER,
-                "**alloc_shar_mem", "**alloc_shar_mem %s %s",
-                "mmap",MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-            MPIU_SHMW_shmLHandle_close(hnd);
-            MPIU_SHMW_shmHandle_resetVal(hnd);
-            return mpi_errno;
+        MPIU_ERR_CHKANDJUMP2((bufp == MAP_FAILED),
+            mpi_errno, MPI_ERR_OTHER, "**alloc_shar_mem",
+            "**alloc_shar_mem %s %s", "mmap",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
+        *ppShm = bufp;
+    }
+
+fn_exit:
+    /* FIXME: Close local handle only when closing the shm handle */
+    if(MPIU_SHMW_shmLHandle_isValid(hnd)){
+        MPIU_SHMW_shmLHandle_close(hnd);
+    }
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+/* Detach from an attached SHM segment */
+#undef FUNCNAME
+#define FUNCNAME MPIU_SHMW_shmSeg_detach
+#undef FCNAME
+#define FCNAME MPIU_QUOTE(FUNCNAME)
+static inline int MPIU_SHMW_shmSeg_detach(
+    MPIU_SHMW_shmHandle_t hnd, char **ppShm, int shmSegSize){
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "ppShm");
+    MPIU_ERR_CHKANDJUMP1(!(*ppShm), mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "*ppShm");
+
+    MPIU_ERR_CHKANDJUMP2((munmap(*ppShm, shmSegSize) != 0), mpi_errno,
+            MPI_ERR_OTHER, "**detach_shar_mem",
+            "**detach_shar_mem %s %s","munmap",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+    *ppShm = NULL;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+/* Remove an existing SHM segment */
+static inline int  MPIU_SHMW_shmSeg_remove(MPIU_SHMW_shmHandle_t hnd){
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmGHandle_isValid(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+
+    MPIU_ERR_CHKANDJUMP2(
+        (unlink(MPIU_SHMW_shmGHandle_getByRef(hnd)) != 0),
+        mpi_errno, MPI_ERR_OTHER, "**remove_shar_mem",
+        "**remove_shar_mem %s %s","unlink",
+        MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+#elif defined (USE_NT_SHM)
+/************************** NT shm **************************/
+/* A template function which creates/attaches shm seg handle
+ * to the shared memory. Used by user-exposed functions below
+ */
+#undef FUNCNAME
+#define FUNCNAME MPIU_SHMW_shmSeg_createAttachTempl
+#undef FCNAME
+#define FCNAME MPIU_QUOTE(FUNCNAME)
+static inline int MPIU_SHMW_shmSeg_createAttachTempl(
+    MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
+    int attachOffset, int flag){
+    int mpi_errno = MPI_SUCCESS;
+    HANDLE lhnd = INVALID_HANDLE_VALUE;
+
+    if(!MPIU_SHMW_shmGHandle_isValid(hnd)){
+        MPIU_ERR_CHKANDJUMP(!(flag & MPIU_SHMW_FLAG_SHMCREATENEW),
+            mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+
+        MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmGHandle_setUniq(hnd),
+            mpi_errno, MPI_ERR_OTHER, "**shmw_sethnd");
+    }
+
+    if(flag & MPIU_SHMW_FLAG_SHMCREATENEW){
+        lhnd = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
+                PAGE_READWRITE, 0, shmSegSize,
+                MPIU_SHMW_shmGHandle_getByRef(hnd));
+        /* Return error if SHM seg already exists or create fails */
+        MPIU_ERR_CHKANDJUMP2((lhnd == INVALID_HANDLE_VALUE) ||
+            (MPIU_OSW_getErrno() == MPIU_SHMW_SHMSEG_ALREADY_EXISTS),
+            mpi_errno, MPI_ERR_OTHER, "**alloc_shar_mem",
+            "**alloc_shar_mem %s %s", "CreateFileMapping",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
+        MPIU_SHMW_shmLHandle_set(hnd, lhnd);
+    }
+    else{
+        if(!MPIU_SHMW_shmLHandle_isValid(hnd)){
+            /* Strangely OpenFileMapping() returns NULL on error! */
+            lhnd = OpenFileMapping(FILE_MAP_WRITE, FALSE,
+                    MPIU_SHMW_shmGHandle_getByRef(hnd));
+            MPIU_ERR_CHKANDJUMP2((lhnd == NULL), mpi_errno,
+                MPI_ERR_OTHER, "**alloc_shar_mem",
+                "**alloc_shar_mem %s %s", "OpenFileMapping",
+                MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+
+            MPIU_SHMW_shmLHandle_set(hnd, lhnd);
         }
-        *ppShm = bufp;
-    }
-    MPIU_SHMW_shmLHandle_close(hnd);
-    return mpi_errno;
-}
+    }
+
+    if(flag & MPIU_SHMW_FLAG_SHMATTACH){
+        MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+            "**nullptr", "**nullptr %s", "ppShm");
+
+        *ppShm = (char *)MapViewOfFile(MPIU_SHMW_shmLHandle_get(hnd),
+                            FILE_MAP_WRITE, 0, attachOffset, 0);
+        MPIU_ERR_CHKANDJUMP2(!(*ppShm), mpi_errno, MPI_ERR_OTHER,
+            "**attach_shar_mem", "**attach_shar_mem %s %s",
+            "MapViewOfFile", MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+    }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+/* Detach from an attached SHM segment */
+#undef FUNCNAME
+#define FUNCNAME MPIU_SHMW_shmSeg_detach
+#undef FCNAME
+#define FCNAME MPIU_QUOTE(FUNCNAME)
+static inline int MPIU_SHMW_shmSeg_detach(
+    MPIU_SHMW_shmHandle_t hnd, char **ppShm, int shmSegSize){
+    int mpi_errno = MPI_SUCCESS;
+    MPIU_UNREFERENCED_ARG(shmSegSize);
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isValid(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "ppShm");
+    MPIU_ERR_CHKANDJUMP1(!(*ppShm), mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "*ppShm");
+
+    MPIU_ERR_CHKANDJUMP2((!UnmapViewOfFile(*ppShm)), mpi_errno,
+            MPI_ERR_OTHER, "**detach_shar_mem",
+            "**detach_shar_mem %s %s","UnmapViewOfFile",
+            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
+    *ppShm = NULL;
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+/* Nothing to be done when removing an SHM segment */
+#define  MPIU_SHMW_shmSeg_remove(hnd) MPI_SUCCESS
+
+#endif
 /* Create new SHM segment */
@@ -1366,9 +795,22 @@
 static inline int MPIU_SHMW_shmSeg_create(
     MPIU_SHMW_shmHandle_t hnd, int shmSegSize){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            NULL, 0, MPIU_SHMW_FLAG_SHMCREATENEW));
-}
-
-/* Open existing SHM segment */
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1((shmSegSize > 0), mpi_errno,
+        MPI_ERR_OTHER, "**arg", "**arg %s","shared mem segment size");
+
+    mpi_errno = MPIU_SHMW_shmSeg_createAttachTempl(hnd,
+                    shmSegSize, NULL, 0, MPIU_SHMW_FLAG_SHMCREATENEW);
+    if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
+/* Open an existing SHM segment */
 #undef FUNCNAME
 #define FUNCNAME MPIU_SHMW_shmSeg_open
@@ -1377,6 +819,17 @@
 static inline int MPIU_SHMW_shmSeg_open(
     MPIU_SHMW_shmHandle_t hnd, int shmSegSize){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            NULL, 0, MPIU_SHMW_FLAG_CLEAR));
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+
+    mpi_errno = MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
+            NULL, 0, MPIU_SHMW_FLAG_CLEAR);
+    if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -1389,7 +842,22 @@
     MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
     int attachOffset){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
-            ppShm, attachOffset, MPIU_SHMW_FLAG_SHMCREATENEW |
-            MPIU_SHMW_FLAG_SHMATTACH));
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1((shmSegSize <= 0), mpi_errno,
+        MPI_ERR_OTHER, "**arg", "**arg %s","shared mem segment size");
+    MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "ppShm");
+
+    mpi_errno = MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
+                    ppShm, attachOffset, MPIU_SHMW_FLAG_SHMCREATENEW |
+                    MPIU_SHMW_FLAG_SHMATTACH);
+    if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
 }
@@ -1402,58 +870,21 @@
     MPIU_SHMW_shmHandle_t hnd, int shmSegSize, char **ppShm,
     int attachOffset){
-    return (MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize, ppShm,
-                attachOffset, MPIU_SHMW_FLAG_SHMATTACH));
-}
-
-/* Detach from an attached SHM segment */
-#undef FUNCNAME
-#define FUNCNAME MPIU_SHMW_shmSeg_detach
-#undef FCNAME
-#define FCNAME MPIU_QUOTE(FUNCNAME)
-static inline int MPIU_SHMW_shmSeg_detach(
-    MPIU_SHMW_shmHandle_t hnd, char **ppShm, int shmSegSize){
-    int mpi_errno = MPI_SUCCESS;
-    if(!ppShm || !(*ppShm)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem ptr not valid");
-        return mpi_errno;
-    }
-
-    if(munmap(*ppShm, shmSegSize) != 0){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**detach_shar_mem",
-            "**detach_shar_mem %s %s","munmap",
-            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-        return mpi_errno;
-    }
-    *ppShm = NULL;
-    return mpi_errno;
-}
-
-/* Remove an existing SHM segment */
-static inline int  MPIU_SHMW_shmSeg_remove(MPIU_SHMW_shmHandle_t hnd){
-    int mpi_errno = MPI_SUCCESS;
-
-    if(!MPIU_SHMW_shmGHandle_isValid(hnd)){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**arg", "**arg %s",
-            "shared mem Handle not valid");
-        return mpi_errno;
-    }
-
-    if( unlink(MPIU_SHMW_shmGHandle_getByRef(hnd)) != 0){
-        mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
-            FCNAME, __LINE__, MPI_ERR_OTHER, "**remove_shar_mem",
-            "**remove_shar_mem %s %s","unlink",
-            MPIU_OSW_strerror(MPIU_OSW_getErrno()));
-        return mpi_errno;
-    }
-    return mpi_errno;
-}
-
-#else
-#   error "No shared memory mechanism specified"
-#endif
+    int mpi_errno = MPI_SUCCESS;
+
+    MPIU_ERR_CHKANDJUMP(!MPIU_SHMW_shmHandle_isInit(hnd),
+        mpi_errno, MPI_ERR_OTHER, "**shmw_badhnd");
+    MPIU_ERR_CHKANDJUMP1(!ppShm, mpi_errno, MPI_ERR_OTHER,
+        "**nullptr", "**nullptr %s", "ppShm");
+
+    mpi_errno = MPIU_SHMW_shmSeg_createAttachTempl(hnd, shmSegSize,
+                ppShm, attachOffset, MPIU_SHMW_FLAG_SHMATTACH);
+    if(mpi_errno != MPI_SUCCESS) { MPIU_ERR_POP(mpi_errno); }
+
+fn_exit:
+    return mpi_errno;
+fn_fail:
+    goto fn_exit;
+}
+
 #undef FUNCNAME
Index: /mpich2/branches/dev/NemesisOnWindows/winconfigure.wsf
===================================================================
--- /mpich2/branches/dev/NemesisOnWindows/winconfigure.wsf (revision 3689)
+++ /mpich2/branches/dev/NemesisOnWindows/winconfigure.wsf (revision 3727)
@@ -2992,5 +2992,5 @@
 					fout.WriteLine("#undef MPICH_DEBUG_MUTEX")
 				Case "MPICH_DEBUG_HANDLEALLOC"
-					fout.WriteLine("#define MPICH_DEBUG_HANDLEALLOC 1")
+					fout.WriteLine("/* #undef MPICH_DEBUG_HANDLEALLOC */")
 				Case "STACK_DIRECTION"
 					fout.WriteLine("#undef STACK_DIRECTION")
@@ -3066,5 +3066,5 @@
                                         fout.WriteLine("#define MPIO_USES_MPI_REQUEST 1")
                                 Case "USE_STRICT_MPI"
-                                        fout.WriteLine("#define USE_STRICT_MPI 1")
+                                        fout.WriteLine("/* #undef USE_STRICT_MPI */")
 				Case Else
 					'MsgBox("Unhandled definition <" & define & ">")