From f7e2f88c9dfae9e3f41712dd9a29c1bcc56aed37 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Fri, 6 Feb 2004 16:40:10 +0000 Subject: [PATCH] use ensc_vector to cache xid->name mappings git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@822 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/src/lsxid.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/util-vserver/src/lsxid.c b/util-vserver/src/lsxid.c index a3064cd..d6157b6 100644 --- a/util-vserver/src/lsxid.c +++ b/util-vserver/src/lsxid.c @@ -23,6 +23,7 @@ #include "fstool.h" #include "util.h" +#include #include #include @@ -33,6 +34,10 @@ #include #include #include +#include + +#define ENSC_WRAPPERS_STDLIB 1 +#include struct option const CMDLINE_OPTIONS[] = { @@ -43,6 +48,13 @@ CMDLINE_OPTIONS[] = { char const CMDLINE_OPTIONS_SHORT[] = "Radnx"; +struct XidNameMapping { + xid_t xid; + char const * name; +}; + +static struct Vector xid_name_mapping; + void showHelp(int fd, char const *cmd, int res) { @@ -75,6 +87,40 @@ showVersion() void fixupParams(struct Arguments UNUSED * args, int UNUSED argc) { + Vector_init(&xid_name_mapping, sizeof (struct XidNameMapping)); +} + +static int +cmpMap(void const *xid_v, void const *map_v) +{ + xid_t const * const xid = xid_v; + struct XidNameMapping const * const map = map_v; + + return *xid - map->xid; +} + +static char const * +lookupContext(xid_t xid) +{ + struct XidNameMapping *res; + + res = Vector_search(&xid_name_mapping, &xid, cmpMap); + + if (res==0) { + vcCfgStyle style = vcCFG_AUTO; + char const * vcfg = vc_getVserverByCtx(xid, &style, 0); + + res = Vector_insert(&xid_name_mapping, &xid, cmpMap); + if (vcfg) res->name = vc_getVserverName(vcfg, style); + else res->name = 0; + res->xid = xid; + + free(const_cast(char *)(vcfg)); + } + + assert(res!=0); + + return res->name; } static xid_t @@ -100,7 +146,12 @@ handleFile(char const *name, char const *display_name, memset(buf, ' ', sizeof buf); +#if 1 ctx = getFileContext(name, exp_st); +#else +# warning Compiling in debug-code + ctx = random() % 10 + 49213; +#endif if (ctx==VC_NOCTX) { memcpy(buf, "!!ERR!!", 7); @@ -108,14 +159,13 @@ handleFile(char const *name, char const *display_name, need_write = false; } else if (global_args->do_mapping) { - char const * vname = vc_getVserverByCtx(ctx, 0,0); + char const * vname = lookupContext(ctx); if (!vname) buf[0] = '\0'; else { size_t l = strlen(vname); if (l