Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/fifo_char.cc

Issue 23498015: [NaCl SDK] Support non blocking TCP/UDP (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove event friends, rename EventListenerPoll Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "nacl_io/fifo_char.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <algorithm>
11
12 namespace nacl_io {
13
14 FIFOChar::FIFOChar(size_t size)
15 : buffer_(NULL),
16 size_(size),
17 avail_(0),
18 tail_(0) {
19 if (size) {
20 buffer_ = new char[size];
binji 2013/09/12 01:47:57 use a std::vector instead of an array?
noelallen1 2013/09/12 23:19:03 Since this is a circular buffer, the resize case g
21 } else {
22 buffer_ = NULL;
23 }
24 }
25
26 FIFOChar::~FIFOChar() {
27 delete[] buffer_;
28 }
29
30 bool FIFOChar::IsEmpty() {
31 return avail_ == 0;
32 }
33
34 bool FIFOChar::IsFull() {
35 return avail_ >= size_;
36 }
37
38 bool FIFOChar::Resize(size_t len) {
39 // Can not resize smaller than the current size
40 if (len < avail_)
41 return false;
42
43 // Read current data into new buffer
44 char* data = new char[len];
45 avail_ = Read(data, avail_);
46
47 // Replace buffer
48 delete[] buffer_;
49 buffer_ = data;
50 size_ = len;
51 return true;
52 }
53
54
55 size_t FIFOChar::ReadAvailable() {
56 return avail_;
57 }
58
59 size_t FIFOChar::WriteAvailable() {
60 return size_ - avail_;
61 }
62
63 size_t FIFOChar::Peek(char* buf, size_t len) {
64 size_t out = 0;
65 len = std::min(len, avail_);
66
67 size_t offs = tail_;
68 while (len > 0) {
binji 2013/09/12 01:47:57 Seems unnecessary: You only need to do one or two
69 size_t read_len = std::min(len, size_ - offs);
70 memcpy(buf, &buffer_[offs], read_len);
71
72 buf += read_len;
73 offs += read_len;
74 if (static_cast<size_t>(offs) == size_)
75 offs = 0;
76
77 out += read_len;
78 len -= read_len;
79 }
80
81 return out;
82 }
83
84 size_t FIFOChar::Read(char* buf, size_t len) {
85 size_t read_len = Peek(buf, len);
86 if (read_len > 0) {
87 avail_ -= read_len;
88 tail_ = (tail_ + read_len) % size_;
89 }
90 return read_len;
91 }
92
93 size_t FIFOChar::Write(const char* buf, size_t len) {
binji 2013/09/12 01:47:57 tests for this class?
94 size_t out = 0;
95 size_t room = size_ - avail_;
96 len = std::min(len, room);
97
98 size_t offs = tail_ + avail_;
99 while (len > 0) {
100 size_t write_len = std::min(len, size_ - offs);
101 memcpy(&buffer_[offs], buf, write_len);
102
103 buf += write_len;
104 offs += write_len;
105 if (offs == size_)
106 offs = 0;
107
108 out += write_len;
109 len -= write_len;
110 }
111
112 avail_ += out;
113 return out;
114 }
115
116
117 } // namespace nacl_io
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698