Lesser General Public License for more details.
*/
-#include <errno.h>
#include <stdlib.h>
-#include <loc/as.h>
-#include <loc/as-list.h>
-#include <loc/private.h>
+#include <libloc/as.h>
+#include <libloc/as-list.h>
+#include <libloc/compat.h>
+#include <libloc/private.h>
struct loc_as_list {
struct loc_ctx* ctx;
size_t size;
};
-static int loc_as_list_grow(struct loc_as_list* list, size_t size) {
+static int loc_as_list_grow(struct loc_as_list* list) {
+ size_t size = list->elements_size * 2;
+ if (size < 1024)
+ size = 1024;
+
DEBUG(list->ctx, "Growing AS list %p by %zu to %zu\n",
list, size, list->elements_size + size);
struct loc_as** elements = reallocarray(list->elements,
list->elements_size + size, sizeof(*list->elements));
if (!elements)
- return -errno;
+ return 1;
list->elements = elements;
list->elements_size += size;
struct loc_as_list** list) {
struct loc_as_list* l = calloc(1, sizeof(*l));
if (!l)
- return -ENOMEM;
+ return 1;
l->ctx = loc_ref(ctx);
l->refcount = 1;
loc_as_unref(list->elements[i]);
free(list->elements);
+ list->elements = NULL;
list->elements_size = 0;
list->size = 0;
// Check if we have space left
if (list->size >= list->elements_size) {
- int r = loc_as_list_grow(list, 64);
+ int r = loc_as_list_grow(list);
if (r)
return r;
}
return r;
}
+
+static int __loc_as_cmp(const void* as1, const void* as2) {
+ return loc_as_cmp(*(struct loc_as**)as1, *(struct loc_as**)as2);
+}
+
+LOC_EXPORT void loc_as_list_sort(struct loc_as_list* list) {
+ // Sort everything
+ qsort(list->elements, list->size, sizeof(*list->elements), __loc_as_cmp);
+}