| 1 | /* |
| 2 | * Copyright 2018 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Jan Černý <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include "util.h" |
| 31 | #include "oscap_queue.h" |
| 32 | |
| 33 | struct oscap_queue_item { |
| 34 | void *data; |
| 35 | struct oscap_queue_item *next; |
| 36 | }; |
| 37 | |
| 38 | struct oscap_queue { |
| 39 | struct oscap_queue_item *begin; |
| 40 | struct oscap_queue_item *end; |
| 41 | }; |
| 42 | |
| 43 | struct oscap_queue *oscap_queue_new() |
| 44 | { |
| 45 | struct oscap_queue *queue = malloc(sizeof(struct oscap_queue)); |
| 46 | queue->begin = NULL; |
| 47 | queue->end = NULL; |
| 48 | return queue; |
| 49 | } |
| 50 | |
| 51 | void oscap_queue_add(struct oscap_queue *queue, void *data) |
| 52 | { |
| 53 | struct oscap_queue_item *temp = malloc(sizeof(struct oscap_queue_item)); |
| 54 | temp->data = data; |
| 55 | temp->next = NULL; |
| 56 | if (queue->begin == NULL) { |
| 57 | queue->begin = temp; |
| 58 | } else { |
| 59 | queue->end->next = temp; |
| 60 | } |
| 61 | queue->end = temp; |
| 62 | } |
| 63 | |
| 64 | void *oscap_queue_remove(struct oscap_queue *queue) |
| 65 | { |
| 66 | if (queue->begin == NULL) { |
| 67 | return NULL; |
| 68 | } |
| 69 | void *data = queue->begin->data; |
| 70 | struct oscap_queue_item *temp = queue->begin; |
| 71 | if (queue->begin == queue->end) { |
| 72 | queue->end = NULL; |
| 73 | } |
| 74 | queue->begin = queue->begin->next; |
| 75 | free(temp); |
| 76 | return data; |
| 77 | } |
| 78 | |
| 79 | void oscap_queue_free(struct oscap_queue *queue, oscap_destruct_func destructor) |
| 80 | { |
| 81 | if (queue == NULL) { |
| 82 | return; |
| 83 | } |
| 84 | struct oscap_queue_item *current, *temp; |
| 85 | current = queue->begin; |
| 86 | while (current != NULL) { |
| 87 | temp = current; |
| 88 | current = current->next; |
| 89 | if (destructor != NULL) { |
| 90 | free(temp->data); |
| 91 | } |
| 92 | free(temp); |
| 93 | } |
| 94 | free(queue); |
| 95 | } |