2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: freeFile.c,v 1.4 1999/05/05 10:33:15 sof Exp $
11 #include "fileObject.h"
18 /* sigh, the FILEs attached to the standard descriptors are
19 handled differently. We don't want them freed via the
20 ForeignObj finaliser, as we probably want to use these
21 before we *really* shut down (dumping stats etc.)
27 void freeStdFileObject(ptr)
30 IOFileObject* fo = (IOFileObject*)ptr;
32 /* Don't close the file, just flush the buffer */
33 if (fo != NULL && fo->fd != -1) {
34 if (fo->buf != NULL && (fo->flags & FILEOBJ_FLUSH) && fo->bufWPtr > 0) {
35 /* Flush buffer contents */
36 writeBuffer((StgForeignPtr)fo, fo->bufWPtr);
41 void freeFileObject(ptr)
45 * The finaliser for the file objects embedded in Handles. The RTS
46 * assumes that the finaliser runs without problems, so all
47 * we can do here is flish buffers + close(), and hope nothing went wrong.
52 IOFileObject* fo = (IOFileObject*)ptr;
57 if ( fo->fd == -1 || (rc = unlockFile(fo->fd)) ) {
58 /* If the file handle has been explicitly closed
59 * (via closeFile()), we will have given
60 * up our process lock, so we break off and just return.
65 if (fo->buf != NULL && fo->bufWPtr > 0) {
66 /* Flush buffer contents before closing underlying file */
67 fo->flags &= ~FILEOBJ_RW_WRITE | ~FILEOBJ_RW_READ;
71 if ( fo->flags & FILEOBJ_WINSOCK )
72 /* Sigh - the cleanup call at the end will do this for us */
75 rc = ( fo->flags & FILEOBJ_WINSOCK ? closesocket(fo->fd) : close(fo->fd) );
79 /* Error or no error, we don't care.. */
84 StgAddr ref_freeStdFileObject(void)
86 return (StgAddr)&freeStdFileObject;
89 StgAddr ref_freeFileObject(void)
91 return (StgAddr)&freeFileObject;