blob: dcdef1267e8df4fae0216c5700e3c703e8b1970f [file] [log] [blame]
andrew@webrtc.org325cff02014-10-01 17:42:18 +00001/*
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 Wiberg29e7bee2018-03-22 14:11:52 +010011#ifndef RTC_BASE_MEMORY_ALIGNED_ARRAY_H_
12#define RTC_BASE_MEMORY_ALIGNED_ARRAY_H_
andrew@webrtc.org325cff02014-10-01 17:42:18 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010015#include "rtc_base/memory/aligned_malloc.h"
andrew@webrtc.org325cff02014-10-01 17:42:18 +000016
17namespace webrtc {
18
19// Wrapper class for aligned arrays. Every row (and the first dimension) are
20// aligned to the given byte alignment.
Karl Wiberg79eb1d92017-11-08 12:26:07 +010021template <typename T>
22class AlignedArray {
andrew@webrtc.org325cff02014-10-01 17:42:18 +000023 public:
Peter Kasting69558702016-01-12 16:26:35 -080024 AlignedArray(size_t rows, size_t cols, size_t alignment)
Karl Wiberg79eb1d92017-11-08 12:26:07 +010025 : rows_(rows), cols_(cols) {
kwibergaf476c72016-11-28 15:21:39 -080026 RTC_CHECK_GT(alignment, 0);
Karl Wiberg79eb1d92017-11-08 12:26:07 +010027 head_row_ =
28 static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_), alignment));
Peter Kasting69558702016-01-12 16:26:35 -080029 for (size_t i = 0; i < rows_; ++i) {
Karl Wiberg79eb1d92017-11-08 12:26:07 +010030 head_row_[i] = static_cast<T*>(
31 AlignedMalloc(cols_ * sizeof(**head_row_), alignment));
andrew@webrtc.org325cff02014-10-01 17:42:18 +000032 }
33 }
34
35 ~AlignedArray() {
Peter Kasting69558702016-01-12 16:26:35 -080036 for (size_t i = 0; i < rows_; ++i) {
andrew@webrtc.org325cff02014-10-01 17:42:18 +000037 AlignedFree(head_row_[i]);
38 }
39 AlignedFree(head_row_);
40 }
41
Karl Wiberg79eb1d92017-11-08 12:26:07 +010042 T* const* Array() { return head_row_; }
andrew@webrtc.org325cff02014-10-01 17:42:18 +000043
Karl Wiberg79eb1d92017-11-08 12:26:07 +010044 const T* const* Array() const { return head_row_; }
andrew@webrtc.org325cff02014-10-01 17:42:18 +000045
Peter Kasting69558702016-01-12 16:26:35 -080046 T* Row(size_t row) {
henrikg91d6ede2015-09-17 00:24:34 -070047 RTC_CHECK_LE(row, rows_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000048 return head_row_[row];
49 }
50
Peter Kasting69558702016-01-12 16:26:35 -080051 const T* Row(size_t row) const {
henrikg91d6ede2015-09-17 00:24:34 -070052 RTC_CHECK_LE(row, rows_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000053 return head_row_[row];
54 }
55
Peter Kasting69558702016-01-12 16:26:35 -080056 T& At(size_t row, size_t col) {
henrikg91d6ede2015-09-17 00:24:34 -070057 RTC_CHECK_LE(col, cols_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000058 return Row(row)[col];
59 }
60
Peter Kasting69558702016-01-12 16:26:35 -080061 const T& At(size_t row, size_t col) const {
henrikg91d6ede2015-09-17 00:24:34 -070062 RTC_CHECK_LE(col, cols_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000063 return Row(row)[col];
64 }
65
Karl Wiberg79eb1d92017-11-08 12:26:07 +010066 size_t rows() const { return rows_; }
Henrik Kjellandere8d191f2015-06-20 20:10:57 +020067
Karl Wiberg79eb1d92017-11-08 12:26:07 +010068 size_t cols() const { return cols_; }
Henrik Kjellandere8d191f2015-06-20 20:10:57 +020069
andrew@webrtc.org325cff02014-10-01 17:42:18 +000070 private:
Peter Kasting69558702016-01-12 16:26:35 -080071 size_t rows_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070072 size_t cols_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000073 T** head_row_;
74};
75
76} // namespace webrtc
77
Karl Wiberg29e7bee2018-03-22 14:11:52 +010078#endif // RTC_BASE_MEMORY_ALIGNED_ARRAY_H_