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 PPAPI_SHARED_IMPL_CIRCULAR_BUFFER_H_ | |
| 6 #define PPAPI_SHARED_IMPL_CIRCULAR_BUFFER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "ppapi/shared_impl/ppapi_shared_export.h" | |
| 10 | |
| 11 namespace ppapi { | |
| 12 | |
| 13 class PPAPI_SHARED_EXPORT CircularBuffer { | |
| 14 public: | |
| 15 // Construct a CircularBuffer by given underlying buffer and size. | |
| 16 CircularBuffer(void* buffer, uint32_t size); | |
| 17 | |
| 18 ~CircularBuffer(); | |
| 19 | |
| 20 // Remaining buffer size for reading or writing. | |
| 21 uint32_t remaining() const { | |
| 22 if (position_ < limit_) | |
| 23 return limit_ - position_; | |
| 24 if (position_ > limit_) | |
| 25 return buffer_size_ + limit_ - position_; | |
| 26 return maybe_empty_ ? 0 : buffer_size_; | |
| 27 } | |
|
bbudge
2013/12/31 00:51:53
This is a bit long and complicated to be an inline
Peng
2013/12/31 23:33:54
Add remaining_ field, and this function returns it
| |
| 28 | |
| 29 // Moves position forward by given offset. | |
| 30 void MovePosition(uint32_t offset); | |
| 31 | |
| 32 // Moves limit forward by given offset. | |
| 33 void MoveLimit(uint32_t offset); | |
| 34 | |
| 35 // Reads data and move position forward. The actual read size will be | |
| 36 // returned. | |
| 37 int32_t Read(void* buffer, uint32_t size); | |
| 38 | |
| 39 // Similar to |Read()|, but it will fail, if the circular buffer does not have | |
| 40 // enough data. | |
| 41 int32_t ReadAll(void* buffer, uint32_t size); | |
| 42 | |
| 43 // Writes data and move position forward. The actual write size will be | |
| 44 // returned. | |
| 45 int32_t Write(const void* buffer, uint32_t size); | |
| 46 | |
| 47 // Similar to |Write()|, but it will fail, if the circular buffer does not | |
| 48 // have enough space for the given size of data. | |
| 49 int32_t WriteAll(const void* buffer, uint32_t size); | |
| 50 | |
| 51 // Locks the given size of underlying buffer for direct accessing. | |
| 52 int32_t Lock(void** buffer, uint32_t size); | |
| 53 | |
| 54 // Relocks underlying buffer. It is used for adjusting locked block size; | |
| 55 int32_t Relock(void* buffer, uint32_t size); | |
| 56 | |
| 57 // Unlocks buffer which is locked by previous |Lock()| call. The locked block | |
| 58 // size will be returned. | |
| 59 int32_t Unlock(const void* buffer); | |
| 60 | |
| 61 // Returns true if the circular buffer is locked. | |
| 62 bool IsLocked() const { return locked_buffer_; } | |
| 63 | |
| 64 private: | |
| 65 int32_t ReadInternal(void* buffer, uint32_t size); | |
| 66 | |
| 67 int32_t WriteInternal(const void* buffer, uint32_t size); | |
| 68 | |
| 69 // Underlying buffer pointer. | |
| 70 uint8_t* buffer_; | |
| 71 | |
| 72 // Underlying buffer size. | |
| 73 uint32_t buffer_size_; | |
| 74 | |
| 75 // Current position for reading or writing. | |
| 76 uint32_t position_; | |
| 77 | |
| 78 // The limit position for reading or writing. | |
| 79 uint32_t limit_; | |
| 80 | |
| 81 // When position_ is equal to limit_, use this value to indicate the buffer is | |
| 82 // empty or full. | |
| 83 bool maybe_empty_; | |
|
bbudge
2013/12/31 00:51:53
It's a little hard to understand how this works. W
Peng
2013/12/31 23:33:54
Done.
| |
| 84 | |
| 85 // Locked buffer pointer returned by |Lock()|. It will be reset to NULL, | |
| 86 // When |Unlock()| is called. | |
| 87 uint8_t* locked_buffer_; | |
| 88 | |
| 89 // Locked buffer size. | |
| 90 uint32_t locked_buffer_size_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(CircularBuffer); | |
| 93 }; | |
| 94 | |
| 95 } // namespace ppapi | |
| 96 | |
| 97 #endif // PPAPI_SHARED_IMPL_CIRCULAR_BUFFER_H_ | |
| OLD | NEW |