You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iobuf.c 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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.
  17. */
  18. #include <stdint.h>
  19. #include <gpxe/malloc.h>
  20. #include <gpxe/iobuf.h>
  21. /** @file
  22. *
  23. * I/O buffers
  24. *
  25. */
  26. /**
  27. * Allocate I/O buffer
  28. *
  29. * @v len Required length of buffer
  30. * @ret iobuf I/O buffer, or NULL if none available
  31. *
  32. * The I/O buffer will be physically aligned to a multiple of
  33. * @c IOBUF_SIZE.
  34. */
  35. struct io_buffer * alloc_iob ( size_t len ) {
  36. struct io_buffer *iobuf = NULL;
  37. void *data;
  38. /* Pad to minimum length */
  39. if ( len < IOB_ZLEN )
  40. len = IOB_ZLEN;
  41. /* Align buffer length */
  42. len = ( len + __alignof__( *iobuf ) - 1 ) &
  43. ~( __alignof__( *iobuf ) - 1 );
  44. /* Allocate memory for buffer plus descriptor */
  45. data = malloc_dma ( len + sizeof ( *iobuf ), IOB_ALIGN );
  46. if ( ! data )
  47. return NULL;
  48. iobuf = ( struct io_buffer * ) ( data + len );
  49. iobuf->head = iobuf->data = iobuf->tail = data;
  50. iobuf->end = iobuf;
  51. return iobuf;
  52. }
  53. /**
  54. * Free I/O buffer
  55. *
  56. * @v iobuf I/O buffer
  57. */
  58. void free_iob ( struct io_buffer *iobuf ) {
  59. if ( iobuf ) {
  60. assert ( iobuf->head <= iobuf->data );
  61. assert ( iobuf->data <= iobuf->tail );
  62. assert ( iobuf->tail <= iobuf->end );
  63. free_dma ( iobuf->head,
  64. ( iobuf->end - iobuf->head ) + sizeof ( *iobuf ) );
  65. }
  66. }