Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

preprocessor definition

franzw82
Beginner
807 Views
I have a question concerning preprocessor definitions. I wrote the following code.

[fxfortran]module memory  
use iso_c_binding
#define my_allocate(fpntr) call c_f_pointer\
(my_allocate_call (sizeof(fpntr)), fpntr)
implicit none

type :: my_alloc_type
integer*4 :: a
real*8 :: b
real*8, dimension(:), pointer :: c
end type my_alloc_type

type :: my_alloc_type2
logical :: a
real*8 :: b
real*8 :: c(1:20)
end type my_alloc_type2

contains

integer (c_intptr_t) function my_allocate_call (size)
integer (c_size_t) :: size
my_allocate_call = malloc (size)
end function my_allocate_call

subroutine test_my_allocation
type (my_alloc_type), pointer :: t
my_allocate (t)
type (my_alloc_type2), pointer :: t2
my_allocate (t2)
end subroutine test_my_allocation

end module memory [/fxfortran]

Using the preprocessor definition "my_allocate" I can allocate any derived type. I could of course simply use "allocate". For other reasons I want to write my own allocation routines. Now, my problem is that I cannot call the subroutine (i.e. preprocessor definition) "my_allocate" from outside the module. How can I implement a "regular" subroutine which is as flexible as this preprocessor definition? Any help would be greatly appreciated.
0 Kudos
3 Replies
Steven_L_Intel1
Employee
807 Views
Why not just write a small wrapper routine that does what you want? I don't see preprocessor macros as the right tool for this.
0 Kudos
franzw82
Beginner
807 Views
Thank you for the reply. How would you implement this "small wrapper routine"? The only solution that I could think of was something like this.

[fortran]interface my_allocate
   module procedure my_allocate_type1
   module procedure my_allocate_type2
end interface

...

subroutine my_allocate_type1 (fpntr)
   type (my_alloc_type), pointer :: fpntr
   integer (c_intptr_t) :: cpntr
   cpntr = my_allocate_call (sizeof(fpntr))
   call c_f_pointer (cpntr, fpntr)
end subroutine my_allocate_type1 subroutine my_allocate_type2 (fpntr) type (my_alloc_type2), pointer :: fpntr integer (c_intptr_t) :: cpntr cpntr = my_allocate_call (sizeof(fpntr)) call c_f_pointer (cpntr, fpntr) end subroutine my_allocate_type2 [/fortran]
0 Kudos
Steven_L_Intel1
Employee
807 Views
Yes, something like that.
0 Kudos
Reply