static mama_status
mamaQueue_createReuseableMsg (mamaQueueImpl*  impl)
{
	 mama_status     status      =   MAMA_STATUS_OK;
	/*Create the reuseable cached mamaMsg for this queue*/
	if (MAMA_STATUS_OK != (status = mamaMsgImpl_createForPayload (&(impl->mMsg), NULL,NULL,0)))
	{
		mama_log (MAMA_LOG_LEVEL_ERROR, "mamaQueue_create(): "
				  "Could not create message for queue.");
		mamaQueue_destroy ((mamaQueue)impl);
		return status;
	}

	/*Set the bridge impl structure on the new message.
	  It will be needed for any bridge specific operations.*/
	if (MAMA_STATUS_OK!=(status=mamaMsgImpl_setBridgeImpl (impl->mMsg,
							impl->mBridgeImpl)))
	{
		mama_log (MAMA_LOG_LEVEL_ERROR, "mamaQueue_create(): "
				  "Could not set bridge impl on new message.");
		mamaQueue_destroy ((mamaQueue)impl);
		return status;
	}

	/*We don't want to destroy the bridge message when the mamamsg is
	 * destroyed as we won't own it*/
	if (MAMA_STATUS_OK!=(status=mamaMsgImpl_setMessageOwner (impl->mMsg, 0)))
	{
		mama_log (MAMA_LOG_LEVEL_ERROR, "mamaQueue_create():"
				  " Could not set owner on new message.");
		return status;
	}

	/*Associate the queue with the message*/
	if (MAMA_STATUS_OK!=(status=mamaMsgImpl_setQueue (impl->mMsg,
					(mamaQueue)impl)))
	{
		mama_log (MAMA_LOG_LEVEL_ERROR, "mamaQueue_create(): "
				  "Could not set queue on new message.");
		mamaQueue_destroy ((mamaQueue)impl);
		return status;
	}
    return status;
}
mama_status
mamaMsgField_getVectorMsg (
    const mamaMsgField     msgField,
    const mamaMsg**        result,
    mama_size_t*           resultLen)
{
    /* Initialise to error as NULL check will not cause status to be set */
    mama_status status        = MAMA_STATUS_OK;
    void*       vp   = NULL;
    mama_size_t size = 0;

    mamaMsgFieldImpl* impl = (mamaMsgFieldImpl*)(msgField);
    if (!impl) return MAMA_STATUS_NULL_ARG;

    *result    = NULL;
    *resultLen = 0;

    if (impl->myPayloadBridge)
    {
        msgPayload* payloadVector = NULL;
        size_t      i             = 0;

        if (MAMA_STATUS_OK == (status = impl->myPayloadBridge->msgFieldPayloadGetVectorMsg (
                                            impl->myPayload,
                                            (const msgPayload**) &payloadVector,
                                            &size)))
        {
            /*Either create or reallocate the vector if it is not big enough*/
            if (!impl->myLastVectorPayloadMsg)
            {
                impl->myLastVectorPayloadMsg = (mamaMsg*)calloc(size,
                                               sizeof(mamaMsg));

                if(impl->myLastVectorPayloadMsg == NULL)
                    return MAMA_STATUS_NOMEM;
            }
            else if (size > impl->myLastVectorPayloadMsgLen)
            {
                vp = realloc (impl->myLastVectorPayloadMsg,
                              size*sizeof(mamaMsg));

                if(vp == NULL)
                    return MAMA_STATUS_NOMEM;

                impl->myLastVectorPayloadMsg = (mamaMsg*)vp;
            }

            for (i = 0; (i != impl->myLastVectorPayloadMsgLen) && (i != size);
                    ++i)
            {
                mamaMsgImpl_setPayloadBridge (impl->myLastVectorPayloadMsg[i],
                                              impl->myPayloadBridge);
                /*
                 * We do not detach the middleware message so we do
                 * not own it.
                 */
                mamaMsgImpl_setPayload       (impl->myLastVectorPayloadMsg[i],
                                              payloadVector[i],
                                              0);
            }

            for (; i != size; ++i)
            {
                /*
                 * We create from payload so payload owns
                 * We set owner to false to not delete the payload
                 */
                mamaMsgImpl_createForPayload (&(impl->myLastVectorPayloadMsg[i]),
                                              payloadVector[i],
                                              impl->myPayloadBridge,
                                              0);
            }

            /*Store the maxumim vector we have encountered*/
            if (size > impl->myLastVectorPayloadMsgLen)
            {
                impl->myLastVectorPayloadMsgLen = size;
            }

            *resultLen =  size;
            *result    = impl->myLastVectorPayloadMsg;
            return MAMA_STATUS_OK;
        }
        else
        {
            mama_log (MAMA_LOG_LEVEL_ERROR, "Failed to get payload msg vector from"
                      "message");
            return status;
        }
    }
    return MAMA_STATUS_NULL_ARG;
}