Chromium Code Reviews| Index: ppapi/shared_impl/circular_buffer.h |
| diff --git a/ppapi/shared_impl/circular_buffer.h b/ppapi/shared_impl/circular_buffer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f05da6359545b93d0452eec677e0c5fe9492dade |
| --- /dev/null |
| +++ b/ppapi/shared_impl/circular_buffer.h |
| @@ -0,0 +1,97 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef PPAPI_SHARED_IMPL_CIRCULAR_BUFFER_H_ |
| +#define PPAPI_SHARED_IMPL_CIRCULAR_BUFFER_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "ppapi/shared_impl/ppapi_shared_export.h" |
| + |
| +namespace ppapi { |
| + |
| +class PPAPI_SHARED_EXPORT CircularBuffer { |
| + public: |
| + // Construct a CircularBuffer by given underlying buffer and size. |
| + CircularBuffer(void* buffer, uint32_t size); |
| + |
| + ~CircularBuffer(); |
| + |
| + // Remaining buffer size for reading or writing. |
| + uint32_t remaining() const { |
| + if (position_ < limit_) |
| + return limit_ - position_; |
| + if (position_ > limit_) |
| + return buffer_size_ + limit_ - position_; |
| + return maybe_empty_ ? 0 : buffer_size_; |
| + } |
|
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
|
| + |
| + // Moves position forward by given offset. |
| + void MovePosition(uint32_t offset); |
| + |
| + // Moves limit forward by given offset. |
| + void MoveLimit(uint32_t offset); |
| + |
| + // Reads data and move position forward. The actual read size will be |
| + // returned. |
| + int32_t Read(void* buffer, uint32_t size); |
| + |
| + // Similar to |Read()|, but it will fail, if the circular buffer does not have |
| + // enough data. |
| + int32_t ReadAll(void* buffer, uint32_t size); |
| + |
| + // Writes data and move position forward. The actual write size will be |
| + // returned. |
| + int32_t Write(const void* buffer, uint32_t size); |
| + |
| + // Similar to |Write()|, but it will fail, if the circular buffer does not |
| + // have enough space for the given size of data. |
| + int32_t WriteAll(const void* buffer, uint32_t size); |
| + |
| + // Locks the given size of underlying buffer for direct accessing. |
| + int32_t Lock(void** buffer, uint32_t size); |
| + |
| + // Relocks underlying buffer. It is used for adjusting locked block size; |
| + int32_t Relock(void* buffer, uint32_t size); |
| + |
| + // Unlocks buffer which is locked by previous |Lock()| call. The locked block |
| + // size will be returned. |
| + int32_t Unlock(const void* buffer); |
| + |
| + // Returns true if the circular buffer is locked. |
| + bool IsLocked() const { return locked_buffer_; } |
| + |
| + private: |
| + int32_t ReadInternal(void* buffer, uint32_t size); |
| + |
| + int32_t WriteInternal(const void* buffer, uint32_t size); |
| + |
| + // Underlying buffer pointer. |
| + uint8_t* buffer_; |
| + |
| + // Underlying buffer size. |
| + uint32_t buffer_size_; |
| + |
| + // Current position for reading or writing. |
| + uint32_t position_; |
| + |
| + // The limit position for reading or writing. |
| + uint32_t limit_; |
| + |
| + // When position_ is equal to limit_, use this value to indicate the buffer is |
| + // empty or full. |
| + 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.
|
| + |
| + // Locked buffer pointer returned by |Lock()|. It will be reset to NULL, |
| + // When |Unlock()| is called. |
| + uint8_t* locked_buffer_; |
| + |
| + // Locked buffer size. |
| + uint32_t locked_buffer_size_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CircularBuffer); |
| +}; |
| + |
| +} // namespace ppapi |
| + |
| +#endif // PPAPI_SHARED_IMPL_CIRCULAR_BUFFER_H_ |