RAMSize.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "RAMSize.h"
  27. #include "StdLibExtras.h"
  28. #if OS(DARWIN)
  29. #include <sys/param.h>
  30. #include <sys/types.h>
  31. #include <sys/sysctl.h>
  32. #elif OS(UNIX)
  33. #include <unistd.h>
  34. #elif OS(WINDOWS)
  35. #include <windows.h>
  36. #elif OS(QNX)
  37. #include <sys/stat.h>
  38. #endif
  39. namespace WTF {
  40. static const size_t ramSizeGuess = 128 * MB;
  41. static size_t computeRAMSize()
  42. {
  43. #if OS(DARWIN)
  44. int mib[2];
  45. uint64_t ramSize;
  46. size_t length;
  47. mib[0] = CTL_HW;
  48. mib[1] = HW_MEMSIZE;
  49. length = sizeof(int64_t);
  50. int sysctlResult = sysctl(mib, 2, &ramSize, &length, 0, 0);
  51. if (sysctlResult == -1)
  52. return ramSizeGuess;
  53. return ramSize > std::numeric_limits<size_t>::max() ? std::numeric_limits<size_t>::max() : static_cast<size_t>(ramSize);
  54. #elif OS(UNIX)
  55. long pages = sysconf(_SC_PHYS_PAGES);
  56. long pageSize = sysconf(_SC_PAGE_SIZE);
  57. if (pages == -1 || pageSize == -1)
  58. return ramSizeGuess;
  59. return pages * pageSize;
  60. #elif OS(WINCE)
  61. MEMORYSTATUS status;
  62. status.dwLength = sizeof(status);
  63. GlobalMemoryStatus(&status);
  64. if (status.dwTotalPhys <= 0)
  65. return ramSizeGuess;
  66. return status.dwTotalPhys;
  67. #elif OS(WINDOWS)
  68. MEMORYSTATUSEX status;
  69. status.dwLength = sizeof(status);
  70. bool result = GlobalMemoryStatusEx(&status);
  71. if (!result)
  72. return ramSizeGuess;
  73. return status.ullTotalPhys;
  74. #elif OS(PSP2)
  75. class RamSize {
  76. public:
  77. RamSize() {
  78. m_ramSize = ramSizeGuess;
  79. const char* ramSizeString = getenv("RAM_SIZE"); // In MiB.
  80. if (ramSizeString) {
  81. int ramSize = atoi(ramSizeString) * MB;
  82. if (ramSize > 0 && ramSize < ramSizeGuess)
  83. m_ramSize = ramSize;
  84. }
  85. }
  86. size_t m_ramSize;
  87. };
  88. static RamSize ramSize;
  89. return ramSize.m_ramSize;
  90. #elif OS(QNX)
  91. struct stat mst;
  92. if (stat("/proc", &mst))
  93. return ramSizeGuess;
  94. return mst.st_size;
  95. #endif
  96. }
  97. size_t ramSize()
  98. {
  99. static const size_t ramSize = computeRAMSize();
  100. return ramSize;
  101. }
  102. } // namespace WTF