[compiler-rt] r316406 - [XRay][compiler-rt] Remove C++ STL from the buffer queue implementation

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 18:39:59 PDT 2017


Author: dberris
Date: Mon Oct 23 18:39:59 2017
New Revision: 316406

URL: http://llvm.org/viewvc/llvm-project?rev=316406&view=rev
Log:
[XRay][compiler-rt] Remove C++ STL from the buffer queue implementation

Summary:
This change removes the dependency on C++ standard library
types/functions in the implementation of the buffer queue. This is an
incremental step in resolving llvm.org/PR32274.

Reviewers: dblaikie, pelikan

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D39175

Modified:
    compiler-rt/trunk/lib/xray/xray_buffer_queue.cc
    compiler-rt/trunk/lib/xray/xray_buffer_queue.h

Modified: compiler-rt/trunk/lib/xray/xray_buffer_queue.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_buffer_queue.cc?rev=316406&r1=316405&r2=316406&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_buffer_queue.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_buffer_queue.cc Mon Oct 23 18:39:59 2017
@@ -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 <algorithm>
-#include <cstdlib>
-#include <tuple>
-
 using namespace __xray;
 using namespace __sanitizer;
 
-BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success)
-    : BufferSize(B), Buffers(new std::tuple<Buffer, bool>[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 @@ BufferQueue::ErrorCode BufferQueue::getB
   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::ErrorCode BufferQueue::fina
 }
 
 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;
 }

Modified: compiler-rt/trunk/lib/xray/xray_buffer_queue.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_buffer_queue.h?rev=316406&r1=316405&r2=316406&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_buffer_queue.h (original)
+++ compiler-rt/trunk/lib/xray/xray_buffer_queue.h Mon Oct 23 18:39:59 2017
@@ -15,11 +15,9 @@
 #ifndef XRAY_BUFFER_QUEUE_H
 #define XRAY_BUFFER_QUEUE_H
 
+#include <cstddef>
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_mutex.h"
-#include <cstdint>
-#include <memory>
-#include <utility>
 
 namespace __xray {
 
@@ -29,38 +27,45 @@ namespace __xray {
 /// 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<std::tuple<Buffer, bool>[]> 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<void *[]> OwnedBuffers;
+  void **OwnedBuffers;
 
   // Pointer to the next buffer to be handed out.
-  std::tuple<Buffer, bool> *Next;
+  BufferRep *Next;
 
   // Pointer to the entry in the array where the next released buffer will be
   // placed.
-  std::tuple<Buffer, bool> *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 @@ public:
 
   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 @@ public:
   /// 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 <class F> void apply(F Fn) {
+  template <class F>
+  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 @@ public:
   ~BufferQueue();
 };
 
-} // namespace __xray
+}  // namespace __xray
 
-#endif // XRAY_BUFFER_QUEUE_H
+#endif  // XRAY_BUFFER_QUEUE_H




More information about the llvm-commits mailing list