2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "utils/MemUtils.h"
16 #include <mach/mach.h>
17 #include <sys/sysctl.h>
18 #include <sys/types.h>
26 void* AlignedMalloc(size_t s
, size_t alignTo
)
29 posix_memalign(&p
, alignTo
, s
);
34 void AlignedFree(void* p
)
39 void GetMemoryStatus(MemoryStatus
* buffer
)
45 size_t len
= sizeof physmem
;
47 #if defined(__apple_build_version__) && __apple_build_version__ < 10000000
48 #pragma clang diagnostic push
49 #pragma clang diagnostic ignored "-Wmissing-braces"
51 std::array
<int, 2> mib
=
56 #if defined(__apple_build_version__) && __apple_build_version__ < 10000000
57 #pragma clang diagnostic pop
60 // Total physical memory.
61 if (sysctl(mib
.data(), mib
.size(), &physmem
, &len
, nullptr, 0) == 0 && len
== sizeof(physmem
))
62 buffer
->totalPhys
= physmem
;
65 mach_port_t stat_port
= mach_host_self();
66 vm_statistics_data_t vm_stat
;
67 mach_msg_type_number_t count
= sizeof(vm_stat
) / sizeof(natural_t
);
68 if (host_statistics(stat_port
, HOST_VM_INFO
, reinterpret_cast<host_info_t
>(&vm_stat
), &count
) == 0)
71 #if defined(TARGET_DARWIN_IOS)
72 // on ios with 64bit ARM CPU the page size is wrongly given as 16K
73 // when using the sysctl approach. We can use the host_page_size
74 // function instead which will give the proper 4k pagesize
75 // on both 32 and 64 bit ARM CPUs
77 host_page_size(stat_port
, &pageSize
);
83 if (sysctl(mib
.data(), mib
.size(), &pageSize
, &len
, nullptr, 0) == 0)
86 uint64_t used
= (vm_stat
.active_count
+ vm_stat
.inactive_count
+ vm_stat
.wire_count
) * pageSize
;
87 buffer
->availPhys
= buffer
->totalPhys
- used
;