fifobuf.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_FIFOBUF_H__
  20. #define __PJ_FIFOBUF_H__
  21. /**
  22. * @file fifobuf.h
  23. * @brief Circular buffer
  24. */
  25. /**
  26. * @defgroup PJ_FIFOBUF Circular buffer
  27. * @ingroup PJ_DS
  28. * @{
  29. */
  30. #include <pj/types.h>
  31. PJ_BEGIN_DECL
  32. /**
  33. * A FIFO buffer provides chunks of memory to the application with first in
  34. * first out policy (or more correctly, first out first in). The fifobuf is
  35. * created by providing it with a fixed buffer. After that, application may
  36. * request chunks of memory from this buffer. When the app is done with a
  37. * chunk of memory, it must return that chunk back to the fifobuf, with the
  38. * requirement that the oldest allocated chunk must be returned first.
  39. */
  40. typedef struct pj_fifobuf_t
  41. {
  42. /** The start of the buffer */
  43. char *first;
  44. /** The end of the buffer */
  45. char *last;
  46. /** The start of used area in the buffer */
  47. char *ubegin;
  48. /** The end of used area in the buffer */
  49. char *uend;
  50. /** Full flag when ubegin==uend */
  51. int full;
  52. } pj_fifobuf_t;
  53. /**
  54. * Initialize the fifobuf by giving it a buffer and size.
  55. *
  56. * @param fb The fifobuf
  57. * @param buffer Buffer to be used to allocate/free chunks of memory from by
  58. * the fifo buffer.
  59. * @param size The size of the buffer.
  60. */
  61. PJ_DECL(void) pj_fifobuf_init(pj_fifobuf_t *fb, void *buffer, unsigned size);
  62. /**
  63. * Returns the capacity (initial size) of the buffer.
  64. *
  65. * @param fb The fifobuf
  66. *
  67. * @return Capacity in bytes.
  68. */
  69. PJ_DECL(unsigned) pj_fifobuf_capacity(pj_fifobuf_t *fb);
  70. /**
  71. * Returns maximum size of memory chunk that can be allocated from the buffer.
  72. *
  73. * @param fb The fifobuf
  74. *
  75. * @return Size in bytes
  76. */
  77. PJ_DECL(unsigned) pj_fifobuf_available_size(pj_fifobuf_t *fb);
  78. /**
  79. * Allocate a chunk of memory from the fifobuf.
  80. *
  81. * @param fb The fifobuf
  82. * @param size Size to allocate
  83. *
  84. * @return Allocated buffer or NULL if the buffer cannot be allocated
  85. */
  86. PJ_DECL(void*) pj_fifobuf_alloc(pj_fifobuf_t *fb, unsigned size);
  87. /**
  88. * Return the space used by the earliest allocated memory chunk back to the
  89. * fifobuf. For example, if app previously allocated ptr0, ptr1, and ptr2
  90. * (in that order), then pj_fifobuf_free() can only be called with ptr0 as
  91. * parameter. Subsequent pj_fifobuf_free() must be called with ptr1, and
  92. * the next one with ptr2, and so on.
  93. *
  94. * @param fb The fifobuf
  95. * @param buf Pointer to memory chunk previously returned by
  96. * pj_fifobuf_alloc()
  97. *
  98. * @return PJ_SUCCESS or the appropriate error.
  99. */
  100. PJ_DECL(pj_status_t) pj_fifobuf_free(pj_fifobuf_t *fb, void *buf);
  101. PJ_END_DECL
  102. /**
  103. * @} // PJ_FIFOBUF group
  104. */
  105. #endif /* __PJ_FIFOBUF_H__ */