1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
Index: Source/bmalloc/bmalloc/AvailableMemory.cpp
--- Source/bmalloc/bmalloc/AvailableMemory.cpp.orig
+++ Source/bmalloc/bmalloc/AvailableMemory.cpp
@@ -44,11 +44,15 @@
#import <mach/mach_error.h>
#import <math.h>
#elif BOS(UNIX)
+#if BOS(OPENBSD)
+#include <kvm.h>
+#else
#include <sys/sysinfo.h>
+#endif
#if BOS(LINUX)
#include <algorithm>
#include <fcntl.h>
-#elif BOS(FREEBSD)
+#elif BOS(FREEBSD) || BOS(OPENBSD)
#include "VMAllocate.h"
#include <sys/sysctl.h>
#include <sys/types.h>
@@ -164,6 +168,24 @@ static size_t computeAvailableMemory()
if (!sysinfo(&info))
return info.totalram * info.mem_unit;
return availableMemoryGuess;
+#elif BOS(OPENBSD)
+ struct uvmexp uvmexp;
+ size_t length;
+ static int uvmexp_mib [] = { CTL_VM, VM_UVMEXP };
+ long pages;
+ long pageSize = sysconf(_SC_PAGE_SIZE);
+ if (pageSize == -1)
+ return availableMemoryGuess;
+ length = sizeof (uvmexp);
+ if (sysctl(uvmexp_mib, std::size(uvmexp_mib), &uvmexp, &length, NULL, 0) == 0)
+ return uvmexp.npages * pageSize;
+ else {
+ bzero(&uvmexp, sizeof(length));
+ pages = sysconf(_SC_PHYS_PAGES);
+ if (pages == -1)
+ return availableMemoryGuess;
+ return pages * pageSize;
+ }
#elif BOS(UNIX)
long pages = sysconf(_SC_PHYS_PAGES);
long pageSize = sysconf(_SC_PAGE_SIZE);
@@ -178,14 +200,19 @@ static size_t computeAvailableMemory()
size_t availableMemory()
{
static size_t availableMemory;
+#if BOS(OPENBSD)
+ // We always calculate the available memory because we take care...
+ availableMemory = computeAvailableMemory();
+#else
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
availableMemory = computeAvailableMemory();
});
+#endif
return availableMemory;
}
-#if BPLATFORM(IOS_FAMILY) || BOS(LINUX) || BOS(FREEBSD)
+#if BPLATFORM(IOS_FAMILY) || BOS(LINUX) || BOS(FREEBSD) || BOS(OPENBSD)
MemoryStatus memoryStatus()
{
#if BPLATFORM(IOS_FAMILY)
@@ -211,6 +238,22 @@ MemoryStatus memoryStatus()
size_t memoryFootprint = 0;
if (!sysctl(mib, 4, &info, &infolen, nullptr, 0))
memoryFootprint = static_cast<size_t>(info.ki_rssize) * vmPageSize();
+#elif BOS(OPENBSD)
+ size_t memoryFootprint = 0, length;
+ struct kinfo_proc *info;
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid(), sizeof(struct kinfo_proc), 0 };
+
+ if (sysctl(mib, std::size(mib), NULL, &length, NULL, 0) == 0) {
+ info = (struct kinfo_proc *)malloc(length);
+ mib[5] = (length / sizeof(struct kinfo_proc));
+ if (sysctl(mib, std::size(mib), info, &length, NULL, 0) == 0) {
+ memoryFootprint = static_cast<size_t>(
+ info->p_vm_tsize +
+ info->p_vm_dsize +
+ info->p_vm_ssize) * vmPageSize();
+ }
+ free(info);
+ }
#endif
double percentInUse = static_cast<double>(memoryFootprint) / static_cast<double>(availableMemory());
|