VirtualBox

Changeset 57407 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Aug 18, 2015 9:52:53 AM (9 years ago)
Author:
vboxsync
Message:

VBoxGuestR3LibGuestProp.cpp: Elimiate RTCMemAutoPtr.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGuestProp.cpp

    r57358 r57407  
    3535#include <iprt/string.h>
    3636#ifndef VBOX_VBGLR3_XSERVER
    37 # include <iprt/cpp/mem.h>
     37# include <iprt/mem.h>
    3838#endif
    3939#include <iprt/assert.h>
     
    6161#ifdef VBOX_VBGLR3_XSERVER
    6262
    63 # undef RTSTrEnd
     63# undef RTStrEnd
    6464# define RTStrEnd xf86RTStrEnd
    6565
     
    580580/**
    581581 * Start enumerating guest properties which match a given pattern.
     582 *
    582583 * This function creates a handle which can be used to continue enumerating.
    583584 *
     
    600601 *                          on success.  This must be freed with
    601602 *                          VbglR3GuestPropEnumFree when it is no longer needed.
    602  * @param  ppszName      Where to store the next property name.  This will be
    603  *                       set to NULL if there are no more properties to
    604  *                       enumerate.  This pointer should not be freed. Optional.
    605  * @param  ppszValue     Where to store the next property value.  This will be
    606  *                       set to NULL if there are no more properties to
    607  *                       enumerate.  This pointer should not be freed. Optional.
    608  * @param  pu64Timestamp Where to store the next property timestamp.  This
    609  *                       will be set to zero if there are no more properties
    610  *                       to enumerate. Optional.
    611  * @param  ppszFlags     Where to store the next property flags.  This will be
    612  *                       set to NULL if there are no more properties to
    613  *                       enumerate.  This pointer should not be freed. Optional.
     603 * @param   ppszName        Where to store the next property name.  This will be
     604 *                          set to NULL if there are no more properties to
     605 *                          enumerate.  This pointer should not be freed. Optional.
     606 * @param   ppszValue       Where to store the next property value.  This will be
     607 *                          set to NULL if there are no more properties to
     608 *                          enumerate.  This pointer should not be freed. Optional.
     609 * @param   pu64Timestamp  Where to store the next property timestamp.  This
     610 *                          will be set to zero if there are no more properties
     611 *                          to enumerate. Optional.
     612 * @param   ppszFlags       Where to store the next property flags.  This will be
     613 *                          set to NULL if there are no more properties to
     614 *                          enumerate.  This pointer should not be freed. Optional.
    614615 *
    615616 * @remarks While all output parameters are optional, you need at least one to
     
    626627{
    627628    /* Create the handle. */
    628     RTCMemAutoPtr<VBGLR3GUESTPROPENUM, VbglR3GuestPropEnumFree> Handle;
    629     Handle = (PVBGLR3GUESTPROPENUM)RTMemAllocZ(sizeof(VBGLR3GUESTPROPENUM));
    630     if (!Handle)
     629    PVBGLR3GUESTPROPENUM pHandle = (PVBGLR3GUESTPROPENUM)RTMemAllocZ(sizeof(VBGLR3GUESTPROPENUM));
     630    if (RT_LIKELY(pHandle))
     631    {/* likely */}
     632    else
    631633        return VERR_NO_MEMORY;
    632634
    633635    /* Get the length of the pattern string, including the final terminator. */
    634     size_t cchPatterns = 1;
     636    size_t cbPatterns = 1;
    635637    for (uint32_t i = 0; i < cPatterns; ++i)
    636         cchPatterns += strlen(papszPatterns[i]) + 1;
    637 
    638     /* Pack the pattern array */
    639     RTCMemAutoPtr<char> Patterns;
    640     Patterns = (char *)RTMemAlloc(cchPatterns);
     638        cbPatterns += strlen(papszPatterns[i]) + 1;
     639
     640    /* Pack the pattern array. */
     641    char *pszzPatterns = (char *)RTMemAlloc(cbPatterns);
    641642    size_t off = 0;
    642643    for (uint32_t i = 0; i < cPatterns; ++i)
    643644    {
    644645        size_t cb = strlen(papszPatterns[i]) + 1;
    645         memcpy(&Patterns[off], papszPatterns[i], cb);
     646        memcpy(&pszzPatterns[off], papszPatterns[i], cb);
    646647        off += cb;
    647648    }
    648     Patterns[off] = '\0';
    649 
    650     /* Randomly chosen initial size for the buffer to hold the enumeration
    651      * information. */
    652     uint32_t cchBuf = 4096;
    653     RTCMemAutoPtr<char> Buf;
     649    pszzPatterns[off] = '\0';
    654650
    655651    /* In reading the guest property data we are racing against the host
    656652     * adding more of it, so loop a few times and retry on overflow. */
    657     int rc = VINF_SUCCESS;
     653    uint32_t cbBuf  = 4096; /* picked out of thin air */
     654    char    *pchBuf = NULL;
     655    int      rc     = VINF_SUCCESS;
    658656    for (int i = 0; i < 10; ++i)
    659657    {
    660         if (!Buf.realloc(cchBuf))
     658        void *pvNew = RTMemRealloc(pchBuf, cbBuf);
     659        if (pvNew)
     660            pchBuf = (char *)pvNew;
     661        else
    661662        {
    662663            rc = VERR_NO_MEMORY;
    663664            break;
    664665        }
    665         rc = VbglR3GuestPropEnumRaw(u32ClientId, Patterns.get(),
    666                                     Buf.get(), cchBuf, &cchBuf);
     666        rc = VbglR3GuestPropEnumRaw(u32ClientId, pszzPatterns, pchBuf, cbBuf, &cbBuf);
    667667        if (rc != VERR_BUFFER_OVERFLOW)
    668668            break;
    669         cchBuf += 4096;  /* Just to increase our chances */
    670     }
     669        cbBuf += 4096;  /* Just to increase our chances */
     670    }
     671    RTMemFree(pszzPatterns);
    671672    if (RT_SUCCESS(rc))
    672673    {
    673674        /*
    674          * Transfer ownership of the buffer to the handle structure and
    675          * call VbglR3GuestPropEnumNext to retrieve the first entry.
     675         * Complete the handle and call VbglR3GuestPropEnumNext to retrieve the first entry.
    676676         */
    677         Handle->pchNext = Handle->pchBuf = Buf.release();
    678         Handle->pchBufEnd = Handle->pchBuf + cchBuf;
     677        pHandle->pchNext   = pchBuf;
     678        pHandle->pchBuf    = pchBuf;
     679        pHandle->pchBufEnd = pchBuf + cbBuf;
    679680
    680681        const char *pszNameTmp;
    681682        if (!ppszName)
    682683            ppszName = &pszNameTmp;
    683         rc = VbglR3GuestPropEnumNext(Handle.get(), ppszName, ppszValue,
    684                                      pu64Timestamp, ppszFlags);
     684        rc = VbglR3GuestPropEnumNext(pHandle, ppszName, ppszValue, pu64Timestamp, ppszFlags);
    685685        if (RT_SUCCESS(rc))
    686             *ppHandle = Handle.release();
     686        {
     687            *ppHandle = pHandle;
     688            return rc;
     689        }
    687690    }
    688691    else if (rc == VERR_BUFFER_OVERFLOW)
    689692        rc = VERR_TOO_MUCH_DATA;
     693    RTMemFree(pchBuf);
     694    RTMemFree(pHandle);
    690695    return rc;
    691696}
Note: See TracChangeset for help on using the changeset viewer.

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