Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util MP - Hash Table.c
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Interface */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCM_UTM_HT_Init_Virtual ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCM_UTM_HT_Init_Virtual( P_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX nuiEntries, C_TgUINTXX nuiTotal )
{
    TgSIZE                              nuiEntry_Node;
    TgSIZE                              uiNode_Per_Page;
    TgSIZE                              nuiNode_Commit;
    P_TgVOID                            pReserve;
    P_TgVOID                            pCommit;

    TgERROR(nuiEntries >= 1);
    TgERROR(nuiTotal >= nuiEntries);

    nuiEntry_Node = 1 + nuiEntries * sizeof( P_STg2_UTM_Hash_Node ) / sizeof( STg2_UTM_Hash_Node );
    if (nuiTotal < nuiEntry_Node + 1)
        return (KTgE_FAIL);

    if (TgFAILED( tgCM_UTM_AM_RW_Init( &psHash_Table->m_sLock.m_sLock ) ))
        return (KTgE_FAIL);

    uiNode_Per_Page = tgMM_Page_Size() / sizeof( STg2_UTM_Hash_Node );
    nuiNode_Commit = (((nuiEntry_Node + 1) / uiNode_Per_Page) + 1) * uiNode_Per_Page;
    nuiNode_Commit = tgCM_MIN_UXX( nuiNode_Commit, nuiEntry_Node + nuiTotal );

    pReserve = TgRESERVE_VIRTUAL( (nuiEntry_Node + nuiTotal)*sizeof( STg2_UTM_Hash_Node ) );
    pCommit = TgCOMMIT_VIRTUAL( pReserve, nuiNode_Commit*sizeof( STg2_UTM_Hash_Node ) );

    psHash_Table->m_psHash_List = (PP_STg2_UTM_Hash_Node)pCommit;
    psHash_Table->m_psNode_List = (P_STg2_UTM_Hash_Node)pCommit;
    psHash_Table->m_uiFree_Node = nuiEntry_Node;
    psHash_Table->m_nuiEntries = nuiEntries;
    psHash_Table->m_nuiNode_Commit = nuiNode_Commit;
    psHash_Table->m_nuiNode_Virtual = nuiEntry_Node + nuiTotal;

    tgCM_UTM_HT_Clear( psHash_Table );
    return (KTgS_OK);
}


/* ---- tgCM_UTM_HT_Init_Fixed ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCM_UTM_HT_Init_Fixed( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX nuiEntries, C_TgUINTXX nuiBuffer_Size, P_TgVOID psBuffer )
{
    TgSIZE                              nuiEntry_Node;
    TgSIZE                              nuiNode_Commit;

    TgERROR(nuiEntries >= 1);

    nuiEntry_Node = 1 + nuiEntries * sizeof( P_STg2_UTM_Hash_Node ) / sizeof( STg2_UTM_Hash_Node );
    if (nuiBuffer_Size < (nuiEntry_Node + 1)*sizeof( STg2_UTM_Hash_Node ))
        return (KTgE_FAIL);

    nuiNode_Commit = nuiBuffer_Size / sizeof( STg2_UTM_Hash_Node );
    if (TgFAILED( tgCM_UTM_AM_RW_Init( &psHash_Table->m_sLock.m_sLock ) ))
        return (KTgE_FAIL);

    psHash_Table->m_psHash_List = (PP_STg2_UTM_Hash_Node)psBuffer;
    psHash_Table->m_psNode_List = (P_STg2_UTM_Hash_Node)psBuffer;
    psHash_Table->m_uiFree_Node = nuiEntry_Node;
    psHash_Table->m_nuiEntries = nuiEntries;
    psHash_Table->m_nuiNode_Commit = nuiNode_Commit;
    psHash_Table->m_nuiNode_Virtual = 0;

    tgCM_UTM_HT_Clear( psHash_Table );
    return (KTgS_OK);
}


/* ---- tgCM_UTM_HT_Remove_Internal ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgCM_UTM_HT_Remove_Internal( PCU_STg2_UTM_AM_HT psHash_Table, C_TgUINTXX uiHash )
{
    TgUINTXX                            uiIndex;
    TgUINTXX                            uiFree_Node;
    P_STg2_UTM_Hash_Node                psNode, psPrev_Node;

    uiIndex = uiHash % psHash_Table->m_nuiEntries;
    psNode = psHash_Table->m_psHash_List[uiIndex];

    if (nullptr == psNode)
        return (KTgE_FAIL);

    if (psNode->m_uiHash == uiHash)
    {
        if (0 != psNode->m_uiNext)
        {
            psHash_Table->m_psHash_List[uiIndex] = psHash_Table->m_psNode_List + psNode->m_uiNext;
        };

        uiFree_Node = (TgUINT32)(psNode - psHash_Table->m_psNode_List);
    }
    else
    {
        while (1)
        {
            /* Failed to find the requested hash in the array | table */
            if (0 == psNode->m_uiNext)
            {
                return (KTgE_FAIL);
            };

            psPrev_Node = psNode;
            psNode = psHash_Table->m_psNode_List + psNode->m_uiNext;

            if (psNode->m_uiHash == uiHash)
            {
                psPrev_Node->m_uiNext = psNode->m_uiNext;
                uiFree_Node = (TgUINT32)(psNode - psHash_Table->m_psNode_List);
                break;
            };
        };
    };

    psHash_Table->m_psNode_List[uiFree_Node].m_uiHash = KTgEMPTY_HASH;
    psHash_Table->m_psNode_List[uiFree_Node].m_uiNext = psHash_Table->m_uiFree_Node;
    psHash_Table->m_psNode_List[uiFree_Node].m_uiVal = ~((TgUINTPTR)0);
    psHash_Table->m_uiFree_Node = uiFree_Node;
    return (KTgS_OK);
}