VirtualBox

Changeset 106917 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Nov 10, 2024 2:26:41 AM (5 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
165843
Message:

IPRT: Added C++ versions of bzero, memmove, strcmp & strncpy for win.arm64 to use in RuntimeR0 and other nocrt situations. jiraref:VBP-1442

Location:
trunk/src/VBox/Runtime/common/string
Files:
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/bzero.cpp

    r106914 r106917  
    11/* $Id$ */
    22/** @file
    3  * IPRT - CRT Strings, memset().
     3 * IPRT - CRT Strings, bzero().
    44 */
    55
     
    4444
    4545/**
    46  * Fill a memory block with specific byte.
     46 * Fill a memory block with zeros.
    4747 *
    4848 * @returns pvDst.
    49  * @param   pvDst      Pointer to the block.
    50  * @param   ch      The filler char.
     49 * @param   pvDst   Pointer to the block.
    5150 * @param   cb      The size of the block.
    5251 */
    53 #undef memset
    54 #ifdef _MSC_VER
    55 # if _MSC_VER >= 1400
    56 void *  __cdecl RT_NOCRT(memset)(__out_bcount_full_opt(_Size) void *pvDst, __in int ch, __in size_t cb)
    57 # else
    58 void *RT_NOCRT(memset)(void *pvDst, int ch, size_t cb)
    59 # endif
    60 #else
    61 void *RT_NOCRT(memset)(void *pvDst, int ch, size_t cb)
    62 #endif
     52#undef bzero
     53void *RT_NOCRT(bzero)(void *pvDst, size_t cb)
    6354{
    6455    union
    6556    {
    6657        uint8_t  *pu8;
    67         uint32_t *pu32;
     58        size_t   *puz;
    6859        void     *pvDst;
     60        uintptr_t uPtr;
    6961    } u;
    7062    u.pvDst = pvDst;
    7163
    72     /* 32-bit word moves. */
    73     uint32_t u32 = ch | (ch << 8);
    74     u32 |= u32 << 16;
    75     size_t c = cb >> 2;
     64    /* Align the attack. */
     65    if (cb > sizeof(size_t) * 2 && (u.uPtr % sizeof(size_t)))
     66    {
     67        size_t c = sizeof(size_t) - (u.uPtr % sizeof(size_t));
     68        cb -= c;
     69        while (c-- > 0)
     70            *u.pu8++ = 0;
     71    }
     72
     73    /* size_t sized moves. */
     74    size_t c = cb / sizeof(size_t);
    7675    while (c-- > 0)
    77         *u.pu32++ = u32;
     76        *u.puz++ = 0;
    7877
    7978    /* Remaining byte moves. */
    80     c = cb & 3;
     79    c = cb % sizeof(size_t);
    8180    while (c-- > 0)
    82         *u.pu8++ = (uint8_t)u32;
     81        *u.pu8++ = 0;
    8382
    8483    return pvDst;
    8584}
    86 RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(memset);
     85RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(bzero);
  • trunk/src/VBox/Runtime/common/string/memmove.cpp

    r106914 r106917  
    11/* $Id$ */
    22/** @file
    3  * IPRT - CRT Strings, memcpy().
     3 * IPRT - CRT Strings, memmove().
    44 */
    55
     
    5252 */
    5353#ifdef IPRT_NO_CRT
    54 # undef memcpy
    55 void *RT_NOCRT(memcpy)(void *pvDst, const void *pvSrc, size_t cb)
     54# undef memmove
     55void *RT_NOCRT(memmove)(void *pvDst, const void *pvSrc, size_t cb)
    5656#elif RT_MSC_PREREQ(RT_MSC_VER_VS2005)
    57 _CRT_INSECURE_DEPRECATE_MEMORY(memcpy_s) void * __cdecl
    58 memcpy(__out_bcount_full_opt(_Size) void *pvDst, __in_bcount_opt(_Size) const void *pvSrc, __in size_t cb)
     57_CRT_INSECURE_DEPRECATE_MEMORY(memmove_s) void * __cdecl
     58memmove(__out_bcount_full_opt(_Size) void *pvDst, __in_bcount_opt(_Size) const void *pvSrc, __in size_t cb)
    5959#else
    60 void *memcpy(void *pvDst, const void *pvSrc, size_t cb)
     60void *memmove(void *pvDst, const void *pvSrc, size_t cb)
    6161#endif
    6262{
    6363    union
    6464    {
    65         uint8_t  *pu8;
    66         uint32_t *pu32;
    67         void     *pv;
     65        uint8_t volatile  *pu8;
     66        size_t volatile   *puz;
     67        void              *pv;
     68        uintptr_t          uPtr;
    6869    } uTrg;
    6970    uTrg.pv = pvDst;
     
    7172    union
    7273    {
    73         uint8_t const  *pu8;
    74         uint32_t const *pu32;
    75         void const     *pv;
     74        uint8_t const volatile  *pu8;
     75        size_t const volatile   *puz;
     76        void const              *pv;
     77        uintptr_t                uPtr;
    7678    } uSrc;
    7779    uSrc.pv = pvSrc;
    7880
    79     /* 32-bit word moves. */
    80     size_t c = cb >> 2;
    81     while (c-- > 0)
    82         *uTrg.pu32++ = *uSrc.pu32++;
     81    if (uTrg.uPtr < uSrc.uPtr || uTrg.uPtr >= uSrc.uPtr + cb)
     82    {
     83        /*
     84         * Copy forwards.
     85         */
     86        /* size word moves. */
     87        size_t c = cb / sizeof(size_t);
     88        while (c-- > 0)
     89            *uTrg.puz++ = *uSrc.puz++;
    8390
    84     /* Remaining byte moves. */
    85     c = cb & 3;
    86     while (c-- > 0)
    87         *uTrg.pu8++ = *uSrc.pu8++;
     91        /* Remaining byte moves. */
     92        c = cb % sizeof(*uTrg.puz);
     93        while (c-- > 0)
     94            *uTrg.pu8++ = *uSrc.pu8++;
     95    }
     96    else if (uTrg.uPtr != uSrc.uPtr)
     97    {
     98        /*
     99         * Copy backwards.
     100         */
     101        uTrg.uPtr += cb;
     102        uSrc.uPtr += cb;
     103
     104        /* Byte stuff. */
     105        size_t c = cb % sizeof(*uTrg.puz);
     106        while (c-- > 0)
     107            *--uTrg.pu8 = *--uSrc.pu8;
     108
     109        /* size word moves. */
     110        c = cb / sizeof(size_t);
     111        while (c-- > 0)
     112            *--uTrg.puz = *--uSrc.puz;
     113    }
    88114
    89115    return pvDst;
    90116}
    91 RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(memcpy);
     117RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(memmove);
    92118
  • trunk/src/VBox/Runtime/common/string/strcmp.cpp

    r106914 r106917  
    11/* $Id$ */
    22/** @file
    3  * IPRT - CRT Strings, strcpy().
     3 * IPRT - CRT Strings, strcmp().
    44 */
    55
     
    4444
    4545/**
    46  * Copy a string
     46 * Compares two strings.
    4747 *
    48  * @returns Pointer to destination string
    49  * @param   pszDst      Will contain a copy of pszSrc.
    50  * @param   pszSrc      Zero terminated string.
     48 * @returns -1, 0 or 1.
     49 * @param   psz1    Left side string.
     50 * @param   psz2    Right side stirng.
    5151 */
    5252#ifdef IPRT_NO_CRT
    53 # undef strcpy
    54 char *RT_NOCRT(strcpy)(char *pszDst, const char *pszSrc)
     53# undef strcmp
     54int RT_NOCRT(strcmp)(const char *psz1, const char *psz2)
    5555#else
    56 char *strcpy(char *pszDst, const char *pszSrc)
     56int strcmp(const char *psz1, const char *psz2)
    5757#endif
    5858{
    59     char * const pszRet = pszDst;
    60     while ((*pszDst = *pszSrc++) != '\0')
    61         pszDst++;
     59    for (;;)
     60    {
     61        char const ch1 = *psz1++;
     62        char const ch2 = *psz2++;
     63        if (ch1 == ch2)
     64        {
     65            if (ch1 != 0)
     66            { /* likely*/ }
     67            else
     68                return 0;
     69        }
     70        else
     71            return ch1 < ch2 ? -1 : 1;
     72    }
     73}
     74RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(strcmp);
    6275
    63     return pszRet;
    64 }
    65 RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(strcpy);
    66 
  • trunk/src/VBox/Runtime/common/string/strncpy.cpp

    r106914 r106917  
    11/* $Id$ */
    22/** @file
    3  * IPRT - CRT Strings, strcpy().
     3 * IPRT - CRT Strings, strncpy().
    44 */
    55
     
    4949 * @param   pszDst      Will contain a copy of pszSrc.
    5050 * @param   pszSrc      Zero terminated string.
     51 * @param   cbDst       Size of the destination buffer.
    5152 */
    5253#ifdef IPRT_NO_CRT
    53 # undef strcpy
    54 char *RT_NOCRT(strcpy)(char *pszDst, const char *pszSrc)
     54# undef strncpy
     55char *RT_NOCRT(strncpy)(char *pszDst, const char *pszSrc, size_t cbDst)
    5556#else
    56 char *strcpy(char *pszDst, const char *pszSrc)
     57char *strncpy(char *pszDst, const char *pszSrc, size_t cbDst)
    5758#endif
    5859{
    59     char * const pszRet = pszDst;
    60     while ((*pszDst = *pszSrc++) != '\0')
    61         pszDst++;
     60    size_t off = 0;
     61    while (off < cbDst)
     62    {
     63        char const ch = *pszSrc;
     64        pszDst[off++] = ch;
     65        if (ch)
     66        { /* likely */ }
     67        else
     68        {
     69            /* (this zeroing is not very efficient) */
     70            while (off < cbDst)
     71                pszDst[off++] = '\0';
     72            break;
     73        }
     74    }
    6275
    63     return pszRet;
     76    return pszDst;
    6477}
    65 RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(strcpy);
     78RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(strncpy);
    6679
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