aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDean Michael Berris <dberris@google.com>2017-10-24 01:39:59 +0000
committerDean Michael Berris <dberris@google.com>2017-10-24 01:39:59 +0000
commit6c9ee8c61a70980db9abd1cc481473b11eb206ef (patch)
tree2d109f189309467e9a573836fd65df68828ab8ec
parented5545d851416c54828787c615f8f87334ce855c (diff)
[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 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@316406 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/xray/xray_buffer_queue.cc60
-rw-r--r--lib/xray/xray_buffer_queue.h61
2 files changed, 66 insertions, 55 deletions
diff --git a/lib/xray/xray_buffer_queue.cc b/lib/xray/xray_buffer_queue.cc
index 61a0add0e..c42bba322 100644
--- a/lib/xray/xray_buffer_queue.cc
+++ b/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 <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::getBuffer(Buffer &Buf) {
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::finalize() {
}
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;
}
diff --git a/lib/xray/xray_buffer_queue.h b/lib/xray/xray_buffer_queue.h
index 05e22eece..1634a21e6 100644
--- a/lib/xray/xray_buffer_queue.h
+++ b/lib/xray/xray_buffer_queue.h
@@ -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