Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 9/08/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

FOR__SET_FTN_ALLOC

Run-Time Function: Lets you specify your own routine to dynamically allocate common blocks. This function is especially useful when you are sharing libraries.

result = FOR__SET_FTN_ALLOC(alloc_routine)

alloc_routine

(Input) Character. Is the name of a user-defined allocation routine. The routine takes the same arguments as the routine prototype _FTN_ALLOC, which is defined in module IFCORE. For more information on _FTN_ALLOC and its arguments, see Allocating Common Blocks.

Results

The result has the same type as the type of the argument. The return value is a pointer to the previous allocation routine you specified for allocation of COMMONs, or to a null pointer if you did not specify a previous allocation routine.

The caller of FOR__SET_FTN_ALLOC must include a USE IFCOMMONALLOC statement. The allocation routine should include ISO_C_BINDING so it can correctly declare the arguments.

This function takes precedence over _FTN_ALLOC.

Example

The following shows an example of a user-defined routine that can be used with FOR__SET_FTN_ALLOC.

Note that you must compile the program using option [Q]dyncom to name the commons you want to be dynamically allocated.


! User's allocation routine
!
subroutine my_Fortran_alloc_routine (mem, size, name)
    use, intrinsic                :: ISO_C_BINDING
    implicit none

    type(C_PTR),    intent(OUT)         :: mem
    integer(C_INT), intent(INOUT)       :: size
    character, dimension(*), intent(IN) :: name

    ! Users would put their allocation code here.  This example text
    ! does not contain code to allocate memory.

end subroutine my_Fortran_alloc_routine

! This routine uses module IFCOMMONALLOC to swap allocation
! routines for dynamic COMMONs.
!
subroutine swap_alloc_routines( for_old )
    use ifcommonalloc
    use, intrinsic :: ISO_C_BINDING
    implicit none

    logical for_old

    ! The routine to use, defined above.
    !
    procedure(alloc_rtn) :: my_Fortran_alloc_routine

    ! Where to save the old one.
    !
    type(C_FUNPTR) :: saved_alloc_routine

    ! Do the swap
    !
    print *, "my_Fortran_alloc_routine"
    if (for_old) then
        saved_alloc_routine = set_ftn_alloc( C_FUNLOC(my_Fortran_alloc_routine) )
    else
        saved_alloc_routine = set_ftn_alloc( saved_alloc_routine )
    end if

end subroutine swap_alloc_routines


! Routines with dynamic commons would go here


! The main program doesn't need to know about module IFCOMMONALLOC.
!
program main
    implicit none 

    ! Dynamic commons in routines first called in this region will use the
    !   default allocation method.

    swap_alloc_routines( .true. )

    ! Dynamic commons in routines first called in this region will use
    !   my_Fortran_alloc_routine.

    swap_alloc_routines( .false. )

    ! Dynamic commons in routines first called in this region will use the
    !   default allocation method.

end program main