PR 48587 Newunit allocator
[gcc.git] / libgfortran / io / io.h
1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 F2003 I/O support contributed by Jerry DeLisle
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26 #ifndef GFOR_IO_H
27 #define GFOR_IO_H
28
29 /* IO library include. */
30
31 #include "libgfortran.h"
32
33 #include <gthr.h>
34
35
36 /* POSIX 2008 specifies that the extended locale stuff is found in
37 locale.h, but some systems have them in xlocale.h. */
38
39 #include <locale.h>
40
41 #ifdef HAVE_XLOCALE_H
42 #include <xlocale.h>
43 #endif
44
45
46 /* Forward declarations. */
47 struct st_parameter_dt;
48 typedef struct stream stream;
49 struct fbuf;
50 struct format_data;
51 typedef struct fnode fnode;
52 struct gfc_unit;
53
54 #ifdef HAVE_NEWLOCALE
55 /* We have POSIX 2008 extended locale stuff. */
56 extern locale_t c_locale;
57 internal_proto(c_locale);
58 #else
59 extern char* old_locale;
60 internal_proto(old_locale);
61 extern int old_locale_ctr;
62 internal_proto(old_locale_ctr);
63 extern __gthread_mutex_t old_locale_lock;
64 internal_proto(old_locale_lock);
65 #endif
66
67
68 /* Macros for testing what kinds of I/O we are doing. */
69
70 #define is_array_io(dtp) ((dtp)->internal_unit_desc)
71
72 #define is_internal_unit(dtp) ((dtp)->u.p.current_unit->internal_unit_kind)
73
74 #define is_stream_io(dtp) ((dtp)->u.p.current_unit->flags.access == ACCESS_STREAM)
75
76 #define is_char4_unit(dtp) ((dtp)->u.p.current_unit->internal_unit_kind == 4)
77
78 /* The array_loop_spec contains the variables for the loops over index ranges
79 that are encountered. */
80
81 typedef struct array_loop_spec
82 {
83 /* Index counter for this dimension. */
84 index_type idx;
85
86 /* Start for the index counter. */
87 index_type start;
88
89 /* End for the index counter. */
90 index_type end;
91
92 /* Step for the index counter. */
93 index_type step;
94 }
95 array_loop_spec;
96
97 /* User defined input/output iomsg length. */
98
99 #define IOMSG_LEN 256
100
101 /* Subroutine formatted_dtio (struct, unit, iotype, v_list, iostat,
102 iomsg, (_iotype), (_iomsg)) */
103 typedef void (*formatted_dtio)(void *, GFC_INTEGER_4 *, char *, gfc_array_i4 *,
104 GFC_INTEGER_4 *, char *,
105 gfc_charlen_type, gfc_charlen_type);
106
107 /* Subroutine unformatted_dtio (struct, unit, iostat, iomsg, (_iomsg)) */
108 typedef void (*unformatted_dtio)(void *, GFC_INTEGER_4 *, GFC_INTEGER_4 *,
109 char *, gfc_charlen_type);
110
111 /* The dtio calls for namelist require a CLASS object to be built. */
112 typedef struct gfc_class
113 {
114 void *data;
115 void *vptr;
116 index_type len;
117 }
118 gfc_class;
119
120
121 /* A structure to build a hash table for format data. */
122
123 #define FORMAT_HASH_SIZE 16
124
125 typedef struct format_hash_entry
126 {
127 char *key;
128 gfc_charlen_type key_len;
129 struct format_data *hashed_fmt;
130 }
131 format_hash_entry;
132
133 /* Representation of a namelist object in libgfortran
134
135 Namelist Records
136 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]].../
137 or
138 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]]...&END
139
140 The object can be a fully qualified, compound name for an intrinsic
141 type, derived types or derived type components. So, a substring
142 a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
143 read. Hence full information about the structure of the object has
144 to be available to list_read.c and write.
145
146 These requirements are met by the following data structures.
147
148 namelist_info type contains all the scalar information about the
149 object and arrays of descriptor_dimension and array_loop_spec types for
150 arrays. */
151
152 typedef struct namelist_type
153 {
154 /* Object type. */
155 bt type;
156
157 /* Object name. */
158 char * var_name;
159
160 /* Address for the start of the object's data. */
161 void * mem_pos;
162
163 /* Address of specific DTIO subroutine. */
164 void * dtio_sub;
165
166 /* Address of vtable if dtio_sub non-null. */
167 void * vtable;
168
169 /* Flag to show that a read is to be attempted for this node. */
170 int touched;
171
172 /* Length of intrinsic type in bytes. */
173 int len;
174
175 /* Rank of the object. */
176 int var_rank;
177
178 /* Overall size of the object in bytes. */
179 index_type size;
180
181 /* Length of character string. */
182 index_type string_length;
183
184 descriptor_dimension * dim;
185 array_loop_spec * ls;
186 struct namelist_type * next;
187 }
188 namelist_info;
189
190 /* Options for the OPEN statement. */
191
192 typedef enum
193 { ACCESS_SEQUENTIAL, ACCESS_DIRECT, ACCESS_APPEND, ACCESS_STREAM,
194 ACCESS_UNSPECIFIED
195 }
196 unit_access;
197
198 typedef enum
199 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
200 ACTION_UNSPECIFIED
201 }
202 unit_action;
203
204 typedef enum
205 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
206 unit_blank;
207
208 typedef enum
209 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
210 DELIM_UNSPECIFIED
211 }
212 unit_delim;
213
214 typedef enum
215 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
216 unit_form;
217
218 typedef enum
219 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
220 POSITION_UNSPECIFIED
221 }
222 unit_position;
223
224 typedef enum
225 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
226 STATUS_REPLACE, STATUS_UNSPECIFIED
227 }
228 unit_status;
229
230 typedef enum
231 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
232 unit_pad;
233
234 typedef enum
235 { DECIMAL_POINT, DECIMAL_COMMA, DECIMAL_UNSPECIFIED }
236 unit_decimal;
237
238 typedef enum
239 { ENCODING_UTF8, ENCODING_DEFAULT, ENCODING_UNSPECIFIED }
240 unit_encoding;
241
242 typedef enum
243 { ROUND_UP = GFC_FPE_UPWARD,
244 ROUND_DOWN = GFC_FPE_DOWNWARD,
245 ROUND_ZERO = GFC_FPE_TOWARDZERO,
246 ROUND_NEAREST = GFC_FPE_TONEAREST,
247 ROUND_COMPATIBLE = 10, /* round away from zero. */
248 ROUND_PROCDEFINED, /* Here as ROUND_NEAREST. */
249 ROUND_UNSPECIFIED /* Should never occur. */
250 }
251 unit_round;
252
253 /* NOTE: unit_sign must correspond with the sign_status enumerator in
254 st_parameter_dt to not break the ABI. */
255 typedef enum
256 { SIGN_PROCDEFINED, SIGN_SUPPRESS, SIGN_PLUS, SIGN_UNSPECIFIED }
257 unit_sign;
258
259 typedef enum
260 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
261 unit_advance;
262
263 typedef enum
264 {READING, WRITING, LIST_READING, LIST_WRITING}
265 unit_mode;
266
267 typedef enum
268 { ASYNC_YES, ASYNC_NO, ASYNC_UNSPECIFIED }
269 unit_async;
270
271 typedef enum
272 { SIGN_S, SIGN_SS, SIGN_SP }
273 unit_sign_s;
274
275 #define CHARACTER1(name) \
276 char * name; \
277 gfc_charlen_type name ## _len
278 #define CHARACTER2(name) \
279 gfc_charlen_type name ## _len; \
280 char * name
281
282 typedef struct
283 {
284 st_parameter_common common;
285 GFC_INTEGER_4 recl_in;
286 CHARACTER2 (file);
287 CHARACTER1 (status);
288 CHARACTER2 (access);
289 CHARACTER1 (form);
290 CHARACTER2 (blank);
291 CHARACTER1 (position);
292 CHARACTER2 (action);
293 CHARACTER1 (delim);
294 CHARACTER2 (pad);
295 CHARACTER1 (convert);
296 CHARACTER2 (decimal);
297 CHARACTER1 (encoding);
298 CHARACTER2 (round);
299 CHARACTER1 (sign);
300 CHARACTER2 (asynchronous);
301 GFC_INTEGER_4 *newunit;
302 }
303 st_parameter_open;
304
305 #define IOPARM_CLOSE_HAS_STATUS (1 << 7)
306
307 typedef struct
308 {
309 st_parameter_common common;
310 CHARACTER1 (status);
311 }
312 st_parameter_close;
313
314 typedef struct
315 {
316 st_parameter_common common;
317 }
318 st_parameter_filepos;
319
320 #define IOPARM_INQUIRE_HAS_EXIST (1 << 7)
321 #define IOPARM_INQUIRE_HAS_OPENED (1 << 8)
322 #define IOPARM_INQUIRE_HAS_NUMBER (1 << 9)
323 #define IOPARM_INQUIRE_HAS_NAMED (1 << 10)
324 #define IOPARM_INQUIRE_HAS_NEXTREC (1 << 11)
325 #define IOPARM_INQUIRE_HAS_RECL_OUT (1 << 12)
326 #define IOPARM_INQUIRE_HAS_STRM_POS_OUT (1 << 13)
327 #define IOPARM_INQUIRE_HAS_FILE (1 << 14)
328 #define IOPARM_INQUIRE_HAS_ACCESS (1 << 15)
329 #define IOPARM_INQUIRE_HAS_FORM (1 << 16)
330 #define IOPARM_INQUIRE_HAS_BLANK (1 << 17)
331 #define IOPARM_INQUIRE_HAS_POSITION (1 << 18)
332 #define IOPARM_INQUIRE_HAS_ACTION (1 << 19)
333 #define IOPARM_INQUIRE_HAS_DELIM (1 << 20)
334 #define IOPARM_INQUIRE_HAS_PAD (1 << 21)
335 #define IOPARM_INQUIRE_HAS_NAME (1 << 22)
336 #define IOPARM_INQUIRE_HAS_SEQUENTIAL (1 << 23)
337 #define IOPARM_INQUIRE_HAS_DIRECT (1 << 24)
338 #define IOPARM_INQUIRE_HAS_FORMATTED (1 << 25)
339 #define IOPARM_INQUIRE_HAS_UNFORMATTED (1 << 26)
340 #define IOPARM_INQUIRE_HAS_READ (1 << 27)
341 #define IOPARM_INQUIRE_HAS_WRITE (1 << 28)
342 #define IOPARM_INQUIRE_HAS_READWRITE (1 << 29)
343 #define IOPARM_INQUIRE_HAS_CONVERT (1 << 30)
344 #define IOPARM_INQUIRE_HAS_FLAGS2 (1u << 31)
345
346 #define IOPARM_INQUIRE_HAS_ASYNCHRONOUS (1 << 0)
347 #define IOPARM_INQUIRE_HAS_DECIMAL (1 << 1)
348 #define IOPARM_INQUIRE_HAS_ENCODING (1 << 2)
349 #define IOPARM_INQUIRE_HAS_ROUND (1 << 3)
350 #define IOPARM_INQUIRE_HAS_SIGN (1 << 4)
351 #define IOPARM_INQUIRE_HAS_PENDING (1 << 5)
352 #define IOPARM_INQUIRE_HAS_SIZE (1 << 6)
353 #define IOPARM_INQUIRE_HAS_ID (1 << 7)
354 #define IOPARM_INQUIRE_HAS_IQSTREAM (1 << 8)
355
356 typedef struct
357 {
358 st_parameter_common common;
359 GFC_INTEGER_4 *exist, *opened, *number, *named;
360 GFC_INTEGER_4 *nextrec, *recl_out;
361 GFC_IO_INT *strm_pos_out;
362 CHARACTER1 (file);
363 CHARACTER2 (access);
364 CHARACTER1 (form);
365 CHARACTER2 (blank);
366 CHARACTER1 (position);
367 CHARACTER2 (action);
368 CHARACTER1 (delim);
369 CHARACTER2 (pad);
370 CHARACTER1 (name);
371 CHARACTER2 (sequential);
372 CHARACTER1 (direct);
373 CHARACTER2 (formatted);
374 CHARACTER1 (unformatted);
375 CHARACTER2 (read);
376 CHARACTER1 (write);
377 CHARACTER2 (readwrite);
378 CHARACTER1 (convert);
379 GFC_INTEGER_4 flags2;
380 CHARACTER1 (asynchronous);
381 CHARACTER2 (decimal);
382 CHARACTER1 (encoding);
383 CHARACTER2 (round);
384 CHARACTER1 (sign);
385 GFC_INTEGER_4 *pending;
386 GFC_IO_INT *size;
387 GFC_INTEGER_4 *id;
388 CHARACTER1 (iqstream);
389 }
390 st_parameter_inquire;
391
392
393 #define IOPARM_DT_LIST_FORMAT (1 << 7)
394 #define IOPARM_DT_NAMELIST_READ_MODE (1 << 8)
395 #define IOPARM_DT_HAS_REC (1 << 9)
396 #define IOPARM_DT_HAS_SIZE (1 << 10)
397 #define IOPARM_DT_HAS_IOLENGTH (1 << 11)
398 #define IOPARM_DT_HAS_FORMAT (1 << 12)
399 #define IOPARM_DT_HAS_ADVANCE (1 << 13)
400 #define IOPARM_DT_HAS_INTERNAL_UNIT (1 << 14)
401 #define IOPARM_DT_HAS_NAMELIST_NAME (1 << 15)
402 #define IOPARM_DT_HAS_ID (1 << 16)
403 #define IOPARM_DT_HAS_POS (1 << 17)
404 #define IOPARM_DT_HAS_ASYNCHRONOUS (1 << 18)
405 #define IOPARM_DT_HAS_BLANK (1 << 19)
406 #define IOPARM_DT_HAS_DECIMAL (1 << 20)
407 #define IOPARM_DT_HAS_DELIM (1 << 21)
408 #define IOPARM_DT_HAS_PAD (1 << 22)
409 #define IOPARM_DT_HAS_ROUND (1 << 23)
410 #define IOPARM_DT_HAS_SIGN (1 << 24)
411 #define IOPARM_DT_HAS_F2003 (1 << 25)
412 #define IOPARM_DT_HAS_UDTIO (1 << 26)
413 /* Internal use bit. */
414 #define IOPARM_DT_IONML_SET (1u << 31)
415
416
417 typedef struct st_parameter_dt
418 {
419 st_parameter_common common;
420 GFC_IO_INT rec;
421 GFC_IO_INT *size, *iolength;
422 gfc_array_char *internal_unit_desc;
423 CHARACTER1 (format);
424 CHARACTER2 (advance);
425 CHARACTER1 (internal_unit);
426 CHARACTER2 (namelist_name);
427 /* Private part of the structure. The compiler just needs
428 to reserve enough space. */
429 union
430 {
431 struct
432 {
433 void (*transfer) (struct st_parameter_dt *, bt, void *, int,
434 size_t, size_t);
435 struct gfc_unit *current_unit;
436 /* Item number in a formatted data transfer. Also used in namelist
437 read_logical as an index into line_buffer. */
438 int item_count;
439 unit_mode mode;
440 unit_blank blank_status;
441 unit_sign sign_status;
442 int scale_factor;
443 int max_pos; /* Maximum righthand column written to. */
444 /* Number of skips + spaces to be done for T and X-editing. */
445 int skips;
446 /* Number of spaces to be done for T and X-editing. */
447 int pending_spaces;
448 /* Whether an EOR condition was encountered. Value is:
449 0 if no EOR was encountered
450 1 if an EOR was encountered due to a 1-byte marker (LF)
451 2 if an EOR was encountered due to a 2-bytes marker (CRLF) */
452 int sf_seen_eor;
453 unit_advance advance_status;
454 unsigned reversion_flag : 1; /* Format reversion has occurred. */
455 unsigned first_item : 1;
456 unsigned seen_dollar : 1;
457 unsigned eor_condition : 1;
458 unsigned no_leading_blank : 1;
459 unsigned char_flag : 1;
460 unsigned input_complete : 1;
461 unsigned at_eol : 1;
462 unsigned comma_flag : 1;
463 /* A namelist specific flag used in the list directed library
464 to flag that calls are being made from namelist read (e.g. to
465 ignore comments or to treat '/' as a terminator) */
466 unsigned namelist_mode : 1;
467 /* A namelist specific flag used in the list directed library
468 to flag read errors and return, so that an attempt can be
469 made to read a new object name. */
470 unsigned nml_read_error : 1;
471 /* A sequential formatted read specific flag used to signal that a
472 character string is being read so don't use commas to shorten a
473 formatted field width. */
474 unsigned sf_read_comma : 1;
475 /* A namelist specific flag used to enable reading input from
476 line_buffer for logical reads. */
477 unsigned line_buffer_enabled : 1;
478 /* An internal unit specific flag used to identify that the associated
479 unit is internal. */
480 unsigned unit_is_internal : 1;
481 /* An internal unit specific flag to signify an EOF condition for list
482 directed read. */
483 unsigned at_eof : 1;
484 /* Used for g0 floating point output. */
485 unsigned g0_no_blanks : 1;
486 /* Used to signal use of free_format_data. */
487 unsigned format_not_saved : 1;
488 /* A flag used to identify when a non-standard expanded namelist read
489 has occurred. */
490 unsigned expanded_read : 1;
491 /* 13 unused bits. */
492
493 /* Used for ungetc() style functionality. Possible values
494 are an unsigned char, EOF, or EOF - 1 used to mark the
495 field as not valid. */
496 int last_char; /* No longer used, moved to gfc_unit. */
497 char nml_delim;
498
499 int repeat_count;
500 int saved_length;
501 int saved_used;
502 bt saved_type;
503 char *saved_string;
504 char *scratch;
505 char *line_buffer;
506 struct format_data *fmt;
507 namelist_info *ionml;
508 #ifdef HAVE_NEWLOCALE
509 locale_t old_locale;
510 #endif
511 /* Current position within the look-ahead line buffer. */
512 int line_buffer_pos;
513 /* Storage area for values except for strings. Must be
514 large enough to hold a complex value (two reals) of the
515 largest kind. */
516 char value[32];
517 GFC_IO_INT size_used;
518 formatted_dtio fdtio_ptr;
519 unformatted_dtio ufdtio_ptr;
520 } p;
521 /* This pad size must be equal to the pad_size declared in
522 trans-io.c (gfc_build_io_library_fndecls). The above structure
523 must be smaller or equal to this array. */
524 char pad[16 * sizeof (char *) + 32 * sizeof (int)];
525 } u;
526 GFC_INTEGER_4 *id;
527 GFC_IO_INT pos;
528 CHARACTER1 (asynchronous);
529 CHARACTER2 (blank);
530 CHARACTER1 (decimal);
531 CHARACTER2 (delim);
532 CHARACTER1 (pad);
533 CHARACTER2 (round);
534 CHARACTER1 (sign);
535 }
536 st_parameter_dt;
537
538 /* Ensure st_parameter_dt's u.pad is bigger or equal to u.p. */
539 extern char check_st_parameter_dt[sizeof (((st_parameter_dt *) 0)->u.pad)
540 >= sizeof (((st_parameter_dt *) 0)->u.p)
541 ? 1 : -1];
542
543 #define IOPARM_WAIT_HAS_ID (1 << 7)
544
545 typedef struct
546 {
547 st_parameter_common common;
548 CHARACTER1 (id);
549 }
550 st_parameter_wait;
551
552
553 #undef CHARACTER1
554 #undef CHARACTER2
555
556 typedef struct
557 {
558 unit_access access;
559 unit_action action;
560 unit_blank blank;
561 unit_delim delim;
562 unit_form form;
563 int is_notpadded;
564 unit_position position;
565 unit_status status;
566 unit_pad pad;
567 unit_convert convert;
568 int has_recl;
569 unit_decimal decimal;
570 unit_encoding encoding;
571 unit_round round;
572 unit_sign sign;
573 unit_async async;
574 }
575 unit_flags;
576
577
578 typedef struct gfc_unit
579 {
580 int unit_number;
581 stream *s;
582
583 /* Treap links. */
584 struct gfc_unit *left, *right;
585 int priority;
586
587 int read_bad, current_record, saved_pos, previous_nonadvancing_write;
588
589 enum
590 { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
591 endfile;
592
593 unit_mode mode;
594 unit_flags flags;
595 unit_pad pad_status;
596 unit_decimal decimal_status;
597 unit_delim delim_status;
598 unit_round round_status;
599
600 /* recl -- Record length of the file.
601 last_record -- Last record number read or written
602 maxrec -- Maximum record number in a direct access file
603 bytes_left -- Bytes left in current record.
604 strm_pos -- Current position in file for STREAM I/O.
605 recl_subrecord -- Maximum length for subrecord.
606 bytes_left_subrecord -- Bytes left in current subrecord. */
607 gfc_offset recl, last_record, maxrec, bytes_left, strm_pos,
608 recl_subrecord, bytes_left_subrecord;
609
610 /* Set to 1 if we have read a subrecord. */
611
612 int continued;
613
614 __gthread_mutex_t lock;
615 /* Number of threads waiting to acquire this unit's lock.
616 When non-zero, close_unit doesn't only removes the unit
617 from the UNIT_ROOT tree, but doesn't free it and the
618 last of the waiting threads will do that.
619 This must be either atomically increased/decreased, or
620 always guarded by UNIT_LOCK. */
621 int waiting;
622 /* Flag set by close_unit if the unit as been closed.
623 Must be manipulated under unit's lock. */
624 int closed;
625
626 /* For traversing arrays */
627 array_loop_spec *ls;
628 int rank;
629
630 /* Name of the file at the time OPEN was executed, as a
631 null-terminated C string. */
632 char *filename;
633
634 /* The format hash table. */
635 struct format_hash_entry format_hash_table[FORMAT_HASH_SIZE];
636
637 /* Formatting buffer. */
638 struct fbuf *fbuf;
639
640 /* Function pointer, points to list_read worker functions. */
641 int (*next_char_fn_ptr) (st_parameter_dt *);
642 void (*push_char_fn_ptr) (st_parameter_dt *, int);
643
644 /* Internal unit char string data. */
645 char * internal_unit;
646 gfc_charlen_type internal_unit_len;
647 gfc_array_char *string_unit_desc;
648 int internal_unit_kind;
649
650 /* DTIO Parent/Child procedure, 0 = parent, >0 = child level. */
651 int child_dtio;
652 int last_char;
653 }
654 gfc_unit;
655
656 typedef struct gfc_saved_unit
657 {
658 GFC_INTEGER_4 unit_number;
659 gfc_unit *unit;
660 }
661 gfc_saved_unit;
662
663 /* TEMP_FAILURE_RETRY macro from glibc. */
664
665 #ifndef TEMP_FAILURE_RETRY
666 /* Evaluate EXPRESSION, and repeat as long as it returns -1 with `errno'
667 set to EINTR. */
668
669 # define TEMP_FAILURE_RETRY(expression) \
670 (__extension__ \
671 ({ long int __result; \
672 do __result = (long int) (expression); \
673 while (__result == -1L && errno == EINTR); \
674 __result; }))
675 #endif
676
677
678 /* unit.c */
679
680 /* Maximum file offset, computed at library initialization time. */
681 extern gfc_offset max_offset;
682 internal_proto(max_offset);
683
684 /* Unit tree root. */
685 extern gfc_unit *unit_root;
686 internal_proto(unit_root);
687
688 extern __gthread_mutex_t unit_lock;
689 internal_proto(unit_lock);
690
691 extern int close_unit (gfc_unit *);
692 internal_proto(close_unit);
693
694 extern gfc_unit *set_internal_unit (st_parameter_dt *, gfc_unit *, int);
695 internal_proto(set_internal_unit);
696
697 extern void stash_internal_unit (st_parameter_dt *);
698 internal_proto(stash_internal_unit);
699
700 extern gfc_unit *find_unit (int);
701 internal_proto(find_unit);
702
703 extern gfc_unit *find_or_create_unit (int);
704 internal_proto(find_or_create_unit);
705
706 extern gfc_unit *get_unit (st_parameter_dt *, int);
707 internal_proto(get_unit);
708
709 extern void unlock_unit (gfc_unit *);
710 internal_proto(unlock_unit);
711
712 extern void finish_last_advance_record (gfc_unit *u);
713 internal_proto (finish_last_advance_record);
714
715 extern int unit_truncate (gfc_unit *, gfc_offset, st_parameter_common *);
716 internal_proto (unit_truncate);
717
718 extern int newunit_alloc (void);
719 internal_proto(newunit_alloc);
720
721
722 /* open.c */
723
724 extern gfc_unit *new_unit (st_parameter_open *, gfc_unit *, unit_flags *);
725 internal_proto(new_unit);
726
727
728 /* transfer.c */
729
730 #define SCRATCH_SIZE 300
731
732 extern const char *type_name (bt);
733 internal_proto(type_name);
734
735 extern void * read_block_form (st_parameter_dt *, int *);
736 internal_proto(read_block_form);
737
738 extern void * read_block_form4 (st_parameter_dt *, int *);
739 internal_proto(read_block_form4);
740
741 extern void *write_block (st_parameter_dt *, int);
742 internal_proto(write_block);
743
744 extern gfc_offset next_array_record (st_parameter_dt *, array_loop_spec *,
745 int*);
746 internal_proto(next_array_record);
747
748 extern gfc_offset init_loop_spec (gfc_array_char *, array_loop_spec *,
749 gfc_offset *);
750 internal_proto(init_loop_spec);
751
752 extern void next_record (st_parameter_dt *, int);
753 internal_proto(next_record);
754
755 extern void st_wait (st_parameter_wait *);
756 export_proto(st_wait);
757
758 extern void hit_eof (st_parameter_dt *);
759 internal_proto(hit_eof);
760
761 /* read.c */
762
763 extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
764 internal_proto(set_integer);
765
766 extern GFC_UINTEGER_LARGEST si_max (int);
767 internal_proto(si_max);
768
769 extern int convert_real (st_parameter_dt *, void *, const char *, int);
770 internal_proto(convert_real);
771
772 extern int convert_infnan (st_parameter_dt *, void *, const char *, int);
773 internal_proto(convert_infnan);
774
775 extern void read_a (st_parameter_dt *, const fnode *, char *, int);
776 internal_proto(read_a);
777
778 extern void read_a_char4 (st_parameter_dt *, const fnode *, char *, int);
779 internal_proto(read_a);
780
781 extern void read_f (st_parameter_dt *, const fnode *, char *, int);
782 internal_proto(read_f);
783
784 extern void read_l (st_parameter_dt *, const fnode *, char *, int);
785 internal_proto(read_l);
786
787 extern void read_x (st_parameter_dt *, int);
788 internal_proto(read_x);
789
790 extern void read_radix (st_parameter_dt *, const fnode *, char *, int, int);
791 internal_proto(read_radix);
792
793 extern void read_decimal (st_parameter_dt *, const fnode *, char *, int);
794 internal_proto(read_decimal);
795
796 extern void read_user_defined (st_parameter_dt *, void *);
797 internal_proto(read_user_defined);
798
799 extern void read_user_defined (st_parameter_dt *, void *);
800 internal_proto(read_user_defined);
801
802 /* list_read.c */
803
804 extern void list_formatted_read (st_parameter_dt *, bt, void *, int, size_t,
805 size_t);
806 internal_proto(list_formatted_read);
807
808 extern void finish_list_read (st_parameter_dt *);
809 internal_proto(finish_list_read);
810
811 extern void namelist_read (st_parameter_dt *);
812 internal_proto(namelist_read);
813
814 extern void namelist_write (st_parameter_dt *);
815 internal_proto(namelist_write);
816
817 /* write.c */
818
819 extern void write_a (st_parameter_dt *, const fnode *, const char *, int);
820 internal_proto(write_a);
821
822 extern void write_a_char4 (st_parameter_dt *, const fnode *, const char *, int);
823 internal_proto(write_a_char4);
824
825 extern void write_b (st_parameter_dt *, const fnode *, const char *, int);
826 internal_proto(write_b);
827
828 extern void write_d (st_parameter_dt *, const fnode *, const char *, int);
829 internal_proto(write_d);
830
831 extern void write_e (st_parameter_dt *, const fnode *, const char *, int);
832 internal_proto(write_e);
833
834 extern void write_en (st_parameter_dt *, const fnode *, const char *, int);
835 internal_proto(write_en);
836
837 extern void write_es (st_parameter_dt *, const fnode *, const char *, int);
838 internal_proto(write_es);
839
840 extern void write_f (st_parameter_dt *, const fnode *, const char *, int);
841 internal_proto(write_f);
842
843 extern void write_i (st_parameter_dt *, const fnode *, const char *, int);
844 internal_proto(write_i);
845
846 extern void write_l (st_parameter_dt *, const fnode *, char *, int);
847 internal_proto(write_l);
848
849 extern void write_o (st_parameter_dt *, const fnode *, const char *, int);
850 internal_proto(write_o);
851
852 extern void write_real (st_parameter_dt *, const char *, int);
853 internal_proto(write_real);
854
855 extern void write_real_g0 (st_parameter_dt *, const char *, int, int);
856 internal_proto(write_real_g0);
857
858 extern void write_x (st_parameter_dt *, int, int);
859 internal_proto(write_x);
860
861 extern void write_z (st_parameter_dt *, const fnode *, const char *, int);
862 internal_proto(write_z);
863
864 extern void write_user_defined (st_parameter_dt *, void *);
865 internal_proto(write_user_defined);
866
867 extern void write_user_defined (st_parameter_dt *, void *);
868 internal_proto(write_user_defined);
869
870 extern void list_formatted_write (st_parameter_dt *, bt, void *, int, size_t,
871 size_t);
872 internal_proto(list_formatted_write);
873
874 /* size_from_kind.c */
875 extern size_t size_from_real_kind (int);
876 internal_proto(size_from_real_kind);
877
878 extern size_t size_from_complex_kind (int);
879 internal_proto(size_from_complex_kind);
880
881
882 /* lock.c */
883 extern void free_ionml (st_parameter_dt *);
884 internal_proto(free_ionml);
885
886 static inline void
887 inc_waiting_locked (gfc_unit *u)
888 {
889 #ifdef HAVE_SYNC_FETCH_AND_ADD
890 (void) __sync_fetch_and_add (&u->waiting, 1);
891 #else
892 u->waiting++;
893 #endif
894 }
895
896 static inline int
897 predec_waiting_locked (gfc_unit *u)
898 {
899 #ifdef HAVE_SYNC_FETCH_AND_ADD
900 return __sync_add_and_fetch (&u->waiting, -1);
901 #else
902 return --u->waiting;
903 #endif
904 }
905
906 static inline void
907 dec_waiting_unlocked (gfc_unit *u)
908 {
909 #ifdef HAVE_SYNC_FETCH_AND_ADD
910 (void) __sync_fetch_and_add (&u->waiting, -1);
911 #else
912 __gthread_mutex_lock (&unit_lock);
913 u->waiting--;
914 __gthread_mutex_unlock (&unit_lock);
915 #endif
916 }
917
918
919 static inline void
920 memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
921 {
922 int j;
923 for (j = 0; j < k; j++)
924 *p++ = c;
925 }
926
927 #endif
928