Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 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 #include "ppapi/shared_impl/circular_buffer.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "ppapi/c/pp_errors.h" | |
| 11 | |
| 12 namespace ppapi { | |
| 13 | |
| 14 CircularBuffer::CircularBuffer(void* buffer, uint32_t size) | |
| 15 : buffer_(static_cast<uint8_t*>(buffer)), | |
| 16 buffer_size_(size), | |
| 17 position_(0), | |
| 18 limit_(0), | |
| 19 remaining_(0), | |
| 20 locked_buffer_(NULL), | |
| 21 locked_buffer_size_(0) { | |
| 22 } | |
| 23 | |
| 24 CircularBuffer::~CircularBuffer() { | |
| 25 } | |
| 26 | |
| 27 void CircularBuffer::MovePosition(uint32_t offset) { | |
| 28 CHECK(offset); | |
|
yzshen1
2014/01/03 21:51:41
nit, optional: it seems harmless to allow offset==
| |
| 29 CHECK(offset <= remaining_); | |
|
yzshen1
2014/01/03 21:51:41
Please use CHECK_LE, or maybe we can just return f
| |
| 30 | |
| 31 position_ += offset; | |
| 32 remaining_ -= offset; | |
| 33 if (position_ >= buffer_size_) | |
| 34 position_ -= buffer_size_; | |
| 35 } | |
| 36 | |
| 37 void CircularBuffer::MoveLimit(uint32_t offset) { | |
| 38 CHECK(offset); | |
|
yzshen1
2014/01/03 21:51:41
please see my comment of the previous method.
| |
| 39 CHECK(offset <= (buffer_size_ - remaining_)); | |
|
yzshen1
2014/01/03 21:51:41
Please use CHECK_LE, or maybe we can just return f
| |
| 40 | |
| 41 limit_ += offset; | |
| 42 remaining_ += offset; | |
| 43 if (limit_ >= buffer_size_) | |
| 44 limit_ -= buffer_size_; | |
| 45 } | |
| 46 | |
| 47 int32_t CircularBuffer::Read(void* buffer, uint32_t size) { | |
| 48 // Buffer has been locked. | |
| 49 if (locked_buffer_) | |
| 50 return PP_ERROR_INPROGRESS; | |
| 51 | |
| 52 size = std::min(size, remaining_); | |
| 53 | |
| 54 if (!size) | |
| 55 return 0; | |
| 56 | |
| 57 return ReadInternal(buffer, size); | |
| 58 } | |
| 59 | |
| 60 int32_t CircularBuffer::ReadAll(void* buffer, uint32_t size) { | |
| 61 // Buffer has been locked. | |
| 62 if (locked_buffer_) | |
| 63 return PP_ERROR_INPROGRESS; | |
| 64 | |
| 65 if (size > buffer_size_) | |
| 66 return PP_ERROR_BADARGUMENT; | |
| 67 | |
| 68 if (size > remaining_) | |
| 69 return PP_ERROR_NOSPACE; | |
| 70 | |
| 71 return ReadInternal(buffer, size); | |
| 72 | |
| 73 } | |
| 74 | |
| 75 int32_t CircularBuffer::ReadInternal(void* buffer, uint32_t size) { | |
|
yzshen1
2014/01/03 21:51:41
Order definitions in the same order as declaration
| |
| 76 uint32_t n = std::min(buffer_size_ - position_, size); | |
| 77 memcpy(buffer, buffer_ + position_, n); | |
|
dmichael (off chromium)
2014/01/03 18:05:29
Maybe it would be good to call the parameter somet
| |
| 78 if (n != size) { | |
|
dmichael (off chromium)
2014/01/03 18:05:29
nit: It might be clearer to say:
if (n < size)
(t
| |
| 79 memcpy(static_cast<uint8_t*>(buffer) + n, buffer_, size - n); | |
| 80 } | |
| 81 MovePosition(size); | |
| 82 return size; | |
| 83 } | |
| 84 | |
| 85 int32_t CircularBuffer::Write(const void* buffer, uint32_t size) { | |
| 86 // Buffer has been locked. | |
| 87 if (locked_buffer_) | |
| 88 return PP_ERROR_INPROGRESS; | |
| 89 | |
| 90 size = std::min(size, remaining_); | |
|
yzshen1
2014/01/03 21:51:41
Ah, now I realize that remaining_ has a different
| |
| 91 if (!size) | |
| 92 return 0; | |
| 93 | |
| 94 return WriteInternal(buffer, size); | |
| 95 } | |
| 96 | |
| 97 int32_t CircularBuffer::WriteAll(const void* buffer, uint32_t size) { | |
| 98 // Buffer has been locked. | |
| 99 if (locked_buffer_) | |
| 100 return PP_ERROR_INPROGRESS; | |
| 101 | |
| 102 if (size > buffer_size_) | |
| 103 return PP_ERROR_BADARGUMENT; | |
| 104 | |
| 105 if (size > remaining_) | |
| 106 return PP_ERROR_NOSPACE; | |
| 107 | |
| 108 return WriteInternal(buffer, size); | |
| 109 } | |
| 110 | |
| 111 int32_t CircularBuffer::WriteInternal(const void* buffer, uint32_t size) { | |
| 112 uint32_t n = std::min(buffer_size_ - position_, size); | |
| 113 memcpy(buffer_ + position_, buffer, n); | |
| 114 if (n != size) { | |
| 115 memcpy(buffer_, static_cast<const uint8_t*>(buffer) + n, size - n); | |
| 116 } | |
| 117 MovePosition(size); | |
| 118 return size; | |
| 119 } | |
| 120 | |
| 121 int32_t CircularBuffer::Lock(void** buffer, uint32_t size) { | |
| 122 CHECK(buffer); | |
|
yzshen1
2014/01/03 21:51:41
[for discussion] My understanding:
- if you assume
| |
| 123 CHECK(size); | |
| 124 | |
| 125 if (locked_buffer_) | |
| 126 return PP_ERROR_INPROGRESS; | |
| 127 | |
| 128 // Request memory is not continuous. | |
|
dmichael (off chromium)
2014/01/03 18:05:29
continuous->contiguous?
| |
| 129 if (position_ + size > buffer_size_) | |
| 130 return PP_ERROR_FAILED; | |
| 131 | |
| 132 // Do not have enough space for locking. | |
| 133 if (size > remaining_) | |
| 134 return PP_ERROR_NOSPACE; | |
| 135 | |
| 136 locked_buffer_ = buffer_ + position_; | |
| 137 *buffer = locked_buffer_; | |
| 138 locked_buffer_size_ = size; | |
| 139 return PP_OK; | |
| 140 } | |
| 141 | |
| 142 int32_t CircularBuffer::Relock(void* buffer, uint32_t size) { | |
| 143 if (locked_buffer_ != buffer) | |
| 144 return PP_ERROR_BADARGUMENT; | |
|
dmichael (off chromium)
2014/01/03 18:05:29
Why do you not have the same checks here for enoug
| |
| 145 locked_buffer_size_ = size; | |
| 146 return PP_OK; | |
| 147 } | |
| 148 | |
| 149 int32_t CircularBuffer::Unlock(const void* buffer) { | |
| 150 if (!buffer) | |
| 151 return PP_ERROR_BADARGUMENT; | |
| 152 if (locked_buffer_ != buffer) | |
| 153 return PP_ERROR_BADARGUMENT; | |
| 154 | |
| 155 int32_t size = locked_buffer_size_; | |
| 156 | |
| 157 locked_buffer_ = NULL; | |
| 158 locked_buffer_size_ = 0; | |
| 159 | |
| 160 MovePosition(size); | |
| 161 return size; | |
| 162 } | |
| 163 | |
| 164 } // namespace ppapi | |
| OLD | NEW |