depmod: simple memory optimization

function                                             old     new   delta
filename2modname                                      67      86     +19
parse_module                                         374     351     -23

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2015-01-24 22:30:30 +01:00
parent 86031a5ffd
commit cc70b6f8b6
2 changed files with 14 additions and 11 deletions

View File

@ -33,7 +33,6 @@ typedef struct module_info {
static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM, static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
void *data, int depth UNUSED_PARAM) void *data, int depth UNUSED_PARAM)
{ {
char modname[MODULE_NAME_LEN];
module_info **first = (module_info **) data; module_info **first = (module_info **) data;
char *image, *ptr; char *image, *ptr;
module_info *info; module_info *info;
@ -51,11 +50,10 @@ static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARA
info->dnext = info->dprev = info; info->dnext = info->dprev = info;
info->name = xstrdup(fname + 2); /* skip "./" */ info->name = xstrdup(fname + 2); /* skip "./" */
info->modname = xstrdup( info->modname = filename2modname(
filename2modname(
bb_get_last_path_component_nostrip(fname), bb_get_last_path_component_nostrip(fname),
modname NULL
)); );
for (ptr = image; ptr < image + len - 10; ptr++) { for (ptr = image; ptr < image + len - 10; ptr++) {
if (strncmp(ptr, "depends=", 8) == 0) { if (strncmp(ptr, "depends=", 8) == 0) {
char *u; char *u;
@ -250,11 +248,12 @@ int depmod_main(int argc UNUSED_PARAM, char **argv)
const char *fname = bb_basename(m->name); const char *fname = bb_basename(m->name);
filename2modname(fname, modname); filename2modname(fname, modname);
while (m->aliases) { while (m->aliases) {
/* Last word can well be m->modname instead, /*
* but depmod from module-init-tools 3.4 * Last word used to be a basename
* uses module basename, i.e., no s/-/_/g. * (filename with path and .ko.* stripped)
* (pathname and .ko.* are still stripped) * at the time of module-init-tools 3.4.
* Mimicking that... */ * kmod v.12 uses module name, i.e., s/-/_/g.
*/
printf("alias %s %s\n", printf("alias %s %s\n",
(char*)llist_pop(&m->aliases), (char*)llist_pop(&m->aliases),
modname); modname);

View File

@ -47,13 +47,14 @@ int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
char* FAST_FUNC filename2modname(const char *filename, char *modname) char* FAST_FUNC filename2modname(const char *filename, char *modname)
{ {
char local_modname[MODULE_NAME_LEN];
int i; int i;
const char *from; const char *from;
if (filename == NULL) if (filename == NULL)
return NULL; return NULL;
if (modname == NULL) if (modname == NULL)
modname = xmalloc(MODULE_NAME_LEN); modname = local_modname;
// Disabled since otherwise "modprobe dir/name" would work // Disabled since otherwise "modprobe dir/name" would work
// as if it is "modprobe name". It is unclear why // as if it is "modprobe name". It is unclear why
// 'basenamization' was here in the first place. // 'basenamization' was here in the first place.
@ -63,6 +64,9 @@ char* FAST_FUNC filename2modname(const char *filename, char *modname)
modname[i] = (from[i] == '-') ? '_' : from[i]; modname[i] = (from[i] == '-') ? '_' : from[i];
modname[i] = '\0'; modname[i] = '\0';
if (modname == local_modname)
return xstrdup(modname);
return modname; return modname;
} }