Sylvain Munaut | 3c0a4fb | 2010-11-11 20:40:29 +0100 | [diff] [blame] | 1 | /* Processing Queue Management */ |
| 2 | |
| 3 | /* |
| 4 | * This file is part of gapk (GSM Audio Pocket Knife). |
| 5 | * |
| 6 | * gapk is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * gapk is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with gapk. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include <gapk/procqueue.h> |
| 25 | |
| 26 | |
| 27 | #define MAX_PQ_ITEMS 8 |
| 28 | |
| 29 | struct pq { |
| 30 | int n_items; |
| 31 | struct pq_item* items[MAX_PQ_ITEMS]; |
| 32 | void * buffers[MAX_PQ_ITEMS+1]; |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | struct pq * |
| 37 | pq_create(void) |
| 38 | { |
| 39 | return (struct pq *) calloc(1, sizeof(struct pq)); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | pq_destroy(struct pq *pq) |
| 44 | { |
| 45 | int i; |
| 46 | |
| 47 | if (!pq) |
| 48 | return; |
| 49 | |
| 50 | for (i=0; i<pq->n_items; i++) { |
| 51 | if (!pq->items[i]) |
| 52 | continue; |
| 53 | if (pq->items[i]->exit) |
| 54 | pq->items[i]->exit(pq->items[i]->state); |
| 55 | free(pq->items[i]); |
| 56 | } |
| 57 | |
| 58 | for (i=0; i<pq->n_items-1; i++) |
| 59 | free(pq->buffers[i]); /* free is NULL safe */ |
| 60 | |
| 61 | free(pq); |
| 62 | } |
| 63 | |
| 64 | struct pq_item * |
| 65 | pq_add_item(struct pq *pq) |
| 66 | { |
| 67 | struct pq_item *item; |
| 68 | |
| 69 | if (pq->n_items == MAX_PQ_ITEMS) |
| 70 | return NULL; |
| 71 | |
| 72 | item = calloc(1, sizeof(struct pq_item)); |
| 73 | if (!item) |
| 74 | return NULL; |
| 75 | |
| 76 | pq->items[pq->n_items++] = item; |
| 77 | |
| 78 | return item; |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | pq_prepare(struct pq *pq) |
| 83 | { |
| 84 | int i; |
| 85 | unsigned int len_prev; |
| 86 | |
| 87 | len_prev = 0; |
| 88 | |
| 89 | for (i=0; i<pq->n_items; i++) { |
| 90 | struct pq_item *item = pq->items[i]; |
| 91 | |
| 92 | if (item->len_in != len_prev) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | if (i < (pq->n_items-1)) { |
| 96 | pq->buffers[i] = malloc(item->len_out); |
| 97 | if (!pq->buffers[i]) |
| 98 | return -ENOMEM; |
| 99 | } else{ |
| 100 | if (item->len_out) |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | len_prev = item->len_out; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | pq_execute(struct pq *pq) |
| 112 | { |
| 113 | int i; |
| 114 | void *buf_prev, *buf; |
| 115 | |
| 116 | buf_prev = NULL; |
| 117 | |
| 118 | for (i=0; i<pq->n_items; i++) { |
| 119 | int rv; |
| 120 | struct pq_item *item = pq->items[i]; |
| 121 | |
| 122 | buf = i < (pq->n_items-1) ? pq->buffers[i] : NULL; |
| 123 | |
| 124 | rv = item->proc(item->state, buf, buf_prev); |
| 125 | if (rv) |
| 126 | return rv; |
| 127 | |
| 128 | buf_prev = buf; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |