VirtualBox

Changeset 23587 in vbox for trunk/src/VBox/VMM


Ignore:
Timestamp:
Oct 6, 2009 5:10:52 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
53253
Message:

CFGM: Optimize the lookups.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/CFGM.cpp

    r23586 r23587  
    340340
    341341/**
    342  * Compares two names.
    343  *
    344  * @returns Similar to memcpy.
    345  * @param   pszName1            The first name.
    346  * @param   cchName1            The length of the first name.
    347  * @param   pszName2            The second name.
    348  * @param   cchName2            The length of the second name.
    349  */
    350 DECLINLINE(int) cfgmR3CompareNames(const char *pszName1, size_t cchName1, const char *pszName2, size_t cchName2)
    351 {
    352     int iDiff;
    353     if (cchName1 <= cchName2)
    354     {
    355         iDiff = memcmp(pszName1, pszName2, cchName1);
    356         if (!iDiff && cchName1 < cchName2)
    357             iDiff = -1;
    358     }
    359     else
    360     {
    361         iDiff = memcmp(pszName1, pszName2, cchName2);
    362         if (!iDiff)
    363             iDiff = 1;
    364     }
    365     return iDiff;
    366 }
    367 
    368 
    369 /**
    370342 * Validates that the child nodes are within a set of valid names.
    371343 *
     
    10351007static int cfgmR3ResolveNode(PCFGMNODE pNode, const char *pszPath, PCFGMNODE *ppChild)
    10361008{
    1037     if (pNode)
    1038     {
    1039         PCFGMNODE pChild = NULL;
    1040         for (;;)
     1009    if (!pNode)
     1010        return VERR_CFGM_NO_PARENT;
     1011    PCFGMNODE pChild = NULL;
     1012    for (;;)
     1013    {
     1014        /* skip leading slashes. */
     1015        while (*pszPath == '/')
     1016            pszPath++;
     1017
     1018        /* End of path? */
     1019        if (!*pszPath)
    10411020        {
    1042             /* skip leading slashes. */
    1043             while (*pszPath == '/')
    1044                 pszPath++;
    1045 
    1046             /* End of path? */
    1047             if (!*pszPath)
     1021            if (!pChild)
     1022                return VERR_CFGM_INVALID_CHILD_PATH;
     1023            *ppChild = pChild;
     1024            return VINF_SUCCESS;
     1025        }
     1026
     1027        /* find end of component. */
     1028        const char *pszNext = strchr(pszPath, '/');
     1029        if (!pszNext)
     1030            pszNext = strchr(pszPath,  '\0');
     1031        RTUINT cchName = pszNext - pszPath;
     1032
     1033        /* search child list. */
     1034        pChild = pNode->pFirstChild;
     1035        for ( ; pChild; pChild = pChild->pNext)
     1036            if (pChild->cchName == cchName)
    10481037            {
    1049                 if (!pChild)
    1050                     return VERR_CFGM_INVALID_CHILD_PATH;
    1051                 *ppChild = pChild;
    1052                 return VINF_SUCCESS;
     1038                int iDiff = memcmp(pszPath, pChild->szName, cchName);
     1039                if (iDiff <= 0)
     1040                {
     1041                    if (iDiff != 0)
     1042                        pChild = NULL;
     1043                    break;
     1044                }
    10531045            }
    1054 
    1055             /* find end of component. */
    1056             const char *pszNext = strchr(pszPath, '/');
    1057             if (!pszNext)
    1058                 pszNext = strchr(pszPath,  '\0');
    1059             RTUINT cchName = pszNext - pszPath;
    1060 
    1061             /* search child list. */ /** @todo the list is ordered now, consider optimizing the search. */
    1062             pChild = pNode->pFirstChild;
    1063             for ( ; pChild; pChild = pChild->pNext)
    1064                 if (    pChild->cchName == cchName
    1065                     &&  !memcmp(pszPath, pChild->szName, cchName) )
    1066                     break;
    1067 
    1068             /* if not found, we're done. */
    1069             if (!pChild)
    1070                 return VERR_CFGM_CHILD_NOT_FOUND;
    1071 
    1072             /* next iteration */
    1073             pNode = pChild;
    1074             pszPath = pszNext;
    1075         }
    1076 
    1077         /* won't get here */
    1078     }
    1079     else
    1080         return VERR_CFGM_NO_PARENT;
     1046        if (!pChild)
     1047            return VERR_CFGM_CHILD_NOT_FOUND;
     1048
     1049        /* next iteration */
     1050        pNode = pChild;
     1051        pszPath = pszNext;
     1052    }
     1053
     1054    /* won't get here */
    10811055}
    10821056
     
    10921066static int cfgmR3ResolveLeaf(PCFGMNODE pNode, const char *pszName, PCFGMLEAF *ppLeaf)
    10931067{
    1094     int rc;
    1095     if (pNode)
    1096     {
    1097         size_t      cchName = strlen(pszName);
    1098         PCFGMLEAF   pLeaf = pNode->pFirstLeaf;
    1099         while (pLeaf)
     1068    if (!pNode)
     1069        return VERR_CFGM_NO_PARENT;
     1070
     1071    size_t      cchName = strlen(pszName);
     1072    PCFGMLEAF   pLeaf   = pNode->pFirstLeaf;
     1073    while (pLeaf)
     1074    {
     1075        if (cchName == pLeaf->cchName)
    11001076        {
    1101             /** @todo the list is ordered now, consider optimizing the search. */
    1102             if (    cchName == pLeaf->cchName
    1103                 && !memcmp(pszName, pLeaf->szName, cchName) )
     1077            int iDiff = memcmp(pszName, pLeaf->szName, cchName);
     1078            if (iDiff <= 0)
    11041079            {
     1080                if (iDiff != 0)
     1081                    break;
    11051082                *ppLeaf = pLeaf;
    11061083                return VINF_SUCCESS;
    11071084            }
    1108 
    1109             /* next */
    1110             pLeaf = pLeaf->pNext;
    11111085        }
    1112         rc = VERR_CFGM_VALUE_NOT_FOUND;
    1113     }
    1114     else
    1115         rc = VERR_CFGM_NO_PARENT;
    1116     return rc;
     1086
     1087        /* next */
     1088        pLeaf = pLeaf->pNext;
     1089    }
     1090    return VERR_CFGM_VALUE_NOT_FOUND;
    11171091}
    11181092
     
    12001174    }
    12011175    return rc;
     1176}
     1177
     1178
     1179/**
     1180 * Compares two names.
     1181 *
     1182 * @returns Similar to memcpy.
     1183 * @param   pszName1            The first name.
     1184 * @param   cchName1            The length of the first name.
     1185 * @param   pszName2            The second name.
     1186 * @param   cchName2            The length of the second name.
     1187 */
     1188DECLINLINE(int) cfgmR3CompareNames(const char *pszName1, size_t cchName1, const char *pszName2, size_t cchName2)
     1189{
     1190    int iDiff;
     1191    if (cchName1 <= cchName2)
     1192    {
     1193        iDiff = memcmp(pszName1, pszName2, cchName1);
     1194        if (!iDiff && cchName1 < cchName2)
     1195            iDiff = -1;
     1196    }
     1197    else
     1198    {
     1199        iDiff = memcmp(pszName1, pszName2, cchName2);
     1200        if (!iDiff)
     1201            iDiff = 1;
     1202    }
     1203    return iDiff;
    12021204}
    12031205
     
    12861288            if (pNext)
    12871289            {
    1288                 for (; pNext; pPrev = pNext, pNext = pNext->pNext)
     1290                for ( ; pNext; pPrev = pNext, pNext = pNext->pNext)
    12891291                {
    12901292                    int iDiff = cfgmR3CompareNames(pszName, cchName, pNext->szName, pNext->cchName);
     
    14281430            if (pNext)
    14291431            {
    1430                 for (; pNext; pPrev = pNext, pNext = pNext->pNext)
     1432                for ( ; pNext; pPrev = pNext, pNext = pNext->pNext)
    14311433                {
    14321434                    int iDiff = cfgmR3CompareNames(pszName, cchName, pNext->szName, pNext->cchName);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette