[comp.sys.amiga.tech] How to close a device

vinsci@abo.fi (Leonard Norrgard) (12/22/88)

  What is the *correct* order to close a device and free buffer memory &
replyports?
  The RKM examples does this in several ways, but my cautious mind says that
one should *first* close the device to prevent access to buffers etc, *then*
free the buffers. It looks like the RKM examples would work in 99% of the
cases...

  Currently, I'm doing this (where ReadRequest is a struct IOExtSer *):

  if (ReadRequest) {
      CloseDevice(ReadRequest);
      DeletePort(ReadRequest->IOSer.io_Message.mn_ReplyPort);
      FreeMem(ReadRequest, sizeof(*ReadRequest));
  }

-- 
Leonard Norrgaard, vinsci@abo.fi, vinsci@finabo.bitnet, +358-21-654474, EET.

dillon@POSTGRES.BERKELEY.EDU (Matt Dillon) (12/24/88)

	You have it correct.  Actually, if the IORequest is not active,
(it had better not be active if you are closing the device!) it doesn't 
really matter whether you deallocate the reply port first because the device
only uses the reply port to return packets.

	A commonly misunderstood problem is that of multiple requests on
the same device.  You allocate two or more requests, OpenDevice() one of
them, then copy it's now initialized contents to the other requests.  For
example if fooling around with the serial.device you most likely want two
IORequests ... one for reading and one for writing.

	When you eventually want to close the device, you must AbortIO()/
WaitIO() any active requests (i.e. all requests must be owned by you), then
close the device using one of the requests (doesn't matter which), then
deallocate the requests.

					-Matt

:  What is the *correct* order to close a device and free buffer memory &
:replyports?
:  The RKM examples does this in several ways, but my cautious mind says that
:one should *first* close the device to prevent access to buffers etc, *then*
:free the buffers. It looks like the RKM examples would work in 99% of the
:cases...
:
:  Currently, I'm doing this (where ReadRequest is a struct IOExtSer *):
:
:  if (ReadRequest) {
:      CloseDevice(ReadRequest);
:      DeletePort(ReadRequest->IOSer.io_Message.mn_ReplyPort);
:      FreeMem(ReadRequest, sizeof(*ReadRequest));
:  }

ewhac@well.UUCP (Leo L. Schwab) (12/25/88)

In article <587@abo.fi> vinsci@abo.fi (Leonard Norrgard) writes:
>  What is the *correct* order to close a device and free buffer memory &
>replyports?
>
>  Currently, I'm doing this (where ReadRequest is a struct IOExtSer *):
>
>  if (ReadRequest) {
>      CloseDevice(ReadRequest);
>      DeletePort(ReadRequest->IOSer.io_Message.mn_ReplyPort);
>      FreeMem(ReadRequest, sizeof(*ReadRequest));
	ReadRequest = NULL;	/*  <----  I'd add this  */
>  }
>
	You may want to add that line in case you plan on re-using the
ReadRequest pointer after FreeMem()ing the memory it points to.  If you do
what I do:

opencrud ()
{
	if (!(librarybase = OpenLibrary(...)))
		die ();
	if (!(ReadRequest = CreateStdReq()))
		die ();
	.
	.
	.
}

closecrud ()
{
	.
	.
	.
	if (ReadRequest)
		/*  Close it  */
	if (librarybase)
		CloseLibrary (librarybase);
}

	...and plan on calling opencrud()/closecrud() multiple times, then
you'll want to set ReadRequest, and all other global pointers, to NULL in
your closecrud() routine.  This is so that, when you call opencrud() again,
all the pointers will start out as NULL, and in case something fails to
open, you won't end up trying to close something that you didn't open
because the pointer was left non-NULL.

	Did this make sense?

_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
Leo L. Schwab -- The Guy in The Cape	INET: well!ewhac@ucbvax.Berkeley.EDU
 \_ -_		Recumbent Bikes:	UUCP: pacbell > !{well,unicom}!ewhac
O----^o	      The Only Way To Fly.	      hplabs / (pronounced "AE-wack")
"Work FOR?  I don't work FOR anybody!  I'm just having fun."  -- The Doctor