blob: b4a3433334a3941f775ab5cab1dfdadecce12416 [file] [log] [blame]
Song Zhaoc03ba122020-12-23 21:54:02 -08001/*
2 * Copyright (c) 2020 Amlogic, Inc. All rights reserved.
3 *
4 * This source code is subject to the terms and conditions defined in the
5 * file 'LICENSE' which is part of this source code package.
6 *
7 * Description: fifo implementation for single reader single writer
8 * Author: song.zhao@amlogic.com
9 */
10#include <stdbool.h>
11#include <stdlib.h>
12#include <stdio.h>
13#include "aml_avsync.h"
Song Zhao4f632952021-12-16 09:00:18 -080014#include "aml_queue.h"
Song Zhaoc03ba122020-12-23 21:54:02 -080015
16struct queue {
17 int max_len;
18 int ri; //read index
19 int wi; //write index
Song Zhaoc03ba122020-12-23 21:54:02 -080020 void **items;
21};
22
23void* create_q(int max_len)
24{
25 struct queue *q;
26
27 if (max_len <= 0) {
28 printf("%s %d invalid max_len:%d\n",
29 __func__, __LINE__, max_len);
30 return NULL;
31 }
32
33 q = (struct queue*)calloc(1, sizeof(*q));
34 if (!q) {
35 printf("%s %d OOM\n", __func__, __LINE__);
36 return NULL;
37 }
38 q->items = (void **)calloc(max_len, sizeof(void *));
39 if (!q->items) {
40 printf("%s %d OOM\n", __func__, __LINE__);
41 free(q);
42 return NULL;
43 }
44
45 q->max_len = max_len;
46 q->ri = q->wi = 0;
Song Zhaoc03ba122020-12-23 21:54:02 -080047 return q;
48}
49
50void destroy_q(void * queue)
51{
52 struct queue *q = queue;
53
54 if (!q)
55 return;
56 free(q->items);
57 free(q);
58}
59
60int queue_item(void *queue, void * item)
61{
62 struct queue *q = queue;
yongchun.li0ee6e372021-08-20 04:26:04 -070063 int fullness;
Song Zhaoc03ba122020-12-23 21:54:02 -080064
yongchun.li0ee6e372021-08-20 04:26:04 -070065 if (!q)
Song Zhaoc03ba122020-12-23 21:54:02 -080066 return -1;
yongchun.li0ee6e372021-08-20 04:26:04 -070067 fullness = q->wi - q->ri;
68 if (fullness < 0) fullness += q->max_len;
69 if (fullness >= q->max_len - 1)
70 return -1; // not enough space
71
Song Zhaoc03ba122020-12-23 21:54:02 -080072 q->items[q->wi] = item;
73 if (q->wi == q->max_len - 1)
74 q->wi = 0;
75 else
76 q->wi++;
Song Zhaoc03ba122020-12-23 21:54:02 -080077 return 0;
78}
79
80int peek_item(void *queue, void** p_item, uint32_t cnt)
81{
82 struct queue *q = queue;
83 int32_t index;
yongchun.li0ee6e372021-08-20 04:26:04 -070084 int fullness;
Song Zhaoc03ba122020-12-23 21:54:02 -080085
yongchun.li0ee6e372021-08-20 04:26:04 -070086 if (!q)
Song Zhaoc03ba122020-12-23 21:54:02 -080087 return -1;
88
yongchun.li0ee6e372021-08-20 04:26:04 -070089 fullness = q->wi - q->ri;
90 if (fullness < 0) fullness += q->max_len;
91 if (fullness == 0 || fullness <= cnt)
92 return -1; //no enough to peek
Song Zhaoc03ba122020-12-23 21:54:02 -080093 index = q->ri;
94 index += cnt;
95 if (index >= q->max_len)
96 index -= q->max_len;
97 *p_item = q->items[index];
98
99 return 0;
100}
101
102int dqueue_item(void *queue, void** p_item)
103{
104 struct queue *q = queue;
yongchun.li0ee6e372021-08-20 04:26:04 -0700105 int fullness;
Song Zhaoc03ba122020-12-23 21:54:02 -0800106
yongchun.li0ee6e372021-08-20 04:26:04 -0700107 if (!q)
Song Zhaoc03ba122020-12-23 21:54:02 -0800108 return -1;
yongchun.li0ee6e372021-08-20 04:26:04 -0700109 fullness = q->wi - q->ri;
110 if (fullness < 0) fullness += q->max_len;
111 if (fullness == 0)
112 return -1; //empty
Song Zhaoc03ba122020-12-23 21:54:02 -0800113 *p_item = q->items[q->ri];
114 if (q->ri == q->max_len - 1)
115 q->ri = 0;
116 else
117 q->ri++;
Song Zhaoc03ba122020-12-23 21:54:02 -0800118 return 0;
119}
120
121int queue_size(void *queue)
122{
123 struct queue *q = queue;
yongchun.li0ee6e372021-08-20 04:26:04 -0700124 int fullness;
Song Zhaoc03ba122020-12-23 21:54:02 -0800125
126 if (!q)
127 return -1;
yongchun.li0ee6e372021-08-20 04:26:04 -0700128 fullness = q->wi - q->ri;
129 if (fullness < 0) fullness += q->max_len;
130 return fullness;
Song Zhaoc03ba122020-12-23 21:54:02 -0800131}