xref: /netbsd-src/lib/libc/gen/getpagesize.c (revision 3d365e7447a96c9a8d60ba350e9a77732c5aede8)
1*3d365e74Schristos /*	$NetBSD: getpagesize.c,v 1.11 2012/06/24 15:26:03 christos Exp $	*/
22c4d3c4cScgd 
3060d0e3dScgd /*
4060d0e3dScgd  * Copyright (c) 1989, 1993
5060d0e3dScgd  *	The Regents of the University of California.  All rights reserved.
6060d0e3dScgd  *
7060d0e3dScgd  * Redistribution and use in source and binary forms, with or without
8060d0e3dScgd  * modification, are permitted provided that the following conditions
9060d0e3dScgd  * are met:
10060d0e3dScgd  * 1. Redistributions of source code must retain the above copyright
11060d0e3dScgd  *    notice, this list of conditions and the following disclaimer.
12060d0e3dScgd  * 2. Redistributions in binary form must reproduce the above copyright
13060d0e3dScgd  *    notice, this list of conditions and the following disclaimer in the
14060d0e3dScgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
16060d0e3dScgd  *    may be used to endorse or promote products derived from this software
17060d0e3dScgd  *    without specific prior written permission.
18060d0e3dScgd  *
19060d0e3dScgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20060d0e3dScgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21060d0e3dScgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22060d0e3dScgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23060d0e3dScgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24060d0e3dScgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25060d0e3dScgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26060d0e3dScgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27060d0e3dScgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28060d0e3dScgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29060d0e3dScgd  * SUCH DAMAGE.
30060d0e3dScgd  */
31060d0e3dScgd 
328b1ce694Schristos #include <sys/cdefs.h>
33060d0e3dScgd #if defined(LIBC_SCCS) && !defined(lint)
342c4d3c4cScgd #if 0
35060d0e3dScgd static char sccsid[] = "@(#)getpagesize.c	8.1 (Berkeley) 6/4/93";
362c4d3c4cScgd #else
37*3d365e74Schristos __RCSID("$NetBSD: getpagesize.c,v 1.11 2012/06/24 15:26:03 christos Exp $");
382c4d3c4cScgd #endif
39060d0e3dScgd #endif /* LIBC_SCCS and not lint */
40060d0e3dScgd 
4143fa6fe3Sjtc #include "namespace.h"
42060d0e3dScgd #include <sys/param.h>
43060d0e3dScgd #include <sys/sysctl.h>
44dc05324fSmatt #include <assert.h>
458b1ce694Schristos #include <unistd.h>
46060d0e3dScgd 
4743fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(getpagesize,_getpagesize)4860549036Smycroft __weak_alias(getpagesize,_getpagesize)
4943fa6fe3Sjtc #endif
5043fa6fe3Sjtc 
51060d0e3dScgd int
52*3d365e74Schristos getpagesize(void)
53060d0e3dScgd {
549c216a1fSpk 	static int pagsz;
551ac05a7aSmycroft 
561ac05a7aSmycroft 	if (pagsz == 0) {
573270e71aSmycroft 		int mib[2];
58060d0e3dScgd 		size_t size;
59060d0e3dScgd 
60060d0e3dScgd 		mib[0] = CTL_HW;
61060d0e3dScgd 		mib[1] = HW_PAGESIZE;
621ac05a7aSmycroft 		size = sizeof pagsz;
633270e71aSmycroft 		if (sysctl(mib, 2, &pagsz, &size, NULL, 0) == -1)
64060d0e3dScgd 			return (-1);
65dc05324fSmatt 		_DIAGASSERT(pagsz);
663270e71aSmycroft 	}
673270e71aSmycroft 	return (pagsz);
68060d0e3dScgd }
69