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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <strings.h>
  21. #include <errno.h>
  22. #include <ipxe/malloc.h>
  23. #include <ipxe/iobuf.h>
  24. /** @file
  25. *
  26. * I/O buffers
  27. *
  28. */
  29. /**
  30. * Allocate I/O buffer
  31. *
  32. * @v len Required length of buffer
  33. * @ret iobuf I/O buffer, or NULL if none available
  34. *
  35. * The I/O buffer will be physically aligned to a multiple of
  36. * @c IOBUF_SIZE.
  37. */
  38. struct io_buffer * alloc_iob ( size_t len ) {
  39. struct io_buffer *iobuf = NULL;
  40. size_t align;
  41. void *data;
  42. /* Pad to minimum length */
  43. if ( len < IOB_ZLEN )
  44. len = IOB_ZLEN;
  45. /* Align buffer length to ensure that struct io_buffer is aligned */
  46. len = ( len + __alignof__ ( *iobuf ) - 1 ) &
  47. ~( __alignof__ ( *iobuf ) - 1 );
  48. /* Align buffer on its own size to avoid potential problems
  49. * with boundary-crossing DMA.
  50. */
  51. align = ( 1 << fls ( len - 1 ) );
  52. /* Allocate memory for buffer plus descriptor */
  53. data = malloc_dma ( len + sizeof ( *iobuf ), align );
  54. if ( ! data )
  55. return NULL;
  56. iobuf = ( struct io_buffer * ) ( data + len );
  57. iobuf->head = iobuf->data = iobuf->tail = data;
  58. iobuf->end = iobuf;
  59. return iobuf;
  60. }
  61. /**
  62. * Free I/O buffer
  63. *
  64. * @v iobuf I/O buffer
  65. */
  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 ) );
  73. }
  74. }
  75. /**
  76. * Ensure I/O buffer has sufficient headroom
  77. *
  78. * @v iobuf I/O buffer
  79. * @v len Required headroom
  80. *
  81. * This function currently only checks for the required headroom; it
  82. * does not reallocate the I/O buffer if required. If we ever have a
  83. * code path that requires this functionality, it's a fairly trivial
  84. * change to make.
  85. */
  86. int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
  87. if ( iob_headroom ( iobuf ) >= len )
  88. return 0;
  89. return -ENOBUFS;
  90. }