Skip to content

Commit

Permalink
depmod: Use strbuf for dependency output
Browse files Browse the repository at this point in the history
Use shared/strbuf instead of manually re-implementing its features.
Reduces the amount of custom code in depmod, simplifies auditing,
reduces binary size, and has the nice benefit of slightly faster
runtime due to memory reusage.

Signed-off-by: Tobias Stoeckmann <[email protected]>
  • Loading branch information
stoeckmann committed Oct 18, 2024
1 parent d1b98c5 commit 20cc140
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions tools/depmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <shared/array.h>
#include <shared/hash.h>
#include <shared/macro.h>
#include <shared/strbuf.h>
#include <shared/util.h>
#include <shared/scratchbuf.h>

Expand Down Expand Up @@ -2073,6 +2074,7 @@ static int output_deps_bin(struct depmod *depmod, FILE *out)
struct index_node *idx;
size_t i;
struct array array;
struct strbuf sbuf;

if (out == stdout)
return 0;
Expand All @@ -2082,58 +2084,43 @@ static int output_deps_bin(struct depmod *depmod, FILE *out)
return -ENOMEM;

array_init(&array, 8);
strbuf_init(&sbuf);

for (i = 0; i < depmod->modules.count; i++) {
const struct mod *mod = depmod->modules.array[i];
const char *p = mod_get_compressed_path(mod);
char *line;
size_t j, linepos, linelen, slen;
const char *line;
size_t j;
int duplicate;

if (!mod_get_all_sorted_dependencies(mod, &array)) {
ERR("could not get all sorted dependencies of %s\n", p);
continue;
}

linelen = strlen(p) + 1;
for (j = 0; j < array.count; j++) {
const struct mod *d = array.array[j];
linelen += 1 + strlen(mod_get_compressed_path(d));
}

line = malloc(linelen + 1);
if (line == NULL) {
ERR("modules.deps.bin: out of memory\n");
strbuf_clear(&sbuf);
if (!strbuf_pushchars(&sbuf, p) || !strbuf_pushchar(&sbuf, ':')) {
ERR("could not write dependencies of %s\n", p);
continue;
}

linepos = 0;
slen = strlen(p);
memcpy(line + linepos, p, slen);
linepos += slen;
line[linepos] = ':';
linepos++;

for (j = 0; j < array.count; j++) {
const struct mod *d = array.array[j];
const char *dp;

line[linepos] = ' ';
linepos++;
const char *dp = mod_get_compressed_path(d);

dp = mod_get_compressed_path(d);
slen = strlen(dp);
memcpy(line + linepos, dp, slen);
linepos += slen;
if (!strbuf_pushchar(&sbuf, ' ') || !strbuf_pushchars(&sbuf, dp)) {
ERR("could not write dependencies of %s\n", p);
continue;
}
}
line[linepos] = '\0';
line = sbuf.used > 0 ? strbuf_str(&sbuf) : "";

duplicate = index_insert(idx, mod->modname, line, mod->idx);
if (duplicate && depmod->cfg->warn_dups)
WRN("duplicate module deps:\n%s\n", line);
free(line);
}

strbuf_release(&sbuf);
array_free_array(&array);
index_write(idx, out);
index_destroy(idx);
Expand Down

0 comments on commit 20cc140

Please sign in to comment.