VirtualBox

Changeset 31526 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Aug 10, 2010 12:03:09 PM (14 years ago)
Author:
vboxsync
Message:

iprt/sg.h: Made RTSgBufMapToNative a bit safer and fixed some left over variable refs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/sg.h

    r31452 r31526  
    205205
    206206/**
    207  * Maps the given S/G buffer to a segment array of another type
    208  * (for example to iovec on Posix or WSABUF on Windows).
    209  *
    210  * @param   paMapped    Where to store the pointer to the start of the native array or NULL.
    211  *                      The memory needs to be freed with RTMemTmpFree().
     207 * Maps the given S/G buffer to a segment array of another type (for example to
     208 * iovec on POSIX or WSABUF on Windows).
     209 *
     210 * @param   paMapped    Where to store the pointer to the start of the native
     211 *                      array or NULL.  The memory needs to be freed with
     212 *                      RTMemTmpFree().
    212213 * @param   pSgBuf      The S/G buffer to map.
    213214 * @param   Struct      Struct used as the destination.
    214  * @param   pvBuf       Name of the field holding the pointer to a buffer.
     215 * @param   pvBufField  Name of the field holding the pointer to a buffer.
    215216 * @param   TypeBufPtr  Type of the buffer pointer.
    216  * @param   cbBuf       Name of the field holding the size of the buffer.
     217 * @param   cbBufField  Name of the field holding the size of the buffer.
    217218 * @param   TypeBufSize Type of the field for the buffer size.
    218  * @param   cSegsMapped Where to store the number of segments the native array has.
    219  *
    220  * @note This operation maps the whole S/G buffer starting at the current internal position.
    221  *       The internal buffer position is unchanged by this operation.
    222  *
    223  * @remark Usage is a bit ugly but saves a few lines of duplicated code somewhere else and makes it possible to
    224  *         keep the S/G buffer members private without going through RTSgBufSegArrayCreate() first.
    225  */
    226 #define RTSgBufMapToNative(paMapped, pSgBuf, Struct, pvBuf, TypeBufPtr, cbBuf, TypeBufSize, cSegsMapped) \
    227     do                                                                                        \
    228     {                                                                                         \
    229         AssertCompileMemberSize(Struct, pvBuf, RT_SIZEOFMEMB(RTSGSEG, pvSeg));                \
    230         /*AssertCompile(RT_SIZEOFMEMB(Struct, cbBuf) >=  RT_SIZEOFMEMB(RTSGSEG, cbSeg));*/    \
    231         cSegsMapped = pSgBuf->cSegs - pSgBuf->idxSeg;                                         \
    232                                                                                               \
    233         paMapped = NULL;                                                                      \
    234                                                                                               \
    235         /* We need room for at least one segment. */                                          \
    236         if (pSgBuf->cSegs == pSgBuf->idxSeg)                                                  \
    237             cSegsMapped++;                                                                    \
    238                                                                                               \
    239         paMapped = (Struct *)RTMemTmpAllocZ(cSegsMapped * sizeof(Struct));                    \
    240         if (paMapped)                                                                         \
    241         {                                                                                     \
     219 * @param   cSegsMapped Where to store the number of segments the native array
     220 *                      has.
     221 *
     222 * @note    This operation maps the whole S/G buffer starting at the current
     223 *          internal position.  The internal buffer position is unchanged by
     224 *          this operation.
     225 *
     226 * @remark  Usage is a bit ugly but saves a few lines of duplicated code
     227 *          somewhere else and makes it possible to keep the S/G buffer members
     228 *          private without going through RTSgBufSegArrayCreate() first.
     229 */
     230#define RTSgBufMapToNative(paMapped, pSgBuf, Struct, pvBufField, TypeBufPtr, cbBufField, TypeBufSize, cSegsMapped) \
     231    do \
     232    { \
     233        AssertCompileMemberSize(Struct, pvBufField, RT_SIZEOFMEMB(RTSGSEG, pvSeg)); \
     234        /*AssertCompile(RT_SIZEOFMEMB(Struct, cbBufField) >=  RT_SIZEOFMEMB(RTSGSEG, cbSeg));*/ \
     235        (cSegsMapped) = (pSgBuf)->cSegs - (pSgBuf)->idxSeg; \
     236        \
     237        /* We need room for at least one segment. */ \
     238        if ((pSgBuf)->cSegs == (pSgBuf)->idxSeg) \
     239            (cSegsMapped)++; \
     240        \
     241        (paMapped) = (Struct *)RTMemTmpAllocZ((cSegsMapped) * sizeof(Struct)); \
     242        if ((paMapped)) \
     243        { \
    242244            /* The first buffer is special because we could be in the middle of a segment. */ \
    243             paMapped[0].pvBuf = (TypeBufPtr)pSgBuf->pvSegCur;                                 \
    244             paMapped[0].cbBuf = (TypeBufSize)pSgBuf->cbSegLeft;                               \
    245                                                                                               \
    246             for (unsigned i = 1; i < cSegsMapped; i++)                                        \
    247             {                                                                                 \
    248                 paMsg[i].pvBuf = (TypeBufPtr)pSgBuf->paSegs[pSgBuf->idxSeg + i].pvSeg;        \
    249                 paMsg[i].cbBuf = (TypeBufSize)pSgBuf->paSegs[pSgBuf->idxSeg + i].cbSeg;       \
    250             }                                                                                 \
    251         }                                                                                     \
    252     }                                                                                         \
    253     while (0)
     245            (paMapped)[0].pvBufField = (TypeBufPtr)(pSgBuf)->pvSegCur; \
     246            (paMapped)[0].cbBufField = (TypeBufSize)(pSgBuf)->cbSegLeft; \
     247            \
     248            for (unsigned i = 1; i < (cSegsMapped); i++) \
     249            { \
     250                (paMapped)[i].pvBufField = (TypeBufPtr)(pSgBuf)->paSegs[(pSgBuf)->idxSeg + i].pvSeg; \
     251                (paMapped)[i].cbBufField = (TypeBufSize)(pSgBuf)->paSegs[(pSgBuf)->idxSeg + i].cbSeg; \
     252            } \
     253        } \
     254    } while (0)
    254255
    255256RT_C_DECLS_END
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