Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RESOURCE_BUFFER_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_RESOURCE_BUFFER_H_ | |
| 7 | |
| 8 #include <queue> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/shared_memory.h" | |
| 13 #include "content/common/content_export.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 // ResourceBuffer implements a simple "circular buffer" allocation strategy. | |
| 18 // Allocations are recycled in FIFO order. | |
| 19 // | |
| 20 // You can think of the ResourceBuffer as a FIFO. The Allocate method reserves | |
| 21 // space in the buffer. Allocate may be called multiple times until the buffer | |
|
James Simonsen
2012/09/15 02:19:06
This class has an implicit state machine, mostly b
| |
| 22 // is fully reserved (at which point CanAllocate returns false). Allocations | |
| 23 // are freed in FIFO order via a call to RecycleLeastRecentlyAllocated. | |
| 24 // | |
| 25 // ResourceBuffer is reference-counted for the benefit of consumers, who need | |
| 26 // to ensure that ResourceBuffer stays alive while they are using its memory. | |
| 27 // | |
| 28 class CONTENT_EXPORT ResourceBuffer | |
| 29 : public base::RefCountedThreadSafe<ResourceBuffer> { | |
| 30 public: | |
| 31 ResourceBuffer(); | |
| 32 | |
| 33 // Initialize the shared memory buffer. It will be buffer_size bytes in | |
| 34 // length. The min/max_allocation_size parameters control the behavior of | |
| 35 // the Allocate mtehod. It will prefer to return segments that are | |
|
James Simonsen
2012/09/15 02:19:06
mtehod -> method
| |
| 36 // max_allocation_size in length, but will return segments less than that if | |
| 37 // space is limited. It will not return allocations smaller than | |
| 38 // min_allocation_size. | |
| 39 bool Initialize(int buffer_size, | |
| 40 int min_allocation_size, | |
| 41 int max_allocation_size); | |
| 42 bool IsInitialized() const; | |
| 43 | |
| 44 // Returns a shared memory handle that can be passed to the given process. | |
| 45 // The shared memory handle is only intended to be interpretted by code | |
| 46 // running in the specified process. NOTE: The caller should ensure that | |
| 47 // this memory eventually be returned to the operating system. | |
| 48 bool ShareToProcess(base::ProcessHandle process_handle, | |
| 49 base::SharedMemoryHandle* shared_memory_handle, | |
| 50 int* shared_memory_size); | |
| 51 | |
| 52 // Returns true if Allocate will succeed. | |
| 53 bool CanAllocate() const; | |
| 54 | |
| 55 // Returns a pointer into the shared memory buffer or NULL if the buffer is | |
| 56 // already fully allocated. The returned size will be max_allocation_size | |
| 57 // unless the buffer is close to being full. | |
| 58 char* Allocate(int* size); | |
| 59 | |
| 60 // Returns the offset into the shared memory buffer where the last allocation | |
| 61 // returned by Allocate can be found. | |
| 62 int GetLastAllocationOffset() const; | |
| 63 | |
| 64 // Called to reduce the size of the last allocation returned by Allocate. It | |
| 65 // is OK for new_size to match the current size of the last allocation. | |
| 66 void ShrinkLastAllocation(int new_size); | |
| 67 | |
| 68 // Called to allow reuse of memory that was previously allocated. See notes | |
| 69 // above the class for more details about this method. | |
| 70 void RecycleLeastRecentlyAllocated(); | |
| 71 | |
| 72 private: | |
| 73 friend class base::RefCountedThreadSafe<ResourceBuffer>; | |
| 74 ~ResourceBuffer(); | |
| 75 | |
| 76 base::SharedMemory shared_mem_; | |
| 77 | |
| 78 int buf_size_; | |
| 79 int min_alloc_size_; | |
| 80 int max_alloc_size_; | |
| 81 | |
| 82 int alloc_start_; | |
| 83 int alloc_end_; | |
| 84 std::queue<int> alloc_sizes_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(ResourceBuffer); | |
| 87 }; | |
| 88 | |
| 89 } // namespace content | |
| 90 | |
| 91 #endif // CONTENT_BROWSER_RENDERER_HOST_RESOURCE_BUFFER_H_ | |
| OLD | NEW |