Directory: | src/ |
---|---|
File: | src/run_queue.c |
Date: | 2023-08-28 07:33:56 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 43 | 60 | 71.7% |
Branches: | 6 | 18 | 33.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com> | ||
3 | * This file is part of the "cimple" project. | ||
4 | * For details, see https://github.com/egor-tensin/cimple. | ||
5 | * Distributed under the MIT License. | ||
6 | */ | ||
7 | |||
8 | #include "run_queue.h" | ||
9 | #include "log.h" | ||
10 | |||
11 | #include <stdlib.h> | ||
12 | #include <string.h> | ||
13 | #include <sys/queue.h> | ||
14 | |||
15 | struct run { | ||
16 | int id; | ||
17 | char *url; | ||
18 | char *rev; | ||
19 | SIMPLEQ_ENTRY(run) entries; | ||
20 | }; | ||
21 | |||
22 | 27540 | int run_create(struct run **_entry, int id, const char *_url, const char *_rev) | |
23 | { | ||
24 | 27540 | struct run *entry = malloc(sizeof(struct run)); | |
25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27540 times.
|
27540 | if (!entry) { |
26 | ✗ | log_errno("malloc"); | |
27 | ✗ | goto fail; | |
28 | } | ||
29 | |||
30 | 27540 | char *url = strdup(_url); | |
31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27540 times.
|
27540 | if (!url) { |
32 | ✗ | log_errno("strdup"); | |
33 | ✗ | goto free_entry; | |
34 | } | ||
35 | |||
36 | 27540 | char *rev = strdup(_rev); | |
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27540 times.
|
27540 | if (!rev) { |
38 | ✗ | log_errno("strdup"); | |
39 | ✗ | goto free_url; | |
40 | } | ||
41 | |||
42 | 27540 | entry->id = id; | |
43 | 27540 | entry->url = url; | |
44 | 27540 | entry->rev = rev; | |
45 | |||
46 | 27540 | *_entry = entry; | |
47 | 27540 | return 0; | |
48 | |||
49 | ✗ | free_url: | |
50 | ✗ | free(url); | |
51 | |||
52 | ✗ | free_entry: | |
53 | ✗ | free(entry); | |
54 | |||
55 | ✗ | fail: | |
56 | ✗ | return -1; | |
57 | } | ||
58 | |||
59 | 27540 | void run_destroy(struct run *entry) | |
60 | { | ||
61 | 27540 | free(entry->rev); | |
62 | 27540 | free(entry->url); | |
63 | 27540 | free(entry); | |
64 | 27540 | } | |
65 | |||
66 | 45900 | int run_get_id(const struct run *entry) | |
67 | { | ||
68 | 45900 | return entry->id; | |
69 | } | ||
70 | |||
71 | 64260 | const char *run_get_url(const struct run *entry) | |
72 | { | ||
73 | 64260 | return entry->url; | |
74 | } | ||
75 | |||
76 | 36720 | const char *run_get_rev(const struct run *entry) | |
77 | { | ||
78 | 36720 | return entry->rev; | |
79 | } | ||
80 | |||
81 | 9180 | void run_set_id(struct run *entry, int id) | |
82 | { | ||
83 | 9180 | entry->id = id; | |
84 | 9180 | } | |
85 | |||
86 | 29 | void run_queue_create(struct run_queue *queue) | |
87 | { | ||
88 | 29 | SIMPLEQ_INIT(queue); | |
89 | 29 | } | |
90 | |||
91 | 29 | void run_queue_destroy(struct run_queue *queue) | |
92 | { | ||
93 | 29 | struct run *entry1 = SIMPLEQ_FIRST(queue); | |
94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | while (entry1) { |
95 | ✗ | struct run *entry2 = SIMPLEQ_NEXT(entry1, entries); | |
96 | ✗ | run_destroy(entry1); | |
97 | ✗ | entry1 = entry2; | |
98 | } | ||
99 | 29 | SIMPLEQ_INIT(queue); | |
100 | 29 | } | |
101 | |||
102 | 27375 | int run_queue_is_empty(const struct run_queue *queue) | |
103 | { | ||
104 | 27375 | return SIMPLEQ_EMPTY(queue); | |
105 | } | ||
106 | |||
107 | ✗ | void run_queue_add_first(struct run_queue *queue, struct run *entry) | |
108 | { | ||
109 | ✗ | SIMPLEQ_INSERT_HEAD(queue, entry, entries); | |
110 | } | ||
111 | |||
112 | 9180 | void run_queue_add_last(struct run_queue *queue, struct run *entry) | |
113 | { | ||
114 | 9180 | SIMPLEQ_INSERT_TAIL(queue, entry, entries); | |
115 | 9180 | } | |
116 | |||
117 | 9180 | struct run *run_queue_remove_first(struct run_queue *queue) | |
118 | { | ||
119 | 9180 | struct run *entry = SIMPLEQ_FIRST(queue); | |
120 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 9106 times.
|
9180 | SIMPLEQ_REMOVE_HEAD(queue, entries); |
121 | 9180 | return entry; | |
122 | } | ||
123 |