Extend struct ucred with group table.

This saves one malloc + free with typical cases and better utilizes
memory.

Submitted by:	Tiwei Bie <btw mail.ustc.edu.cn> (slightly modified)
X-Additional:	JuniorJobs project
This commit is contained in:
Mateusz Guzik 2014-11-05 02:08:37 +00:00
parent 492b82715f
commit a99500a912
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=274122
2 changed files with 9 additions and 5 deletions

View file

@ -1817,7 +1817,9 @@ crget(void)
#ifdef MAC
mac_cred_init(cr);
#endif
crextend(cr, XU_NGROUPS);
cr->cr_groups = cr->cr_smallgroups;
cr->cr_agroups =
sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
return (cr);
}
@ -1864,7 +1866,8 @@ crfree(struct ucred *cr)
#ifdef MAC
mac_cred_destroy(cr);
#endif
free(cr->cr_groups, M_CRED);
if (cr->cr_groups != cr->cr_smallgroups)
free(cr->cr_groups, M_CRED);
free(cr, M_CRED);
}
}
@ -1997,7 +2000,7 @@ crextend(struct ucred *cr, int n)
cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
/* Free the old array. */
if (cr->cr_groups)
if (cr->cr_groups != cr->cr_smallgroups)
free(cr->cr_groups, M_CRED);
cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);

View file

@ -37,6 +37,8 @@
struct loginclass;
#define XU_NGROUPS 16
/*
* Credentials.
*
@ -64,13 +66,12 @@ struct ucred {
struct auditinfo_addr cr_audit; /* Audit properties. */
gid_t *cr_groups; /* groups */
int cr_agroups; /* Available groups */
gid_t cr_smallgroups[XU_NGROUPS]; /* storage for small groups */
};
#define NOCRED ((struct ucred *)0) /* no credential available */
#define FSCRED ((struct ucred *)-1) /* filesystem credential */
#endif /* _KERNEL || _WANT_UCRED */
#define XU_NGROUPS 16
/*
* Flags for cr_flags.
*/