aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Denys Vlasenko <vda.linux@googlemail.com>2020-12-30 01:04:56 +0100
committerGravatar Denys Vlasenko <vda.linux@googlemail.com>2020-12-30 01:04:56 +0100
commit89a55972fd5d7f0ab815c2a62be69a8f34718353 (patch)
treebfd5b6e17a596a3a244ec9996b0df5baf42e1da3
parentb230ff9d8f1a393c13f338b030cace0899d5ec26 (diff)
downloadbusybox-89a55972fd5d7f0ab815c2a62be69a8f34718353.tar.gz
busybox-89a55972fd5d7f0ab815c2a62be69a8f34718353.tar.bz2
libbb/procps: smaller global data for username/groupname cache
function old new delta get_cached 101 133 +32 cache_user_group - 4 +4 get_cached_username 17 14 -3 username 8 - -8 groupname 8 - -8 clear_username_cache 65 47 -18 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 1/2 up/down: 36/-37) Total: -1 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/procps.c45
1 files changed, 17 insertions, 28 deletions
diff --git a/libbb/procps.c b/libbb/procps.c
index 75969947b..f56b71b21 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -21,40 +21,29 @@ typedef struct cache_t {
int size;
} cache_t;
-static cache_t username, groupname;
+static cache_t *cache_user_group;
-static void clear_cache(cache_t *cp)
-{
- free(cp->cache);
- cp->cache = NULL;
- cp->size = 0;
-}
void FAST_FUNC clear_username_cache(void)
{
- clear_cache(&username);
- clear_cache(&groupname);
-}
-
-#if 0 /* more generic, but we don't need that yet */
-/* Returns -N-1 if not found. */
-/* cp->cache[N] is allocated and must be filled in this case */
-static int get_cached(cache_t *cp, uid_t id)
-{
- int i;
- for (i = 0; i < cp->size; i++)
- if (cp->cache[i].id == id)
- return i;
- i = cp->size++;
- cp->cache = xrealloc_vector(cp->cache, 2, i);
- cp->cache[i++].id = id;
- return -i;
+ if (cache_user_group) {
+ free(cache_user_group[0].cache);
+ free(cache_user_group[1].cache);
+ free(cache_user_group);
+ cache_user_group = NULL;
+ }
}
-#endif
-static char* get_cached(cache_t *cp, uid_t id,
+static char* get_cached(int user_group, uid_t id,
char* FAST_FUNC x2x_utoa(uid_t id))
{
+ cache_t *cp;
int i;
+
+ if (!cache_user_group)
+ cache_user_group = xzalloc(sizeof(cache_user_group[0]) * 2);
+
+ cp = &cache_user_group[user_group];
+
for (i = 0; i < cp->size; i++)
if (cp->cache[i].id == id)
return cp->cache[i].name;
@@ -67,11 +56,11 @@ static char* get_cached(cache_t *cp, uid_t id,
}
const char* FAST_FUNC get_cached_username(uid_t uid)
{
- return get_cached(&username, uid, uid2uname_utoa);
+ return get_cached(0, uid, uid2uname_utoa);
}
const char* FAST_FUNC get_cached_groupname(gid_t gid)
{
- return get_cached(&groupname, gid, gid2group_utoa);
+ return get_cached(1, gid, gid2group_utoa);
}