Chromium Code Reviews| Index: native_client_sdk/src/libraries/nacl_io/fifo_packet.cc |
| diff --git a/native_client_sdk/src/libraries/nacl_io/fifo_packet.cc b/native_client_sdk/src/libraries/nacl_io/fifo_packet.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..42cf76ade3e0a31704305d86b50fd2f711d0d660 |
| --- /dev/null |
| +++ b/native_client_sdk/src/libraries/nacl_io/fifo_packet.cc |
| @@ -0,0 +1,59 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "nacl_io/fifo_packet.h" |
| + |
| +#include <stdlib.h> |
| +#include <string.h> |
| + |
| +#include <algorithm> |
| + |
| +namespace nacl_io { |
| + |
| +FIFOPacket::FIFOPacket(size_t size) |
| + : max_bytes_(size), |
| + cur_bytes_(0) {} |
| + |
| +FIFOPacket::~FIFOPacket() { |
| + while (!IsEmpty()) |
| + delete Read(); |
| +} |
| + |
| +bool FIFOPacket::IsEmpty() { |
| + return packets_.empty(); |
| +} |
| + |
| +bool FIFOPacket::Resize(size_t len) { |
| + max_bytes_ = len; |
| + return true; |
|
binji
2013/09/12 01:47:57
why always return true here? What if len < cur_byt
noelallen1
2013/09/12 23:19:03
For Packet FIFOs, the requested size is a hint so
|
| +} |
| + |
| +bool FIFOPacket::IsFull() { |
| + return cur_bytes_ >= max_bytes_; |
| +} |
| + |
| +UDPPacket* FIFOPacket::Peek() { |
| + if (packets_.empty()) |
| + return NULL; |
| + |
| + return packets_.back(); |
| +} |
| + |
| +UDPPacket* FIFOPacket::Read() { |
| + if (packets_.empty()) |
| + return NULL; |
| + |
| + UDPPacket* out = packets_.back(); |
| + packets_.pop_back(); |
| + |
| + cur_bytes_ -= out->len_; |
| + return out; |
| +} |
| + |
| +void FIFOPacket::Write(UDPPacket* packet) { |
| + cur_bytes_ += packet->len_; |
| + packets_.push_front(packet); |
| +} |
| + |
| +} // namespace nacl_io |