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