VirtualBox

Changeset 37208 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 25, 2011 9:22:18 AM (14 years ago)
Author:
vboxsync
Message:

circbuf: code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/circbuf.cpp

    r33286 r37208  
    2626
    2727
    28 /******************************************************************************
    29  *   Header Files                                                             *
    30  ******************************************************************************/
     28/*******************************************************************************
     29*   Header Files                                                               *
     30*******************************************************************************/
    3131#include <iprt/circbuf.h>
    3232#include <iprt/mem.h>
     
    3535#include <iprt/err.h>
    3636
    37 /******************************************************************************
    38  *   Public Functions                                                         *
    39  ******************************************************************************/
    4037
    4138RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
     
    5047        return VERR_NO_MEMORY;
    5148
    52     int rc = VINF_SUCCESS;
    53     do
     49    pTmpBuf->pvBuf = RTMemAlloc(cbSize);
     50    if (pTmpBuf->pvBuf)
    5451    {
    55         pTmpBuf->pvBuf = RTMemAlloc(cbSize);
    56         if (!pTmpBuf)
    57         {
    58             rc = VERR_NO_MEMORY;
    59             break;
    60         }
    61 
    6252        pTmpBuf->cbBufSize = cbSize;
    6353        *ppBuf = pTmpBuf;
    64     }while (0);
    65 
    66     if (RT_FAILURE(rc))
    67         RTMemFree(pTmpBuf);
    68 
    69     return rc;
    70 }
     54        return VINF_SUCCESS;
     55    }
     56
     57    RTMemFree(pTmpBuf);
     58    return VERR_NO_MEMORY;
     59}
     60
    7161
    7262RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf)
    7363{
    7464    /* Validate input. */
    75     AssertPtrNull(pBuf);
    76 
    77     if (pBuf)
    78     {
    79         if (pBuf->pvBuf)
    80             RTMemFree(pBuf->pvBuf);
    81         RTMemFree(pBuf);
    82     }
    83 }
     65    if (!pBuf)
     66        return;
     67    AssertPtr(pBuf);
     68    RTMemFree(pBuf->pvBuf);
     69    RTMemFree(pBuf);
     70}
     71
    8472
    8573RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf)
     
    8876    AssertPtr(pBuf);
    8977
    90     pBuf->uReadPos = 0;
     78    pBuf->uReadPos  = 0;
    9179    pBuf->uWritePos = 0;
    9280    pBuf->cbBufUsed = 0;
    9381}
     82
    9483
    9584RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf)
     
    10392}
    10493
     94
    10595RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
    10696{
     
    113103}
    114104
     105
    115106RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
    116107{
     
    120111    return pBuf->cbBufSize;
    121112}
     113
    122114
    123115RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
     
    146138            /* Return the pointer address which point to the current read
    147139             * position. */
    148             *ppvStart = (char*)pBuf->pvBuf + pBuf->uReadPos;
     140            *ppvStart = (char *)pBuf->pvBuf + pBuf->uReadPos;
    149141            *pcbSize = uSize;
    150142        }
     
    152144}
    153145
     146
    154147RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
    155148{
     
    163156    ASMAtomicSubSize(&pBuf->cbBufUsed, cbSize, &cbOld);
    164157}
     158
    165159
    166160RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
     
    172166    AssertPtr(pcbSize);
    173167
    174     size_t uFree;
    175     size_t uSize;
    176 
    177168    *ppvStart = 0;
    178169    *pcbSize = 0;
     
    181172    size_t cbSize = 0;
    182173    ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
    183     uFree = pBuf->cbBufSize - cbSize;
     174    size_t uFree = pBuf->cbBufSize - cbSize;
    184175    if (uFree > 0)
    185176    {
    186177        /* Get the size out of the requested size, the write block till the end
    187178         * of the buffer & the currently free size. */
    188         uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, uFree));
     179        size_t uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, uFree));
    189180        if (uSize > 0)
    190181        {
     
    197188}
    198189
     190
    199191RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
    200192{
     
    205197    pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;
    206198
    207     size_t cbOld = 0;
    208     ASMAtomicAddSize(&pBuf->cbBufUsed, cbSize, &cbOld);
    209 }
    210 
     199    size_t cbOldIgnored = 0;
     200    ASMAtomicAddSize(&pBuf->cbBufUsed, cbSize, &cbOldIgnored);
     201}
     202
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