Commits

Harald Klimach committed 7ef1304

Encapsulate the character buffer, used to store Lua Bytecode in an extra
datatype and carry around the original C pointer.
Mainly needed to deallocate the buffer later on again, using C free instead of
deallocate in Fortran.
(Cray compiler complained when attempting to deallocate the C-allocated array)

Comments (0)

Files changed (3)

LuaFortran/flu_binding.f90

     logical :: opened_libs = .false.
   end type flu_State
 
+  type :: cbuf_type
+    type(c_ptr) :: ptr = c_null_ptr
+    character, pointer :: buffer(:) => NULL()
+  end type cbuf_type
+
   public :: flu_State
+  public :: cbuf_type
 
   public :: flu_close, flu_isopen
   public :: flu_createTable
   public :: flu_register
 
   public :: flu_dump
+  public :: flu_free_cbuf
 
   public :: fluL_loadfile, fluL_newstate, fluL_openlibs, fluL_loadstring
   public :: fluL_loadbuffer
     end function lua_Function
   end interface
 
+  interface
+    subroutine c_free(ptr) bind(c, name="free")
+      use, intrinsic :: iso_c_binding, only: c_ptr
+      type(c_ptr), value :: ptr
+    end subroutine c_free
+  end interface
+
+
 contains
 
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   !> Dump to a buffer and return the pointer to the resulting string.
   subroutine flu_dump_toBuf(L, buf, length, iError)
     type(flu_State) :: L
-    character, pointer :: buf(:)
+    type(cbuf_type), intent(out) :: buf
     integer :: length
     integer :: iError
 
     integer(kind=c_int) :: length_c
     integer(kind=c_int) :: iErr
 
-    nullify(buf)
     string_c = dump_lua_toBuf(L%state, length_c, iErr)
     iError = int(iErr)
     if (iError == 0) then
       length = int(length_c)
-      call c_f_pointer(string_c, buf, [length])
+      buf%ptr = string_c
+      call c_f_pointer(string_c, buf%buffer, [length])
     else
       length = 0
     end if
   end subroutine flu_dump_toBuf
 
+
+  !> Free an allocated cbuf.
+  !!
+  !! This is a helping routine to deallocate memory that was allocated for
+  !! the cbuf by C. (Cray compiler complained about its deallocation in Fortran)
+  subroutine flu_free_cbuf(buf)
+    type(cbuf_type) :: buf
+
+    call c_free(buf%ptr)
+    nullify(buf%buffer)
+  end subroutine flu_free_cbuf
+
 end module flu_binding

source/aotus_module.f90

     !!
     !! It might be handy to reuse the loaded script later on, this argument
     !! allows you to obtain the script in compiled form, before it is executed.
-    !! The pointer will be allocated and filled with the Lua data.
-    character, pointer, optional :: buffer(:)
+    !! The buffer will be allocated and filled with the Lua data.
+    !! It contains the actual string in buffer%buffer which is a character
+    !! pointer, and the original c_ptr to this 
+    type(cbuf_type), intent(out), optional :: buffer
 
     integer :: err
     integer :: length
     character(len=*), intent(in) :: filename
 
     !> Buffer to store the script in the given file in
-    character, pointer :: buffer(:)
+    type(cbuf_type), intent(out) :: buffer
 
     !> Error code returned by Lua during loading or executing the file.
     !!

utests/aot_buffer_test.f90

 program aot_buffer_test
-  use flu_binding, only: flu_State, fluL_newstate, fluL_loadfile, flu_dump
+  use flu_binding, only: flu_State, cbuf_type, fluL_newstate, fluL_loadfile, &
+    &                    flu_dump
   use aotus_module, only: aot_get_val, aot_err_handler, &
     &                     open_config_buffer, close_config, &
     &                     aoterr_Fatal, aoterr_NonExistent, aoterr_WrongType
   integer :: glob_int
   integer :: iError
   logical :: passed
-  character, pointer :: scriptBuffer(:)
+  type(cbuf_type) :: scriptBuffer
   type(flu_State) :: L
 
   passed = .true.
   call close_config(L)
 
   write(*,*) 'Reading the script back from a buffer'
-  call open_config_buffer(L, scriptbuffer)
+  call open_config_buffer(L, scriptbuffer%buffer)
 
   ! Testing for global INTEGER
   write(*,*) ' * reading a global integer'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.