LinuxMemory.cpp 847 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * vim: sw=2 ts=8 et :
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  6. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  7. #include <stdio.h>
  8. #include "Hal.h"
  9. namespace mozilla {
  10. namespace hal_impl {
  11. uint32_t
  12. GetTotalSystemMemory()
  13. {
  14. static uint32_t sTotalMemory;
  15. static bool sTotalMemoryObtained = false;
  16. if (!sTotalMemoryObtained) {
  17. sTotalMemoryObtained = true;
  18. FILE* fd = fopen("/proc/meminfo", "r");
  19. if (!fd) {
  20. return 0;
  21. }
  22. int rv = fscanf(fd, "MemTotal: %i kB", &sTotalMemory);
  23. if (fclose(fd) || rv != 1) {
  24. return 0;
  25. }
  26. }
  27. return sTotalMemory * 1024;
  28. }
  29. } // namespace hal_impl
  30. } // namespace mozilla