VirtualBox

Changeset 32883 in vbox for trunk/src


Ignore:
Timestamp:
Oct 4, 2010 12:16:19 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
66364
Message:

Storage/VMDK: fix memory leak, and squash another non-sequential write in streamOptimized

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/VmdkHDDCore.cpp

    r32882 r32883  
    538538*******************************************************************************/
    539539
    540 static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent);
    541 
    542540static void vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
    543541                               bool fDelete);
     
    999997        DeflateState.pvCompGrain = pExtent->pvCompGrain;
    1000998
    1001         rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper, RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
     999        rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper,
     1000                             RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
    10021001        if (RT_FAILURE(rc))
    10031002            return rc;
     
    10341033                return rc;
    10351034
     1035/** @todo remove this code */
    10361036            /* Set the file size to remove old garbage in case the block is
    10371037             * rewritten. Cannot cause data loss as the code calling this
     
    11671167}
    11681168
     1169/**
     1170 * Internal: free all buffers associated with grain directories.
     1171 */
     1172static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
     1173{
     1174    if (pExtent->pGD)
     1175    {
     1176        RTMemFree(pExtent->pGD);
     1177        pExtent->pGD = NULL;
     1178    }
     1179    if (pExtent->pRGD)
     1180    {
     1181        RTMemFree(pExtent->pRGD);
     1182        pExtent->pRGD = NULL;
     1183    }
     1184    if (pExtent->pvCompGrain)
     1185    {
     1186        RTMemFree(pExtent->pvCompGrain);
     1187        pExtent->pvCompGrain = NULL;
     1188    }
     1189    if (pExtent->pvGrain)
     1190    {
     1191        RTMemFree(pExtent->pvGrain);
     1192        pExtent->pvGrain = NULL;
     1193    }
     1194}
     1195
     1196/**
     1197 * Internal: allocate all buffers associated with grain directories. This
     1198 * includes the compressed/uncompressed buffers for streamOptimized images.
     1199 */
     1200static int vmdkAllocGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
     1201{
     1202    size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
     1203    uint32_t *pGD = NULL, *pRGD = NULL;
     1204
     1205    pGD = (uint32_t *)RTMemAllocZ(cbGD);
     1206    if (!pGD)
     1207    {
     1208        rc = VERR_NO_MEMORY;
     1209        goto out;
     1210    }
     1211    pExtent->pGD = pGD;
     1212
     1213    if (pExtent->uSectorRGD)
     1214    {
     1215        pRGD = (uint32_t *)RTMemAllocZ(cbGD);
     1216        if (!pRGD)
     1217        {
     1218            rc = VERR_NO_MEMORY;
     1219            goto out;
     1220        }
     1221        pExtent->pRGD = pRGD;
     1222    }
     1223
     1224    if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
     1225    {
     1226        /* streamOptimized extents need a compressed grain buffer, which must
     1227         * be big enough to hold uncompressible data (which needs ~8 bytes
     1228         * more than the uncompressed data), the marker and padding. */
     1229        pExtent->cbCompGrain = RT_ALIGN_Z(  VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
     1230                                          + 8 + sizeof(VMDKMARKER), 512);
     1231        pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
     1232        if (!pExtent->pvCompGrain)
     1233        {
     1234            rc = VERR_NO_MEMORY;
     1235            goto out;
     1236        }
     1237
     1238        /* streamOptimized extents need a decompressed grain buffer. */
     1239        pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
     1240        if (!pExtent->pvGrain)
     1241        {
     1242            rc = VERR_NO_MEMORY;
     1243            goto out;
     1244        }
     1245    }
     1246
     1247out:
     1248    if (RT_FAILURE(rc))
     1249        vmdkFreeGrainDirectory(pExtent);
     1250    return rc;
     1251}
     1252
    11691253static int vmdkReadGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
    11701254{
    11711255    int rc = VINF_SUCCESS;
    11721256    unsigned i;
    1173     uint32_t *pGD = NULL, *pRGD = NULL, *pGDTmp, *pRGDTmp;
     1257    uint32_t *pGDTmp, *pRGDTmp;
    11741258    size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
    11751259
     
    11771261        goto out;
    11781262
    1179     pGD = (uint32_t *)RTMemAllocZ(cbGD);
    1180     if (!pGD)
    1181     {
    1182         rc = VERR_NO_MEMORY;
    1183         goto out;
    1184     }
    1185     pExtent->pGD = pGD;
     1263    rc = vmdkAllocGrainDirectory(pImage, pExtent);
     1264    if (RT_FAILED(rc))
     1265        goto out;
     1266
    11861267    /* The VMDK 1.1 spec seems to talk about compressed grain directories,
    11871268     * but in reality they are not compressed. */
     
    12001281    if (pExtent->uSectorRGD)
    12011282    {
    1202         pRGD = (uint32_t *)RTMemAllocZ(cbGD);
    1203         if (!pRGD)
    1204         {
    1205             rc = VERR_NO_MEMORY;
    1206             goto out;
    1207         }
    12081283        pExtent->pRGD = pRGD;
    12091284        /* The VMDK 1.1 spec seems to talk about compressed grain directories,
     
    13431418        RTMemTmpFree(pTmpGT);
    13441419
    1345         /* streamOptimized extents need a compressed grain buffer, which must
    1346          * be big enough to hold uncompressible data (which needs ~8 bytes
    1347          * more than the uncompressed data), the marker and padding. */
    1348         pExtent->cbCompGrain = RT_ALIGN_Z(  VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
    1349                                           + 8 + sizeof(VMDKMARKER), 512);
    1350         pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
    1351         if (!pExtent->pvCompGrain)
    1352         {
    1353             rc = VERR_NO_MEMORY;
    1354             goto out;
    1355         }
    1356 
    1357         /* streamOptimized extents need a decompressed grain buffer. */
    1358         pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
    1359         if (!pExtent->pvGrain)
    1360         {
    1361             rc = VERR_NO_MEMORY;
    1362             goto out;
    1363         }
    1364 
    13651420        if (uLastGrainSector)
    13661421        {
     
    13941449    int rc = VINF_SUCCESS;
    13951450    unsigned i;
    1396     uint32_t *pGD = NULL, *pRGD = NULL;
    13971451    size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
    13981452    size_t cbGDRounded = RT_ALIGN_64(pExtent->cGDEntries * sizeof(uint32_t), 512);
     
    14051459        cbGTRounded = 0;
    14061460
    1407     pGD = (uint32_t *)RTMemAllocZ(cbGD);
    1408     if (!pGD)
    1409     {
    1410         rc = VERR_NO_MEMORY;
    1411         goto out;
    1412     }
    1413     pExtent->pGD = pGD;
    1414     if (!(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
    1415     {
    1416         pRGD = (uint32_t *)RTMemAllocZ(cbGD);
    1417         if (!pRGD)
    1418         {
    1419             rc = VERR_NO_MEMORY;
    1420             goto out;
    1421         }
    1422         pExtent->pRGD = pRGD;
    1423     }
    1424     else
    1425         pExtent->pRGD = NULL;
    1426 
    14271461    if (uStartSector != VMDK_GD_AT_END)
    14281462    {
     
    14621496        pExtent->uSectorGD = uStartSector;
    14631497    }
     1498
     1499    rc = vmdkAllocGrainDirectory(pImage, pExtent);
     1500    if (RT_FAILED(rc))
     1501        goto out;
    14641502
    14651503    if (fPreAlloc)
     
    15071545    pExtent->cOverheadSectors = VMDK_BYTE2SECTOR(cbOverhead);
    15081546
    1509     /* streamOptimized extents need a compressed grain buffer, which must
    1510      * be big enough to hold uncompressible data (which needs ~8 bytes
    1511      * more than the uncompressed data), the marker and padding. */
    1512     pExtent->cbCompGrain = RT_ALIGN_Z(  VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
    1513                                       + 8 + sizeof(VMDKMARKER), 512);
    1514     pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
    1515     if (!pExtent->pvCompGrain)
    1516     {
    1517         rc = VERR_NO_MEMORY;
    1518         goto out;
    1519     }
    1520 
    1521     /* streamOptimized extents need a grain decompress buffer. */
    1522     if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
    1523     {
    1524         pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
    1525         if (!pExtent->pvGrain)
    1526         {
    1527             rc = VERR_NO_MEMORY;
    1528             goto out;
    1529         }
    1530     }
    1531 
    15321547out:
    15331548    if (RT_FAILURE(rc))
    15341549        vmdkFreeGrainDirectory(pExtent);
    15351550    return rc;
    1536 }
    1537 
    1538 static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
    1539 {
    1540     if (pExtent->pGD)
    1541     {
    1542         RTMemFree(pExtent->pGD);
    1543         pExtent->pGD = NULL;
    1544     }
    1545     if (pExtent->pRGD)
    1546     {
    1547         RTMemFree(pExtent->pRGD);
    1548         pExtent->pRGD = NULL;
    1549     }
    15501551}
    15511552
     
    45194520    }
    45204521
     4522    if (pfnProgress)
     4523        pfnProgress(pvUser, uPercentStart + uPercentSpan * 70 / 100);
     4524
     4525    rc = vmdkWriteDescriptor(pImage);
     4526    if (RT_FAILURE(rc))
     4527    {
     4528        rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK descriptor in '%s'"), pImage->pszFilename);
     4529        goto out;
     4530    }
     4531
    45214532    /* Skip over the overhead area. */
    45224533    rc = vmdkFileSetSize(pImage, pExtent->pFile,
    45234534                         VMDK_SECTOR2BYTE(pExtent->cOverheadSectors));
    4524 
    4525     if (pfnProgress)
    4526         pfnProgress(pvUser, uPercentStart + uPercentSpan * 70 / 100);
    4527 
    4528     rc = vmdkWriteDescriptor(pImage);
    4529     if (RT_FAILURE(rc))
    4530     {
    4531         rc = vmdkError(pImage, rc, RT_SRC_POS, N_("VMDK: cannot write VMDK descriptor in '%s'"), pImage->pszFilename);
    4532         goto out;
    4533     }
    45344535
    45354536out:
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