diff options
author | Lukas Rusak <lorusak@gmail.com> | 2019-05-29 18:43:34 -0700 |
---|---|---|
committer | Lukas Rusak <lorusak@gmail.com> | 2019-06-11 20:08:13 -0700 |
commit | 264184be46b7363b035ef496385fee8741aa5bf5 (patch) | |
tree | aed97033a386c82a71ad8a78d40799badad95591 | |
parent | e18c43cdbe0ec6aefa092d6cafee5b00117cc448 (diff) |
MemUtils: breakup into platform specific files
-rw-r--r-- | cmake/treedata/android/subdirs.txt | 1 | ||||
-rw-r--r-- | xbmc/platform/android/CMakeLists.txt | 3 | ||||
-rw-r--r-- | xbmc/platform/android/MemUtils.cpp | 38 | ||||
-rw-r--r-- | xbmc/platform/darwin/CMakeLists.txt | 1 | ||||
-rw-r--r-- | xbmc/platform/darwin/MemUtils.cpp | 107 | ||||
-rw-r--r-- | xbmc/platform/freebsd/CMakeLists.txt | 3 | ||||
-rw-r--r-- | xbmc/platform/freebsd/MemUtils.cpp | 90 | ||||
-rw-r--r-- | xbmc/platform/linux/CMakeLists.txt | 1 | ||||
-rw-r--r-- | xbmc/platform/linux/MemUtils.cpp | 49 | ||||
-rw-r--r-- | xbmc/platform/posix/CMakeLists.txt | 1 | ||||
-rw-r--r-- | xbmc/platform/posix/MemUtils.cpp | 175 |
11 files changed, 292 insertions, 177 deletions
diff --git a/cmake/treedata/android/subdirs.txt b/cmake/treedata/android/subdirs.txt index 81767ad93e..57c89ea843 100644 --- a/cmake/treedata/android/subdirs.txt +++ b/cmake/treedata/android/subdirs.txt @@ -3,6 +3,7 @@ xbmc/cores/VideoPlayer/Process/android cores/VideoPlayer/Process/android xbmc/input/touch input/touch xbmc/input/touch/generic input/touch/generic xbmc/media/decoderfilter media/decoderfilter +xbmc/platform/android platform/android xbmc/platform/android/activity platform/android/activity xbmc/platform/android/bionic_supplement platform/android/bionicsupplement xbmc/platform/android/filesystem platform/android/filesystem diff --git a/xbmc/platform/android/CMakeLists.txt b/xbmc/platform/android/CMakeLists.txt new file mode 100644 index 0000000000..e20ff907aa --- /dev/null +++ b/xbmc/platform/android/CMakeLists.txt @@ -0,0 +1,3 @@ +set(SOURCES MemUtils.cpp) + +core_add_library(androidsupport) diff --git a/xbmc/platform/android/MemUtils.cpp b/xbmc/platform/android/MemUtils.cpp new file mode 100644 index 0000000000..cbda4bd9f0 --- /dev/null +++ b/xbmc/platform/android/MemUtils.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2005-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "utils/MemUtils.h" + +#include <stdlib.h> + +namespace KODI +{ +namespace MEMORY +{ + +void* AlignedMalloc(size_t s, size_t alignTo) +{ + void* p; + posix_memalign(&p, alignTo, s); + + return p; +} + +void AlignedFree(void* p) +{ + free(p); +} + +void GetMemoryStatus(MemoryStatus* buffer) +{ + if (!buffer) + return; +} + +} +} diff --git a/xbmc/platform/darwin/CMakeLists.txt b/xbmc/platform/darwin/CMakeLists.txt index 934bf41dce..430edff2da 100644 --- a/xbmc/platform/darwin/CMakeLists.txt +++ b/xbmc/platform/darwin/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES AutoPool.mm DarwinUtils.mm DarwinNSUserDefaults.mm DictionaryUtils.mm + MemUtils.cpp PlatformDarwin.cpp) set(HEADERS AutoPool.h diff --git a/xbmc/platform/darwin/MemUtils.cpp b/xbmc/platform/darwin/MemUtils.cpp new file mode 100644 index 0000000000..e0fc66f7ba --- /dev/null +++ b/xbmc/platform/darwin/MemUtils.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2005-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "utils/MemUtils.h" + +#include <array> +#include <cstdlib> +#include <cstring> + +#include <stdio.h> +#include <unistd.h> + +#include <mach/mach.h> +#include <sys/sysctl.h> +#include <sys/types.h> + +namespace KODI +{ +namespace MEMORY +{ + +void* AlignedMalloc(size_t s, size_t alignTo) +{ + void* p; + posix_memalign(&p, alignTo, s); + + return p; +} + +void AlignedFree(void* p) +{ + free(p); +} + +void GetMemoryStatus(MemoryStatus* buffer) +{ + if (!buffer) + return; + + uint64_t physmem; + size_t len = sizeof physmem; + +#if defined(__apple_build_version__) && __apple_build_version__ < 10000000 +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wmissing-braces" +#endif + std::array<int, 2> mib = + { + CTL_HW, + HW_MEMSIZE, + }; +#if defined(__apple_build_version__) && __apple_build_version__ < 10000000 +#pragma clang diagnostic pop +#endif + + // Total physical memory. + if (sysctl(mib.data(), mib.size(), &physmem, &len, nullptr, 0) == 0 && len == sizeof(physmem)) + buffer->totalPhys = physmem; + + // Virtual memory. + mib[0] = CTL_VM; + mib[1] = VM_SWAPUSAGE; + struct xsw_usage swap; + len = sizeof(struct xsw_usage); + if (sysctl(mib.data(), mib.size(), &swap, &len, NULL, 0) == 0) + { + buffer->availPageFile = swap.xsu_avail; + buffer->totalVirtual = buffer->totalPhys + swap.xsu_total; + } + + // In use. + mach_port_t stat_port = mach_host_self(); + vm_statistics_data_t vm_stat; + mach_msg_type_number_t count = sizeof(vm_stat) / sizeof(natural_t); + if (host_statistics(stat_port, HOST_VM_INFO, reinterpret_cast<host_info_t>(&vm_stat), &count) == 0) + { + // Find page size. +#if defined(TARGET_DARWIN_IOS) + // on ios with 64bit ARM CPU the page size is wrongly given as 16K + // when using the sysctl approach. We can use the host_page_size + // function instead which will give the proper 4k pagesize + // on both 32 and 64 bit ARM CPUs + vm_size_t pageSize; + host_page_size(stat_port, &pageSize); +#else + int pageSize; + mib[0] = CTL_HW; + mib[1] = HW_PAGESIZE; + len = sizeof(int); + if (sysctl(mib.data(), mib.size(), &pageSize, &len, nullptr, 0) == 0) +#endif + { + uint64_t used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pageSize; + + buffer->availPhys = buffer->totalPhys - used; + buffer->availVirtual = buffer->availPhys; // FIXME. + } + } +} + +} +} diff --git a/xbmc/platform/freebsd/CMakeLists.txt b/xbmc/platform/freebsd/CMakeLists.txt index 68bd104b14..568ca38723 100644 --- a/xbmc/platform/freebsd/CMakeLists.txt +++ b/xbmc/platform/freebsd/CMakeLists.txt @@ -1,4 +1,5 @@ -set(SOURCES ../linux/OptionalsReg.cpp +set(SOURCES MemUtils.cpp + ../linux/OptionalsReg.cpp ../linux/TimeUtils.cpp) set(HEADERS ../linux/OptionalsReg.h diff --git a/xbmc/platform/freebsd/MemUtils.cpp b/xbmc/platform/freebsd/MemUtils.cpp new file mode 100644 index 0000000000..aeb2666370 --- /dev/null +++ b/xbmc/platform/freebsd/MemUtils.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2005-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "utils/MemUtils.h" + +#include <array> +#include <cstdlib> +#include <cstring> + +#include <stdio.h> +#include <unistd.h> + +#include <sys/sysctl.h> +#include <sys/types.h> + +namespace KODI +{ +namespace MEMORY +{ + +void* AlignedMalloc(size_t s, size_t alignTo) +{ + void* p; + posix_memalign(&p, alignTo, s); + + return p; +} + +void AlignedFree(void* p) +{ + free(p); +} + +void GetMemoryStatus(MemoryStatus* buffer) +{ + if (!buffer) + return; + + /* sysctl hw.physmem */ + size_t len = 0; + + /* physmem */ + size_t physmem = 0; + len = sizeof(physmem); + if (sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) == 0) + { + buffer->totalPhys = physmem; + buffer->totalVirtual = physmem; + } + + /* pagesize */ + size_t pagesize = 0; + len = sizeof(pagesize); + if (sysctlbyname("hw.pagesize", &pagesize, &len, NULL, 0) != 0) + pagesize = 4096; + + /* mem_inactive */ + size_t mem_inactive = 0; + len = sizeof(mem_inactive); + if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &len, NULL, 0) == 0) + mem_inactive *= pagesize; + + /* mem_cache */ + size_t mem_cache = 0; + len = sizeof(mem_cache); + if (sysctlbyname("vm.stats.vm.v_cache_count", &mem_cache, &len, NULL, 0) == 0) + mem_cache *= pagesize; + + /* mem_free */ + size_t mem_free = 0; + len = sizeof(mem_free); + if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &len, NULL, 0) == 0) + mem_free *= pagesize; + + /* mem_avail = mem_inactive + mem_cache + mem_free */ + buffer->availPhys = mem_inactive + mem_cache + mem_free; + buffer->availVirtual = mem_inactive + mem_cache + mem_free; + + size_t swap_free = 0; + if (sysctlbyname("vm.stats.vm.v_swappgsout", &swap_free, &len, NULL, 0) == 0) + buffer->availPageFile = swap_free * pagesize; +} + +} +} diff --git a/xbmc/platform/linux/CMakeLists.txt b/xbmc/platform/linux/CMakeLists.txt index 242754005f..9c46a7a276 100644 --- a/xbmc/platform/linux/CMakeLists.txt +++ b/xbmc/platform/linux/CMakeLists.txt @@ -1,4 +1,5 @@ set(SOURCES OptionalsReg.cpp + MemUtils.cpp TimeUtils.cpp) set(HEADERS OptionalsReg.h diff --git a/xbmc/platform/linux/MemUtils.cpp b/xbmc/platform/linux/MemUtils.cpp new file mode 100644 index 0000000000..44f82ba22b --- /dev/null +++ b/xbmc/platform/linux/MemUtils.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2005-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "utils/MemUtils.h" + +#include <cstdlib> + +#include <sys/sysinfo.h> + +namespace KODI +{ +namespace MEMORY +{ + +void* AlignedMalloc(size_t s, size_t alignTo) +{ + return aligned_alloc(alignTo, s); +} + +void AlignedFree(void* p) +{ + if (!p) + return; + + free(p); +} + +void GetMemoryStatus(MemoryStatus* buffer) +{ + if (!buffer) + return; + + struct sysinfo info; + sysinfo(&info); + + buffer->availPageFile = (info.freeswap * info.mem_unit); + buffer->availPhys = ((info.freeram + info.bufferram) * info.mem_unit); + buffer->availVirtual = ((info.freeram + info.bufferram) * info.mem_unit); + buffer->totalPhys = (info.totalram * info.mem_unit); + buffer->totalVirtual = (info.totalram * info.mem_unit); +} + +} +} diff --git a/xbmc/platform/posix/CMakeLists.txt b/xbmc/platform/posix/CMakeLists.txt index 4b5e3abac4..56c7487238 100644 --- a/xbmc/platform/posix/CMakeLists.txt +++ b/xbmc/platform/posix/CMakeLists.txt @@ -1,6 +1,5 @@ set(SOURCES ConvUtils.cpp Filesystem.cpp - MemUtils.cpp MessagePrinter.cpp PlatformPosix.cpp PosixMountProvider.cpp diff --git a/xbmc/platform/posix/MemUtils.cpp b/xbmc/platform/posix/MemUtils.cpp deleted file mode 100644 index 07c3c1934e..0000000000 --- a/xbmc/platform/posix/MemUtils.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright (C) 2005-2018 Team Kodi - * This file is part of Kodi - https://kodi.tv - * - * SPDX-License-Identifier: GPL-2.0-or-later - * See LICENSES/README.md for more information. - */ - -#include "utils/MemUtils.h" - -#include <array> -#include <cstring> -#include <stdlib.h> -#include <stdio.h> -#include <unistd.h> - -#include <sys/types.h> - -#if !defined(TARGET_ANDROID) -#include <sys/sysctl.h> -#endif - -#if defined(TARGET_LINUX) -#include <sys/sysinfo.h> -#endif - -#if defined(TARGET_DARWIN) -#include <mach/mach.h> -#endif - -#undef ALIGN -#define ALIGN(value, alignment) (((value)+(alignment-1))&~(alignment-1)) - -namespace KODI -{ -namespace MEMORY -{ - -// aligned memory allocation. -// in order to do so - we alloc extra space and store the original allocation in it (so that we can free later on). -// the returned address will be the nearest aligned address within the space allocated. -void* AlignedMalloc(size_t s, size_t alignTo) -{ - char *pFull = (char*)malloc(s + alignTo + sizeof(char *)); - char *pAligned = (char *)ALIGN(((unsigned long)pFull + sizeof(char *)), alignTo); - - *(char **)(pAligned - sizeof(char*)) = pFull; - - return(pAligned); -} - -void AlignedFree(void* p) -{ - if (!p) - return; - - char *pFull = *(char **)(((char *)p) - sizeof(char *)); - free(pFull); -} - -void GetMemoryStatus(MemoryStatus* buffer) -{ - if (!buffer) - return; - - std::memset(buffer, 0, sizeof(MemoryStatus)); - -#if defined(TARGET_DARWIN) - uint64_t physmem; - size_t len = sizeof physmem; - -#if defined(__apple_build_version__) && __apple_build_version__ < 10000000 -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wmissing-braces" -#endif - std::array<int, 2> mib = - { - CTL_HW, - HW_MEMSIZE, - }; -#if defined(__apple_build_version__) && __apple_build_version__ < 10000000 -#pragma clang diagnostic pop -#endif - - // Total physical memory. - if (sysctl(mib.data(), mib.size(), &physmem, &len, NULL, 0) == 0 && len == sizeof (physmem)) - buffer->totalPhys = physmem; - - // Virtual memory. - mib[0] = CTL_VM; - mib[1] = VM_SWAPUSAGE; - struct xsw_usage swap; - len = sizeof(struct xsw_usage); - if (sysctl(mib.data(), mib.size(), &swap, &len, NULL, 0) == 0) - { - buffer->availPageFile = swap.xsu_avail; - buffer->totalVirtual = buffer->totalPhys + swap.xsu_total; - } - - // In use. - mach_port_t stat_port = mach_host_self(); - vm_statistics_data_t vm_stat; - mach_msg_type_number_t count = sizeof(vm_stat) / sizeof(natural_t); - if (host_statistics(stat_port, HOST_VM_INFO, (host_info_t)&vm_stat, &count) == 0) - { - // Find page size. -#if defined(TARGET_DARWIN_IOS) - // on ios with 64bit ARM CPU the page size is wrongly given as 16K - // when using the sysctl approach. We can use the host_page_size - // function instead which will give the proper 4k pagesize - // on both 32 and 64 bit ARM CPUs - vm_size_t pageSize; - host_page_size(stat_port, &pageSize); -#else - int pageSize; - mib[0] = CTL_HW; - mib[1] = HW_PAGESIZE; - len = sizeof(int); - if (sysctl(mib.data(), mib.size(), &pageSize, &len, NULL, 0) == 0) -#endif - { - uint64_t used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pageSize; - - buffer->availPhys = buffer->totalPhys - used; - buffer->availVirtual = buffer->availPhys; // FIXME. - } - } -#elif defined(TARGET_FREEBSD) - /* sysctl hw.physmem */ - size_t physmem = 0, mem_free = 0, pagesize = 0, swap_free = 0; - size_t mem_inactive = 0, mem_cache = 0, len = 0; - - /* physmem */ - len = sizeof(physmem); - if (sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) == 0) { - buffer->totalPhys = physmem; - buffer->totalVirtual = physmem; - } - /* pagesize */ - len = sizeof(pagesize); - if (sysctlbyname("hw.pagesize", &pagesize, &len, NULL, 0) != 0) - pagesize = 4096; - /* mem_inactive */ - len = sizeof(mem_inactive); - if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &len, NULL, 0) == 0) - mem_inactive *= pagesize; - /* mem_cache */ - len = sizeof(mem_cache); - if (sysctlbyname("vm.stats.vm.v_cache_count", &mem_cache, &len, NULL, 0) == 0) - mem_cache *= pagesize; - /* mem_free */ - len = sizeof(mem_free); - if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &len, NULL, 0) == 0) - mem_free *= pagesize; - - /* mem_avail = mem_inactive + mem_cache + mem_free */ - buffer->availPhys = mem_inactive + mem_cache + mem_free; - buffer->availVirtual = mem_inactive + mem_cache + mem_free; - - if (sysctlbyname("vm.stats.vm.v_swappgsout", &swap_free, &len, NULL, 0) == 0) - buffer->availPageFile = swap_free * pagesize; -#else - struct sysinfo info; - sysinfo(&info); - - buffer->availPageFile = (info.freeswap * info.mem_unit); - buffer->availPhys = ((info.freeram + info.bufferram) * info.mem_unit); - buffer->availVirtual = ((info.freeram + info.bufferram) * info.mem_unit); - buffer->totalPhys = (info.totalram * info.mem_unit); - buffer->totalVirtual = (info.totalram * info.mem_unit); -#endif -} - -} -} |