[llvm-commits] [compiler-rt] r162262 - in /compiler-rt/trunk/lib/sanitizer_common: sanitizer_common.h tests/sanitizer_allocator_test.cc

Alexey Samsonov samsonov at google.com
Tue Aug 21 01:13:37 PDT 2012


Author: samsonov
Date: Tue Aug 21 03:13:37 2012
New Revision: 162262

URL: http://llvm.org/viewvc/llvm-project?rev=162262&view=rev
Log:
[Sanitizer] define InternalScopedBuffer to replace large arrays on stack. It is defined analogous to similar class in tsan and should replace it.

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=162262&r1=162261&r2=162262&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Tue Aug 21 03:13:37 2012
@@ -53,6 +53,30 @@
 // returns a pointer to the beginning of the block.
 void *InternalAllocBlock(void *p);
 
+// InternalScopedBuffer can be used instead of large stack arrays to
+// keep frame size low.
+template<typename T>
+class InternalScopedBuffer {
+ public:
+  explicit InternalScopedBuffer(uptr cnt) {
+    cnt_ = cnt;
+    ptr_ = (T*)InternalAlloc(cnt * sizeof(T));
+  }
+  ~InternalScopedBuffer() {
+    InternalFree(ptr_);
+  }
+  T &operator[](uptr i) { return ptr_[i]; }
+  T *data() { return ptr_; }
+  uptr size() { return cnt_ * sizeof(T); }
+
+ private:
+  T *ptr_;
+  uptr cnt_;
+  // Disallow evil constructors.
+  InternalScopedBuffer(const InternalScopedBuffer&);
+  void operator=(const InternalScopedBuffer&);
+};
+
 // IO
 void RawWrite(const char *buffer);
 void Printf(const char *format, ...);

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc?rev=162262&r1=162261&r2=162262&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator_test.cc Tue Aug 21 03:13:37 2012
@@ -53,4 +53,18 @@
   }
 }
 
+TEST(Allocator, ScopedBuffer) {
+  const int kSize = 512;
+  {
+    InternalScopedBuffer<int> int_buf(kSize);
+    EXPECT_EQ(sizeof(int) * kSize, int_buf.size());  // NOLINT
+  }
+  InternalScopedBuffer<char> char_buf(kSize);
+  EXPECT_EQ(sizeof(char) * kSize, char_buf.size());  // NOLINT
+  memset(char_buf.data(), 'c', kSize);
+  for (int i = 0; i < kSize; i++) {
+    EXPECT_EQ('c', char_buf[i]);
+  }
+}
+
 }  // namespace __sanitizer





More information about the llvm-commits mailing list