sysctl: try to avoid malloc in name2oid

name2oid is called all the time and passed names are almost always very short
(< 16 characters).
This commit is contained in:
mjg 2017-11-11 21:50:36 +00:00
parent 01a8046591
commit 524a7f5fe0

View File

@ -1209,17 +1209,21 @@ sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
int error, oid[CTL_MAXNAME], len = 0;
struct sysctl_oid *op = NULL;
struct rm_priotracker tracker;
char buf[32];
if (!req->newlen)
return (ENOENT);
if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
return (ENAMETOOLONG);
p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
p = buf;
if (req->newlen >= sizeof(buf))
p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
error = SYSCTL_IN(req, p, req->newlen);
if (error) {
free(p, M_SYSCTL);
if (p != buf)
free(p, M_SYSCTL);
return (error);
}
@ -1229,7 +1233,8 @@ sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
error = name2oid(p, oid, &len, &op);
SYSCTL_RUNLOCK(&tracker);
free(p, M_SYSCTL);
if (p != buf)
free(p, M_SYSCTL);
if (error)
return (error);