VirtualBox

Changeset 2984 in kBuild


Ignore:
Timestamp:
Nov 1, 2016 6:24:11 PM (8 years ago)
Author:
bird
Message:

md5sum: buffer fun.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/md5sum.c

    r2414 r2984  
    300300    int cb;
    301301    int rc = 0;
    302     char abBuf[32*1024];
    303302    struct MD5Context Ctx;
    304303    unsigned uPercent = 0;
    305     double cbFile = 0.0;
    306304    double off = 0.0;
    307 
    308     if (fProgress)
    309     {
    310         cbFile = size_file(pvFile);
    311         if (cbFile < 1024*1024)
    312             fProgress = 0;
    313     }
     305    double cbFile = size_file(pvFile);
     306
     307    /* Get a decent sized buffer assuming we'll be spending more time reading
     308       from the storage than doing MD5 sums.  (2MB was choosen based on recent
     309       SATA storage benchmarks which used that block size for sequential
     310       tests.) We align the buffer address on a 16K boundrary to avoid most
     311       transfer alignment issues. */
     312    char        *pabBufAligned;
     313    size_t const cbBufAlign = 16*1024 - 1;
     314    size_t const cbBufMax = 2048*1024;
     315    size_t       cbBuf    = cbFile >= cbBufMax ? cbBufMax : ((size_t)cbFile + cbBufAlign) & ~(size_t)cbBufAlign;
     316    char        *pabBuf   = (char *)malloc(cbBuf + cbBufAlign);
     317    if (pabBuf)
     318        pabBufAligned = (char *)(((uintptr_t)pabBuf + cbBufAlign) & ~(uintptr_t)cbBufAlign );
     319    else
     320    {
     321        do
     322        {
     323            cbBuf /= 2;
     324            pabBuf = (char *)malloc(cbBuf);
     325        } while (!pabBuf && cbBuf > 4096);
     326        if (!pabBuf)
     327            return ENOMEM;
     328        pabBufAligned = pabBuf;
     329    }
     330
     331    if (cbFile < cbBuf * 4)
     332        fProgress = 0;
    314333
    315334    MD5Init(&Ctx);
     
    317336    {
    318337        /* process a chunk. */
    319         cb = read_file(pvFile, abBuf, sizeof(abBuf));
     338        cb = read_file(pvFile, pabBufAligned, cbBuf);
    320339        if (cb > 0)
    321             MD5Update(&Ctx, (unsigned char *)&abBuf[0], cb);
     340            MD5Update(&Ctx, (unsigned char *)pabBufAligned, cb);
    322341        else if (!cb)
    323342            break;
     
    349368        printf("\b\b\b\b    \b\b\b\b");
    350369
     370    free(pabBuf);
    351371    return rc;
    352372}
     
    572592
    573593    /*
    574      * Calcuate and print the MD5 sum for one file.
     594     * Calculate and print the MD5 sum for one file.
    575595     */
    576596    pvFile = open_file(pszFilename, fText);
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