From d71458ee72dc019a408ccd80b4f12abc573da287 Mon Sep 17 00:00:00 2001 From: Poul-Henning Kamp Date: Thu, 2 May 1996 08:43:05 +0000 Subject: Cache the result of getpagesize() so we only make one syscall. Use getpagesize instead of CLBYTES. --- lib/libc/gen/getpagesize.c | 20 ++++++++++++++------ lib/libc/gen/opendir.c | 15 +++++---------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/libc/gen/getpagesize.c b/lib/libc/gen/getpagesize.c index d586cf68d5343..556ff9e032daa 100644 --- a/lib/libc/gen/getpagesize.c +++ b/lib/libc/gen/getpagesize.c @@ -38,16 +38,24 @@ static char sccsid[] = "@(#)getpagesize.c 8.1 (Berkeley) 6/4/93"; #include #include +/* + * 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); } diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c index 8cba9cf75390c..a3db45b18d779 100644 --- a/lib/libc/gen/opendir.c +++ b/lib/libc/gen/opendir.c @@ -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; -- cgit v1.2.3