Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Zhao | 4f63295 | 2021-12-16 09:00:18 -0800 | [diff] [blame^] | 14 | #include "aml_queue.h" |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 15 | |
| 16 | struct queue { |
| 17 | int max_len; |
| 18 | int ri; //read index |
| 19 | int wi; //write index |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 20 | void **items; |
| 21 | }; |
| 22 | |
| 23 | void* 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 Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 47 | return q; |
| 48 | } |
| 49 | |
| 50 | void 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 | |
| 60 | int queue_item(void *queue, void * item) |
| 61 | { |
| 62 | struct queue *q = queue; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 63 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 64 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 65 | if (!q) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 66 | return -1; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 67 | 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 Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 72 | q->items[q->wi] = item; |
| 73 | if (q->wi == q->max_len - 1) |
| 74 | q->wi = 0; |
| 75 | else |
| 76 | q->wi++; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int peek_item(void *queue, void** p_item, uint32_t cnt) |
| 81 | { |
| 82 | struct queue *q = queue; |
| 83 | int32_t index; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 84 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 85 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 86 | if (!q) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 87 | return -1; |
| 88 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 89 | 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 Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 93 | 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 | |
| 102 | int dqueue_item(void *queue, void** p_item) |
| 103 | { |
| 104 | struct queue *q = queue; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 105 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 106 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 107 | if (!q) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 108 | return -1; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 109 | fullness = q->wi - q->ri; |
| 110 | if (fullness < 0) fullness += q->max_len; |
| 111 | if (fullness == 0) |
| 112 | return -1; //empty |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 113 | *p_item = q->items[q->ri]; |
| 114 | if (q->ri == q->max_len - 1) |
| 115 | q->ri = 0; |
| 116 | else |
| 117 | q->ri++; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int queue_size(void *queue) |
| 122 | { |
| 123 | struct queue *q = queue; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 124 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 125 | |
| 126 | if (!q) |
| 127 | return -1; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 128 | fullness = q->wi - q->ri; |
| 129 | if (fullness < 0) fullness += q->max_len; |
| 130 | return fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 131 | } |