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"
15 FIFOChar::FIFOChar(size_t size
)
16 : buffer_(NULL
), size_(size
), avail_(0), tail_(0) {
18 buffer_
= (char*)malloc(size
);
19 assert(buffer_
!= NULL
);
23 FIFOChar::~FIFOChar() {
27 bool FIFOChar::IsEmpty() {
31 bool FIFOChar::IsFull() {
32 return avail_
>= size_
;
35 bool FIFOChar::Resize(size_t len
) {
36 // Can not resize smaller than the current size
41 buffer_
= (char*)realloc(buffer_
, len
);
42 assert(buffer_
!= NULL
);
49 size_t FIFOChar::ReadAvailable() {
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
);
61 len
= std::min(len
, avail_
);
65 size_t read_len
= std::min(len
, size_
- offs
);
66 memcpy(ptr
, &buffer_
[offs
], read_len
);
81 size_t FIFOChar::Read(void* buf
, size_t len
) {
82 size_t read_len
= Peek(buf
, len
);
85 tail_
= (tail_
+ read_len
) % size_
;
90 size_t FIFOChar::Write(const void* buf
, size_t len
) {
91 const char* ptr
= static_cast<const char*>(buf
);
93 size_t room
= size_
- avail_
;
94 len
= std::min(len
, room
);
96 size_t offs
= (tail_
+ avail_
) % size_
;
98 size_t write_len
= std::min(len
, size_
- offs
);
99 memcpy(&buffer_
[offs
], ptr
, write_len
);
114 } // namespace nacl_io