iobuf.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stdint.h>
00022 #include <errno.h>
00023 #include <gpxe/malloc.h>
00024 #include <gpxe/iobuf.h>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 struct io_buffer * alloc_iob ( size_t len ) {
00042 struct io_buffer *iobuf = NULL;
00043 void *data;
00044
00045
00046 if ( len < IOB_ZLEN )
00047 len = IOB_ZLEN;
00048
00049
00050 len = ( len + __alignof__( *iobuf ) - 1 ) &
00051 ~( __alignof__( *iobuf ) - 1 );
00052
00053
00054 data = malloc_dma ( len + sizeof ( *iobuf ), IOB_ALIGN );
00055 if ( ! data )
00056 return NULL;
00057
00058 iobuf = ( struct io_buffer * ) ( data + len );
00059 iobuf->head = iobuf->data = iobuf->tail = data;
00060 iobuf->end = iobuf;
00061 return iobuf;
00062 }
00063
00064
00065
00066
00067
00068
00069 void free_iob ( struct io_buffer *iobuf ) {
00070 if ( iobuf ) {
00071 assert ( iobuf->head <= iobuf->data );
00072 assert ( iobuf->data <= iobuf->tail );
00073 assert ( iobuf->tail <= iobuf->end );
00074 free_dma ( iobuf->head,
00075 ( iobuf->end - iobuf->head ) + sizeof ( *iobuf ) );
00076 }
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
00091
00092 if ( iob_headroom ( iobuf ) >= len )
00093 return 0;
00094 return -ENOBUFS;
00095 }
00096