[iobuf] Add iob_disown() and use it where it simplifies code

There are many functions that take ownership of the I/O buffer they
are passed as a parameter.  The caller should not retain a pointer to
the I/O buffer.  Use iob_disown() to automatically nullify the
caller's pointer, e.g.:

    xfer_deliver_iob ( xfer, iob_disown ( iobuf ) );

This will ensure that iobuf is set to NULL for any code after the call
to xfer_deliver_iob().

iob_disown() is currently used only in places where it simplifies the
code, by avoiding an extra line explicitly setting the I/O buffer
pointer to NULL.  It should ideally be used with each call to any
function that takes ownership of an I/O buffer.  (The SSA
optimisations will ensure that use of iob_disown() gets optimised away
in cases where the caller makes no further use of the I/O buffer
pointer anyway.)

If gcc ever introduces an __attribute__((free)), indicating that use
of a function argument after a function call should generate a
warning, then we should use this to identify all applicable function
call sites, and add iob_disown() as necessary.
This commit is contained in:
Michael Brown
2009-02-01 18:02:28 +00:00
parent 0171098212
commit dbe84c5aad
8 changed files with 31 additions and 18 deletions

View File

@@ -199,6 +199,26 @@ static inline void iob_populate ( struct io_buffer *iobuf,
iobuf->end = ( data + max_len );
}
/**
* Disown an I/O buffer
*
* @v iobuf I/O buffer
*
* There are many functions that take ownership of the I/O buffer they
* are passed as a parameter. The caller should not retain a pointer
* to the I/O buffer. Use iob_disown() to automatically nullify the
* caller's pointer, e.g.:
*
* xfer_deliver_iob ( xfer, iob_disown ( iobuf ) );
*
* This will ensure that iobuf is set to NULL for any code after the
* call to xfer_deliver_iob().
*/
#define iob_disown( iobuf ) ( { \
struct io_buffer *__iobuf = (iobuf); \
(iobuf) = NULL; \
__iobuf; } )
extern struct io_buffer * __malloc alloc_iob ( size_t len );
extern void free_iob ( struct io_buffer *iobuf );
extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );