Branch data Line data Source code
1 : : /*
2 : : * libdpkg - Debian packaging suite library routines
3 : : * pkg-queue.c - primitives for pkg queue handling
4 : : *
5 : : * Copyright © 2010 Guillem Jover <guillem@debian.org>
6 : : *
7 : : * This is free software; you can redistribute it and/or modify
8 : : * it under the terms of the GNU General Public License as published by
9 : : * the Free Software Foundation; either version 2 of the License, or
10 : : * (at your option) any later version.
11 : : *
12 : : * This is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : * GNU General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU General Public License
18 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include <config.h>
22 : : #include <compat.h>
23 : :
24 : : #include <stdlib.h>
25 : :
26 : : #include <dpkg/dpkg-db.h>
27 : : #include <dpkg/pkg-queue.h>
28 : :
29 : : /**
30 : : * Initialize a package queue.
31 : : *
32 : : * @param queue The queue to initialize.
33 : : */
34 : : void
35 : 2 : pkg_queue_init(struct pkg_queue *queue)
36 : : {
37 : 2 : queue->head = NULL;
38 : 2 : queue->tail = NULL;
39 : 2 : queue->length = 0;
40 : 2 : }
41 : :
42 : : /**
43 : : * Destroy a package queue.
44 : : *
45 : : * It frees the contained package list and resets the queue members.
46 : : *
47 : : * @param queue The queue to destroy.
48 : : */
49 : : void
50 : 1 : pkg_queue_destroy(struct pkg_queue *queue)
51 : : {
52 : 1 : pkg_list_free(queue->head);
53 : 1 : pkg_queue_init(queue);
54 : 1 : }
55 : :
56 : : /**
57 : : * Check if a package queue is empty.
58 : : *
59 : : * @param queue The queue to check.
60 : : *
61 : : * @return A boolean value.
62 : : */
63 : : int
64 : 8 : pkg_queue_is_empty(struct pkg_queue *queue)
65 : : {
66 : 8 : return (queue->head == NULL);
67 : : }
68 : :
69 : : /**
70 : : * Push a new node containing pkginfo to the tail of the queue.
71 : : *
72 : : * @param queue The queue to insert to.
73 : : * @param pkg The package to use fo the new node.
74 : : *
75 : : * @return The newly inserted pkg_list node.
76 : : */
77 : : struct pkg_list *
78 : 3 : pkg_queue_push(struct pkg_queue *queue, struct pkginfo *pkg)
79 : : {
80 : : struct pkg_list *node;
81 : :
82 : 3 : node = pkg_list_new(pkg, NULL);
83 : :
84 [ + + ]: 3 : if (queue->tail == NULL)
85 : 1 : queue->head = node;
86 : : else
87 : 2 : queue->tail->next = node;
88 : :
89 : 3 : queue->tail = node;
90 : :
91 : 3 : queue->length++;
92 : :
93 : 3 : return node;
94 : : }
95 : :
96 : : /**
97 : : * Pop a node containing pkginfo from the head of the queue.
98 : : *
99 : : * This removes and frees the node from the queue, effectively reducing its
100 : : * size.
101 : : *
102 : : * @param queue The queue to remove from.
103 : : *
104 : : * @return The pkginfo from the removed node, or NULL if the queue was empty.
105 : : */
106 : : struct pkginfo *
107 : 4 : pkg_queue_pop(struct pkg_queue *queue)
108 : : {
109 : : struct pkg_list *node;
110 : : struct pkginfo *pkg;
111 : :
112 [ + + ]: 4 : if (pkg_queue_is_empty(queue))
113 : 1 : return NULL;
114 : :
115 : 3 : node = queue->head;
116 : 3 : pkg = node->pkg;
117 : :
118 : 3 : queue->head = node->next;
119 [ + + ]: 3 : if (queue->head == NULL)
120 : 1 : queue->tail = NULL;
121 : :
122 : 3 : free(node);
123 : 3 : queue->length--;
124 : :
125 : 3 : return pkg;
126 : : }
|