Skip to content

Commit

Permalink
depmod: Read modules.order only once
Browse files Browse the repository at this point in the history
The current code iterates through modules.order twice. First, it
figures out how many lines exist. Then it iterates again to make
sure that the first line has the lowest sort index, then ascending.

Negative values make sure that the previously assigned positive
values will be larger, i.e. modules in modules.order take precedence,
then modules found in file system which were not listed in
modules.order.

This can be simplified by setting the initial sort index value to
the lowest possible value needed, i.e. -depmod->modules.count.

With this value, it is possible to iterate only once.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Reviewed-by: Emil Velikov <[email protected]>
Link: #158
Signed-off-by: Lucas De Marchi <[email protected]>
  • Loading branch information
stoeckmann authored and lucasdemarchi committed Oct 1, 2024
1 parent d070b10 commit 19c0df1
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions tools/depmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -1463,39 +1463,30 @@ static void depmod_modules_sort(struct depmod *depmod)
char line[PATH_MAX];
const char *order_file = "modules.order";
FILE *fp;
unsigned idx = 0, total = 0;
size_t idx = 0;
// all sorted modules shall have precedence
int i = -(int)depmod->modules.count;

fp = dfdopen(depmod->cfg->dirname, order_file, O_RDONLY, "r");
if (fp == NULL)
return;

while (fgets(line, sizeof(line), fp) != NULL) {
struct mod *mod;
size_t len = strlen(line);
idx++;
if (len == 0)
continue;
if (line[len - 1] != '\n') {
ERR("%s/%s:%u corrupted line misses '\\n'\n",
ERR("%s/%s:%zu corrupted line misses '\\n'\n",
depmod->cfg->dirname, order_file, idx);
goto corrupted;
}
}
total = idx + 1;
idx = 0;
fseek(fp, 0, SEEK_SET);
while (fgets(line, sizeof(line), fp) != NULL) {
size_t len = strlen(line);
struct mod *mod;

idx++;
if (len == 0)
continue;
line[len - 1] = '\0';

mod = hash_find(depmod->modules_by_uncrelpath, line);
if (mod == NULL)
continue;
mod->sort_idx = idx - total;
mod->sort_idx = i++;
}

array_sort(&depmod->modules, mod_cmp);
Expand Down

0 comments on commit 19c0df1

Please sign in to comment.