blob: 88b52699028ddc04f0e4b9c0e850edcb15168f27 [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
kjellanderd56d68c2015-11-02 02:12:41 -080011#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_
12#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_
andrew@webrtc.org325cff02014-10-01 17:42:18 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
15#include "system_wrappers/include/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.
21template<typename T> class AlignedArray {
22 public:
Peter Kasting69558702016-01-12 16:26:35 -080023 AlignedArray(size_t rows, size_t cols, size_t alignment)
andrew@webrtc.org325cff02014-10-01 17:42:18 +000024 : rows_(rows),
pkasting25702cb2016-01-08 13:50:27 -080025 cols_(cols) {
kwibergaf476c72016-11-28 15:21:39 -080026 RTC_CHECK_GT(alignment, 0);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000027 head_row_ = static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_),
pkasting25702cb2016-01-08 13:50:27 -080028 alignment));
Peter Kasting69558702016-01-12 16:26:35 -080029 for (size_t i = 0; i < rows_; ++i) {
andrew@webrtc.org325cff02014-10-01 17:42:18 +000030 head_row_[i] = static_cast<T*>(AlignedMalloc(cols_ * sizeof(**head_row_),
pkasting25702cb2016-01-08 13:50:27 -080031 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
42 T* const* Array() {
43 return head_row_;
44 }
45
46 const T* const* Array() const {
47 return head_row_;
48 }
49
Peter Kasting69558702016-01-12 16:26:35 -080050 T* Row(size_t row) {
henrikg91d6ede2015-09-17 00:24:34 -070051 RTC_CHECK_LE(row, rows_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000052 return head_row_[row];
53 }
54
Peter Kasting69558702016-01-12 16:26:35 -080055 const T* Row(size_t row) const {
henrikg91d6ede2015-09-17 00:24:34 -070056 RTC_CHECK_LE(row, rows_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000057 return head_row_[row];
58 }
59
Peter Kasting69558702016-01-12 16:26:35 -080060 T& At(size_t row, size_t col) {
henrikg91d6ede2015-09-17 00:24:34 -070061 RTC_CHECK_LE(col, cols_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000062 return Row(row)[col];
63 }
64
Peter Kasting69558702016-01-12 16:26:35 -080065 const T& At(size_t row, size_t col) const {
henrikg91d6ede2015-09-17 00:24:34 -070066 RTC_CHECK_LE(col, cols_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000067 return Row(row)[col];
68 }
69
Peter Kasting69558702016-01-12 16:26:35 -080070 size_t rows() const {
Henrik Kjellandere8d191f2015-06-20 20:10:57 +020071 return rows_;
72 }
73
Peter Kastingdce40cf2015-08-24 14:52:23 -070074 size_t cols() const {
Henrik Kjellandere8d191f2015-06-20 20:10:57 +020075 return cols_;
76 }
77
andrew@webrtc.org325cff02014-10-01 17:42:18 +000078 private:
Peter Kasting69558702016-01-12 16:26:35 -080079 size_t rows_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070080 size_t cols_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000081 T** head_row_;
82};
83
84} // namespace webrtc
85
kjellanderd56d68c2015-11-02 02:12:41 -080086#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_