Index: compiler-rt/trunk/lib/xray/xray_buffer_queue.h =================================================================== --- compiler-rt/trunk/lib/xray/xray_buffer_queue.h +++ compiler-rt/trunk/lib/xray/xray_buffer_queue.h @@ -15,11 +15,9 @@ #ifndef XRAY_BUFFER_QUEUE_H #define XRAY_BUFFER_QUEUE_H +#include #include "sanitizer_common/sanitizer_atomic.h" #include "sanitizer_common/sanitizer_mutex.h" -#include -#include -#include namespace __xray { @@ -29,38 +27,45 @@ /// the "flight data recorder" (FDR) mode to support ongoing XRay function call /// trace collection. class BufferQueue { -public: + public: struct Buffer { void *Buffer = nullptr; size_t Size = 0; }; -private: + private: + struct BufferRep { + // The managed buffer. + Buffer Buffer; + + // This is true if the buffer has been returned to the available queue, and + // is considered "used" by another thread. + bool Used = false; + }; + // Size of each individual Buffer. size_t BufferSize; - // We use a bool to indicate whether the Buffer has been used in this - // freelist implementation. - std::unique_ptr[]> Buffers; + BufferRep *Buffers; size_t BufferCount; __sanitizer::SpinMutex Mutex; __sanitizer::atomic_uint8_t Finalizing; // Pointers to buffers managed/owned by the BufferQueue. - std::unique_ptr OwnedBuffers; + void **OwnedBuffers; // Pointer to the next buffer to be handed out. - std::tuple *Next; + BufferRep *Next; // Pointer to the entry in the array where the next released buffer will be // placed. - std::tuple *First; + BufferRep *First; // Count of buffers that have been handed out through 'getBuffer'. size_t LiveBuffers; -public: + public: enum class ErrorCode : unsigned { Ok, NotEnoughMemory, @@ -71,16 +76,16 @@ static const char *getErrorString(ErrorCode E) { switch (E) { - case ErrorCode::Ok: - return "(none)"; - case ErrorCode::NotEnoughMemory: - return "no available buffers in the queue"; - case ErrorCode::QueueFinalizing: - return "queue already finalizing"; - case ErrorCode::UnrecognizedBuffer: - return "buffer being returned not owned by buffer queue"; - case ErrorCode::AlreadyFinalized: - return "queue already finalized"; + case ErrorCode::Ok: + return "(none)"; + case ErrorCode::NotEnoughMemory: + return "no available buffers in the queue"; + case ErrorCode::QueueFinalizing: + return "queue already finalizing"; + case ErrorCode::UnrecognizedBuffer: + return "buffer being returned not owned by buffer queue"; + case ErrorCode::AlreadyFinalized: + return "queue already finalized"; } return "unknown error"; } @@ -131,12 +136,12 @@ /// Applies the provided function F to each Buffer in the queue, only if the /// Buffer is marked 'used' (i.e. has been the result of getBuffer(...) and a /// releaseBuffer(...) operation). - template void apply(F Fn) { + template + void apply(F Fn) { __sanitizer::SpinMutexLock G(&Mutex); - for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) { + for (auto I = Buffers, E = Buffers + BufferCount; I != E; ++I) { const auto &T = *I; - if (std::get<1>(T)) - Fn(std::get<0>(T)); + if (T.Used) Fn(T.Buffer); } } @@ -144,6 +149,6 @@ ~BufferQueue(); }; -} // namespace __xray +} // namespace __xray -#endif // XRAY_BUFFER_QUEUE_H +#endif // XRAY_BUFFER_QUEUE_H Index: compiler-rt/trunk/lib/xray/xray_buffer_queue.cc =================================================================== --- compiler-rt/trunk/lib/xray/xray_buffer_queue.cc +++ compiler-rt/trunk/lib/xray/xray_buffer_queue.cc @@ -13,29 +13,30 @@ // //===----------------------------------------------------------------------===// #include "xray_buffer_queue.h" +#include "sanitizer_common/sanitizer_allocator_internal.h" #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_libc.h" -#include -#include -#include - using namespace __xray; using namespace __sanitizer; -BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success) - : BufferSize(B), Buffers(new std::tuple[N]()), - BufferCount(N), Finalizing{0}, OwnedBuffers(new void *[N]()), - Next(Buffers.get()), First(Buffers.get()), LiveBuffers(0) { +BufferQueue::BufferQueue(size_t B, size_t N, bool &Success) + : BufferSize(B), + Buffers(new BufferRep[N]()), + BufferCount(N), + Finalizing{0}, + OwnedBuffers(new void *[N]()), + Next(Buffers), + First(Buffers), + LiveBuffers(0) { for (size_t i = 0; i < N; ++i) { auto &T = Buffers[i]; - void *Tmp = malloc(BufferSize); + void *Tmp = InternalAlloc(BufferSize); if (Tmp == nullptr) { Success = false; return; } - auto &Buf = std::get<0>(T); - std::get<1>(T) = false; + auto &Buf = T.Buffer; Buf.Buffer = Tmp; Buf.Size = B; OwnedBuffers[i] = Tmp; @@ -47,39 +48,42 @@ if (__sanitizer::atomic_load(&Finalizing, __sanitizer::memory_order_acquire)) return ErrorCode::QueueFinalizing; __sanitizer::SpinMutexLock Guard(&Mutex); - if (LiveBuffers == BufferCount) - return ErrorCode::NotEnoughMemory; + if (LiveBuffers == BufferCount) return ErrorCode::NotEnoughMemory; auto &T = *Next; - auto &B = std::get<0>(T); + auto &B = T.Buffer; Buf = B; ++LiveBuffers; - if (++Next == (Buffers.get() + BufferCount)) - Next = Buffers.get(); + if (++Next == (Buffers + BufferCount)) Next = Buffers; return ErrorCode::Ok; } BufferQueue::ErrorCode BufferQueue::releaseBuffer(Buffer &Buf) { // 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; + bool Found = false; + for (auto I = OwnedBuffers, E = OwnedBuffers + BufferCount; I != E; ++I) { + if (*I == Buf.Buffer) { + Found = true; + break; + } + } + if (!Found) return ErrorCode::UnrecognizedBuffer; + __sanitizer::SpinMutexLock Guard(&Mutex); // This points to a semantic bug, we really ought to not be releasing more // buffers than we actually get. - if (LiveBuffers == 0) - return ErrorCode::NotEnoughMemory; + if (LiveBuffers == 0) return ErrorCode::NotEnoughMemory; // Now that the buffer has been released, we mark it as "used". - *First = std::make_tuple(Buf, true); + First->Buffer = Buf; + First->Used = true; Buf.Buffer = nullptr; Buf.Size = 0; --LiveBuffers; - if (++First == (Buffers.get() + BufferCount)) - First = Buffers.get(); + if (++First == (Buffers + BufferCount)) First = Buffers; return ErrorCode::Ok; } @@ -92,9 +96,11 @@ } BufferQueue::~BufferQueue() { - for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) { + for (auto I = Buffers, E = Buffers + BufferCount; I != E; ++I) { auto &T = *I; - auto &Buf = std::get<0>(T); - free(Buf.Buffer); + auto &Buf = T.Buffer; + InternalFree(Buf.Buffer); } + delete[] Buffers; + delete[] OwnedBuffers; }