zero_copy_queue.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #ifndef ZERO_COPY_QUEUE_H_
  39. #define ZERO_COPY_QUEUE_H_
  40. /**
  41. * Handle for ZeroCopyQueues.
  42. *
  43. * These queues only copy a single pointer instead of the
  44. * entire data structure, which is how FreeRTOS works by default.
  45. */
  46. typedef void * ZeroCopyQueue_t;
  47. /**
  48. * Create a Zero Copy Queue.
  49. *
  50. * @param itemSize Maximum size of the item.
  51. * @param itemCount What's the maximum number of items allowed?
  52. * @param Alignment Power of 2 value denoting on which address boundary the
  53. * memory will be aligned to. Must be at least sizeof(unsigned char *).
  54. * @return A Handle to the ZeroCopyQueue, or NULL on failure.
  55. */
  56. ZeroCopyQueue_t ZcqCreateQueue( int itemSize,
  57. int itemCount,
  58. int Alignment);
  59. /**
  60. * Allocate an item to use or queue.
  61. *
  62. * Note that this can block, and cannnot be used from ISR context.
  63. *
  64. * @param zcq A handle to a ZeroCopyQueue_t.
  65. * @return A pointer or NULL on failure.
  66. */
  67. void *ZcqAllocateItem(ZeroCopyQueue_t zcq);
  68. /**
  69. * Free a previously allocated item back into the base pool.
  70. *
  71. * @note This can block, and cannnot be used from ISR context.
  72. * @note There is no check that the memory passed in is valid.
  73. *
  74. * @param zcq A handle to a ZeroCopyQueue_t.
  75. * @param item An item obtained from ZcqAllocateItem().
  76. */
  77. void ZcqFreeItem(ZeroCopyQueue_t zcq, void *item);
  78. /**
  79. * Queue an item obtained from ZcqAllocateItem().
  80. *
  81. * @param zcq A handle to a ZeroCopyQueue_t.
  82. * @param item An item obtained from ZcqAllocateItem().
  83. * @param Timeout Timeout in FreeRTOS ticks.
  84. * @return 1 on success, 0 on failure.
  85. */
  86. int ZcqEnqueueItem(ZeroCopyQueue_t zcq, void *item, TickType_t Timeout);
  87. /**
  88. * Dequeue an item from ZcqEnqueueItem().
  89. *
  90. * @param zcq A handle to a ZeroCopyQueue_t.
  91. * @param Timeout Timeout in FreeRTOS ticks.
  92. * @return An item obtained from ZcqAllocateItem() on success.
  93. * NULL on failure.
  94. */
  95. void *ZcqDequeueItem(ZeroCopyQueue_t zcq, TickType_t Timeout);
  96. #endif