summaryrefslogtreecommitdiff
path: root/chromium/net/base/datagram_buffer.cc
blob: ecb1e73cabe5f9765f2e8f35b0c53ab66e349a76 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2018 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 "net/base/datagram_buffer.h"

#include <cstring>

#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"

namespace net {

DatagramBufferPool::DatagramBufferPool(size_t max_buffer_size)
    : max_buffer_size_(max_buffer_size) {}

DatagramBufferPool::~DatagramBufferPool() {}

void DatagramBufferPool::Enqueue(const char* buffer,
                                 size_t buf_len,
                                 DatagramBuffers* buffers) {
  DCHECK_LE(buf_len, max_buffer_size_);
  std::unique_ptr<DatagramBuffer> datagram_buffer;
  if (free_list_.empty()) {
    datagram_buffer = quic::QuicWrapUnique<DatagramBuffer>(
        new DatagramBuffer(max_buffer_size_));
  } else {
    datagram_buffer = std::move(free_list_.front());
    free_list_.pop_front();
  }
  datagram_buffer->Set(buffer, buf_len);
  buffers->emplace_back(std::move(datagram_buffer));
}

void DatagramBufferPool::Dequeue(DatagramBuffers* buffers) {
  if (buffers->size() == 0)
    return;

  free_list_.splice(free_list_.cend(), *buffers);
}

DatagramBuffer::DatagramBuffer(size_t max_buffer_size)
    : data_(new char[max_buffer_size]), length_(0) {}

DatagramBuffer::~DatagramBuffer() {}

void DatagramBuffer::Set(const char* buffer, size_t buf_len) {
  length_ = buf_len;
  std::memcpy(data_.get(), buffer, buf_len);
}

char* DatagramBuffer::data() const {
  return data_.get();
}

size_t DatagramBuffer::length() const {
  return length_;
}

}  // namespace net