| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <dirent.h> | 8 #include <dirent.h> |
| 9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 CHECK(false) << "Out of memory."; | 506 CHECK(false) << "Out of memory."; |
| 507 } | 507 } |
| 508 | 508 |
| 509 void OnNoMemory() { | 509 void OnNoMemory() { |
| 510 OnNoMemorySize(0); | 510 OnNoMemorySize(0); |
| 511 } | 511 } |
| 512 | 512 |
| 513 } // namespace | 513 } // namespace |
| 514 | 514 |
| 515 extern "C" { | 515 extern "C" { |
| 516 // This code tries to make malloc failures fatal for security reasons. However, |
| 517 // it breaks builds depending on fine details of the linker command line and |
| 518 // the dependencies of other .so's that we pull in. So it's disabled for the |
| 519 // moment. See crbug.com/31809 |
| 520 |
| 521 #if 0 |
| 516 #if !defined(LINUX_USE_TCMALLOC) | 522 #if !defined(LINUX_USE_TCMALLOC) |
| 517 | 523 |
| 518 typedef void* (*malloc_type)(size_t size); | 524 typedef void* (*malloc_type)(size_t size); |
| 519 typedef void* (*valloc_type)(size_t size); | 525 typedef void* (*valloc_type)(size_t size); |
| 520 typedef void* (*pvalloc_type)(size_t size); | 526 typedef void* (*pvalloc_type)(size_t size); |
| 521 | 527 |
| 522 typedef void* (*calloc_type)(size_t nmemb, size_t size); | 528 typedef void* (*calloc_type)(size_t nmemb, size_t size); |
| 523 typedef void* (*realloc_type)(void *ptr, size_t size); | 529 typedef void* (*realloc_type)(void *ptr, size_t size); |
| 524 typedef void* (*memalign_type)(size_t boundary, size_t size); | 530 typedef void* (*memalign_type)(size_t boundary, size_t size); |
| 525 | 531 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 int posix_memalign(void** ptr, size_t alignment, size_t size) { | 598 int posix_memalign(void** ptr, size_t alignment, size_t size) { |
| 593 static posix_memalign_type original_function = | 599 static posix_memalign_type original_function = |
| 594 reinterpret_cast<posix_memalign_type>(dlsym(RTLD_NEXT, "posix_memalign")); | 600 reinterpret_cast<posix_memalign_type>(dlsym(RTLD_NEXT, "posix_memalign")); |
| 595 int ret = original_function(ptr, alignment, size); | 601 int ret = original_function(ptr, alignment, size); |
| 596 if (ret == ENOMEM) | 602 if (ret == ENOMEM) |
| 597 OnNoMemorySize(size); | 603 OnNoMemorySize(size); |
| 598 return ret; | 604 return ret; |
| 599 } | 605 } |
| 600 | 606 |
| 601 #endif // !defined(LINUX_USE_TCMALLOC) | 607 #endif // !defined(LINUX_USE_TCMALLOC) |
| 608 #endif |
| 602 } // extern C | 609 } // extern C |
| 603 | 610 |
| 604 void EnableTerminationOnOutOfMemory() { | 611 void EnableTerminationOnOutOfMemory() { |
| 605 // Set the new-out of memory handler. | 612 // Set the new-out of memory handler. |
| 606 std::set_new_handler(&OnNoMemory); | 613 std::set_new_handler(&OnNoMemory); |
| 607 // If we're using glibc's allocator, the above functions will override | 614 // If we're using glibc's allocator, the above functions will override |
| 608 // malloc and friends and make them die on out of memory. | 615 // malloc and friends and make them die on out of memory. |
| 609 } | 616 } |
| 610 | 617 |
| 611 bool AdjustOOMScore(ProcessId process, int score) { | 618 bool AdjustOOMScore(ProcessId process, int score) { |
| 612 if (score < 0 || score > 15) | 619 if (score < 0 || score > 15) |
| 613 return false; | 620 return false; |
| 614 | 621 |
| 615 FilePath oom_adj("/proc"); | 622 FilePath oom_adj("/proc"); |
| 616 oom_adj = oom_adj.Append(Int64ToString(process)); | 623 oom_adj = oom_adj.Append(Int64ToString(process)); |
| 617 oom_adj = oom_adj.AppendASCII("oom_adj"); | 624 oom_adj = oom_adj.AppendASCII("oom_adj"); |
| 618 | 625 |
| 619 if (!file_util::PathExists(oom_adj)) | 626 if (!file_util::PathExists(oom_adj)) |
| 620 return false; | 627 return false; |
| 621 | 628 |
| 622 std::string score_str = IntToString(score); | 629 std::string score_str = IntToString(score); |
| 623 return (static_cast<int>(score_str.length()) == | 630 return (static_cast<int>(score_str.length()) == |
| 624 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); | 631 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); |
| 625 } | 632 } |
| 626 | 633 |
| 627 } // namespace base | 634 } // namespace base |
| OLD | NEW |