datastructure.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2019, Chips&Media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "main_helper.h"
  26. Queue* Queue_Create(
  27. Uint32 itemCount,
  28. Uint32 itemSize
  29. )
  30. {
  31. Queue* queue = NULL;
  32. if ((queue=(Queue *)osal_malloc(sizeof(Queue))) == NULL)
  33. return NULL;
  34. queue->size = itemCount;
  35. queue->itemSize = itemSize;
  36. queue->count = 0;
  37. queue->front = 0;
  38. queue->rear = 0;
  39. queue->buffer = (Uint8*)osal_malloc(itemCount*itemSize);
  40. queue->lock = NULL;
  41. return queue;
  42. }
  43. Queue* Queue_Create_With_Lock(
  44. Uint32 itemCount,
  45. Uint32 itemSize
  46. )
  47. {
  48. Queue* queue = NULL;
  49. if ((queue=(Queue *)osal_malloc(sizeof(Queue))) == NULL)
  50. return NULL;
  51. queue->size = itemCount;
  52. queue->itemSize = itemSize;
  53. queue->count = 0;
  54. queue->front = 0;
  55. queue->rear = 0;
  56. queue->buffer = (Uint8*)osal_malloc(itemCount*itemSize);
  57. queue->lock = osal_mutex_create();
  58. return queue;
  59. }
  60. void Queue_Destroy(
  61. Queue* queue
  62. )
  63. {
  64. if (queue == NULL)
  65. return;
  66. if (queue->buffer)
  67. osal_free(queue->buffer);
  68. if (queue->lock)
  69. osal_mutex_destroy(queue->lock);
  70. osal_free(queue);
  71. }
  72. BOOL Queue_Enqueue(
  73. Queue* queue,
  74. void* data
  75. )
  76. {
  77. Uint8* ptr;
  78. Uint32 offset;
  79. if (queue == NULL) return FALSE;
  80. if (data == NULL) return FALSE;
  81. if (queue->lock)
  82. osal_mutex_lock(queue->lock);
  83. /* Queue is full */
  84. if (queue->count == queue->size) {
  85. if (queue->lock)
  86. osal_mutex_unlock(queue->lock);
  87. return FALSE;
  88. }
  89. offset = queue->rear * queue->itemSize;
  90. ptr = &queue->buffer[offset];
  91. osal_memcpy(ptr, data, queue->itemSize);
  92. queue->rear++;
  93. queue->rear %= queue->size;
  94. queue->count++;
  95. if (queue->lock)
  96. osal_mutex_unlock(queue->lock);
  97. return TRUE;
  98. }
  99. void* Queue_Dequeue(
  100. Queue* queue
  101. )
  102. {
  103. void* data;
  104. Uint32 offset;
  105. if (queue == NULL)
  106. return NULL;
  107. if (queue->lock)
  108. osal_mutex_lock(queue->lock);
  109. /* Queue is empty */
  110. if (queue->count == 0) {
  111. if (queue->lock)
  112. osal_mutex_unlock(queue->lock);
  113. return NULL;
  114. }
  115. offset = queue->front * queue->itemSize;
  116. data = (void*)&queue->buffer[offset];
  117. queue->front++;
  118. queue->front %= queue->size;
  119. queue->count--;
  120. if (queue->lock)
  121. osal_mutex_unlock(queue->lock);
  122. return data;
  123. }
  124. void Queue_Flush(
  125. Queue* queue
  126. )
  127. {
  128. if (queue == NULL)
  129. return;
  130. if (queue->lock)
  131. osal_mutex_lock(queue->lock);
  132. queue->count = 0;
  133. queue->front = 0;
  134. queue->rear = 0;
  135. if (queue->lock)
  136. osal_mutex_unlock(queue->lock);
  137. return;
  138. }
  139. void* Queue_Peek(
  140. Queue* queue
  141. )
  142. {
  143. Uint32 offset;
  144. void* temp;
  145. if (queue == NULL)
  146. return NULL;
  147. if (queue->lock)
  148. osal_mutex_lock(queue->lock);
  149. /* Queue is empty */
  150. if (queue->count == 0) {
  151. if (queue->lock)
  152. osal_mutex_unlock(queue->lock);
  153. return NULL;
  154. }
  155. offset = queue->front * queue->itemSize;
  156. temp = (void*)&queue->buffer[offset];
  157. if (queue->lock)
  158. osal_mutex_unlock(queue->lock);
  159. return temp;
  160. }
  161. Uint32 Queue_Get_Cnt(
  162. Queue* queue
  163. )
  164. {
  165. Uint32 cnt;
  166. if (queue == NULL)
  167. return 0;
  168. if (queue->lock)
  169. osal_mutex_lock(queue->lock);
  170. cnt = queue->count;
  171. if (queue->lock)
  172. osal_mutex_unlock(queue->lock);
  173. return cnt;
  174. }
  175. BOOL Queue_IsFull(
  176. Queue* queue
  177. )
  178. {
  179. if (queue == NULL) {
  180. return FALSE;
  181. }
  182. return (queue->count == queue->size);
  183. }
  184. Queue* Queue_Copy(
  185. Queue* dstQ,
  186. Queue* srcQ
  187. )
  188. {
  189. Queue* queue = NULL;
  190. Uint32 bufferSize;
  191. if (dstQ == NULL) {
  192. if ((queue=(Queue *)osal_malloc(sizeof(Queue))) == NULL)
  193. return NULL;
  194. osal_memset((void*)queue, 0x00, sizeof(Queue));
  195. }
  196. else {
  197. queue = dstQ;
  198. }
  199. bufferSize = srcQ->size * srcQ->itemSize;
  200. queue->size = srcQ->size;
  201. queue->itemSize = srcQ->itemSize;
  202. queue->count = srcQ->count;
  203. queue->front = srcQ->front;
  204. queue->rear = srcQ->rear;
  205. if (queue->buffer) {
  206. osal_free(queue->buffer);
  207. }
  208. queue->buffer = (Uint8*)osal_malloc(bufferSize);
  209. osal_memcpy(queue->buffer, srcQ->buffer, bufferSize);
  210. return queue;
  211. }