Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 248

Show
Ignore:
Timestamp:
06/16/06 14:39:49 (6 years ago)
Author:
sean
Message:

Now allocating all Win32 arg buffer data on the stack using alloca. The code is a tad messier, but it's worth avoiding using the GC or DMA in this portion of the runtime code. Unless it turns out later that huge arg strings are eating up too much stack space, that is.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/compiler/digitalmars/dmain2.d

    r246 r248  
    6767} 
    6868 
    69 extern (C) bool no_catch_exceptions = false; 
    70  
    7169/*********************************** 
    7270 * The D main() function supplied by the user's program 
     
    10098    } 
    10199 
    102     void run() 
     100    version (Win32) 
     101    { 
     102        wchar_t*  wcbuf = GetCommandLineW(); 
     103        size_t    wclen = wcslen(wcbuf); 
     104        int       wargc = 0; 
     105        wchar_t** wargs = CommandLineToArgvW(GetCommandLineW(), &wargc); 
     106        assert(wargc == argc); 
     107 
     108        char*     cargp = null; 
     109        size_t    cargl = WideCharToMultiByte(65001, 0, wcbuf, wclen, null, 0, null, 0); 
     110 
     111        cargp = cast(char*) alloca(cargl); 
     112        args  = (cast(char[]*) alloca(wargc * (char[]).sizeof))[0 .. wargc]; 
     113 
     114        for (size_t i = 0, p = 0; i < wargc; i++) 
     115        { 
     116            int wlen = wcslen( wargs[i] ); 
     117            int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, null, 0, null, 0); 
     118            args[i]  = cargp[p .. p+clen]; 
     119            p += clen; assert(p <= cargl); 
     120            WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, &args[i][0], clen, null, 0); 
     121        } 
     122        LocalFree(wargs); 
     123        wargs = null; 
     124        wargc = 0; 
     125    } 
     126    else 
     127    { 
     128        char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof); 
     129        scope(exit) free(am); 
     130 
     131        for (int i = 0; i < argc; i++) 
     132        { 
     133            int len = strlen(argv[i]); 
     134            am[i] = argv[i][0 .. len]; 
     135        } 
     136        args = am[0 .. argc]; 
     137    } 
     138 
     139    try 
    103140    { 
    104141        _moduleCtor(); 
    105142        _moduleUnitTests(); 
    106  
    107         version (Win32) 
    108         { 
    109             int       wargc = 0; 
    110             wchar_t** wargs = CommandLineToArgvW(GetCommandLineW(), &wargc); 
    111             assert(wargc == argc); 
    112  
    113             args = new char[][wargc]; 
    114             for (int i = 0; i < wargc; i++) 
    115             { 
    116                 int wlen = wcslen( wargs[i] ); 
    117                 int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, null, 0, null, 0); 
    118                 args[i] = new char[clen]; 
    119                 WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, &args[i][0], clen, null, 0); 
    120             } 
    121             LocalFree(wargs); 
    122             wargs = null; 
    123             wargc = 0; 
    124         } 
    125         else 
    126         { 
    127             char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof); 
    128             scope(exit) free(am); 
    129  
    130             for (int i = 0; i < argc; i++) 
    131             { 
    132                 int len = strlen(argv[i]); 
    133                 am[i] = argv[i][0 .. len]; 
    134             } 
    135             args = am[0 .. argc]; 
    136         } 
    137  
    138143        result = main(args); 
    139144        _moduleDtor(); 
    140145        gc_term(); 
    141146    } 
    142  
    143     if (no_catch_exceptions) 
     147    catch (Exception e) 
    144148    { 
    145         run(); 
     149        while (e) 
     150        { 
     151            if (e.file) 
     152                fprintf(stderr, "%.*s(%u): %.*s\n", e.file, e.line, e.msg); 
     153            else 
     154                fprintf(stderr, "%.*s\n", e.toString()); 
     155            e = e.next; 
     156        } 
     157        exit(EXIT_FAILURE); 
    146158    } 
    147     else 
     159    catch (Object o) 
    148160    { 
    149         try 
    150         { 
    151             run(); 
    152         } 
    153         catch (Exception e) 
    154         { 
    155             while (e) 
    156             { 
    157                 if (e.file) 
    158                     fprintf(stderr, "%.*s(%u): %.*s\n", e.file, e.line, e.msg); 
    159                 else 
    160                     fprintf(stderr, "%.*s\n", e.toString()); 
    161                 e = e.next; 
    162             } 
    163             exit(EXIT_FAILURE); 
    164         } 
    165         catch (Object o) 
    166         { 
    167             fprintf(stderr, "%.*s\n", o.toString()); 
    168             exit(EXIT_FAILURE); 
    169         } 
     161        fprintf(stderr, "%.*s\n", o.toString()); 
     162        exit(EXIT_FAILURE); 
    170163    } 
    171164