summaryrefslogtreecommitdiff
path: root/sc/inc/stlalgorithm.hxx
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@collabora.com>2014-02-26 16:29:27 -0500
committerKohei Yoshida <kohei.yoshida@collabora.com>2014-02-27 21:19:36 -0500
commit03f7a342011a4f69cfcbec7af3e4f1a2e835618b (patch)
tree8e11d1f162ed3908e007c9bdf1fc1b6952960d83 /sc/inc/stlalgorithm.hxx
parent6ef6dd0122b8e44d8547ec31f40def42173e4e41 (diff)
Ensure that numeric array storage is aligned to 256-byte boundary.
OpenCL devices require this else we would get a performance hit. Change-Id: I6b1db6320fa84f933b6446022a0fd02ba267bf21
Diffstat (limited to 'sc/inc/stlalgorithm.hxx')
-rw-r--r--sc/inc/stlalgorithm.hxx60
1 files changed, 60 insertions, 0 deletions
diff --git a/sc/inc/stlalgorithm.hxx b/sc/inc/stlalgorithm.hxx
index fb5509f50f9c..37d7ba85066a 100644
--- a/sc/inc/stlalgorithm.hxx
+++ b/sc/inc/stlalgorithm.hxx
@@ -11,6 +11,9 @@
#define __SC_STLALGORITHM_HXX__
#include <functional>
+#include <limits>
+
+#include <rtl/alloc.h>
/**
* Function object to allow deleting instances stored in STL containers as
@@ -25,6 +28,63 @@ struct ScDeleteObjectByPtr : public ::std::unary_function<T*, void>
}
};
+namespace sc {
+
+/**
+ * Custom allocator for STL container to ensure that the base address of
+ * allocated storage is aligned to a specified boundary.
+ */
+template<typename T, size_t _Alignment>
+class AlignedAllocator
+{
+public:
+ typedef T value_type;
+ typedef size_t size_type;
+ typedef std::ptrdiff_t difference_type;
+
+ typedef T* pointer;
+ typedef const T* const_pointer;
+ typedef T* void_pointer;
+
+ typedef T& reference;
+ typedef const T& const_reference;
+
+ template<typename _Type2>
+ struct rebind
+ {
+ typedef AlignedAllocator<_Type2,_Alignment> other;
+ };
+
+ AlignedAllocator() {}
+ ~AlignedAllocator() {}
+
+ template<typename _Type2>
+ AlignedAllocator(const AlignedAllocator<_Type2,_Alignment>&) {}
+
+ void construct(T* p, const value_type& val) { new(p) value_type(val); }
+ void destroy(T* p) { p->~value_type(); }
+
+ size_type max_size() const
+ {
+ return std::numeric_limits<size_type>::max() / sizeof(value_type);
+ }
+
+ bool operator== (const AlignedAllocator&) const { return true; }
+ bool operator!= (const AlignedAllocator&) const { return false; }
+
+ pointer allocate(size_type n)
+ {
+ return (pointer)rtl_allocateAlinedMemory(_Alignment, n*sizeof(value_type));
+ }
+
+ void deallocate(pointer p, size_type)
+ {
+ rtl_freeAlignedMemory(p);
+ }
+};
+
+}
+
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */