aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cubeb_utils.h
diff options
context:
space:
mode:
authorPaul Adenot <[email protected]>2016-12-01 14:55:10 +1300
committerMatthew Gregan <[email protected]>2016-12-01 14:55:53 +1300
commit4aa67a9a9a720741004130540e270ee944d5adc7 (patch)
tree7b7b46652ce472196280a8f717fca1fb241594c4 /src/cubeb_utils.h
parent9770b85bf7857016f2d0adcc3bdf0191f47870b6 (diff)
downloadcubeb-4aa67a9a9a720741004130540e270ee944d5adc7.tar.gz
cubeb-4aa67a9a9a720741004130540e270ee944d5adc7.zip
Implement a lock-free single consumer single producer queue.
Diffstat (limited to 'src/cubeb_utils.h')
-rw-r--r--src/cubeb_utils.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/cubeb_utils.h b/src/cubeb_utils.h
index 94e5ab0..a7f4c8c 100644
--- a/src/cubeb_utils.h
+++ b/src/cubeb_utils.h
@@ -46,6 +46,63 @@ void PodZero(T * destination, size_t count)
memset(destination, 0, count * sizeof(T));
}
+namespace {
+template<typename T, typename Trait>
+void Copy(T * destination, const T * source, size_t count, Trait)
+{
+ for (size_t i = 0; i < count; i++) {
+ destination[i] = source[i];
+ }
+}
+
+template<typename T>
+void Copy(T * destination, const T * source, size_t count, std::true_type)
+{
+ PodCopy(destination, source, count);
+}
+}
+
+/**
+ * This allows copying a number of elements from a `source` pointer to a
+ * `destination` pointer, using `memcpy` if it is safe to do so, or a loop that
+ * calls the constructors and destructors otherwise.
+ */
+template<typename T>
+void Copy(T * destination, const T * source, size_t count)
+{
+ assert(destination && source);
+ Copy(destination, source, count, typename std::is_trivial<T>::type());
+}
+
+namespace {
+template<typename T, typename Trait>
+void ConstructDefault(T * destination, size_t count, Trait)
+{
+ for (size_t i = 0; i < count; i++) {
+ destination[i] = T();
+ }
+}
+
+template<typename T>
+void ConstructDefault(T * destination,
+ size_t count, std::true_type)
+{
+ PodZero(destination, count);
+}
+}
+
+/**
+ * This allows zeroing (using memset) or default-constructing a number of
+ * elements calling the constructors and destructors if necessary.
+ */
+template<typename T>
+void ConstructDefault(T * destination, size_t count)
+{
+ assert(destination);
+ ConstructDefault(destination, count,
+ typename std::is_arithmetic<T>::type());
+}
+
template<typename T>
class auto_array
{