datastructure.c 5.2 KB

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