1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
33 #include "libgfortran.h"
37 /* Subroutines related to units */
41 static gfc_unit internal_unit
, *unit_cache
[CACHE_SIZE
];
44 /* This implementation is based on Stefan Nilsson's article in the
45 * July 1997 Doctor Dobb's Journal, "Treaps in Java". */
47 /* pseudo_random()-- Simple linear congruential pseudorandom number
48 * generator. The period of this generator is 44071, which is plenty
49 * for our purposes. */
56 x0
= (22611 * x0
+ 10) % 44071;
61 /* rotate_left()-- Rotate the treap left */
64 rotate_left (gfc_unit
* t
)
69 t
->right
= t
->right
->left
;
76 /* rotate_right()-- Rotate the treap right */
79 rotate_right (gfc_unit
* t
)
84 t
->left
= t
->left
->right
;
93 compare (int a
, int b
)
104 /* insert()-- Recursive insertion function. Returns the updated treap. */
107 insert (gfc_unit
* new, gfc_unit
* t
)
114 c
= compare (new->unit_number
, t
->unit_number
);
118 t
->left
= insert (new, t
->left
);
119 if (t
->priority
< t
->left
->priority
)
120 t
= rotate_right (t
);
125 t
->right
= insert (new, t
->right
);
126 if (t
->priority
< t
->right
->priority
)
131 internal_error ("insert(): Duplicate key found!");
137 /* insert_unit()-- Given a new node, insert it into the treap. It is
138 * an error to insert a key that already exists. */
141 insert_unit (gfc_unit
* new)
143 new->priority
= pseudo_random ();
144 g
.unit_root
= insert (new, g
.unit_root
);
149 delete_root (gfc_unit
* t
)
155 if (t
->right
== NULL
)
158 if (t
->left
->priority
> t
->right
->priority
)
160 temp
= rotate_right (t
);
161 temp
->right
= delete_root (t
);
165 temp
= rotate_left (t
);
166 temp
->left
= delete_root (t
);
173 /* delete_treap()-- Delete an element from a tree. The 'old' value
174 * does not necessarily have to point to the element to be deleted, it
175 * must just point to a treap structure with the key to be deleted.
176 * Returns the new root node of the tree. */
179 delete_treap (gfc_unit
* old
, gfc_unit
* t
)
186 c
= compare (old
->unit_number
, t
->unit_number
);
189 t
->left
= delete_treap (old
, t
->left
);
191 t
->right
= delete_treap (old
, t
->right
);
199 /* delete_unit()-- Delete a unit from a tree */
202 delete_unit (gfc_unit
* old
)
204 g
.unit_root
= delete_treap (old
, g
.unit_root
);
208 /* find_unit()-- Given an integer, return a pointer to the unit
209 * structure. Returns NULL if the unit does not exist. */
217 for (c
= 0; c
< CACHE_SIZE
; c
++)
218 if (unit_cache
[c
] != NULL
&& unit_cache
[c
]->unit_number
== n
)
227 c
= compare (n
, p
->unit_number
);
238 for (c
= 0; c
< CACHE_SIZE
- 1; c
++)
239 unit_cache
[c
] = unit_cache
[c
+ 1];
241 unit_cache
[CACHE_SIZE
- 1] = p
;
247 /* get_unit()-- Returns the unit structure associated with the integer
248 * unit or the internal file. */
251 get_unit (int read_flag
)
253 if (ioparm
.internal_unit
!= NULL
)
256 open_internal (ioparm
.internal_unit
, ioparm
.internal_unit_len
);
258 /* Set flags for the internal unit */
260 internal_unit
.flags
.access
= ACCESS_SEQUENTIAL
;
261 internal_unit
.flags
.action
= ACTION_READWRITE
;
262 internal_unit
.flags
.form
= FORM_FORMATTED
;
263 internal_unit
.flags
.delim
= DELIM_NONE
;
265 return &internal_unit
;
268 /* Has to be an external unit */
270 return find_unit (ioparm
.unit
);
274 /* is_internal_unit()-- Determine if the current unit is internal or
280 return current_unit
== &internal_unit
;
285 /*************************/
286 /* Initialize everything */
295 if (options
.stdin_unit
>= 0)
297 u
= get_mem (sizeof (gfc_unit
));
299 u
->unit_number
= options
.stdin_unit
;
300 u
->s
= input_stream ();
302 u
->flags
.action
= ACTION_READ
;
304 u
->flags
.access
= ACCESS_SEQUENTIAL
;
305 u
->flags
.form
= FORM_FORMATTED
;
306 u
->flags
.status
= STATUS_OLD
;
307 u
->flags
.blank
= BLANK_ZERO
;
308 u
->flags
.position
= POSITION_ASIS
;
310 u
->recl
= options
.default_recl
;
311 u
->endfile
= NO_ENDFILE
;
316 if (options
.stdout_unit
>= 0)
318 u
= get_mem (sizeof (gfc_unit
));
320 u
->unit_number
= options
.stdout_unit
;
321 u
->s
= output_stream ();
323 u
->flags
.action
= ACTION_WRITE
;
325 u
->flags
.access
= ACCESS_SEQUENTIAL
;
326 u
->flags
.form
= FORM_FORMATTED
;
327 u
->flags
.status
= STATUS_OLD
;
328 u
->flags
.blank
= BLANK_ZERO
;
329 u
->flags
.position
= POSITION_ASIS
;
331 u
->recl
= options
.default_recl
;
332 u
->endfile
= AT_ENDFILE
;
337 if (options
.stderr_unit
>= 0)
339 u
= get_mem (sizeof (gfc_unit
));
341 u
->unit_number
= options
.stderr_unit
;
342 u
->s
= error_stream ();
344 u
->flags
.action
= ACTION_WRITE
;
346 u
->flags
.access
= ACCESS_SEQUENTIAL
;
347 u
->flags
.form
= FORM_FORMATTED
;
348 u
->flags
.status
= STATUS_OLD
;
349 u
->flags
.blank
= BLANK_ZERO
;
350 u
->flags
.position
= POSITION_ASIS
;
352 u
->recl
= options
.default_recl
;
353 u
->endfile
= AT_ENDFILE
;
358 /* Calculate the maximum file offset in a portable manner.
359 * max will be the largest signed number for the type gfc_offset.
361 * set a 1 in the LSB and keep a running sum, stopping at MSB-1 bit. */
364 for (i
=0; i
< sizeof(g
.max_offset
) * 8 - 1; i
++)
365 g
.max_offset
= g
.max_offset
+ ((gfc_offset
) 1 << i
);
370 /* close_unit()-- Close a unit. The stream is closed, and any memory
371 * associated with the stream is freed. Returns nonzero on I/O error. */
374 close_unit (gfc_unit
* u
)
378 for (i
= 0; i
< CACHE_SIZE
; i
++)
379 if (unit_cache
[i
] == u
)
380 unit_cache
[i
] = NULL
;
382 rc
= (u
->s
== NULL
) ? 0 : sclose (u
->s
) == FAILURE
;
391 /* close_units()-- Delete units on completion. We just keep deleting
392 * the root of the treap until there is nothing left. */
397 while (g
.unit_root
!= NULL
)
398 close_unit (g
.unit_root
);