rebuild_uname uses {init,add}str to incrementally build a version string, and compare_versions to sort the versions array. Previously those functions were defined as nested functions. This commit turns them into normal functions prefixed with ru_, and also adds ru_finalizestr.
ru_*str use two global variables, ru_{p,end}. Access to those variables is serialized implicitly using the global_lock. * proc/host.c: Turn {init,add}str, compare_versions into normal functions. Add ru_finalizestr. --- proc/host.c | 124 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 53 deletions(-) diff --git a/proc/host.c b/proc/host.c index 488863b..fdaa8ce 100644 --- a/proc/host.c +++ b/proc/host.c @@ -243,54 +243,81 @@ check_dead_execdata_notify (mach_port_t port) char *kernel_name, *kernel_version; +/* ru_initstr, ru_addstr, and ru_finalizestr are string manipulating + functions for rebuild_uname. They are not to be used + elsewhere. */ +static char *ru_p; +static char *ru_end; -/* Rebuild the uname version string. */ +/* Set up for addstr to write into STRING. */ static void -rebuild_uname (void) +ru_initstr (char *string) { - unsigned int i, j; - char *p, *end; + ru_p = string; + ru_end = ru_p + _UTSNAME_LENGTH; +} - /* Set up for addstr to write into STRING. */ - inline void initstr (char *string) - { - p = string; - end = p + _UTSNAME_LENGTH; - } - /* If NAME is not null, write "name-version/", else "version/". */ - inline void addstr (const char *name, const char *version) +/* If NAME is not null, write "name-version/", else "version/". */ +static void +ru_addstr (const char *name, const char *version) +{ + size_t len; + if (name) { - size_t len; - if (name) - { - len = strlen (name); - if (p + len + 1 < end) - memcpy (p, name, len); - p += len; - if (p < end) - *p++ = '-'; - } - len = strlen (version); - if (p + len + 1 < end) - memcpy (p, version, len); - p += len; - if (p < end) - *p++ = '/'; + len = strlen (name); + if (ru_p + len + 1 < ru_end) + memcpy (ru_p, name, len); + ru_p += len; + if (ru_p < ru_end) + *ru_p++ = '-'; } + len = strlen (version); + if (ru_p + len + 1 < ru_end) + memcpy (ru_p, version, len); + ru_p += len; + if (ru_p < ru_end) + *ru_p++ = '/'; +} + +static void +ru_finalizestr (void) +{ + if (ru_p > ru_end) +#ifdef notyet + syslog (LOG_EMERG, + "_UTSNAME_LENGTH %u too short; inform bug-gl...@prep.ai.mit.edu\n", + ru_p - ru_end) +#endif + ; + else + ru_p[-1] = '\0'; + + ru_end[-1] = '\0'; +} + +struct version +{ + const char *version; + unsigned int count; +}; + +static int +compare_versions (const void *a, const void *b) +{ + return (((const struct version *) b)->count - + ((const struct version *) a)->count); +} + +/* Rebuild the uname version string. */ +static void +rebuild_uname (void) +{ + unsigned int i, j; + unsigned int nversions = 0; /* Collect all the differing version strings and count how many servers use each. */ - struct version - { - const char *version; - unsigned int count; - } versions[nserver_versions]; - int compare_versions (const void *a, const void *b) - { - return (((const struct version *) b)->count - - ((const struct version *) a)->count); - } - unsigned int nversions = 0; + struct version versions[nserver_versions]; for (i = 0; i < nserver_versions; ++i) { @@ -316,12 +343,12 @@ rebuild_uname (void) /* release is the most popular version */ strcpy (uname_info.release, versions[0].version); - initstr (uname_info.version); + ru_initstr (uname_info.version); - addstr (kernel_name, kernel_version); + ru_addstr (kernel_name, kernel_version); if (versions[0].count > 1) - addstr ("Hurd", versions[0].version); + ru_addstr ("Hurd", versions[0].version); /* Now, for any which differ (if there might be any), write it out separately. */ @@ -329,18 +356,9 @@ rebuild_uname (void) for (i = 0; i < nserver_versions; i++) if (versions[0].count == 1 || strcmp (server_versions[i].version, versions[0].version)) - addstr (server_versions[i].name, server_versions[i].version); + ru_addstr (server_versions[i].name, server_versions[i].version); - if (p > end) -#ifdef notyet - syslog (LOG_EMERG, - "_UTSNAME_LENGTH %u too short; inform bug-gl...@prep.ai.mit.edu\n", - p - end) -#endif - ; - else - p[-1] = '\0'; - end[-1] = '\0'; + ru_finalizestr (); } void -- 1.7.10.4