Updated email mdc's email address
[gpxe.git] / src / core / iobuf.c
blobd04ede51479376c5e039021e987e9c48d4847935
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 <gpxe/malloc.h>
21 #include <gpxe/iobuf.h>
23 /** @file
25 * I/O buffers
29 /**
30 * Allocate I/O buffer
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
36 * @c IOBUF_SIZE.
38 struct io_buffer * alloc_iob ( size_t len ) {
39 struct io_buffer *iobuf = NULL;
40 void *data;
42 /* Pad to minimum length */
43 if ( len < IOB_ZLEN )
44 len = IOB_ZLEN;
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 );
52 if ( ! data )
53 return NULL;
55 iobuf = ( struct io_buffer * ) ( data + len );
56 iobuf->head = iobuf->data = iobuf->tail = data;
57 iobuf->end = iobuf;
58 return iobuf;
61 /**
62 * Free I/O buffer
64 * @v iobuf I/O buffer
66 void free_iob ( struct io_buffer *iobuf ) {
67 if ( 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 ) );