xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/unix/meminfo.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1*4afad4b7Schristos /*	$NetBSD: meminfo.c,v 1.1 2024/02/18 20:57:57 christos Exp $	*/
2*4afad4b7Schristos 
3*4afad4b7Schristos /*
4*4afad4b7Schristos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5*4afad4b7Schristos  *
6*4afad4b7Schristos  * SPDX-License-Identifier: MPL-2.0
7*4afad4b7Schristos  *
8*4afad4b7Schristos  * This Source Code Form is subject to the terms of the Mozilla Public
9*4afad4b7Schristos  * License, v. 2.0. If a copy of the MPL was not distributed with this
10*4afad4b7Schristos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11*4afad4b7Schristos  *
12*4afad4b7Schristos  * See the COPYRIGHT file distributed with this work for additional
13*4afad4b7Schristos  * information regarding copyright ownership.
14*4afad4b7Schristos  */
15*4afad4b7Schristos 
16*4afad4b7Schristos #include <inttypes.h>
17*4afad4b7Schristos #include <unistd.h>
18*4afad4b7Schristos 
19*4afad4b7Schristos #include <isc/meminfo.h>
20*4afad4b7Schristos #if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__)
21*4afad4b7Schristos #include <sys/sysctl.h>
22*4afad4b7Schristos #endif /* if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) */
23*4afad4b7Schristos 
24*4afad4b7Schristos uint64_t
isc_meminfo_totalphys(void)25*4afad4b7Schristos isc_meminfo_totalphys(void) {
26*4afad4b7Schristos #if defined(CTL_HW) && (defined(HW_PHYSMEM64) || defined(HW_MEMSIZE))
27*4afad4b7Schristos 	int mib[2];
28*4afad4b7Schristos 	mib[0] = CTL_HW;
29*4afad4b7Schristos #if defined(HW_MEMSIZE)
30*4afad4b7Schristos 	mib[1] = HW_MEMSIZE;
31*4afad4b7Schristos #elif defined(HW_PHYSMEM64)
32*4afad4b7Schristos 	mib[1] = HW_PHYSMEM64;
33*4afad4b7Schristos #endif /* if defined(HW_MEMSIZE) */
34*4afad4b7Schristos 	uint64_t size = 0;
35*4afad4b7Schristos 	size_t len = sizeof(size);
36*4afad4b7Schristos 	if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) {
37*4afad4b7Schristos 		return (size);
38*4afad4b7Schristos 	}
39*4afad4b7Schristos #endif /* if defined(CTL_HW) && (defined(HW_PHYSMEM64) || defined(HW_MEMSIZE)) \
40*4afad4b7Schristos 	* */
41*4afad4b7Schristos #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
42*4afad4b7Schristos 	long pages = sysconf(_SC_PHYS_PAGES);
43*4afad4b7Schristos 	long pagesize = sysconf(_SC_PAGESIZE);
44*4afad4b7Schristos 
45*4afad4b7Schristos 	if (pages == -1 || pagesize == -1) {
46*4afad4b7Schristos 		return (0);
47*4afad4b7Schristos 	}
48*4afad4b7Schristos 
49*4afad4b7Schristos 	return ((size_t)pages * pagesize);
50*4afad4b7Schristos #endif /* if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) */
51*4afad4b7Schristos 	return (0);
52*4afad4b7Schristos }
53