//------------------------------------------------------------------------------
static void* getSharedMemory(tHostifInstance pHifInstance_p)
{
    tHostifReturn   hifRet;
    void*           pBase;
    size_t          span;

    hifRet = hostif_getBuf(pHifInstance_p, kHostifInstIdTimesync, &pBase, &span);
    if (hifRet != kHostifSuccessful)
    {
        DEBUG_LVL_ERROR_TRACE("%s() Could not get buffer from host interface (%d)\n",
                              __func__,
                              hifRet);
        return NULL;
    }

    if (span < sizeof(tTimesyncSharedMemory))
    {
        DEBUG_LVL_ERROR_TRACE("%s() Time Synchronization Buffer too small (shall be: %lu)\n",
                              __func__,
                              (ULONG)sizeof(tTimesyncSharedMemory));
        return NULL;
    }

    return pBase;
}
//------------------------------------------------------------------------------
tCircBufError circbuf_connectBuffer(tCircBufInstance* pInstance_p)
{
    if (pInstance_p->pCircBufArchInstance != NULL)
    {   // Queue must use host interface
        tHostifReturn           ret;
        tHostifInstance         pHostif = (tHostifInstance*)pInstance_p->pCircBufArchInstance;
        UINT8*                  pBufBase;
        UINT                    bufSize;
        tCircBufHostiBuffer*    pHostifBuffer;

        // Get buffer for queue
        ret = hostif_getBuf(pHostif, hostifInstance[pInstance_p->bufferId],
                            &pBufBase, &bufSize);
        if (ret != kHostifSuccessful)
        {
            DEBUG_LVL_ERROR_TRACE("%s getting hostif buffer instance failed with 0x%X!\n",
                                  __func__, ret);
            return kCircBufNoResource;
        }

        pHostifBuffer = (tCircBufHostiBuffer*)pBufBase;

        pInstance_p->pCircBufHeader = &(pHostifBuffer->circBufHeader);
        pInstance_p->pCircBuf = (UINT8*)pHostifBuffer + sizeof(tCircBufHostiBuffer);
    }

    return kCircBufOk;
}
//------------------------------------------------------------------------------
tOplkError errhnducal_init(tErrHndObjects* pLocalObjects_p)
{
    tHostifInstance pHostifInstance = hostif_getInstance(0);
    tOplkError      ret = kErrorOk;
    tHostifReturn   hostifRet;
    void*           pBase;
    size_t          span;

    if (pHostifInstance == NULL)
    {
        ret = kErrorNoResource;
        goto Exit;
    }

    // get linear buffer and check span
    hostifRet = hostif_getBuf(pHostifInstance, kHostifInstIdErrCount, &pBase, &span);
    if (hostifRet != kHostifSuccessful)
    {
        ret = kErrorNoResource;
        goto Exit;
    }

    if (span < sizeof(tErrHndObjects))
    {
        DEBUG_LVL_ERROR_TRACE("%s: Error Handler Object Buffer too small\n",
                              __func__);
        ret = kErrorNoResource;
        goto Exit;
    }

    pHostifMem_l = pBase;
    pLocalObjects_l = pLocalObjects_p;

Exit:
    return ret;
}
//------------------------------------------------------------------------------
tOplkError pdokcal_openMem(void)
{
    tHostifReturn   hifret;
    tHostifInstance pInstance = hostif_getInstance(0);

    if (pInstance == NULL)
    {
        DEBUG_LVL_ERROR_TRACE("%s() couldn't get PCP hostif instance\n",
                              __func__);
        return kErrorNoResource;
    }

    hifret = hostif_getBuf(pInstance, kHostifInstIdPdo, &limPdo_l.pBase, &limPdo_l.span);

    if (hifret != kHostifSuccessful)
    {
        DEBUG_LVL_ERROR_TRACE("%s() Could not get buffer from host interface (%d)\n",
                              __func__,
                              hifret);
        return kErrorNoResource;
    }

    return kErrorOk;
}
//------------------------------------------------------------------------------
tCircBufError circbuf_allocBuffer(tCircBufInstance* pInstance_p, size_t* pSize_p)
{
    size_t size = *pSize_p;

    if (pInstance_p->pCircBufArchInstance == NULL)
    {
        // Allocate requested size + header
        size += sizeof(tCircBufHeader);

        pInstance_p->pCircBufHeader = OPLK_MALLOC(size);

        if (pInstance_p->pCircBufHeader == NULL)
        {
            DEBUG_LVL_ERROR_TRACE("%s() malloc failed!\n", __func__);
            return kCircBufNoResource;
        }

        pInstance_p->pCircBuf = ((BYTE*)pInstance_p->pCircBufHeader) + sizeof(tCircBufHeader);

        // Return buffer size: pSize_p already holds the right value!
    }
    else
    {   // Queue must use host interface
        tHostifReturn           ret;
        tHostifInstance         pHostif = (tHostifInstance*)pInstance_p->pCircBufArchInstance;
        UINT8*                  pBufBase;
        UINT                    bufSize;
        tCircBufHostiBuffer*    pHostifBuffer;

        // Get buffer for queue
        ret = hostif_getBuf(pHostif, hostifInstance[pInstance_p->bufferId],
                            &pBufBase, &bufSize);
        if (ret != kHostifSuccessful)
        {
            DEBUG_LVL_ERROR_TRACE("%s getting hostif buffer instance failed with 0x%X!\n",
                                  __func__, ret);
            return kCircBufNoResource;
        }

        // Check if there is enough memory available
        if (size > bufSize)
        {
            DEBUG_LVL_ERROR_TRACE("%s Hostif buffer (id=%d) only provides %d byte instead of %d byte!\n",
                                  __func__, pInstance_p->bufferId, bufSize, size);
            return kCircBufNoResource;
        }

        pHostifBuffer = (tCircBufHostiBuffer*)pBufBase;

        pInstance_p->pCircBufHeader = &(pHostifBuffer->circBufHeader);
        pInstance_p->pCircBuf = (UINT8*)pHostifBuffer + sizeof(tCircBufHostiBuffer);
        size -= sizeof(tCircBufHostiBuffer);

        // Return buffer size
        *pSize_p = size;

        HOSTIF_WR8(&(pHostifBuffer->lock), 0, CIRCBUF_HOSTIF_UNLOCK);
    }

    return kCircBufOk;
}