2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <commons/Exception.h>
23 const char* MEMINFO_FILE = "/proc/meminfo";
24 const char* MEMORY_TOTAL_PATTERN = "^MemTotal:\\s+(\\d+)\\s*kB$";
25 const char* MEMORY_FREE_PATTERN = "^MemFree:\\s+(\\d+)\\s*kB$";
28 namespace WrtPlugins {
31 Manager& Manager::getInstance()
33 static Manager instance;
37 unsigned long Manager::getTotal() const
39 return getMeminfoValue(MEMORY_TOTAL_PATTERN);
42 unsigned long Manager::getFree() const
44 return getMeminfoValue(MEMORY_FREE_PATTERN);
50 unsigned long Manager::getMeminfoValue(const char* pattern) const
52 assert(pattern && "Pattern not set.");
54 std::ifstream file(MEMINFO_FILE);
56 ThrowMsg(Commons::PlatformException, "Memory info file not found.");
59 unsigned long result = 0;
62 while ((std::getline(file, line).rdstate() &
63 (std::ifstream::failbit | std::ifstream::eofbit)) == 0)
65 if (pcrecpp::RE(pattern).FullMatch(line, &result)) {
73 ThrowMsg(Commons::UnsupportedException, "Total memory data not found.");