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); |
40 | 43 if (buffer_ == NULL) |
41 // Replace buffer | 44 return false; |
42 delete[] buffer_; | |
43 buffer_ = data; | |
44 size_ = len; | 45 size_ = len; |
45 return true; | 46 return true; |
46 } | 47 } |
47 | 48 |
48 size_t FIFOChar::ReadAvailable() { | 49 size_t FIFOChar::ReadAvailable() { |
49 return avail_; | 50 return avail_; |
50 } | 51 } |
51 | 52 |
52 size_t FIFOChar::WriteAvailable() { | 53 size_t FIFOChar::WriteAvailable() { |
53 return size_ - avail_; | 54 return size_ - avail_; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 105 |
105 out += write_len; | 106 out += write_len; |
106 len -= write_len; | 107 len -= write_len; |
107 } | 108 } |
108 | 109 |
109 avail_ += out; | 110 avail_ += out; |
110 return out; | 111 return out; |
111 } | 112 } |
112 | 113 |
113 } // namespace nacl_io | 114 } // namespace nacl_io |
OLD | NEW |