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 | |
Karl Wiberg | 29e7bee | 2018-03-22 14:11:52 +0100 | [diff] [blame] | 11 | #ifndef RTC_BASE_MEMORY_ALIGNED_ARRAY_H_ |
| 12 | #define RTC_BASE_MEMORY_ALIGNED_ARRAY_H_ |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Karl Wiberg | 29e7bee | 2018-03-22 14:11:52 +0100 | [diff] [blame] | 17 | #include "rtc_base/memory/aligned_malloc.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // Wrapper class for aligned arrays. Every row (and the first dimension) are |
| 22 | // aligned to the given byte alignment. |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 23 | template <typename T> |
| 24 | class AlignedArray { |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 25 | public: |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 26 | AlignedArray(size_t rows, size_t cols, size_t alignment) |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 27 | : rows_(rows), cols_(cols) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 28 | RTC_CHECK_GT(alignment, 0); |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 29 | head_row_ = |
| 30 | static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_), alignment)); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 31 | for (size_t i = 0; i < rows_; ++i) { |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 32 | head_row_[i] = static_cast<T*>( |
| 33 | AlignedMalloc(cols_ * sizeof(**head_row_), alignment)); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | ~AlignedArray() { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 38 | for (size_t i = 0; i < rows_; ++i) { |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 39 | AlignedFree(head_row_[i]); |
| 40 | } |
| 41 | AlignedFree(head_row_); |
| 42 | } |
| 43 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 44 | T* const* Array() { return head_row_; } |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 45 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 46 | const T* const* Array() const { return head_row_; } |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 47 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 48 | T* Row(size_t row) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 49 | RTC_CHECK_LE(row, rows_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 50 | return head_row_[row]; |
| 51 | } |
| 52 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 53 | const T* Row(size_t row) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 54 | RTC_CHECK_LE(row, rows_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 55 | return head_row_[row]; |
| 56 | } |
| 57 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 58 | T& At(size_t row, size_t col) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 59 | RTC_CHECK_LE(col, cols_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 60 | return Row(row)[col]; |
| 61 | } |
| 62 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 63 | const T& At(size_t row, size_t col) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 64 | RTC_CHECK_LE(col, cols_); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 65 | return Row(row)[col]; |
| 66 | } |
| 67 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 68 | size_t rows() const { return rows_; } |
Henrik Kjellander | e8d191f | 2015-06-20 20:10:57 +0200 | [diff] [blame] | 69 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 70 | size_t cols() const { return cols_; } |
Henrik Kjellander | e8d191f | 2015-06-20 20:10:57 +0200 | [diff] [blame] | 71 | |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 72 | private: |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 73 | size_t rows_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 74 | size_t cols_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 75 | T** head_row_; |
| 76 | }; |
| 77 | |
| 78 | } // namespace webrtc |
| 79 | |
Karl Wiberg | 29e7bee | 2018-03-22 14:11:52 +0100 | [diff] [blame] | 80 | #endif // RTC_BASE_MEMORY_ALIGNED_ARRAY_H_ |