Line data Source code
1 : /**
2 : * @file gensvm_queue.c
3 : * @author G.J.J. van den Burg
4 : * @date 2016-05-01
5 : * @brief Functions for initializing and freeing a GenQueue
6 : *
7 : * @copyright
8 : Copyright 2016, G.J.J. van den Burg.
9 :
10 : This file is part of GenSVM.
11 :
12 : GenSVM is free software: you can redistribute it and/or modify
13 : it under the terms of the GNU General Public License as published by
14 : the Free Software Foundation, either version 3 of the License, or
15 : (at your option) any later version.
16 :
17 : GenSVM is distributed in the hope that it will be useful,
18 : but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : GNU General Public License for more details.
21 :
22 : You should have received a copy of the GNU General Public License
23 : along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
24 :
25 : */
26 :
27 : #include "gensvm_queue.h"
28 :
29 : /**
30 : * @brief Initialize a GenQueue structure
31 : *
32 : * @details
33 : * A GenQueue structure is initialized and the default value for the
34 : * parameters are set. A pointer to the initialized queue is returned.
35 : *
36 : * @returns initialized GenQueue
37 : */
38 6 : struct GenQueue *gensvm_init_queue(void)
39 : {
40 6 : struct GenQueue *q = Malloc(struct GenQueue, 1);
41 :
42 6 : q->tasks = NULL;
43 6 : q->N = 0;
44 6 : q->i = 0;
45 :
46 6 : return q;
47 : }
48 :
49 : /**
50 : * @brief Free the GenQueue struct
51 : *
52 : * @details
53 : * Freeing the allocated memory of the GenQueue means freeing every GenTask
54 : * struct and then freeing the Queue.
55 : *
56 : * @param[in] q GenQueue to be freed
57 : *
58 : */
59 6 : void gensvm_free_queue(struct GenQueue *q)
60 : {
61 : long i;
62 100 : for (i=0; i<q->N; i++) {
63 94 : gensvm_free_task(q->tasks[i]);
64 : }
65 6 : free(q->tasks);
66 6 : free(q);
67 6 : q = NULL;
68 6 : }
69 :
70 : /**
71 : * @brief Get new GenTask from GenQueue
72 : *
73 : * @details
74 : * Return a pointer to the next GenTask in the GenQueue. If no GenTask
75 : * instances are left, NULL is returned. The internal counter GenQueue::i is
76 : * used for finding the next GenTask.
77 : *
78 : * @param[in] q GenQueue instance
79 : * @returns pointer to next GenTask
80 : *
81 : */
82 4 : struct GenTask *get_next_task(struct GenQueue *q)
83 : {
84 4 : long i = q->i;
85 4 : if (i < q->N) {
86 3 : q->i++;
87 3 : return q->tasks[i];
88 : }
89 1 : return NULL;
90 : }
|