]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Move array implementation from depmode to libkmod-util
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 28 Dec 2011 14:53:37 +0000 (12:53 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 28 Dec 2011 14:58:47 +0000 (12:58 -0200)
Makefile.am
libkmod/libkmod-array.c [new file with mode: 0644]
libkmod/libkmod-array.h [new file with mode: 0644]
tools/kmod-depmod.c

index 9ab679f0a3a42beb8bc3fdb1109a029142e719a6..a68145c860dac50537793b4844fc5bf2095c5c8e 100644 (file)
@@ -42,7 +42,9 @@ LIBKMOD_AGE=1
 
 noinst_LTLIBRARIES = libkmod/libkmod-util.la
 libkmod_libkmod_util_la_SOURCES = libkmod/libkmod-hash.c \
-                                 libkmod/libkmod-hash.h
+                                 libkmod/libkmod-hash.h \
+                                 libkmod/libkmod-array.c \
+                                 libkmod/libkmod-array.h
 
 include_HEADERS = libkmod/libkmod.h
 lib_LTLIBRARIES = libkmod/libkmod.la
diff --git a/libkmod/libkmod-array.c b/libkmod/libkmod-array.c
new file mode 100644 (file)
index 0000000..8e95692
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * libkmod - interface to kernel module operations
+ *
+ * Copyright (C) 2011  ProFUSION embedded systems
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "libkmod.h"
+#include "libkmod-array.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+/* basic pointer array growing in steps */
+
+void array_init(struct array *array, size_t step)
+{
+       assert(step > 0);
+       array->array = NULL;
+       array->count = 0;
+       array->total = 0;
+       array->step = step;
+}
+
+int array_append(struct array *array, const void *element)
+{
+       size_t idx;
+
+       if (array->count + 1 >= array->total) {
+               size_t new_total = array->total + array->step;
+               void *tmp = realloc(array->array, sizeof(void *) * new_total);
+               assert(array->step > 0);
+               if (tmp == NULL)
+                       return -ENOMEM;
+               array->array = tmp;
+               array->total = new_total;
+       }
+       idx = array->count;
+       array->array[idx] = (void *)element;
+       array->count++;
+       return idx;
+}
+
+int array_append_unique(struct array *array, const void *element)
+{
+       void **itr = array->array;
+       void **itr_end = itr + array->count;
+       for (; itr < itr_end; itr++)
+               if (*itr == element)
+                       return -EEXIST;
+       return array_append(array, element);
+}
+
+void array_pop(struct array *array) {
+       array->count--;
+       if (array->count + array->step < array->total) {
+               size_t new_total = array->total - array->step;
+               void *tmp = realloc(array->array, sizeof(void *) * new_total);
+               assert(array->step > 0);
+               if (tmp == NULL)
+                       return;
+               array->array = tmp;
+               array->total = new_total;
+       }
+}
+
+void array_free_array(struct array *array) {
+       free(array->array);
+       array->count = 0;
+       array->total = 0;
+}
+
+
+void array_sort(struct array *array, int (*cmp)(const void *a, const void *b))
+{
+       qsort(array->array, array->count, sizeof(void *), cmp);
+}
diff --git a/libkmod/libkmod-array.h b/libkmod/libkmod-array.h
new file mode 100644 (file)
index 0000000..0cbf65b
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef _LIBKMOD_ARRAY_H_
+#define _LIBKMOD_ARRAY_H_
+
+/*
+ * Declaration of struct array is in header because we may want to embed the
+ * structure into another, so we need to know its size
+ */
+struct array {
+       void **array;
+       size_t count;
+       size_t total;
+       size_t step;
+};
+
+void array_init(struct array *array, size_t step);
+int array_append(struct array *array, const void *element);
+int array_append_unique(struct array *array, const void *element);
+void array_pop(struct array *array);
+void array_free_array(struct array *array);
+void array_sort(struct array *array, int (*cmp)(const void *a, const void *b));
+
+#endif
index 84bbced40e531919a578fb85eb54036bc470f678..b87222992afd29c53c7fb227f14be10771232160 100644 (file)
@@ -33,6 +33,7 @@
 #include <ctype.h>
 #include "libkmod.h"
 #include "libkmod-hash.h"
+#include "libkmod-array.h"
 
 #define streq(a, b) (strcmp(a, b) == 0)
 #define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0)
@@ -584,77 +585,6 @@ static void index_write(const struct index_node *node, FILE *out)
 /* END: code from module-init-tools/index.c just modified to compile here.
  */
 
-/* basic pointer array growing in steps  ******************************/
-struct array {
-       void **array;
-       size_t count;
-       size_t total;
-       size_t step;
-};
-
-static void array_init(struct array *array, size_t step)
-{
-       assert(step > 0);
-       array->array = NULL;
-       array->count = 0;
-       array->total = 0;
-       array->step = step;
-}
-
-static int array_append(struct array *array, const void *element)
-{
-       size_t idx;
-
-       if (array->count + 1 >= array->total) {
-               size_t new_total = array->total + array->step;
-               void *tmp = realloc(array->array, sizeof(void *) * new_total);
-               assert(array->step > 0);
-               if (tmp == NULL)
-                       return -ENOMEM;
-               array->array = tmp;
-               array->total = new_total;
-       }
-       idx = array->count;
-       array->array[idx] = (void *)element;
-       array->count++;
-       return idx;
-}
-
-static int array_append_unique(struct array *array, const void *element)
-{
-       void **itr = array->array;
-       void **itr_end = itr + array->count;
-       for (; itr < itr_end; itr++)
-               if (*itr == element)
-                       return -EEXIST;
-       return array_append(array, element);
-}
-
-static void array_pop(struct array *array) {
-       array->count--;
-       if (array->count + array->step < array->total) {
-               size_t new_total = array->total - array->step;
-               void *tmp = realloc(array->array, sizeof(void *) * new_total);
-               assert(array->step > 0);
-               if (tmp == NULL)
-                       return;
-               array->array = tmp;
-               array->total = new_total;
-       }
-}
-
-static void array_free_array(struct array *array) {
-       free(array->array);
-       array->count = 0;
-       array->total = 0;
-}
-
-
-static void array_sort(struct array *array, int (*cmp)(const void *a, const void *b))
-{
-       qsort(array->array, array->count, sizeof(void *), cmp);
-}
-
 /* utils (similar to libkmod-utils.c) *********************************/
 static const char *underscores(const char *input, char *output, size_t outputlen)
 {