| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/loader/resource_buffer.h" | 5 #include "content/browser/loader/resource_buffer.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/trace_event/trace_event.h" |
| 10 | 11 |
| 11 namespace content { | 12 namespace content { |
| 12 | 13 |
| 13 // A circular buffer allocator. | 14 // A circular buffer allocator. |
| 14 // | 15 // |
| 15 // We keep track of the starting offset (alloc_start_) and the ending offset | 16 // We keep track of the starting offset (alloc_start_) and the ending offset |
| 16 // (alloc_end_). There are two layouts to keep in mind: | 17 // (alloc_end_). There are two layouts to keep in mind: |
| 17 // | 18 // |
| 18 // #1: | 19 // #1: |
| 19 // ------------[XXXXXXXXXXXXXXXXXXXXXXX]---- | 20 // ------------[XXXXXXXXXXXXXXXXXXXXXXX]---- |
| (...skipping 17 matching lines...) Expand all Loading... |
| 37 alloc_start_(-1), | 38 alloc_start_(-1), |
| 38 alloc_end_(-1) { | 39 alloc_end_(-1) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 ResourceBuffer::~ResourceBuffer() { | 42 ResourceBuffer::~ResourceBuffer() { |
| 42 } | 43 } |
| 43 | 44 |
| 44 bool ResourceBuffer::Initialize(int buffer_size, | 45 bool ResourceBuffer::Initialize(int buffer_size, |
| 45 int min_allocation_size, | 46 int min_allocation_size, |
| 46 int max_allocation_size) { | 47 int max_allocation_size) { |
| 48 TRACE_EVENT0("toplevel", "ResourceBuffer::Initialize("); |
| 49 |
| 47 CHECK(!IsInitialized()); | 50 CHECK(!IsInitialized()); |
| 48 | 51 |
| 49 // It would be wasteful if these are not multiples of min_allocation_size. | 52 // It would be wasteful if these are not multiples of min_allocation_size. |
| 50 CHECK_EQ(0, buffer_size % min_allocation_size); | 53 CHECK_EQ(0, buffer_size % min_allocation_size); |
| 51 CHECK_EQ(0, max_allocation_size % min_allocation_size); | 54 CHECK_EQ(0, max_allocation_size % min_allocation_size); |
| 52 | 55 |
| 53 buf_size_ = buffer_size; | 56 buf_size_ = buffer_size; |
| 54 min_alloc_size_ = min_allocation_size; | 57 min_alloc_size_ = min_allocation_size; |
| 55 max_alloc_size_ = max_allocation_size; | 58 max_alloc_size_ = max_allocation_size; |
| 56 | 59 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 73 return true; | 76 return true; |
| 74 | 77 |
| 75 int diff = alloc_end_ - alloc_start_; | 78 int diff = alloc_end_ - alloc_start_; |
| 76 if (diff > 0) | 79 if (diff > 0) |
| 77 return (buf_size_ - diff) >= min_alloc_size_; | 80 return (buf_size_ - diff) >= min_alloc_size_; |
| 78 | 81 |
| 79 return -diff >= min_alloc_size_; | 82 return -diff >= min_alloc_size_; |
| 80 } | 83 } |
| 81 | 84 |
| 82 char* ResourceBuffer::Allocate(int* size) { | 85 char* ResourceBuffer::Allocate(int* size) { |
| 86 TRACE_EVENT0("toplevel", "ResourceBuffer::Allocate"); |
| 83 CHECK(CanAllocate()); | 87 CHECK(CanAllocate()); |
| 84 | 88 |
| 85 int alloc_offset = 0; | 89 int alloc_offset = 0; |
| 86 int alloc_size; | 90 int alloc_size; |
| 87 | 91 |
| 88 if (alloc_start_ == -1) { | 92 if (alloc_start_ == -1) { |
| 89 // This is the first allocation. | 93 // This is the first allocation. |
| 90 alloc_start_ = 0; | 94 alloc_start_ = 0; |
| 91 alloc_end_ = buf_size_; | 95 alloc_end_ = buf_size_; |
| 92 alloc_size = buf_size_; | 96 alloc_size = buf_size_; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 CHECK(alloc_sizes_.empty()); | 168 CHECK(alloc_sizes_.empty()); |
| 165 alloc_start_ = -1; | 169 alloc_start_ = -1; |
| 166 alloc_end_ = -1; | 170 alloc_end_ = -1; |
| 167 } else if (alloc_start_ == buf_size_) { | 171 } else if (alloc_start_ == buf_size_) { |
| 168 CHECK(!alloc_sizes_.empty()); | 172 CHECK(!alloc_sizes_.empty()); |
| 169 alloc_start_ = 0; | 173 alloc_start_ = 0; |
| 170 } | 174 } |
| 171 } | 175 } |
| 172 | 176 |
| 173 } // namespace content | 177 } // namespace content |
| OLD | NEW |