OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/discardable_memory/service/discardable_shared_memory_manage
r.h" | 5 #include "components/discardable_memory/service/discardable_shared_memory_manage
r.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/atomic_sequence_num.h" | 10 #include "base/atomic_sequence_num.h" |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 int client_id, | 433 int client_id, |
434 size_t size, | 434 size_t size, |
435 int32_t id, | 435 int32_t id, |
436 base::SharedMemoryHandle* shared_memory_handle) { | 436 base::SharedMemoryHandle* shared_memory_handle) { |
437 base::AutoLock lock(lock_); | 437 base::AutoLock lock(lock_); |
438 | 438 |
439 // Make sure |id| is not already in use. | 439 // Make sure |id| is not already in use. |
440 MemorySegmentMap& client_segments = clients_[client_id]; | 440 MemorySegmentMap& client_segments = clients_[client_id]; |
441 if (client_segments.find(id) != client_segments.end()) { | 441 if (client_segments.find(id) != client_segments.end()) { |
442 LOG(ERROR) << "Invalid discardable shared memory ID"; | 442 LOG(ERROR) << "Invalid discardable shared memory ID"; |
443 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 443 *shared_memory_handle = base::SharedMemoryHandle(); |
444 return; | 444 return; |
445 } | 445 } |
446 | 446 |
447 // Memory usage must be reduced to prevent the addition of |size| from | 447 // Memory usage must be reduced to prevent the addition of |size| from |
448 // taking usage above the limit. Usage should be reduced to 0 in cases | 448 // taking usage above the limit. Usage should be reduced to 0 in cases |
449 // where |size| is greater than the limit. | 449 // where |size| is greater than the limit. |
450 size_t limit = 0; | 450 size_t limit = 0; |
451 // Note: the actual mapped size can be larger than requested and cause | 451 // Note: the actual mapped size can be larger than requested and cause |
452 // |bytes_allocated_| to temporarily be larger than |memory_limit_|. The | 452 // |bytes_allocated_| to temporarily be larger than |memory_limit_|. The |
453 // error is minimized by incrementing |bytes_allocated_| with the actual | 453 // error is minimized by incrementing |bytes_allocated_| with the actual |
454 // mapped size rather than |size| below. | 454 // mapped size rather than |size| below. |
455 if (size < memory_limit_) | 455 if (size < memory_limit_) |
456 limit = memory_limit_ - size; | 456 limit = memory_limit_ - size; |
457 | 457 |
458 if (bytes_allocated_ > limit) | 458 if (bytes_allocated_ > limit) |
459 ReduceMemoryUsageUntilWithinLimit(limit); | 459 ReduceMemoryUsageUntilWithinLimit(limit); |
460 | 460 |
461 std::unique_ptr<base::DiscardableSharedMemory> memory( | 461 std::unique_ptr<base::DiscardableSharedMemory> memory( |
462 new base::DiscardableSharedMemory); | 462 new base::DiscardableSharedMemory); |
463 if (!memory->CreateAndMap(size)) { | 463 if (!memory->CreateAndMap(size)) { |
464 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 464 *shared_memory_handle = base::SharedMemoryHandle(); |
465 return; | 465 return; |
466 } | 466 } |
467 | 467 |
468 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_; | 468 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_; |
469 checked_bytes_allocated += memory->mapped_size(); | 469 checked_bytes_allocated += memory->mapped_size(); |
470 if (!checked_bytes_allocated.IsValid()) { | 470 if (!checked_bytes_allocated.IsValid()) { |
471 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 471 *shared_memory_handle = base::SharedMemoryHandle(); |
472 return; | 472 return; |
473 } | 473 } |
474 | 474 |
475 bytes_allocated_ = checked_bytes_allocated.ValueOrDie(); | 475 bytes_allocated_ = checked_bytes_allocated.ValueOrDie(); |
476 BytesAllocatedChanged(bytes_allocated_); | 476 BytesAllocatedChanged(bytes_allocated_); |
477 | 477 |
478 *shared_memory_handle = base::SharedMemory::DuplicateHandle(memory->handle()); | 478 *shared_memory_handle = base::SharedMemory::DuplicateHandle(memory->handle()); |
479 // Close file descriptor to avoid running out. | 479 // Close file descriptor to avoid running out. |
480 memory->Close(); | 480 memory->Close(); |
481 | 481 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 return; | 625 return; |
626 | 626 |
627 enforce_memory_policy_pending_ = true; | 627 enforce_memory_policy_pending_ = true; |
628 DCHECK(enforce_memory_policy_task_runner_); | 628 DCHECK(enforce_memory_policy_task_runner_); |
629 enforce_memory_policy_task_runner_->PostDelayedTask( | 629 enforce_memory_policy_task_runner_->PostDelayedTask( |
630 FROM_HERE, enforce_memory_policy_callback_, | 630 FROM_HERE, enforce_memory_policy_callback_, |
631 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); | 631 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); |
632 } | 632 } |
633 | 633 |
634 } // namespace discardable_memory | 634 } // namespace discardable_memory |
OLD | NEW |