Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [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" |
| 14 | #include "queue.h" |
| 15 | |
| 16 | struct queue { |
| 17 | int max_len; |
| 18 | int ri; //read index |
| 19 | int wi; //write index |
| 20 | int total_num; |
| 21 | void **items; |
| 22 | }; |
| 23 | |
| 24 | void* create_q(int max_len) |
| 25 | { |
| 26 | struct queue *q; |
| 27 | |
| 28 | if (max_len <= 0) { |
| 29 | printf("%s %d invalid max_len:%d\n", |
| 30 | __func__, __LINE__, max_len); |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | q = (struct queue*)calloc(1, sizeof(*q)); |
| 35 | if (!q) { |
| 36 | printf("%s %d OOM\n", __func__, __LINE__); |
| 37 | return NULL; |
| 38 | } |
| 39 | q->items = (void **)calloc(max_len, sizeof(void *)); |
| 40 | if (!q->items) { |
| 41 | printf("%s %d OOM\n", __func__, __LINE__); |
| 42 | free(q); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | q->max_len = max_len; |
| 47 | q->ri = q->wi = 0; |
| 48 | q->total_num = 0; |
| 49 | return q; |
| 50 | } |
| 51 | |
| 52 | void destroy_q(void * queue) |
| 53 | { |
| 54 | struct queue *q = queue; |
| 55 | |
| 56 | if (!q) |
| 57 | return; |
| 58 | free(q->items); |
| 59 | free(q); |
| 60 | } |
| 61 | |
| 62 | int queue_item(void *queue, void * item) |
| 63 | { |
| 64 | struct queue *q = queue; |
| 65 | |
| 66 | if (!q || q->total_num == q->max_len) |
| 67 | return -1; |
| 68 | q->items[q->wi] = item; |
| 69 | if (q->wi == q->max_len - 1) |
| 70 | q->wi = 0; |
| 71 | else |
| 72 | q->wi++; |
| 73 | q->total_num++; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int peek_item(void *queue, void** p_item, uint32_t cnt) |
| 79 | { |
| 80 | struct queue *q = queue; |
| 81 | int32_t index; |
| 82 | |
| 83 | if (!q || !q->total_num || q->total_num <= cnt) |
| 84 | return -1; |
| 85 | |
| 86 | index = q->ri; |
| 87 | index += cnt; |
| 88 | if (index >= q->max_len) |
| 89 | index -= q->max_len; |
| 90 | *p_item = q->items[index]; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int dqueue_item(void *queue, void** p_item) |
| 96 | { |
| 97 | struct queue *q = queue; |
| 98 | |
| 99 | if (!q || !q->total_num) |
| 100 | return -1; |
| 101 | *p_item = q->items[q->ri]; |
| 102 | if (q->ri == q->max_len - 1) |
| 103 | q->ri = 0; |
| 104 | else |
| 105 | q->ri++; |
| 106 | q->total_num--; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int queue_size(void *queue) |
| 112 | { |
| 113 | struct queue *q = queue; |
| 114 | |
| 115 | if (!q) |
| 116 | return -1; |
| 117 | return q->total_num; |
| 118 | } |