| 151 | } |
| 152 | |
| 153 | pitem *pqueue_insert(pqueue_s *pq, pitem *item) { |
| 154 | pitem *curr, *next; |
| 155 | |
| 156 | if (pq->items == NULL) { |
| 157 | pq->items = item; |
| 158 | return item; |
| 159 | } |
| 160 | |
| 161 | for (curr = NULL, next = pq->items; next != NULL; |
| 162 | curr = next, next = next->next) { |
| 163 | /* we can compare 64-bit value in big-endian encoding with memcmp. */ |
| 164 | int cmp = memcmp(next->priority, item->priority, sizeof(item->priority)); |
| 165 | if (cmp > 0) { |
| 166 | /* next > item */ |
| 167 | item->next = next; |
| 168 | |
| 169 | if (curr == NULL) { |
| 170 | pq->items = item; |
| 171 | } else { |
| 172 | curr->next = item; |
| 173 | } |
| 174 | |
| 175 | return item; |
| 176 | } else if (cmp == 0) { |
| 177 | /* duplicates not allowed */ |
| 178 | return NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | item->next = NULL; |
| 183 | curr->next = item; |
| 184 | |
| 185 | return item; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | pitem *pqueue_pop(pqueue_s *pq) { |
no outgoing calls