1 /*
2  * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "variable_index_iterator.h"
9 
10 #include <stddef.h>
11 
variable_index_iterator_first(struct variable_index_iterator * iter,const struct variable_index * variable_index)12 void variable_index_iterator_first(struct variable_index_iterator *iter,
13 				   const struct variable_index *variable_index)
14 {
15 	iter->variable_index = variable_index;
16 	iter->current_pos = variable_index->max_variables;
17 
18 	for (size_t pos = 0; pos < variable_index->max_variables; pos++) {
19 		if (variable_index->entries[pos].in_use) {
20 			iter->current_pos = pos;
21 			break;
22 		}
23 	}
24 }
25 
variable_index_iterator_is_done(const struct variable_index_iterator * iter)26 bool variable_index_iterator_is_done(const struct variable_index_iterator *iter)
27 {
28 	return iter->current_pos >= iter->variable_index->max_variables;
29 }
30 
variable_index_iterator_current(const struct variable_index_iterator * iter)31 struct variable_info *variable_index_iterator_current(const struct variable_index_iterator *iter)
32 {
33 	struct variable_info *current = NULL;
34 
35 	if (!variable_index_iterator_is_done(iter)) {
36 		current = &iter->variable_index->entries[iter->current_pos].info;
37 	}
38 
39 	return current;
40 }
41 
variable_index_iterator_next(struct variable_index_iterator * iter)42 void variable_index_iterator_next(struct variable_index_iterator *iter)
43 {
44 	if (iter->current_pos < iter->variable_index->max_variables) {
45 		size_t next_pos = iter->current_pos + 1;
46 
47 		while (next_pos < iter->variable_index->max_variables) {
48 			if (iter->variable_index->entries[next_pos].in_use)
49 				break;
50 
51 			++next_pos;
52 		}
53 
54 		iter->current_pos = next_pos;
55 	}
56 }
57