2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: freeFile.c,v 1.9 2000/03/28 08:49:56 simonmar Exp $
12 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
21 /* sigh, the FILEs attached to the standard descriptors are
22 handled differently. We don't want them freed via the
23 ForeignObj finaliser, as we probably want to use these
24 before we *really* shut down (dumping stats etc.)
27 freeStdFile(StgForeignPtr fp)
31 freeStdFileObject(StgForeignPtr ptr)
33 IOFileObject* fo = (IOFileObject*)ptr;
36 /* Don't close the file, just flush the buffer */
37 if (fo != NULL && fo->fd != -1) {
38 if (fo->buf != NULL && (fo->flags & FILEOBJ_WRITE) && fo->bufWPtr > 0) {
39 /* Flush buffer contents */
41 rc = writeBuffer((StgForeignPtr)fo, fo->bufWPtr);
42 } while (rc == FILEOBJ_BLOCKED_WRITE) ;
48 freeFileObject(StgForeignPtr ptr)
51 * The finaliser for the file objects embedded in Handles. The RTS
52 * assumes that the finaliser runs without problems, so all
53 * we can do here is flish buffers + close(), and hope nothing went wrong.
58 IOFileObject* fo = (IOFileObject*)ptr;
63 if ( fo->fd == -1 || (rc = unlockFile(fo->fd)) ) {
64 /* If the file handle has been explicitly closed
65 * (via closeFile()), we will have given
66 * up our process lock, so we break off and just return.
68 if ( fo->buf != NULL ) {
75 if (fo->buf != NULL && fo->bufWPtr > 0) {
76 /* Flush buffer contents before closing underlying file */
77 fo->flags &= ~FILEOBJ_RW_WRITE | ~FILEOBJ_RW_READ;
82 if ( fo->flags & FILEOBJ_WINSOCK )
83 /* Sigh - the cleanup call at the end will do this for us */
85 rc = ( fo->flags & FILEOBJ_WINSOCK ? closesocket(fo->fd) : close(fo->fd) );
89 /* Error or no error, we don't care.. */
91 if ( fo->buf != NULL ) {
100 ref_freeStdFileObject(void)
102 return (StgAddr)&freeStdFileObject;
106 ref_freeFileObject(void)
108 return (StgAddr)&freeFileObject;