Ticket #55278: patch-Source-WTF-wtf-ramsize.cpp.diff

File patch-Source-WTF-wtf-ramsize.cpp.diff, 2.6 KB (added by kencu (Ken), 6 years ago)
  • Source/WTF/wtf/RAMSize.cpp

    diff --git Source/WTF/wtf/RAMSize.cpp Source/WTF/wtf/RAMSize.cpp
    index 5d34d3b..3dd516c 100644
     
    2929#include "StdLibExtras.h"
    3030#include <mutex>
    3131
    32 #if OS(WINDOWS)
     32#if OS(DARWIN)
     33#import <dispatch/dispatch.h>
     34#import <mach/host_info.h>
     35#import <mach/mach.h>
     36#import <mach/mach_error.h>
     37#import <math.h>
     38#elif OS(UNIX)
     39#include <unistd.h>
     40#elif OS(WINDOWS)
    3341#include <windows.h>
    34 #elif defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC
    35 #if OS(UNIX)
    36 #include <sys/sysinfo.h>
    37 #endif // OS(UNIX)
    38 #else
    39 #include <bmalloc/bmalloc.h>
    4042#endif
    4143
    4244namespace WTF {
    4345
    44 #if OS(WINDOWS)
    4546static const size_t ramSizeGuess = 512 * MB;
    46 #endif
    4747
    4848static size_t computeRAMSize()
    4949{
    50 #if OS(WINDOWS)
     50#if PLATFORM(IOS_SIMULATOR)
     51    // Pretend we have 512MB of memory to make cache sizes behave like on device.
     52    return ramSizeGuess;
     53#elif OS(DARWIN)
     54    host_basic_info_data_t hostInfo;
     55
     56    mach_port_t host = mach_host_self();
     57    mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
     58    kern_return_t r = host_info(host, HOST_BASIC_INFO, (host_info_t)&hostInfo, &count);
     59    mach_port_deallocate(mach_task_self(), host);
     60    if (r != KERN_SUCCESS) {
     61        LOG_ERROR("%s : host_info(%d) : %s.\n", __FUNCTION__, r, mach_error_string(r));
     62        return ramSizeGuess;
     63    }
     64
     65    if (hostInfo.max_mem > std::numeric_limits<size_t>::max())
     66        return std::numeric_limits<size_t>::max();
     67
     68    size_t sizeAccordingToKernel = static_cast<size_t>(hostInfo.max_mem);
     69    size_t multiple = 128 * MB;
     70
     71    // Round up the memory size to a multiple of 128MB because max_mem may not be exactly 512MB
     72    // (for example) and we have code that depends on those boundaries.
     73    return ((sizeAccordingToKernel + multiple - 1) / multiple) * multiple;
     74#elif OS(UNIX)
     75    long pages = sysconf(_SC_PHYS_PAGES);
     76    long pageSize = sysconf(_SC_PAGE_SIZE);
     77    if (pages == -1 || pageSize == -1)
     78        return ramSizeGuess;
     79    return pages * pageSize;
     80#elif OS(WINDOWS)
    5181    MEMORYSTATUSEX status;
    5282    status.dwLength = sizeof(status);
    5383    bool result = GlobalMemoryStatusEx(&status);
    5484    if (!result)
    5585        return ramSizeGuess;
    5686    return status.ullTotalPhys;
    57 #elif defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC
    58 #if OS(UNIX)
    59     struct sysinfo si;
    60     sysinfo(&si);
    61     return si.totalram * si.mem_unit;
    62 #else
    63 #error "Missing a platform specific way of determining the available RAM"
    64 #endif // OS(UNIX)
    65 #else
    66     return bmalloc::api::availableMemory();
    6787#endif
    6888}
    6989
    size_t ramSize() 
    7797    return ramSize;
    7898}
    7999
    80 } // namespace WTF
     100} // namespace WTF
     101 No newline at end of file