summaryrefslogtreecommitdiffstats
path: root/net/curvecp/circular_buffer.h
blob: 1c257f9617b43b53643abc04f77e6001fade8380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) 2011 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.

#ifndef NET_CURVECP_CIRCULAR_BUFFER_H_
#define NET_CURVECP_CIRCULAR_BUFFER_H_
#pragma once

namespace net {

// A circular buffer is a fixed sized buffer which can be read or written
class CircularBuffer {
 public:
  // Create a CircularBuffer with maximum size |capacity|.
  explicit CircularBuffer(int capacity);
  ~CircularBuffer();

  int length() const { return length_; }

  // Writes data into the circular buffer.
  // |data| is the bytes to write.
  // |length| is the number of bytes to write.
  // Returns the number of bytes written, which may be less than |length| or
  // 0 if no space is available in the buffer.
  int write(const char* data, int length);

  // Reads up to |length| bytes from the buffer into |data|.
  // Returns the number of bytes read, or 0 if no data is available.
  int read(char* data, int length);

 private:
  int capacity_;
  int head_;
  int length_;
  char* buffer_;
};

}  // namespace net

#endif  //  NET_CURVECP_CIRCULAR_BUFFER_H_