andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_ |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
| 15 | #include "system_wrappers/include/aligned_malloc.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | // Wrapper class for aligned arrays. Every row (and the first dimension) are |
| 20 | // aligned to the given byte alignment. |
| 21 | template<typename T> class AlignedArray { |
| 22 | public: |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 23 | AlignedArray(size_t rows, size_t cols, size_t alignment) |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 24 | : rows_(rows), |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 25 | cols_(cols) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 26 | RTC_CHECK_GT(alignment, 0); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 27 | head_row_ = static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_), |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 28 | alignment)); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 29 | for (size_t i = 0; i < rows_; ++i) { |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 30 | head_row_[i] = static_cast<T*>(AlignedMalloc(cols_ * sizeof(**head_row_), |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 31 | alignment)); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | ~AlignedArray() { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 36 | for (size_t i = 0; i < rows_; ++i) { |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 37 | AlignedFree(head_row_[i]); |
| 38 | } |
| 39 | AlignedFree(head_row_); |
| 40 | } |
| 41 | |
| 42 | T* const* Array() { |
| 43 | return head_row_; |
| 44 | } |
| 45 | |
| 46 | const T* const* Array() const { |
| 47 | return head_row_; |
| 48 | } |
| 49 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 50 | T* Row(size_t row) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 51 | RTC_CHECK_LE(row, rows_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 52 | return head_row_[row]; |
| 53 | } |
| 54 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 55 | const T* Row(size_t row) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 56 | RTC_CHECK_LE(row, rows_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 57 | return head_row_[row]; |
| 58 | } |
| 59 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 60 | T& At(size_t row, size_t col) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 61 | RTC_CHECK_LE(col, cols_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 62 | return Row(row)[col]; |
| 63 | } |
| 64 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 65 | const T& At(size_t row, size_t col) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 66 | RTC_CHECK_LE(col, cols_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 67 | return Row(row)[col]; |
| 68 | } |
| 69 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 70 | size_t rows() const { |
Henrik Kjellander | e8d191f | 2015-06-20 20:10:57 +0200 | [diff] [blame] | 71 | return rows_; |
| 72 | } |
| 73 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 74 | size_t cols() const { |
Henrik Kjellander | e8d191f | 2015-06-20 20:10:57 +0200 | [diff] [blame] | 75 | return cols_; |
| 76 | } |
| 77 | |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 78 | private: |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 79 | size_t rows_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 80 | size_t cols_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 81 | T** head_row_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace webrtc |
| 85 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 86 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_ |