dataQueue.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. * Copyright (c) 2023 Gorgon Meducer embedded_zhuroan@hotmail.com
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. */
  28. #define __DATA_QUEUE_CLASS_IMPLEMENT__
  29. #include "dataQueue.h"
  30. #include "PikaPlatform.h"
  31. #include "dataArgs.h"
  32. void queue_init(Queue* queue) {
  33. args_setInt(queue, "__t", 0);
  34. args_setInt(queue, "__b", 0);
  35. }
  36. Queue* New_queue(void) {
  37. Args* args = New_args(NULL);
  38. queue_init(args);
  39. return (Queue*)args;
  40. }
  41. int32_t queue_deinit(Queue* queue) {
  42. Args* args = queue;
  43. args_deinit(args);
  44. return 0;
  45. }
  46. int32_t queue_pushArg(Queue* queue, Arg* arg) {
  47. Args* args = queue;
  48. uint64_t top = args_getInt(args, "__t");
  49. /* add top */
  50. args_setInt(args, "__t", top + 1);
  51. char buff[11];
  52. arg = arg_setName(arg, fast_itoa(buff, top));
  53. return args_setArg(args, arg);
  54. }
  55. Arg* __queue_popArg_noRmoveArg(Queue* queue) {
  56. Args* args = queue;
  57. uint64_t top = args_getInt(args, "__t");
  58. uint64_t bottom = args_getInt(args, "__b");
  59. if (top - bottom < 1) {
  60. return NULL;
  61. }
  62. /* add bottom */
  63. args_setInt(args, "__b", bottom + 1);
  64. char buff[11];
  65. Arg* res = args_getArg(args, fast_itoa(buff, bottom));
  66. /* not deinit arg to keep str buff */
  67. return res;
  68. }
  69. int32_t queue_pushInt(Queue* queue, int val) {
  70. return queue_pushArg(queue, arg_newInt(val));
  71. }
  72. int64_t queue_popInt(Queue* queue) {
  73. return arg_getInt(__queue_popArg_noRmoveArg(queue));
  74. }
  75. int32_t queue_pushFloat(Queue* queue, pika_float val) {
  76. return queue_pushArg(queue, arg_newFloat(val));
  77. }
  78. pika_float queue_popFloat(Queue* queue) {
  79. return arg_getFloat(__queue_popArg_noRmoveArg(queue));
  80. }
  81. int32_t queue_pushStr(Queue* queue, char* str) {
  82. return queue_pushArg(queue, arg_newStr(str));
  83. }
  84. char* queue_popStr(Queue* queue) {
  85. return arg_getStr(__queue_popArg_noRmoveArg(queue));
  86. }
  87. ByteQueue* byteQueue_init(ByteQueue* queue,
  88. void* buffer,
  89. uint_fast16_t size,
  90. pika_bool is_queue_full) {
  91. pika_assert(NULL != queue);
  92. pika_assert(NULL != buffer);
  93. pika_assert(size > 0);
  94. pika_platform_memset(queue, 0, sizeof(ByteQueue));
  95. queue->buffer = buffer;
  96. queue->buffer_size = size;
  97. if (is_queue_full) {
  98. queue->count = size;
  99. queue->peek_count = size;
  100. }
  101. return queue;
  102. }
  103. pika_bool byteQueue_readOne(ByteQueue* queue, uint8_t* byte_ptr) {
  104. pika_assert(NULL != queue);
  105. uint8_t byte;
  106. pika_bool result = pika_false;
  107. /* ------------------atomicity sensitive start---------------- */
  108. do {
  109. if ((queue->head == queue->tail) && (0 == queue->count)) {
  110. /* empty */
  111. break;
  112. }
  113. byte = queue->buffer[queue->head++];
  114. queue->count--;
  115. if (queue->head >= queue->buffer_size) {
  116. queue->head = 0;
  117. }
  118. /* reset peek */
  119. queue->peek_count = queue->count;
  120. queue->peek = queue->head;
  121. if (NULL != byte_ptr) {
  122. *byte_ptr = byte;
  123. }
  124. result = pika_true;
  125. } while (0);
  126. /* ------------------atomicity sensitive end ---------------- */
  127. return result;
  128. }
  129. pika_bool byteQueue_peekOne(ByteQueue* queue, uint8_t* byte_ptr) {
  130. pika_assert(NULL != queue);
  131. uint8_t byte;
  132. pika_bool result = pika_false;
  133. /* ------------------atomicity sensitive start---------------- */
  134. do {
  135. if ((queue->peek == queue->tail) && (0 == queue->peek_count)) {
  136. /* empty */
  137. break;
  138. }
  139. byte = queue->buffer[queue->peek++];
  140. queue->peek_count--;
  141. if (queue->peek >= queue->buffer_size) {
  142. queue->peek = 0;
  143. }
  144. if (NULL != byte_ptr) {
  145. *byte_ptr = byte;
  146. }
  147. result = pika_true;
  148. } while (0);
  149. /* ------------------atomicity sensitive end ---------------- */
  150. return result;
  151. }
  152. void byteQueue_resetPeek(ByteQueue* queue) {
  153. pika_assert(NULL != queue);
  154. /* ------------------atomicity sensitive start---------------- */
  155. queue->peek_count = queue->count;
  156. queue->peek = queue->head;
  157. /* ------------------atomicity sensitive end ---------------- */
  158. }
  159. uint_fast16_t byteQueue_getPeekedNumber(ByteQueue* queue) {
  160. return queue->count - queue->peek_count;
  161. }
  162. uint_fast16_t byteQueue_peekAvailableCount(ByteQueue* queue) {
  163. return queue->peek_count;
  164. }
  165. void byteQueue_dropAllPeeked(ByteQueue* queue) {
  166. pika_assert(NULL != queue);
  167. /* ------------------atomicity sensitive start---------------- */
  168. queue->count = queue->peek_count;
  169. queue->head = queue->peek;
  170. /* ------------------atomicity sensitive end ---------------- */
  171. }
  172. pika_bool byteQueue_writeOne(ByteQueue* queue, uint8_t byte) {
  173. pika_assert(NULL != queue);
  174. pika_bool result = pika_false;
  175. /* ------------------atomicity sensitive start---------------- */
  176. do {
  177. if ((queue->head == queue->tail) && (0 != queue->count)) {
  178. /* full */
  179. break;
  180. }
  181. queue->buffer[queue->tail++] = byte;
  182. queue->count++;
  183. queue->peek_count++;
  184. if (queue->tail >= queue->buffer_size) {
  185. queue->tail = 0;
  186. }
  187. result = pika_true;
  188. } while (0);
  189. /* ------------------atomicity sensitive end ---------------- */
  190. return result;
  191. }