Skip to content

Commit

Permalink
Implement OS RAM detection
Browse files Browse the repository at this point in the history
Ref: #3
  • Loading branch information
nabijaczleweli committed Jun 5, 2016
1 parent a4a6712 commit 4ddda94
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/cpu_memory_physical_available.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/system.hpp"
#include <iostream>


int main() {
std::cout << iware::system::memory().physical_available << '\n';
}
19 changes: 19 additions & 0 deletions examples/cpu_memory_physical_total.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/system.hpp"
#include <iostream>


int main() {
std::cout << iware::system::memory().physical_total << '\n';
}
19 changes: 19 additions & 0 deletions examples/cpu_memory_virtual_available.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/system.hpp"
#include <iostream>


int main() {
std::cout << iware::system::memory().virtual_available << '\n';
}
19 changes: 19 additions & 0 deletions examples/cpu_memory_virtual_total.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#include "infoware/system.hpp"
#include <iostream>


int main() {
std::cout << iware::system::memory().virtual_total << '\n';
}
14 changes: 14 additions & 0 deletions include/infoware/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@
#pragma once


#include <cstdint>


namespace iware {
namespace system {
struct memory_t {
std::size_t physical_available;
std::size_t physical_total;
std::size_t virtual_available;
std::size_t virtual_total;
};


/// Get amount of connected mice.
unsigned int mouse_amount() noexcept;

Expand All @@ -27,5 +38,8 @@ namespace iware {
///
/// Always returns 0 on Linuxish kernels, as it can not be detected there.
unsigned int other_HID_amount() noexcept;

/// Get RAM statistics.
memory_t memory() noexcept;
}
}
46 changes: 46 additions & 0 deletions src/system/memory/non-windows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#ifndef _WIN32


#include "infoware/system.hpp"
#include <fstream>
#include <string>


iware::system::memory_t iware::system::memory() noexcept {
std::ifstream meminfo("/proc/meminfo");

if(!meminfo.is_open() || !meminfo)
return ret;

iware::system::memory_t ret;
for(std::string line; std::getline(meminfo, line);) {
const auto colon_id = line.find_first_of(':');
const auto value = std::strtoul(line.c_str() + colon_id + 1, nullptr) * 1024;

if(line.find("MemTotal") == 0)
ret.physical_total = value;
else if(line.find("MemAvailable") == 0)
ret.physical_available = value;
else if(line.find("VmallocTotal") == 0)
ret.virtual_total = value;
else if(line.find("VmallocUsed") == 0)
ret.virtual_available = ret.virtual_total - value;
}

return ret;
}


#endif
36 changes: 36 additions & 0 deletions src/system/memory/windows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// infoware - C++ System information Library
//
// Written in 2016 by nabijaczleweli <[email protected]> and ThePhD <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>


#ifdef _WIN32


#include "infoware/system.hpp"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>


iware::system::memory_t iware::system::memory() noexcept {
MEMORYSTATUSEX mem;
mem.dwLength = sizeof(mem);
if(!GlobalMemoryStatusEx(&mem))
return {};

return {
mem.ullTotalPhys,
mem.ullAvailPhys,
mem.ullTotalVirtual,
mem.ullAvailVirtual,
};
}


#endif

0 comments on commit 4ddda94

Please sign in to comment.