summaryrefslogtreecommitdiff
path: root/lib/xray/xray_buffer_queue.cc
blob: bd8f4961e7abc7086f06342520dd8ef588717d18 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//===-- xray_buffer_queue.cc -----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a dynamic runtime instruementation system.
//
// Defines the interface for a buffer queue implementation.
//
//===----------------------------------------------------------------------===//
#include "xray_buffer_queue.h"
#include <cassert>
#include <cstdlib>

using namespace __xray;

BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success)
    : BufferSize(B), Buffers(N), Mutex(), OwnedBuffers(), Finalizing(false) {
  for (auto &T : Buffers) {
    void *Tmp = malloc(BufferSize);
    if (Tmp == nullptr) {
      Success = false;
      return;
    }

    auto &Buf = std::get<0>(T);
    Buf.Buffer = Tmp;
    Buf.Size = B;
    OwnedBuffers.emplace(Tmp);
  }
  Success = true;
}

std::error_code BufferQueue::getBuffer(Buffer &Buf) {
  if (Finalizing.load(std::memory_order_acquire))
    return std::make_error_code(std::errc::state_not_recoverable);
  std::lock_guard<std::mutex> Guard(Mutex);
  if (Buffers.empty())
    return std::make_error_code(std::errc::not_enough_memory);
  auto &T = Buffers.front();
  auto &B = std::get<0>(T);
  Buf = B;
  B.Buffer = nullptr;
  B.Size = 0;
  Buffers.pop_front();
  return {};
}

std::error_code BufferQueue::releaseBuffer(Buffer &Buf) {
  if (OwnedBuffers.count(Buf.Buffer) == 0)
    return std::make_error_code(std::errc::argument_out_of_domain);
  std::lock_guard<std::mutex> Guard(Mutex);

  // Now that the buffer has been released, we mark it as "used".
  Buffers.emplace(Buffers.end(), Buf, true /* used */);
  Buf.Buffer = nullptr;
  Buf.Size = 0;
  return {};
}

std::error_code BufferQueue::finalize() {
  if (Finalizing.exchange(true, std::memory_order_acq_rel))
    return std::make_error_code(std::errc::state_not_recoverable);
  return {};
}

BufferQueue::~BufferQueue() {
  for (auto &T : Buffers) {
    auto &Buf = std::get<0>(T);
    free(Buf.Buffer);
  }
}