summaryrefslogtreecommitdiffstats
path: root/src/usr/trace/TraceBuffer.C
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr/trace/TraceBuffer.C')
-rw-r--r--src/usr/trace/TraceBuffer.C53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/usr/trace/TraceBuffer.C b/src/usr/trace/TraceBuffer.C
new file mode 100644
index 000000000..c10afcfa3
--- /dev/null
+++ b/src/usr/trace/TraceBuffer.C
@@ -0,0 +1,53 @@
+#include <limits.h>
+#include "TraceBuffer.H"
+
+TracePage* TracePage::setNext(TracePage* new_next)
+{
+ return __sync_val_compare_and_swap(&this->next, NULL, new_next);
+}
+
+traceEntry* TracePage::claimEntry(size_t size)
+{
+ size_t position = __sync_fetch_and_add(&this->size, size);
+ if (position > (PAGE_SIZE - sizeof(TracePage)))
+ {
+ return NULL;
+ }
+ else
+ {
+ return (traceEntry*)&((uint8_t*)this)[sizeof(TracePage) + position];
+ }
+}
+
+traceEntry* TraceBuffer::claimEntry(size_t size)
+{
+ traceEntry* result = NULL;
+ TracePage* page = last;
+
+ while (result == NULL)
+ {
+ result = page->claimEntry(size);
+
+ if (NULL == result)
+ {
+ if (NULL != page->getNext())
+ {
+ __sync_bool_compare_and_swap(&this->last,
+ page, page->getNext());
+ page = page->getNext();
+ }
+ else
+ {
+ TracePage* new_page = new (malloc(PAGE_SIZE)) TracePage();
+ TracePage* prev = page;
+
+ while(prev != NULL)
+ {
+ prev = prev->setNext(new_page);
+ }
+ }
+ }
+ }
+
+ return result;
+}
OpenPOWER on IntegriCloud