Ignore:
Timestamp:
Sep 30, 2007, 9:36:23 AM (18 years ago)
Author:
bird
Message:

Optimized kDebIDB a bit for Windows; use nt_fullpath and map the IDB file into memory.

File:
1 edited

Legend:

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

    r1163 r1165  
    3838# include <stdint.h>
    3939#else
     40# define USE_WIN_MMAP
     41# include <io.h>
     42# include <Windows.h>
    4043 typedef unsigned char  uint8_t;
    4144 typedef unsigned short uint16_t;
     
    137140
    138141
     142#ifdef USE_WIN_MMAP
     143/** Handle to the current file mapping object. */
     144static HANDLE g_hMapObj = NULL;
     145#endif
     146
    139147
    140148/**
    141149 * Reads the file specified by the pInput file stream into memory.
    142150 * The size of the file is returned in *pcbFile if specified.
    143  * The returned pointer should be freed by free().
     151 * The returned pointer should be freed by FreeFileMemory().
    144152 */
    145153void *ReadFileIntoMemory(FILE *pInput, size_t *pcbFile)
     
    152160     * Figure out file size.
    153161     */
     162#if defined(_MSC_VER)
     163    cbFile = _filelength(fileno(pInput));
     164    if (cbFile < 0)
     165#else
    154166    if (    fseek(pInput, 0, SEEK_END) < 0
    155167        ||  (cbFile = ftell(pInput)) < 0
    156168        ||  fseek(pInput, 0, SEEK_SET))
     169#endif
    157170    {
    158171        fprintf(stderr, "%s: error: Failed to determin file size.\n", argv0);
     
    163176
    164177    /*
     178     * Try mmap first.
     179     */
     180#ifdef USE_WIN_MMAP
     181    {
     182        HANDLE hMapObj = CreateFileMapping((HANDLE)_get_osfhandle(fileno(pInput)),
     183                                           NULL, PAGE_READONLY, 0, cbFile, NULL);
     184        if (hMapObj != NULL)
     185        {
     186            pvFile = MapViewOfFile(hMapObj, FILE_MAP_READ, 0, 0, cbFile);
     187            if (pvFile)
     188            {
     189                g_hMapObj = hMapObj;
     190                return pvFile;
     191            }
     192            fprintf(stderr, "%s: warning: MapViewOfFile failed, %d.\n", argv0, GetLastError());
     193            CloseHandle(hMapObj);
     194        }
     195        else
     196            fprintf(stderr, "%s: warning: CreateFileMapping failed, %d.\n", argv0, GetLastError());
     197    }
     198
     199#endif
     200
     201    /*
    165202     * Allocate memory and read the file.
    166203     */
     
    168205    if (pvFile)
    169206    {
     207#if 1
     208        long cbRead = (long)read(fileno(pInput), pvFile, cbFile);
     209        if (cbRead == cbFile)
     210#else
    170211        if (fread(pvFile, cbFile, 1, pInput))
     212#endif
    171213        {
    172214            ((uint8_t *)pvFile)[cbFile] = '\0';
     
    181223}
    182224
     225
     226static void FreeFileMemory(void *pvFile)
     227{
     228#if defined(USE_WIN_MMAP)
     229    if (g_hMapObj)
     230    {
     231        UnmapViewOfFile(pvFile);
     232        CloseHandle(g_hMapObj);
     233        return;
     234    }
     235#endif
     236    free(pvFile);
     237}
    183238
    184239
     
    726781    }
    727782
    728     free(pbFile);
     783    FreeFileMemory(pbFile);
    729784    return rc;
    730785}
     
    925980    }
    926981
     982    depCleanup();
    927983    return i;
    928984}
Note: See TracChangeset for help on using the changeset viewer.