Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / fifo_char.cc
blob144f5842be632b750fd6c64f69b79c282683a085
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
3 // found in the LICENSE file.
5 #include "nacl_io/fifo_char.h"
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include <algorithm>
13 namespace nacl_io {
15 FIFOChar::FIFOChar(size_t size)
16 : buffer_(NULL), size_(size), avail_(0), tail_(0) {
17 if (size) {
18 buffer_ = (char*)malloc(size);
19 assert(buffer_ != NULL);
23 FIFOChar::~FIFOChar() {
24 free(buffer_);
27 bool FIFOChar::IsEmpty() {
28 return avail_ == 0;
31 bool FIFOChar::IsFull() {
32 return avail_ >= size_;
35 bool FIFOChar::Resize(size_t len) {
36 // Can not resize smaller than the current size
37 if (len < avail_)
38 return false;
40 // Resize buffer
41 buffer_ = (char*)realloc(buffer_, len);
42 assert(buffer_ != NULL);
43 if (buffer_ == NULL)
44 return false;
45 size_ = len;
46 return true;
49 size_t FIFOChar::ReadAvailable() {
50 return avail_;
53 size_t FIFOChar::WriteAvailable() {
54 return size_ - avail_;
57 size_t FIFOChar::Peek(void* buf, size_t len) {
58 char* ptr = static_cast<char*>(buf);
60 size_t out = 0;
61 len = std::min(len, avail_);
63 size_t offs = tail_;
64 while (len > 0) {
65 size_t read_len = std::min(len, size_ - offs);
66 memcpy(ptr, &buffer_[offs], read_len);
68 ptr += read_len;
69 offs += read_len;
71 if (offs == size_)
72 offs = 0;
74 out += read_len;
75 len -= read_len;
78 return out;
81 size_t FIFOChar::Read(void* buf, size_t len) {
82 size_t read_len = Peek(buf, len);
83 if (read_len > 0) {
84 avail_ -= read_len;
85 tail_ = (tail_ + read_len) % size_;
87 return read_len;
90 size_t FIFOChar::Write(const void* buf, size_t len) {
91 const char* ptr = static_cast<const char*>(buf);
92 size_t out = 0;
93 size_t room = size_ - avail_;
94 len = std::min(len, room);
96 size_t offs = (tail_ + avail_) % size_;
97 while (len > 0) {
98 size_t write_len = std::min(len, size_ - offs);
99 memcpy(&buffer_[offs], ptr, write_len);
101 ptr += write_len;
102 offs += write_len;
103 if (offs == size_)
104 offs = 0;
106 out += write_len;
107 len -= write_len;
110 avail_ += out;
111 return out;
114 } // namespace nacl_io