[sundance] Add reset completion check
[gpxe.git] / src / core / iobuf.c
blobcc4aedeae5d88ab1c687087a286bc2ecb45a068d
1 /*
2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stdint.h>
20 #include <errno.h>
21 #include <gpxe/malloc.h>
22 #include <gpxe/iobuf.h>
24 /** @file
26 * I/O buffers
30 /**
31 * Allocate I/O buffer
33 * @v len Required length of buffer
34 * @ret iobuf I/O buffer, or NULL if none available
36 * The I/O buffer will be physically aligned to a multiple of
37 * @c IOBUF_SIZE.
39 struct io_buffer * alloc_iob ( size_t len ) {
40 struct io_buffer *iobuf = NULL;
41 void *data;
43 /* Pad to minimum length */
44 if ( len < IOB_ZLEN )
45 len = IOB_ZLEN;
47 /* Align buffer length */
48 len = ( len + __alignof__( *iobuf ) - 1 ) &
49 ~( __alignof__( *iobuf ) - 1 );
51 /* Allocate memory for buffer plus descriptor */
52 data = malloc_dma ( len + sizeof ( *iobuf ), IOB_ALIGN );
53 if ( ! data )
54 return NULL;
56 iobuf = ( struct io_buffer * ) ( data + len );
57 iobuf->head = iobuf->data = iobuf->tail = data;
58 iobuf->end = iobuf;
59 return iobuf;
62 /**
63 * Free I/O buffer
65 * @v iobuf I/O buffer
67 void free_iob ( struct io_buffer *iobuf ) {
68 if ( iobuf ) {
69 assert ( iobuf->head <= iobuf->data );
70 assert ( iobuf->data <= iobuf->tail );
71 assert ( iobuf->tail <= iobuf->end );
72 free_dma ( iobuf->head,
73 ( iobuf->end - iobuf->head ) + sizeof ( *iobuf ) );
77 /**
78 * Ensure I/O buffer has sufficient headroom
80 * @v iobuf I/O buffer
81 * @v len Required headroom
83 * This function currently only checks for the required headroom; it
84 * does not reallocate the I/O buffer if required. If we ever have a
85 * code path that requires this functionality, it's a fairly trivial
86 * change to make.
88 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
90 if ( iob_headroom ( iobuf ) >= len )
91 return 0;
92 return -ENOBUFS;