iPXE
xferbuf.h
Go to the documentation of this file.
00001 #ifndef _IPXE_XFERBUF_H
00002 #define _IPXE_XFERBUF_H
00003 
00004 /** @file
00005  *
00006  * Data transfer buffer
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/iobuf.h>
00014 #include <ipxe/uaccess.h>
00015 #include <ipxe/interface.h>
00016 #include <ipxe/xfer.h>
00017 
00018 /** A data transfer buffer */
00019 struct xfer_buffer {
00020         /** Data */
00021         void *data;
00022         /** Size of data */
00023         size_t len;
00024         /** Current offset within data */
00025         size_t pos;
00026         /** Data transfer buffer operations */
00027         struct xfer_buffer_operations *op;
00028 };
00029 
00030 /** Data transfer buffer operations */
00031 struct xfer_buffer_operations {
00032         /** Reallocate data buffer
00033          *
00034          * @v xferbuf           Data transfer buffer
00035          * @v len               New length (or zero to free buffer)
00036          * @ret rc              Return status code
00037          */
00038         int ( * realloc ) ( struct xfer_buffer *xferbuf, size_t len );
00039         /** Write data to buffer
00040          *
00041          * @v xferbuf           Data transfer buffer
00042          * @v offset            Starting offset
00043          * @v data              Data to write
00044          * @v len               Length of data
00045          *
00046          * This call is simply a wrapper for the appropriate
00047          * memcpy()-like operation: the caller is responsible for
00048          * ensuring that the write does not exceed the buffer length.
00049          */
00050         void ( * write ) ( struct xfer_buffer *xferbuf, size_t offset,
00051                            const void *data, size_t len );
00052         /** Read data from buffer
00053          *
00054          * @v xferbuf           Data transfer buffer
00055          * @v offset            Starting offset
00056          * @v data              Data to read
00057          * @v len               Length of data
00058          *
00059          * This call is simply a wrapper for the appropriate
00060          * memcpy()-like operation: the caller is responsible for
00061          * ensuring that the read does not exceed the buffer length.
00062          */
00063         void ( * read ) ( struct xfer_buffer *xferbuf, size_t offset,
00064                           void *data, size_t len );
00065 };
00066 
00067 extern struct xfer_buffer_operations xferbuf_malloc_operations;
00068 extern struct xfer_buffer_operations xferbuf_umalloc_operations;
00069 
00070 /**
00071  * Initialise malloc()-based data transfer buffer
00072  *
00073  * @v xferbuf           Data transfer buffer
00074  */
00075 static inline __attribute__ (( always_inline )) void
00076 xferbuf_malloc_init ( struct xfer_buffer *xferbuf ) {
00077         xferbuf->op = &xferbuf_malloc_operations;
00078 }
00079 
00080 /**
00081  * Initialise umalloc()-based data transfer buffer
00082  *
00083  * @v xferbuf           Data transfer buffer
00084  * @v data              User pointer
00085  */
00086 static inline __attribute__ (( always_inline )) void
00087 xferbuf_umalloc_init ( struct xfer_buffer *xferbuf, userptr_t *data ) {
00088         xferbuf->data = data;
00089         xferbuf->op = &xferbuf_umalloc_operations;
00090 }
00091 
00092 extern void xferbuf_free ( struct xfer_buffer *xferbuf );
00093 extern int xferbuf_write ( struct xfer_buffer *xferbuf, size_t offset,
00094                            const void *data, size_t len );
00095 extern int xferbuf_read ( struct xfer_buffer *xferbuf, size_t offset,
00096                           void *data, size_t len );
00097 extern int xferbuf_deliver ( struct xfer_buffer *xferbuf,
00098                              struct io_buffer *iobuf,
00099                              struct xfer_metadata *meta );
00100 
00101 extern struct xfer_buffer * xfer_buffer ( struct interface *intf );
00102 #define xfer_buffer_TYPE( object_type ) \
00103         typeof ( struct xfer_buffer * ( object_type ) )
00104 
00105 #endif /* _IPXE_XFERBUF_H */