From: <mr...@mr...> - 2005-05-16 22:17:56
|
# This is a BitKeeper generated diff -Nru style patch. # # ChangeSet # 2005/05/16 14:01:00+02:00 mr...@fo... = # call module setup function during init scan # = # tools/tc2make # 2005/05/16 14:01:00+02:00 mr...@fo... +6 -0 # new TC2Module keyword 'setup' # = # kernel/request.c # 2005/05/16 14:01:00+02:00 mr...@fo... +2 -2 # update tc2_add_module() usage # = # kernel/kernel.h # 2005/05/16 14:01:00+02:00 mr...@fo... +2 -2 # pass dlopen handle to tc2_add_module # = # kernel/init.c # 2005/05/16 14:01:00+02:00 mr...@fo... +60 -12 # new function tc2_add_export() # call module setup function # = # kernel/free.c # 2005/05/16 14:01:00+02:00 mr...@fo... +4 -4 # free everything # = # include/tc2.h # 2005/05/16 14:01:00+02:00 mr...@fo... +4 -1 # add tc2_add_export() # = diff -Nru a/include/tc2.h b/include/tc2.h --- a/include/tc2.h 2005-05-16 14:01:07 +02:00 +++ b/include/tc2.h 2005-05-16 14:01:07 +02:00 @@ -1,5 +1,5 @@ /** - Copyright (C) 2001, 2002, 2003, 2004 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd + Copyright (C) 2001-2005 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd = Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -87,6 +87,9 @@ = /* Initialize most things, returns 0 on success. */ extern int tc2_init(void); + +/* Add to the module export list */ +extern int tc2_add_export(tc2_module *, char *intf, char *xname, char *iname); = /* Run the main loop. */ extern int tc2_run(void); diff -Nru a/kernel/free.c b/kernel/free.c --- a/kernel/free.c 2005-05-16 14:01:07 +02:00 +++ b/kernel/free.c 2005-05-16 14:01:07 +02:00 @@ -1,5 +1,5 @@ /** - Copyright (C) 2003 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd + Copyright (C) 2003-2005 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd = Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -41,9 +41,7 @@ tc2_module *m =3D p; int i; = - if(m->filename) - free(m->filename); - + free(m->filename); free(m->module->name); free(m->module->description); = @@ -52,6 +50,8 @@ free(m->module->exports[i].xname); free(m->module->exports[i].iname); } + for(i =3D 0; m->module->interfaces[i]; i++) + free(m->module->interfaces[i]); free(m->module->exports); free(m->module->interfaces); free(m->module); diff -Nru a/kernel/init.c b/kernel/init.c --- a/kernel/init.c 2005-05-16 14:01:07 +02:00 +++ b/kernel/init.c 2005-05-16 14:01:07 +02:00 @@ -1,5 +1,5 @@ /** - Copyright (C) 2001, 2002, 2003, 2004 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd + Copyright (C) 2001-2005 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd = Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -90,19 +90,52 @@ } = extern int -tc2_add_module(char *file, tc2_module_info *md) +tc2_add_export(tc2_module *mod, char *intf, char *xname, char *iname) +{ + tc2_module_info *mi =3D mod->module; + int j; + + for(j =3D 0; mi->exports[j].xname; j++); + mi->exports =3D realloc(mi->exports, (j + 2) * sizeof(*mi->exports)); + + mi->exports[j].interface =3D strdup(intf); + mi->exports[j].xname =3D strdup(xname); + mi->exports[j].iname =3D strdup(iname); + + mi->exports[j+1].xname =3D NULL; + mi->exports[j+1].iname =3D NULL; + + for(j =3D 0; mi->interfaces[j] && strcmp(intf, mi->interfaces[j]); j++); + if(!mi->interfaces[j]){ + mi->interfaces =3D + realloc(mi->interfaces, (j + 2) * sizeof(*mi->interfaces)); + mi->interfaces[j] =3D strdup(intf); + mi->interfaces[j+1] =3D NULL; + } + + return 0; +} + +extern int +tc2_add_module(void *handle, char *file, tc2_module_info *md) { int j, k; tc2_module *mod =3D calloc(1, sizeof(tc2_module)); tc2_module *tmp_mod; + tc2_module_info *mi; + char *setupname =3D NULL; + int (*setup)(void); = mod->filename =3D file; = - mod->module =3D calloc(1, sizeof(tc2_module_info)); + mi =3D calloc(1, sizeof(*mi)); + mod->module =3D mi; mod->module->name =3D strdup(md->name); mod->module->version =3D md->version; mod->module->description =3D strdup(md->description); = + md->this_module =3D mod; + /* Count elements in exportlist */ for(j =3D 0; md->exports[j].xname !=3D NULL; j++); = @@ -116,6 +149,8 @@ mod->module->exports[j].interface =3D strdup(md->exports[j].interface); mod->module->exports[j].xname =3D strdup(md->exports[j].xname); mod->module->exports[j].iname =3D strdup(md->exports[j].iname); + if(!strcmp(md->exports[j].xname, "tc2_setup")) + setupname =3D md->exports[j].iname; } = /* Terminate export tclist_t */ @@ -123,10 +158,24 @@ mod->module->exports[j].iname =3D NULL; = for(j =3D 0; md->interfaces[j]; j++); - mod->interfaces =3D calloc(j + 1, sizeof(tc2_interface *)); mod->module->interfaces =3D calloc(j + 1, sizeof(char *)); - for(j =3D 0; md->interfaces[j]; j++){ - char *i =3D md->interfaces[j]; + for(j =3D 0; md->interfaces[j]; j++) + mod->module->interfaces[j] =3D strdup(md->interfaces[j]); + mod->module->interfaces[j] =3D NULL; + + if(setupname){ + setup =3D dlsym(handle, setupname); + if(setup) + setup(); + else + kprint("init", TC2_PRINT_ERROR, "%s\n", dlerror()); + } + + for(j =3D 0; mi->interfaces[j]; j++); + mod->interfaces =3D calloc(j + 1, sizeof(*mod->interfaces)); + + for(j =3D 0; mi->interfaces[j]; j++){ + char *i =3D mi->interfaces[j]; int replace; = for(k =3D 0; interfaces[k] && strcmp(interfaces[k]->name, i); k++); @@ -148,7 +197,6 @@ tclist_push(interfaces[k]->modules, mod); = mod->interfaces[j] =3D interfaces[k]; - mod->module->interfaces[j] =3D interfaces[k]->name; = if(!(replace =3D tchash_search(modules, i, -1, mod, &tmp_mod))){ if(!strcmp(mod->module->name, tmp_mod->module->name)){ @@ -175,10 +223,10 @@ if(replace){ int l; tchash_replace(modules, i, -1, mod, NULL); - for(l =3D 0; md->exports[l].xname !=3D NULL; l++) { - if(!strcmp(md->exports[l].interface, i)){ - char *s =3D MKSYM(md->exports[l].interface, - md->exports[l].xname); + for(l =3D 0; mi->exports[l].xname !=3D NULL; l++) { + if(!strcmp(mi->exports[l].interface, i)){ + char *s =3D MKSYM(mi->exports[l].interface, + mi->exports[l].xname); tchash_replace(avail_syms, s, -1, mod, NULL); free(s); } @@ -321,7 +369,7 @@ continue; } = - tc2_add_module(buf, mod_info); + tc2_add_module(handle, buf, mod_info); = dlclose(handle); free(modlist[k]); diff -Nru a/kernel/kernel.h b/kernel/kernel.h --- a/kernel/kernel.h 2005-05-16 14:01:07 +02:00 +++ b/kernel/kernel.h 2005-05-16 14:01:07 +02:00 @@ -1,5 +1,5 @@ /** - Copyright (C) 2001, 2002, 2003, 2004 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd + Copyright (C) 2001-2005 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd = Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -38,7 +38,7 @@ extern tchash_table_t *avail_syms, *loaded_syms, *modules, *module_names; extern int unload_modules; = -extern int tc2_add_module(char *file, tc2_module_info *md); +extern int tc2_add_module(void *handle, char *file, tc2_module_info *md); = /* Loads a module */ extern int tc2_load_module(tc2_module *module, char *opt); diff -Nru a/kernel/request.c b/kernel/request.c --- a/kernel/request.c 2005-05-16 14:01:07 +02:00 +++ b/kernel/request.c 2005-05-16 14:01:07 +02:00 @@ -1,5 +1,5 @@ /** - Copyright (C) 2001, 2002, 2003 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd + Copyright (C) 2001-2005 Michael Ahlberg, M=C3=A5ns Rullg=C3=A5rd = Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -209,7 +209,7 @@ break; = case TC2_ADD_MODULE: - tc2_add_module(rq->data[0], rq->data[1]); + tc2_add_module(NULL, rq->data[0], rq->data[1]); break; = default: diff -Nru a/tools/tc2make b/tools/tc2make --- a/tools/tc2make 2005-05-16 14:01:07 +02:00 +++ b/tools/tc2make 2005-05-16 14:01:07 +02:00 @@ -84,6 +84,7 @@ my $shutdown; # cleanup function my $init; # generated init function my $ashutdown; # generated shutdown function +my $setup; # setup function my $cmod; # generated C source file my $hmod; # private generated C header file my $version; # version of module @@ -118,6 +119,7 @@ undef $shutdown; undef $init; undef $ashutdown; + undef $setup; undef $cmod; undef $hmod; undef $version; @@ -273,6 +275,8 @@ $postinit =3D $1; } elsif (/^shutdown\s+(.+)$/) { $shutdown =3D $1; + } elsif (/^setup\s+(.+)$/) { + $setup =3D $1; } elsif (/^sources\s+(.+)$/) { push @sources, split /\s+/, $1; } elsif (/^require\s+\"(.+)\"$/) { @@ -540,6 +544,7 @@ push @exports, ([$name, '_tc2_init', $init], [$name, '_tc2_shutdown', $ashutdown]); + $setup and push @exports, [$name, 'tc2_setup', $setup]; = qprint "tc2make: creating $file... "; open C, ">$file.tc2" or die "Cannot open '$file'\n"; @@ -888,6 +893,7 @@ $preinit and print H qq/extern int $preinit(char *);\n/; $postinit and print H qq/extern int $postinit(char *);\n/; $shutdown and print H qq/extern int $shutdown(void);\n/; + $setup and print H qq/extern int $setup(void);\n/; = print_imports \*H, 'extern '; = |