OLD | NEW |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #include "platforms/stm/disco_fletch/src/circular_buffer.h" | 5 #include "platforms/stm/disco_dartino/src/circular_buffer.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 CircularBuffer::CircularBuffer(size_t capacity) { | 10 CircularBuffer::CircularBuffer(size_t capacity) { |
11 monitor_ = fletch::Platform::CreateMonitor(); | 11 monitor_ = dartino::Platform::CreateMonitor(); |
12 // One additional byte needed to distinguish between empty and full. | 12 // One additional byte needed to distinguish between empty and full. |
13 capacity_ = capacity + 1; | 13 capacity_ = capacity + 1; |
14 buffer_ = new uint8_t[capacity_]; | 14 buffer_ = new uint8_t[capacity_]; |
15 head_ = tail_ = 0; | 15 head_ = tail_ = 0; |
16 } | 16 } |
17 | 17 |
18 CircularBuffer::~CircularBuffer() { | 18 CircularBuffer::~CircularBuffer() { |
19 delete[] buffer_; | 19 delete[] buffer_; |
20 } | 20 } |
21 | 21 |
22 bool CircularBuffer::IsEmpty() { | 22 bool CircularBuffer::IsEmpty() { |
23 fletch::ScopedMonitorLock locker(monitor_); | 23 dartino::ScopedMonitorLock locker(monitor_); |
24 return head_ == tail_; | 24 return head_ == tail_; |
25 } | 25 } |
26 | 26 |
27 bool CircularBuffer::IsFull() { | 27 bool CircularBuffer::IsFull() { |
28 fletch::ScopedMonitorLock locker(monitor_); | 28 dartino::ScopedMonitorLock locker(monitor_); |
29 return ((head_ + 1) % capacity_) == tail_; | 29 return ((head_ + 1) % capacity_) == tail_; |
30 } | 30 } |
31 | 31 |
32 size_t CircularBuffer::Read(uint8_t* data, size_t count, Blocking block) { | 32 size_t CircularBuffer::Read(uint8_t* data, size_t count, Blocking block) { |
33 fletch::ScopedMonitorLock locker(monitor_); | 33 dartino::ScopedMonitorLock locker(monitor_); |
34 | 34 |
35 // If buffer is empty wait for data. | 35 // If buffer is empty wait for data. |
36 if (block == kBlock) { | 36 if (block == kBlock) { |
37 if (head_ == tail_) { | 37 if (head_ == tail_) { |
38 monitor_->Wait(); | 38 monitor_->Wait(); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 int bytes; | 42 int bytes; |
43 int read = 0; | 43 int read = 0; |
(...skipping 12 matching lines...) Expand all Loading... |
56 tail_ = (tail_ + bytes) % capacity_; | 56 tail_ = (tail_ + bytes) % capacity_; |
57 } | 57 } |
58 | 58 |
59 monitor_->Notify(); | 59 monitor_->Notify(); |
60 | 60 |
61 return read; | 61 return read; |
62 } | 62 } |
63 | 63 |
64 size_t CircularBuffer::Write( | 64 size_t CircularBuffer::Write( |
65 const uint8_t* data, size_t count, Blocking block) { | 65 const uint8_t* data, size_t count, Blocking block) { |
66 fletch::ScopedMonitorLock locker(monitor_); | 66 dartino::ScopedMonitorLock locker(monitor_); |
67 | 67 |
68 // If buffer is full wait for room. | 68 // If buffer is full wait for room. |
69 if (block == kBlock) { | 69 if (block == kBlock) { |
70 if (((head_ + 1) % capacity_) == tail_) { | 70 if (((head_ + 1) % capacity_) == tail_) { |
71 monitor_->Wait(); | 71 monitor_->Wait(); |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 int bytes; | 75 int bytes; |
76 int written = 0; | 76 int written = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
87 bytes = MIN(tail_ - head_ - 1, count - written); | 87 bytes = MIN(tail_ - head_ - 1, count - written); |
88 memcpy(buffer_ + head_, data + written, bytes); | 88 memcpy(buffer_ + head_, data + written, bytes); |
89 written += bytes; | 89 written += bytes; |
90 head_ = (head_ + bytes) % capacity_; | 90 head_ = (head_ + bytes) % capacity_; |
91 } | 91 } |
92 | 92 |
93 monitor_->Notify(); | 93 monitor_->Notify(); |
94 | 94 |
95 return written; | 95 return written; |
96 } | 96 } |
OLD | NEW |