@@ -21,7 +21,6 @@ bool is_overcommit_limit_set() {
2121
2222 if (overcommit_file.is_open ()) {
2323 overcommit_file >> overcommit_value;
24- overcommit_file.close ();
2524 } else {
2625 return false ;
2726 }
@@ -31,41 +30,37 @@ bool is_overcommit_limit_set() {
3130size_t get_committed_as () {
3231 std::ifstream meminfo (" /proc/meminfo" );
3332 size_t committed_as = 0 ;
34- char line[ 256 ] ;
33+ std::string line;
3534
3635 if (meminfo.is_open ()) {
37- while (meminfo. getline (line, 256 )) {
38- if (strncmp ( line, " Committed_AS:" , 13 ) == 0 ) {
36+ while (std:: getline (meminfo, line )) {
37+ if (line. find ( " Committed_AS:" ) != std::string::npos ) {
3938 std::istringstream iss (line);
4039 iss.ignore (256 , ' :' );
4140 iss >> committed_as;
4241 committed_as = (iss.fail ()) ? 0 : committed_as * 1024 ;
43- meminfo.close ();
4442 break ;
4543 }
4644 }
47- meminfo.close ();
4845 }
4946 return committed_as;
5047}
5148
5249size_t get_commitLimit () {
5350 std::ifstream meminfo (" /proc/meminfo" );
5451 size_t commitLimit = 0 ;
55- char line[ 256 ] ;
52+ std::string line;
5653
5754 if (meminfo.is_open ()) {
58- while (meminfo. getline (line, 256 )) {
59- if (strncmp ( line, " CommitLimit:" , 12 ) == 0 ) {
55+ while (std:: getline (meminfo, line )) {
56+ if (line. find ( " CommitLimit:" ) != std::string::npos ) {
6057 std::istringstream iss (line);
6158 iss.ignore (256 , ' :' );
6259 iss >> commitLimit;
6360 commitLimit = (iss.fail ()) ? 0 : commitLimit * 1024 ;
64- meminfo.close ();
6561 break ;
6662 }
6763 }
68- meminfo.close ();
6964 }
7065 return commitLimit;
7166}
0 commit comments