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.
21 #include <gpxe/malloc.h>
22 #include <gpxe/iobuf.h>
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
39 struct io_buffer
* alloc_iob ( size_t len
) {
40 struct io_buffer
*iobuf
= NULL
;
43 /* Pad to minimum length */
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
);
56 iobuf
= ( struct io_buffer
* ) ( data
+ len
);
57 iobuf
->head
= iobuf
->data
= iobuf
->tail
= data
;
67 void free_iob ( struct io_buffer
*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
) );
78 * Ensure I/O buffer has sufficient headroom
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
88 int iob_ensure_headroom ( struct io_buffer
*iobuf
, size_t len
) {
90 if ( iob_headroom ( iobuf
) >= len
)