MOM6
MOM_safe_alloc.F90
Go to the documentation of this file.
2 
3 !***********************************************************************
4 !* GNU General Public License *
5 !* This file is a part of MOM. *
6 !* *
7 !* MOM is free software; you can redistribute it and/or modify it and *
8 !* are expected to follow the terms of the GNU General Public License *
9 !* as published by the Free Software Foundation; either version 2 of *
10 !* the License, or (at your option) any later version. *
11 !* *
12 !* MOM is distributed in the hope that it will be useful, but WITHOUT *
13 !* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
14 !* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public *
15 !* License for more details. *
16 !* *
17 !* For the full text of the GNU General Public License, *
18 !* write to: Free Software Foundation, Inc., *
19 !* 675 Mass Ave, Cambridge, MA 02139, USA. *
20 !* or see: http://www.gnu.org/licenses/gpl.html *
21 !***********************************************************************
22 
23 !********+*********+*********+*********+*********+*********+*********+**
24 !* *
25 !* The subroutines here provide a convenient way to safely allocate *
26 !* memory without accidentally reallocating a pointer and causing a *
27 !* memory leak. *
28 !* *
29 !********+*********+*********+*********+*********+*********+*********+**
30 
31 implicit none ; private
32 
34 
35 interface safe_alloc_ptr
38 end interface safe_alloc_ptr
39 
42 end interface safe_alloc_alloc
43 
44 ! This combined interface might work with a later version of Fortran, but
45 ! it fails with the gnu F90 compiler.
46 !
47 ! interface safe_alloc
48 ! module procedure safe_alloc_ptr_3d_2arg, safe_alloc_ptr_2d_2arg
49 ! module procedure safe_alloc_ptr_3d, safe_alloc_ptr_2d, safe_alloc_ptr_1d
50 ! module procedure safe_alloc_allocatable_3d, safe_alloc_allocatable_2d
51 ! end interface safe_alloc
52 
53 contains
54 
55 subroutine safe_alloc_ptr_1d(ptr, i1, i2)
56  real, pointer :: ptr(:)
57  integer, intent(in) :: i1
58  integer, optional, intent(in) :: i2
59  if (.not.ASSOCIATED(ptr)) then
60  if (present(i2)) then
61  allocate(ptr(i1:i2))
62  else
63  allocate(ptr(i1))
64  endif
65  ptr(:) = 0.0
66  endif
67 end subroutine safe_alloc_ptr_1d
68 
69 subroutine safe_alloc_ptr_2d_2arg(ptr, ni, nj)
70  real, pointer :: ptr(:,:)
71  integer, intent(in) :: ni, nj
72  if (.not.ASSOCIATED(ptr)) then
73  allocate(ptr(ni,nj))
74  ptr(:,:) = 0.0
75  endif
76 end subroutine safe_alloc_ptr_2d_2arg
77 
78 subroutine safe_alloc_ptr_3d_2arg(ptr, ni, nj, nk)
79  real, pointer :: ptr(:,:,:)
80  integer, intent(in) :: ni, nj, nk
81  if (.not.ASSOCIATED(ptr)) then
82  allocate(ptr(ni,nj,nk))
83  ptr(:,:,:) = 0.0
84  endif
85 end subroutine safe_alloc_ptr_3d_2arg
86 
87 subroutine safe_alloc_ptr_2d(ptr, is, ie, js, je)
88  real, pointer :: ptr(:,:)
89  integer, intent(in) :: is, ie, js, je
90  if (.not.ASSOCIATED(ptr)) then
91  allocate(ptr(is:ie,js:je))
92  ptr(:,:) = 0.0
93  endif
94 end subroutine safe_alloc_ptr_2d
95 
96 subroutine safe_alloc_ptr_3d(ptr, is, ie, js, je, nk)
97  real, pointer :: ptr(:,:,:)
98  integer, intent(in) :: is, ie, js, je, nk
99  if (.not.ASSOCIATED(ptr)) then
100  allocate(ptr(is:ie,js:je,nk))
101  ptr(:,:,:) = 0.0
102  endif
103 end subroutine safe_alloc_ptr_3d
104 
105 subroutine safe_alloc_allocatable_2d(ptr, is, ie, js, je)
106  real, allocatable :: ptr(:,:)
107  integer, intent(in) :: is, ie, js, je
108  if (.not.ALLOCATED(ptr)) then
109  allocate(ptr(is:ie,js:je))
110  ptr(:,:) = 0.0
111  endif
112 end subroutine safe_alloc_allocatable_2d
113 
114 subroutine safe_alloc_allocatable_3d(ptr, is, ie, js, je, nk)
115  real, allocatable :: ptr(:,:,:)
116  integer, intent(in) :: is, ie, js, je, nk
117  if (.not.ALLOCATED(ptr)) then
118  allocate(ptr(is:ie,js:je,nk))
119  ptr(:,:,:) = 0.0
120  endif
121 end subroutine safe_alloc_allocatable_3d
122 
123 end module mom_safe_alloc
subroutine safe_alloc_ptr_2d(ptr, is, ie, js, je)
subroutine safe_alloc_allocatable_3d(ptr, is, ie, js, je, nk)
subroutine safe_alloc_ptr_3d(ptr, is, ie, js, je, nk)
subroutine safe_alloc_ptr_1d(ptr, i1, i2)
subroutine safe_alloc_allocatable_2d(ptr, is, ie, js, je)
subroutine safe_alloc_ptr_3d_2arg(ptr, ni, nj, nk)
subroutine safe_alloc_ptr_2d_2arg(ptr, ni, nj)