您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

iobuf.c 2.4KB

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