Thunking

Address conversion between 16:16 and 0:32 addressing models is achieved by the use of a thunk. A thunk exists for each programming interface in the system. Thunking implies:

  • Converting the addressing scheme used (0:32 - 16)

    OS/2 Version 2.0 uses a flat linear (0:32) addressing scheme. 16:16 program modules expect a selector:offset (16:16) addressing scheme. The thunk converts memory references between these two schemes. See Figure "Thunk Concept".

  • Different parameter sizes (DWORD versus WORD)

    The 0:32 addressing scheme uses 32-bit (LONG or DWORD) values as the basic data type. The 16:16 scheme uses a 16-bit (SHORT or WORD) value as the basic data type. The thunk converts between WORD and DWORD length data.

  • Structure alignment

    The 16:16 addressing scheme normally causes data structures to be WORD aligned, whereas the 0:32 addressing scheme defaults to DWORD alignment; blank space is included within the structure so that each element is aligned on a DWORD boundary. The thunk must realign data structures where necessary.

  • Stack conversion

    The 0:32 stack is DWORD-based. The 16:16 stack is WORD-based. Stack-based addressing between the two schemes is therefore different. The thunk must make a new copy of the parameters on the stack, realigning when needed.

  • Restrictions on the 16:16 addressing scheme

    The 16:16 code can only address up to 64KB in any segment. The only limit on the 0:32 code is the maximum size of the linear address space (4GB). This disparity creates a problem when a data item in the 0:32 module or resource is larger than 64KB or is allocated across a 64KB boundary, and must be passed to a 16:16 routine. Where possible, the thunk must make the data item addressable by the 16:16 routine.

  • Different call models

    The 0:32 addressing scheme uses near calls for all operating system functions. The 16:16 scheme uses far calls for operating system functions. If a procedure using one scheme tries to call a procedure of the other scheme, different return values may be placed on the stack. The thunk is responsible for producing the correct calling sequence.

    The above considerations apply to all executable programs, libraries (both statically and dynamically linked) and Presentation Manager messages.

    OS/2 Version 2.0 provides a number of thunks to handle function calls from 16-bit applications to 32-bit service layers, and vice versa. Almost all of the thunks in OS/2 Version 2.0 are of the 0:32 to 16:16 conversion kind, except for those that handle semaphores and DosSubxxxx() service calls. Thunks are packaged with the module that contains the supporting code for the 16-bit and/or the 32-bit entry point. See Figure "Thunks - 16-Bit versus 32-Bit" for an overview of the two different types of thunks. This implementation of mixed 16:16 and 0:32 applications also raises a number of considerations:

  • Each resource will effectively occupy at least 4KB (one page), since this is the minimum allocated in the 32-bit environment.

  • Stack size is a maximum of 64KB, since this is the maximum addressable by the 16-bit portion of the application.

  • In Presentation Manager applications, caution must be exercised when passing user-defined messages between window procedures in modules that use different addressing models; such messages will require application-defined thunks.

  • Presentation Manager hooks should only be used with system-defined message contents.

    When developing 32-bit applications using 16-bit modules, the programmer is responsible for supplying appropriate address conversion via thunks, for pointers passed as parameters and for application-defined messages under Presentation Manager. These considerations and restrictions are discussed further in OS/2 Version 2.0 - Volume 4: Application Development.


    [Back: Address Conversion and Translation]
    [Next: Shared Memory]