diff options
Diffstat (limited to 'compiler-rt/lib/xray/xray_buffer_queue.cc')
| -rw-r--r-- | compiler-rt/lib/xray/xray_buffer_queue.cc | 49 | 
1 files changed, 35 insertions, 14 deletions
| diff --git a/compiler-rt/lib/xray/xray_buffer_queue.cc b/compiler-rt/lib/xray/xray_buffer_queue.cc index 7ba755ac306..99175a02580 100644 --- a/compiler-rt/lib/xray/xray_buffer_queue.cc +++ b/compiler-rt/lib/xray/xray_buffer_queue.cc @@ -16,6 +16,7 @@  #include "sanitizer_common/sanitizer_common.h"  #include "sanitizer_common/sanitizer_libc.h" +#include <algorithm>  #include <cstdlib>  #include <tuple> @@ -23,18 +24,21 @@ using namespace __xray;  using namespace __sanitizer;  BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success) -    : BufferSize(B), Buffers(N), Mutex(), OwnedBuffers(), Finalizing{0} { -  for (auto &T : Buffers) { +    : BufferSize(B), Buffers(new std::tuple<Buffer, bool>[N]()), +      BufferCount(N), Finalizing{0}, OwnedBuffers(new void *[N]()), +      Next(Buffers.get()), First(nullptr) { +  for (size_t i = 0; i < N; ++i) { +    auto &T = Buffers[i];      void *Tmp = malloc(BufferSize);      if (Tmp == nullptr) {        Success = false;        return;      } -      auto &Buf = std::get<0>(T); +    std::get<1>(T) = false;      Buf.Buffer = Tmp;      Buf.Size = B; -    OwnedBuffers.emplace(Tmp); +    OwnedBuffers[i] = Tmp;    }    Success = true;  } @@ -42,27 +46,43 @@ BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success)  BufferQueue::ErrorCode BufferQueue::getBuffer(Buffer &Buf) {    if (__sanitizer::atomic_load(&Finalizing, __sanitizer::memory_order_acquire))      return ErrorCode::QueueFinalizing; -  __sanitizer::BlockingMutexLock Guard(&Mutex); -  if (Buffers.empty()) +  __sanitizer::SpinMutexLock Guard(&Mutex); + +  if (Next == First)      return ErrorCode::NotEnoughMemory; -  auto &T = Buffers.front(); + +  auto &T = *Next;    auto &B = std::get<0>(T);    Buf = B; -  B.Buffer = nullptr; -  B.Size = 0; -  Buffers.pop_front(); + +  if (First == nullptr) +    First = Next; +  ++Next; +  if (Next == (Buffers.get() + BufferCount)) +    Next = Buffers.get(); +    return ErrorCode::Ok;  }  BufferQueue::ErrorCode BufferQueue::releaseBuffer(Buffer &Buf) { -  if (OwnedBuffers.count(Buf.Buffer) == 0) +  // Blitz through the buffers array to find the buffer. +  if (std::none_of(OwnedBuffers.get(), OwnedBuffers.get() + BufferCount, +                   [&Buf](void *P) { return P == Buf.Buffer; }))      return ErrorCode::UnrecognizedBuffer; -  __sanitizer::BlockingMutexLock Guard(&Mutex); +  __sanitizer::SpinMutexLock Guard(&Mutex); + +  // This points to a semantic bug, we really ought to not be releasing more +  // buffers than we actually get. +  if (First == nullptr || First == Next) +    return ErrorCode::NotEnoughMemory;    // Now that the buffer has been released, we mark it as "used". -  Buffers.emplace(Buffers.end(), Buf, true /* used */); +  *First = std::make_tuple(Buf, true);    Buf.Buffer = nullptr;    Buf.Size = 0; +  ++First; +  if (First == (Buffers.get() + BufferCount)) +    First = Buffers.get();    return ErrorCode::Ok;  } @@ -74,7 +94,8 @@ BufferQueue::ErrorCode BufferQueue::finalize() {  }  BufferQueue::~BufferQueue() { -  for (auto &T : Buffers) { +  for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) { +    auto &T = *I;      auto &Buf = std::get<0>(T);      free(Buf.Buffer);    } | 

