@@ -0,0 +1,258 @@ |
| 1 | +module ferp_mmap |
| 2 | + !> Memory-mapped file I/O for FERP |
| 3 | + !> Uses POSIX mmap for efficient file reading |
| 4 | + use ferp_kinds |
| 5 | + use, intrinsic :: iso_c_binding |
| 6 | + implicit none |
| 7 | + private |
| 8 | + |
| 9 | + public :: mmap_file_t |
| 10 | + public :: mmap_open, mmap_close, mmap_get_line |
| 11 | + |
| 12 | + ! POSIX constants |
| 13 | + integer(c_int), parameter :: PROT_READ = 1 |
| 14 | + integer(c_int), parameter :: MAP_PRIVATE = 2 |
| 15 | + integer(c_int), parameter :: MAP_FAILED = -1 |
| 16 | + |
| 17 | + ! C interfaces |
| 18 | + interface |
| 19 | + function c_open(pathname, flags) bind(C, name="open") |
| 20 | + import :: c_char, c_int |
| 21 | + character(kind=c_char), intent(in) :: pathname(*) |
| 22 | + integer(c_int), value :: flags |
| 23 | + integer(c_int) :: c_open |
| 24 | + end function c_open |
| 25 | + |
| 26 | + function c_close(fd) bind(C, name="close") |
| 27 | + import :: c_int |
| 28 | + integer(c_int), value :: fd |
| 29 | + integer(c_int) :: c_close |
| 30 | + end function c_close |
| 31 | + |
| 32 | + function c_mmap(addr, length, prot, flags, fd, offset) bind(C, name="mmap") |
| 33 | + import :: c_ptr, c_size_t, c_int, c_long |
| 34 | + type(c_ptr), value :: addr |
| 35 | + integer(c_size_t), value :: length |
| 36 | + integer(c_int), value :: prot |
| 37 | + integer(c_int), value :: flags |
| 38 | + integer(c_int), value :: fd |
| 39 | + integer(c_long), value :: offset |
| 40 | + type(c_ptr) :: c_mmap |
| 41 | + end function c_mmap |
| 42 | + |
| 43 | + function c_munmap(addr, length) bind(C, name="munmap") |
| 44 | + import :: c_ptr, c_size_t, c_int |
| 45 | + type(c_ptr), value :: addr |
| 46 | + integer(c_size_t), value :: length |
| 47 | + integer(c_int) :: c_munmap |
| 48 | + end function c_munmap |
| 49 | + |
| 50 | + function c_fstat(fd, statbuf) bind(C, name="fstat") |
| 51 | + import :: c_int, c_ptr |
| 52 | + integer(c_int), value :: fd |
| 53 | + type(c_ptr), value :: statbuf |
| 54 | + integer(c_int) :: c_fstat |
| 55 | + end function c_fstat |
| 56 | + |
| 57 | + function c_lseek(fd, offset, whence) bind(C, name="lseek") |
| 58 | + import :: c_int, c_long |
| 59 | + integer(c_int), value :: fd |
| 60 | + integer(c_long), value :: offset |
| 61 | + integer(c_int), value :: whence |
| 62 | + integer(c_long) :: c_lseek |
| 63 | + end function c_lseek |
| 64 | + end interface |
| 65 | + |
| 66 | + ! lseek whence values |
| 67 | + integer(c_int), parameter :: SEEK_SET = 0 |
| 68 | + integer(c_int), parameter :: SEEK_END = 2 |
| 69 | + |
| 70 | + !> Memory-mapped file type |
| 71 | + type :: mmap_file_t |
| 72 | + type(c_ptr) :: data = c_null_ptr |
| 73 | + integer(c_size_t) :: size = 0 |
| 74 | + integer(c_size_t) :: pos = 0 ! Current position in file |
| 75 | + integer :: line_number = 0 |
| 76 | + integer(i64) :: byte_offset = 0 |
| 77 | + logical :: is_open = .false. |
| 78 | + character(len=max_path_len) :: filename = '' |
| 79 | + contains |
| 80 | + procedure :: open => mmap_open_method |
| 81 | + procedure :: close => mmap_close_method |
| 82 | + procedure :: read_line => mmap_read_line |
| 83 | + procedure :: reset => mmap_reset |
| 84 | + end type mmap_file_t |
| 85 | + |
| 86 | +contains |
| 87 | + |
| 88 | + function mmap_open(filename, mfile) result(success) |
| 89 | + !> Open a file with memory mapping |
| 90 | + character(len=*), intent(in) :: filename |
| 91 | + type(mmap_file_t), intent(out) :: mfile |
| 92 | + logical :: success |
| 93 | + |
| 94 | + integer(c_int) :: fd, istat |
| 95 | + integer(c_int), parameter :: O_RDONLY = 0 |
| 96 | + integer(c_size_t) :: file_size |
| 97 | + integer(c_long) :: size_long |
| 98 | + |
| 99 | + success = .false. |
| 100 | + mfile%is_open = .false. |
| 101 | + mfile%filename = filename |
| 102 | + |
| 103 | + ! Open file |
| 104 | + fd = c_open(trim(filename) // c_null_char, O_RDONLY) |
| 105 | + if (fd < 0) return |
| 106 | + |
| 107 | + ! Get file size via lseek to end |
| 108 | + size_long = c_lseek(fd, 0_c_long, SEEK_END) |
| 109 | + if (size_long < 0) then |
| 110 | + istat = c_close(fd) |
| 111 | + return |
| 112 | + end if |
| 113 | + file_size = int(size_long, c_size_t) |
| 114 | + |
| 115 | + ! Seek back to beginning |
| 116 | + size_long = c_lseek(fd, 0_c_long, SEEK_SET) |
| 117 | + |
| 118 | + if (file_size == 0) then |
| 119 | + istat = c_close(fd) |
| 120 | + mfile%size = 0 |
| 121 | + mfile%is_open = .true. |
| 122 | + success = .true. |
| 123 | + return |
| 124 | + end if |
| 125 | + |
| 126 | + ! Memory map the file |
| 127 | + mfile%data = c_mmap(c_null_ptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0_c_long) |
| 128 | + istat = c_close(fd) ! Can close fd after mmap |
| 129 | + |
| 130 | + if (.not. c_associated(mfile%data)) return |
| 131 | + |
| 132 | + mfile%size = file_size |
| 133 | + mfile%pos = 0 |
| 134 | + mfile%line_number = 0 |
| 135 | + mfile%byte_offset = 0 |
| 136 | + mfile%is_open = .true. |
| 137 | + success = .true. |
| 138 | + |
| 139 | + end function mmap_open |
| 140 | + |
| 141 | + function mmap_open_method(this, filename) result(success) |
| 142 | + class(mmap_file_t), intent(inout) :: this |
| 143 | + character(len=*), intent(in) :: filename |
| 144 | + logical :: success |
| 145 | + success = mmap_open(filename, this) |
| 146 | + end function mmap_open_method |
| 147 | + |
| 148 | + subroutine mmap_close(mfile) |
| 149 | + !> Close memory-mapped file |
| 150 | + type(mmap_file_t), intent(inout) :: mfile |
| 151 | + |
| 152 | + integer(c_int) :: istat |
| 153 | + |
| 154 | + if (mfile%is_open .and. c_associated(mfile%data)) then |
| 155 | + istat = c_munmap(mfile%data, mfile%size) |
| 156 | + end if |
| 157 | + |
| 158 | + mfile%data = c_null_ptr |
| 159 | + mfile%size = 0 |
| 160 | + mfile%pos = 0 |
| 161 | + mfile%is_open = .false. |
| 162 | + |
| 163 | + end subroutine mmap_close |
| 164 | + |
| 165 | + subroutine mmap_close_method(this) |
| 166 | + class(mmap_file_t), intent(inout) :: this |
| 167 | + call mmap_close(this) |
| 168 | + end subroutine mmap_close_method |
| 169 | + |
| 170 | + subroutine mmap_reset(this) |
| 171 | + !> Reset to beginning of file |
| 172 | + class(mmap_file_t), intent(inout) :: this |
| 173 | + this%pos = 0 |
| 174 | + this%line_number = 0 |
| 175 | + this%byte_offset = 0 |
| 176 | + end subroutine mmap_reset |
| 177 | + |
| 178 | + function mmap_get_line(mfile, line, line_num, byte_off) result(success) |
| 179 | + !> Get next line from memory-mapped file |
| 180 | + type(mmap_file_t), intent(inout) :: mfile |
| 181 | + character(len=:), allocatable, intent(out) :: line |
| 182 | + integer, intent(out) :: line_num |
| 183 | + integer(i64), intent(out) :: byte_off |
| 184 | + logical :: success |
| 185 | + |
| 186 | + success = mfile%read_line(line, line_num, byte_off) |
| 187 | + end function mmap_get_line |
| 188 | + |
| 189 | + function mmap_read_line(this, line, line_num, byte_off) result(success) |
| 190 | + !> Read next line from memory-mapped file |
| 191 | + class(mmap_file_t), intent(inout) :: this |
| 192 | + character(len=:), allocatable, intent(out) :: line |
| 193 | + integer, intent(out) :: line_num |
| 194 | + integer(i64), intent(out) :: byte_off |
| 195 | + logical :: success |
| 196 | + |
| 197 | + character(len=1, kind=c_char), pointer :: file_data(:) |
| 198 | + integer(c_size_t) :: start_pos, end_pos, line_len |
| 199 | + integer :: i |
| 200 | + |
| 201 | + success = .false. |
| 202 | + line_num = 0 |
| 203 | + byte_off = 0 |
| 204 | + if (allocated(line)) deallocate(line) |
| 205 | + |
| 206 | + if (.not. this%is_open) return |
| 207 | + if (this%pos >= this%size) return |
| 208 | + |
| 209 | + ! Map the C pointer to a Fortran character array |
| 210 | + call c_f_pointer(this%data, file_data, [this%size]) |
| 211 | + |
| 212 | + ! Find start and end of line |
| 213 | + start_pos = this%pos + 1 ! 1-based for Fortran |
| 214 | + end_pos = start_pos |
| 215 | + |
| 216 | + ! Scan for newline |
| 217 | + do while (end_pos <= this%size) |
| 218 | + if (file_data(end_pos) == char(10)) exit ! LF |
| 219 | + end_pos = end_pos + 1 |
| 220 | + end do |
| 221 | + |
| 222 | + ! Calculate line length (excluding newline) |
| 223 | + line_len = end_pos - start_pos |
| 224 | + if (line_len > 0 .and. end_pos > start_pos) then |
| 225 | + ! Check for CR before LF (Windows line ending) |
| 226 | + if (file_data(end_pos - 1) == char(13)) then |
| 227 | + line_len = line_len - 1 |
| 228 | + end if |
| 229 | + end if |
| 230 | + |
| 231 | + ! Allocate and copy line |
| 232 | + if (line_len > 0) then |
| 233 | + allocate(character(len=line_len) :: line) |
| 234 | + do i = 1, int(line_len) |
| 235 | + line(i:i) = file_data(start_pos + i - 1) |
| 236 | + end do |
| 237 | + else |
| 238 | + line = '' |
| 239 | + end if |
| 240 | + |
| 241 | + ! Update state |
| 242 | + this%line_number = this%line_number + 1 |
| 243 | + line_num = this%line_number |
| 244 | + byte_off = int(this%pos, i64) |
| 245 | + |
| 246 | + ! Move past the newline |
| 247 | + if (end_pos <= this%size) then |
| 248 | + this%pos = end_pos ! Position after newline (0-based) |
| 249 | + else |
| 250 | + this%pos = this%size |
| 251 | + end if |
| 252 | + this%byte_offset = int(this%pos, i64) |
| 253 | + |
| 254 | + success = .true. |
| 255 | + |
| 256 | + end function mmap_read_line |
| 257 | + |
| 258 | +end module ferp_mmap |