Changeset 21538


Ignore:
Timestamp:
Dec 29, 2010, 4:13:17 PM (14 years ago)
Author:
dmik
Message:

odincrt: Small code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/odincrt/initterm.cpp

    r8759 r21538  
    1010 */
    1111
    12 /*-------------------------------------------------------------*/
    13 /* INITERM.C -- Source for a custom dynamic link library       */
    14 /*              initialization and termination (_DLL_InitTerm) */
    15 /*              function.                                      */
    16 /*                                                             */
    17 /* When called to perform initialization, this sample function */
    18 /* gets storage for an array of integers, and initializes its  */
    19 /* elements with random integers.  At termination time, it     */
    20 /* frees the array.  Substitute your own special processing.   */
    21 /*-------------------------------------------------------------*/
    22 
    23 
    24 /* Include files */
    2512#define  INCL_DOSMODULEMGR
    2613#define  INCL_DOSPROCESS
    2714#define  INCL_DOSERRORS
    28 #include <os2wrap.h>    //Odin32 OS/2 api wrappers
     15#include <os2wrap.h> // Odin32 OS/2 api wrappers
    2916#include <stdlib.h>
    3017#include <stdio.h>
    3118#include <string.h>
    3219#include <odin.h>
    33 #include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/
     20#include <misc.h>
    3421#include <exitlist.h>
    3522#include <initdll.h>
    3623
    3724#ifdef __IBMCPP__
    38 extern "C" {
    3925
    40 /*-------------------------------------------------------------------*/
    41 /* A clean up routine registered with DosExitList must be used if    */
    42 /* runtime calls are required and the runtime is dynamically linked. */
    43 /* This will guarantee that this clean up routine is run before the  */
    44 /* library DLL is terminated.                                        */
    45 /*-------------------------------------------------------------------*/
    4626static void APIENTRY cleanup(ULONG reason);
     27
     28/*
     29 * _DLL_InitTerm is the function that gets called by the operating system
     30 * loader when it loads and frees this DLL for each process that accesses
     31 * this DLL.  However, it only gets called the first time the DLL is loaded
     32 * and the last time it is freed for a particular process.  The system
     33 * linkage convention MUST be used because the operating system loader is
     34 * calling this function.
     35 *
     36 * If ulFlag is zero then the DLL is being loaded so initialization should
     37 * be performed.  If ulFlag is 1 then the DLL is being freed so
     38 * termination should be performed.
     39 *
     40 * A non-zero value must be returned to indicate success.
     41 */
     42unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag)
     43{
     44    APIRET rc;
     45
     46    switch (ulFlag)
     47    {
     48    case 0:
     49    {
     50#ifdef WITH_KLIB
     51        /*
     52         * We need to reserve memory for the executable image
     53         * before initiating any heaps. Lets do reserve 32MBs
     54         */
     55        PVOID pvReserved = NULL;
     56        DosAllocMem(&pvReserved, 32*1024*1024, PAG_READ);
     57#endif
     58        /* initialize C and C++ runtime */
     59        if (_CRT_init() == -1)
     60            return 0UL;
     61        ctordtorInit();
     62
     63        /*
     64         * Register an exit list routine to clean up runtime at termination.
     65         * We can't simply do it at DLL unload time because this is forbidden
     66         * for VAC runtime Odin runtime is based on (see CPPLIB.INF from VAC
     67         * for details).
     68         */
     69        rc = DosExitList(EXITLIST_ODINCRT|EXLST_ADD, cleanup);
     70        if(rc)
     71            return 0UL;
     72#if 1
     73        /*
     74         * Experimental console hack. Sets apptype to PM anyhow.
     75         * First Dll to be initiated should now allways be OdinCrt!
     76         * So include odincrt first!
     77         */
     78        PPIB pPIB;
     79        PTIB pTIB;
     80        rc = DosGetInfoBlocks(&pTIB, &pPIB);
     81        if (rc != NO_ERROR)
     82            return 0UL;
     83        pPIB->pib_ultype = 3;
     84#endif
     85
     86#ifdef WITH_KLIB
     87        /* cleanup - hacking is done */
     88        DosFreeMem(pvReserved);
     89#endif
     90        break;
     91    }
     92    case 1:
     93        break;
     94    default:
     95        return 0UL;
     96    }
     97
     98    /* success */
     99    return 1UL;
    47100}
    48101
    49 /****************************************************************************/
    50 /* _DLL_InitTerm is the function that gets called by the operating system   */
    51 /* loader when it loads and frees this DLL for each process that accesses   */
    52 /* this DLL.  However, it only gets called the first time the DLL is loaded */
    53 /* and the last time it is freed for a particular process.  The system      */
    54 /* linkage convention MUST be used because the operating system loader is   */
    55 /* calling this function.                                                   */
    56 /****************************************************************************/
    57 unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long
    58                                    ulFlag)
     102static void APIENTRY cleanup(ULONG /*ulReason*/)
    59103{
    60    APIRET rc;
    61 
    62    /*-------------------------------------------------------------------------*/
    63    /* If ulFlag is zero then the DLL is being loaded so initialization should */
    64    /* be performed.  If ulFlag is 1 then the DLL is being freed so            */
    65    /* termination should be performed.                                        */
    66    /*-------------------------------------------------------------------------*/
    67 
    68    switch (ulFlag) {
    69       case 0 :
    70          {
    71          #ifdef WITH_KLIB
    72          /*
    73           * We need to reserve memory for the executable image
    74           * before initiating any heaps. Lets do reserve 32MBs
    75           */
    76          PVOID pvReserved = NULL;
    77          DosAllocMem(&pvReserved, 32*1024*1024, PAG_READ);
    78          #endif
    79          /*******************************************************************/
    80          /* The C run-time environment initialization function must be      */
    81          /* called before any calls to C run-time functions that are not    */
    82          /* inlined.                                                        */
    83          /*******************************************************************/
    84 
    85          if (_CRT_init() == -1)
    86             return 0UL;
    87          ctordtorInit();
    88 
    89          /*******************************************************************/
    90          /* A DosExitList routine must be used to clean up if runtime calls */
    91          /* are required and the runtime is dynamically linked.             */
    92          /*******************************************************************/
    93 
    94          rc = DosExitList(EXITLIST_ODINCRT|EXLST_ADD, cleanup);
    95          if(rc)
    96                 return 0UL;
    97          #if 1 /*
    98                 * Experimental console hack. Sets apptype to PM anyhow.
    99                 * First Dll to be initiated should now allways be OdinCrt!
    100                 * So include odincrt first!
    101                 */
    102             PPIB pPIB;
    103             PTIB pTIB;
    104             rc = DosGetInfoBlocks(&pTIB, &pPIB);
    105             if (rc != NO_ERROR)
    106                 return 0UL;
    107             pPIB->pib_ultype = 3;
    108          #endif
    109 
    110          #ifdef WITH_KLIB
    111          /* cleanup - hacking is done */
    112          DosFreeMem(pvReserved);
    113          #endif
    114          break;
    115          }
    116       case 1 :
    117          break;
    118       default  :
    119          return 0UL;
    120    }
    121 
    122    /***********************************************************/
    123    /* A non-zero value must be returned to indicate success.  */
    124    /***********************************************************/
    125    return 1UL;
     104    /* cleanup C++ and C runtime */
     105    ctordtorTerm();
     106    _CRT_term();
     107    DosExitList(EXLST_EXIT, cleanup);
     108    return ;
    126109}
    127110
    128111
    129 static void APIENTRY cleanup(ULONG ulReason)
    130 {
    131    ctordtorTerm();
    132    _CRT_term();
    133    DosExitList(EXLST_EXIT, cleanup);
    134    return ;
    135 }
    136 
    137112#elif defined(__WATCOM_CPLUSPLUS__)
    138 
    139 /*
    140  * Watcom dll init and term routines.
    141  */
    142113
    143114int __dll_initialize(unsigned long hModule, unsigned long ulFlag)
    144115{
     116#if 1
     117    /*
     118     * Experimental console hack. Sets apptype to PM anyhow.
     119     * First Dll to be initiated should now allways be OdinCrt!
     120     * So include odincrt first!
     121     */
    145122    APIRET rc;
    146     #if 1 /*
    147           * Experimental console hack. Sets apptype to PM anyhow.
    148           * First Dll to be initiated should now allways be OdinCrt!
    149           * So include odincrt first!
    150           */
    151123    PPIB pPIB;
    152124    PTIB pTIB;
     
    155127        return 0UL;
    156128    pPIB->pib_ultype = 3;
    157     #endif
     129#endif
    158130    return 1;
    159131}
Note: See TracChangeset for help on using the changeset viewer.