]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/as-list.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2020 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
19 #include <libloc/as.h>
20 #include <libloc/as-list.h>
21 #include <libloc/compat.h>
22 #include <libloc/private.h>
28 struct loc_as
** elements
;
34 static int loc_as_list_grow(struct loc_as_list
* list
) {
35 size_t size
= list
->elements_size
* 2;
39 DEBUG(list
->ctx
, "Growing AS list %p by %zu to %zu\n",
40 list
, size
, list
->elements_size
+ size
);
42 struct loc_as
** elements
= reallocarray(list
->elements
,
43 list
->elements_size
+ size
, sizeof(*list
->elements
));
47 list
->elements
= elements
;
48 list
->elements_size
+= size
;
53 LOC_EXPORT
int loc_as_list_new(struct loc_ctx
* ctx
,
54 struct loc_as_list
** list
) {
55 struct loc_as_list
* l
= calloc(1, sizeof(*l
));
59 l
->ctx
= loc_ref(ctx
);
62 DEBUG(l
->ctx
, "AS list allocated at %p\n", l
);
68 LOC_EXPORT
struct loc_as_list
* loc_as_list_ref(struct loc_as_list
* list
) {
74 static void loc_as_list_free(struct loc_as_list
* list
) {
75 DEBUG(list
->ctx
, "Releasing AS list at %p\n", list
);
77 loc_as_list_clear(list
);
83 LOC_EXPORT
struct loc_as_list
* loc_as_list_unref(struct loc_as_list
* list
) {
87 if (--list
->refcount
> 0)
90 loc_as_list_free(list
);
94 LOC_EXPORT
size_t loc_as_list_size(struct loc_as_list
* list
) {
98 LOC_EXPORT
int loc_as_list_empty(struct loc_as_list
* list
) {
99 return list
->size
== 0;
102 LOC_EXPORT
void loc_as_list_clear(struct loc_as_list
* list
) {
106 for (unsigned int i
= 0; i
< list
->size
; i
++)
107 loc_as_unref(list
->elements
[i
]);
109 free(list
->elements
);
110 list
->elements
= NULL
;
111 list
->elements_size
= 0;
116 LOC_EXPORT
struct loc_as
* loc_as_list_get(struct loc_as_list
* list
, size_t index
) {
118 if (index
>= list
->size
)
121 return loc_as_ref(list
->elements
[index
]);
124 LOC_EXPORT
int loc_as_list_append(
125 struct loc_as_list
* list
, struct loc_as
* as
) {
126 if (loc_as_list_contains(list
, as
))
129 // Check if we have space left
130 if (list
->size
>= list
->elements_size
) {
131 int r
= loc_as_list_grow(list
);
136 DEBUG(list
->ctx
, "%p: Appending AS %p to list\n", list
, as
);
138 list
->elements
[list
->size
++] = loc_as_ref(as
);
143 LOC_EXPORT
int loc_as_list_contains(
144 struct loc_as_list
* list
, struct loc_as
* as
) {
145 for (unsigned int i
= 0; i
< list
->size
; i
++) {
146 if (loc_as_cmp(as
, list
->elements
[i
]) == 0)
153 LOC_EXPORT
int loc_as_list_contains_number(
154 struct loc_as_list
* list
, uint32_t number
) {
157 int r
= loc_as_new(list
->ctx
, &as
, number
);
161 r
= loc_as_list_contains(list
, as
);
167 static int __loc_as_cmp(const void* as1
, const void* as2
) {
168 return loc_as_cmp(*(struct loc_as
**)as1
, *(struct loc_as
**)as2
);
171 LOC_EXPORT
void loc_as_list_sort(struct loc_as_list
* list
) {
173 qsort(list
->elements
, list
->size
, sizeof(*list
->elements
), __loc_as_cmp
);