Cache the result of getpagesize() so we only make one syscall.
Use getpagesize instead of CLBYTES.
This commit is contained in:
parent
3a91de068c
commit
d71458ee72
@ -38,16 +38,24 @@ static char sccsid[] = "@(#)getpagesize.c 8.1 (Berkeley) 6/4/93";
|
||||
#include <sys/param.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
/*
|
||||
* This is unlikely to change over the running time of any
|
||||
* program, so we cache the result to save some syscalls.
|
||||
*/
|
||||
|
||||
int
|
||||
getpagesize()
|
||||
{
|
||||
int mib[2], value;
|
||||
int mib[2];
|
||||
static int value;
|
||||
size_t size;
|
||||
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_PAGESIZE;
|
||||
size = sizeof value;
|
||||
if (sysctl(mib, 2, &value, &size, NULL, 0) == -1)
|
||||
return (-1);
|
||||
if (!value) {
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_PAGESIZE;
|
||||
size = sizeof value;
|
||||
if (sysctl(mib, 2, &value, &size, NULL, 0) == -1)
|
||||
return (-1);
|
||||
}
|
||||
return (value);
|
||||
}
|
||||
|
@ -79,18 +79,13 @@ opendir(name)
|
||||
(dirp = malloc(sizeof(DIR))) == NULL)
|
||||
goto fail;
|
||||
/*
|
||||
* If CLBYTES is an exact multiple of DIRBLKSIZ, use a CLBYTES
|
||||
* buffer that it cluster boundary aligned.
|
||||
* Hopefully this can be a big win someday by allowing page trades
|
||||
* to user space to be done by getdirentries()
|
||||
* Use the system page size if that is a multiple of DIRBLKSIZ
|
||||
* this could speed things up in some cases we hope
|
||||
*/
|
||||
if ((CLBYTES % DIRBLKSIZ) == 0) {
|
||||
dirp->dd_buf = malloc(CLBYTES);
|
||||
dirp->dd_len = CLBYTES;
|
||||
} else {
|
||||
dirp->dd_buf = malloc(DIRBLKSIZ);
|
||||
dirp->dd_len = getpagesize();
|
||||
if ((dirp->dd_len % DIRBLKSIZ) != 0)
|
||||
dirp->dd_len = DIRBLKSIZ;
|
||||
}
|
||||
dirp->dd_buf = malloc(dirp->dd_len);
|
||||
if (dirp->dd_buf == NULL)
|
||||
goto fail;
|
||||
dirp->dd_fd = fd;
|
||||
|
Loading…
Reference in New Issue
Block a user