]> git.tuebingen.mpg.de Git - adu.git/blobdiff - user.c
Cosmetic cleanups.
[adu.git] / user.c
diff --git a/user.c b/user.c
index 3f368e2a6ed6bcecdf176b23122fcb9cf49af7c8..51510256b9994e9291989177b83edae695775045 100644 (file)
--- a/user.c
+++ b/user.c
@@ -50,8 +50,11 @@ static struct user_info *uid_hash_table;
 /** This is always a power of two. It is set in create_hash_table(). */
 static uint32_t uid_hash_table_size;
 
+/* Array of indices to the entries of \a uid_hash_table. */
+static int *uid_hash_table_sort_idx;
+
 /** The number of used slots in the hash table. */
-static uint32_t num_uids = 0;
+static uint32_t num_uids;
 
 /*
  * The columns of the per-user tables.
@@ -276,11 +279,13 @@ err:
 int for_each_admissible_user(int (*func)(struct user_info *, void *),
                void *data)
 {
-       struct user_info *ui;
+       int i;
 
        assert(uid_hash_table);
-       FOR_EACH_USER(ui) {
+       for (i = 0; i < uid_hash_table_size; i++) {
                int ret;
+               struct user_info *ui = uid_hash_table +
+                       uid_hash_table_sort_idx[i];
 
                if (!ui_used(ui) || !ui_admissible(ui))
                        continue;
@@ -296,9 +301,14 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *),
 
 void create_hash_table(unsigned bits)
 {
+       int i;
+
        uid_hash_table_size = 1 << bits;
        uid_hash_table = adu_calloc(uid_hash_table_size *
                sizeof(struct user_info));
+       uid_hash_table_sort_idx = adu_malloc(uid_hash_table_size * sizeof(int));
+       for (i = 0; i < uid_hash_table_size; i++)
+               uid_hash_table_sort_idx[i] = i;
 }
 
 void close_user_tables(void)
@@ -330,11 +340,13 @@ void close_user_tables(void)
        }
        free(uid_hash_table);
        uid_hash_table = NULL;
+       free(uid_hash_table_sort_idx);
+       uid_hash_table_sort_idx = NULL;
 }
 
 /*
  * We use a hash table of size s=2^uid_hash_bits to map the uids into the
- * interval [0..s]. Hash collisions are treated by open addressing, i.e.
+ * interval [0..s-1]. Hash collisions are treated by open addressing, i.e.
  * unused slots in the table are used to store different uids that hash to the
  * same slot.
  *
@@ -346,7 +358,7 @@ void close_user_tables(void)
  * An odd number is sufficient to make sure each entry of the hash table gets
  * probed for probe_num between 0 and s-1 because s is a power of two, hence
  * the second hash value has never a common divisor with the hash table size.
- * IOW: h is invertible in the ring [0..s].
+ * IOW: h is invertible in the ring [0..s-1].
  */
 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
 {
@@ -387,10 +399,20 @@ static char *get_uid_list_name(void)
        return make_message("%s/uid_list", conf.database_dir_arg);
 }
 
-void sort_hash_table(int (*comp)(const void *, const void *))
+static int (*hash_table_comparator)(struct user_info *a, struct user_info *b);
+
+static int comp_wrapper(const void *a, const void *b)
+{
+       struct user_info *x = uid_hash_table + *(unsigned *)a;
+       struct user_info *y = uid_hash_table + *(unsigned *)b;
+       return hash_table_comparator(x, y);
+}
+
+void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
 {
-       qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
-               comp);
+       hash_table_comparator = comp;
+       qsort(uid_hash_table_sort_idx, uid_hash_table_size,
+               sizeof(*uid_hash_table_sort_idx), comp_wrapper);
 }
 
 int open_admissible_user_tables(struct uid_range *admissible_uids)
@@ -402,9 +424,8 @@ int open_admissible_user_tables(struct uid_range *admissible_uids)
        FOR_EACH_USER(ui) {
                int ret;
 
-               if (!ui_used(ui)) {
+               if (!ui_used(ui))
                        continue;
-               }
                if (!uid_is_admissible(ui->uid, admissible_uids)) {
                        DEBUG_LOG("uid %u is not admissible\n", ui->uid);
                        ui->flags &= ~UI_FL_ADMISSIBLE;