Chromium Code Reviews| 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 <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 namespace nacl_io { | 12 namespace nacl_io { |
| 13 | 13 |
| 14 FIFOChar::FIFOChar(size_t size) | 14 FIFOChar::FIFOChar(size_t size) |
| 15 : buffer_(NULL), size_(size), avail_(0), tail_(0) { | 15 : buffer_(NULL), size_(size), avail_(0), tail_(0) { |
| 16 if (size) | 16 if (size) |
| 17 buffer_ = new char[size]; | 17 buffer_ = (char*)malloc(size); |
|
binji
2014/08/05 19:30:29
we should probably check for NULL returns for all
Sam Clegg
2014/08/06 09:10:32
Done. Although there is not much useful we can do
| |
| 18 } | 18 } |
| 19 | 19 |
| 20 FIFOChar::~FIFOChar() { | 20 FIFOChar::~FIFOChar() { |
| 21 delete[] buffer_; | 21 free(buffer_); |
| 22 } | 22 } |
| 23 | 23 |
| 24 bool FIFOChar::IsEmpty() { | 24 bool FIFOChar::IsEmpty() { |
| 25 return avail_ == 0; | 25 return avail_ == 0; |
| 26 } | 26 } |
| 27 | 27 |
| 28 bool FIFOChar::IsFull() { | 28 bool FIFOChar::IsFull() { |
| 29 return avail_ >= size_; | 29 return avail_ >= size_; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool FIFOChar::Resize(size_t len) { | 32 bool FIFOChar::Resize(size_t len) { |
| 33 // Can not resize smaller than the current size | 33 // Can not resize smaller than the current size |
| 34 if (len < avail_) | 34 if (len < avail_) |
| 35 return false; | 35 return false; |
| 36 | 36 |
| 37 // Read current data into new buffer | 37 // Resize buffer |
| 38 char* data = new char[len]; | 38 buffer_ = (char*)realloc(buffer_, len); |
| 39 avail_ = Read(data, avail_); | |
| 40 | |
| 41 // Replace buffer | |
| 42 delete[] buffer_; | |
| 43 buffer_ = data; | |
| 44 size_ = len; | 39 size_ = len; |
| 45 return true; | 40 return true; |
| 46 } | 41 } |
| 47 | 42 |
| 48 size_t FIFOChar::ReadAvailable() { | 43 size_t FIFOChar::ReadAvailable() { |
| 49 return avail_; | 44 return avail_; |
| 50 } | 45 } |
| 51 | 46 |
| 52 size_t FIFOChar::WriteAvailable() { | 47 size_t FIFOChar::WriteAvailable() { |
| 53 return size_ - avail_; | 48 return size_ - avail_; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 | 99 |
| 105 out += write_len; | 100 out += write_len; |
| 106 len -= write_len; | 101 len -= write_len; |
| 107 } | 102 } |
| 108 | 103 |
| 109 avail_ += out; | 104 avail_ += out; |
| 110 return out; | 105 return out; |
| 111 } | 106 } |
| 112 | 107 |
| 113 } // namespace nacl_io | 108 } // namespace nacl_io |
| OLD | NEW |