OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #ifndef PLATFORMS_STM_DISCO_FLETCH_SRC_CIRCULAR_BUFFER_H_ | |
6 #define PLATFORMS_STM_DISCO_FLETCH_SRC_CIRCULAR_BUFFER_H_ | |
7 | |
8 #include <inttypes.h> | |
9 #include <stdlib.h> | |
10 | |
11 #include "src/shared/platform.h" | |
12 | |
13 #define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
14 | |
15 // Circular buffer holding bytes. | |
16 class CircularBuffer { | |
17 public: | |
18 enum Blocking { | |
19 kDontBlock, | |
20 kBlock, | |
21 }; | |
22 | |
23 // Create a new circular buffer holding up to 'capacity' bytes of | |
24 // data. | |
25 explicit CircularBuffer(size_t capacity); | |
26 ~CircularBuffer(); | |
27 | |
28 // Returns whether the buffer is empty. | |
29 bool IsEmpty(); | |
30 | |
31 // Returns whether the buffer is full. | |
32 bool IsFull(); | |
33 | |
34 // Read up to count bytes into buffer. If block is kBlock the call | |
35 // will not return until at least one byte is read. | |
36 size_t Read(uint8_t* buffer, size_t count, Blocking block); | |
37 | |
38 // Write up to count bytes from buffer. If block is kBlock the call | |
39 // will not return until at least one byte is written. | |
40 size_t Write(const uint8_t* buffer, size_t count, Blocking block); | |
41 | |
42 private: | |
43 fletch::Monitor* monitor_; | |
44 int waiting_; | |
45 uint8_t* buffer_; | |
46 size_t capacity_; | |
47 int head_; | |
48 int tail_; | |
49 }; | |
50 | |
51 #endif // PLATFORMS_STM_DISCO_FLETCH_SRC_CIRCULAR_BUFFER_H_ | |
OLD | NEW |