OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "nacl_io/fifo_char.h" | 5 #include "nacl_io/fifo_char.h" |
6 | 6 |
7 #include <assert.h> | |
7 #include <stdlib.h> | 8 #include <stdlib.h> |
8 #include <string.h> | 9 #include <string.h> |
9 | 10 |
10 #include <algorithm> | 11 #include <algorithm> |
11 | 12 |
12 namespace nacl_io { | 13 namespace nacl_io { |
13 | 14 |
14 FIFOChar::FIFOChar(size_t size) | 15 FIFOChar::FIFOChar(size_t size) |
15 : buffer_(NULL), size_(size), avail_(0), tail_(0) { | 16 : buffer_(NULL), size_(size), avail_(0), tail_(0) { |
16 if (size) | 17 if (size) { |
17 buffer_ = new char[size]; | 18 buffer_ = (char*)malloc(size); |
19 assert(buffer_ != NULL); | |
20 } | |
18 } | 21 } |
19 | 22 |
20 FIFOChar::~FIFOChar() { | 23 FIFOChar::~FIFOChar() { |
21 delete[] buffer_; | 24 free(buffer_); |
22 } | 25 } |
23 | 26 |
24 bool FIFOChar::IsEmpty() { | 27 bool FIFOChar::IsEmpty() { |
25 return avail_ == 0; | 28 return avail_ == 0; |
26 } | 29 } |
27 | 30 |
28 bool FIFOChar::IsFull() { | 31 bool FIFOChar::IsFull() { |
29 return avail_ >= size_; | 32 return avail_ >= size_; |
30 } | 33 } |
31 | 34 |
32 bool FIFOChar::Resize(size_t len) { | 35 bool FIFOChar::Resize(size_t len) { |
33 // Can not resize smaller than the current size | 36 // Can not resize smaller than the current size |
34 if (len < avail_) | 37 if (len < avail_) |
35 return false; | 38 return false; |
36 | 39 |
37 // Read current data into new buffer | 40 // Resize buffer |
38 char* data = new char[len]; | 41 buffer_ = (char*)realloc(buffer_, len); |
39 avail_ = Read(data, avail_); | 42 assert(buffer_ != NULL); |
binji
2014/08/06 17:17:59
could return false here?
Sam Clegg
2014/08/07 11:49:52
Done.
| |
40 | |
41 // Replace buffer | |
42 delete[] buffer_; | |
43 buffer_ = data; | |
44 size_ = len; | 43 size_ = len; |
45 return true; | 44 return true; |
46 } | 45 } |
47 | 46 |
48 size_t FIFOChar::ReadAvailable() { | 47 size_t FIFOChar::ReadAvailable() { |
49 return avail_; | 48 return avail_; |
50 } | 49 } |
51 | 50 |
52 size_t FIFOChar::WriteAvailable() { | 51 size_t FIFOChar::WriteAvailable() { |
53 return size_ - avail_; | 52 return size_ - avail_; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 | 103 |
105 out += write_len; | 104 out += write_len; |
106 len -= write_len; | 105 len -= write_len; |
107 } | 106 } |
108 | 107 |
109 avail_ += out; | 108 avail_ += out; |
110 return out; | 109 return out; |
111 } | 110 } |
112 | 111 |
113 } // namespace nacl_io | 112 } // namespace nacl_io |
OLD | NEW |