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.
20 #include <gpxe/malloc.h>
21 #include <gpxe/iobuf.h>
32 * @v len Required length of buffer
33 * @ret iobuf I/O buffer, or NULL if none available
35 * The I/O buffer will be physically aligned to a multiple of
38 struct io_buffer
* alloc_iob ( size_t len
) {
39 struct io_buffer
*iobuf
= NULL
;
42 /* Pad to minimum length */
46 /* Align buffer length */
47 len
= ( len
+ __alignof__( *iobuf
) - 1 ) &
48 ~( __alignof__( *iobuf
) - 1 );
50 /* Allocate memory for buffer plus descriptor */
51 data
= malloc_dma ( len
+ sizeof ( *iobuf
), IOB_ALIGN
);
55 iobuf
= ( struct io_buffer
* ) ( data
+ len
);
56 iobuf
->head
= iobuf
->data
= iobuf
->tail
= data
;
66 void free_iob ( struct io_buffer
*iobuf
) {
68 assert ( iobuf
->head
<= iobuf
->data
);
69 assert ( iobuf
->data
<= iobuf
->tail
);
70 assert ( iobuf
->tail
<= iobuf
->end
);
71 free_dma ( iobuf
->head
,
72 ( iobuf
->end
- iobuf
->head
) + sizeof ( *iobuf
) );