blob: c67d87d4043d5195b11f52ac054058be19629fc0 [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
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010017#include "rtc_base/memory/aligned_malloc.h"
andrew@webrtc.org325cff02014-10-01 17:42:18 +000018
19namespace webrtc {
20
21// Wrapper class for aligned arrays. Every row (and the first dimension) are
22// aligned to the given byte alignment.
Karl Wiberg79eb1d92017-11-08 12:26:07 +010023template <typename T>
24class AlignedArray {
andrew@webrtc.org325cff02014-10-01 17:42:18 +000025 public:
Peter Kasting69558702016-01-12 16:26:35 -080026 AlignedArray(size_t rows, size_t cols, size_t alignment)
Karl Wiberg79eb1d92017-11-08 12:26:07 +010027 : rows_(rows), cols_(cols) {
kwibergaf476c72016-11-28 15:21:39 -080028 RTC_CHECK_GT(alignment, 0);
Karl Wiberg79eb1d92017-11-08 12:26:07 +010029 head_row_ =
30 static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_), alignment));
Peter Kasting69558702016-01-12 16:26:35 -080031 for (size_t i = 0; i < rows_; ++i) {
Karl Wiberg79eb1d92017-11-08 12:26:07 +010032 head_row_[i] = static_cast<T*>(
33 AlignedMalloc(cols_ * sizeof(**head_row_), alignment));
andrew@webrtc.org325cff02014-10-01 17:42:18 +000034 }
35 }
36
37 ~AlignedArray() {
Peter Kasting69558702016-01-12 16:26:35 -080038 for (size_t i = 0; i < rows_; ++i) {
andrew@webrtc.org325cff02014-10-01 17:42:18 +000039 AlignedFree(head_row_[i]);
40 }
41 AlignedFree(head_row_);
42 }
43
Karl Wiberg79eb1d92017-11-08 12:26:07 +010044 T* const* Array() { return head_row_; }
andrew@webrtc.org325cff02014-10-01 17:42:18 +000045
Karl Wiberg79eb1d92017-11-08 12:26:07 +010046 const T* const* Array() const { return head_row_; }
andrew@webrtc.org325cff02014-10-01 17:42:18 +000047
Peter Kasting69558702016-01-12 16:26:35 -080048 T* Row(size_t row) {
henrikg91d6ede2015-09-17 00:24:34 -070049 RTC_CHECK_LE(row, rows_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000050 return head_row_[row];
51 }
52
Peter Kasting69558702016-01-12 16:26:35 -080053 const T* Row(size_t row) const {
henrikg91d6ede2015-09-17 00:24:34 -070054 RTC_CHECK_LE(row, rows_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000055 return head_row_[row];
56 }
57
Peter Kasting69558702016-01-12 16:26:35 -080058 T& At(size_t row, size_t col) {
henrikg91d6ede2015-09-17 00:24:34 -070059 RTC_CHECK_LE(col, cols_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000060 return Row(row)[col];
61 }
62
Peter Kasting69558702016-01-12 16:26:35 -080063 const T& At(size_t row, size_t col) const {
henrikg91d6ede2015-09-17 00:24:34 -070064 RTC_CHECK_LE(col, cols_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +000065 return Row(row)[col];
66 }
67
Karl Wiberg79eb1d92017-11-08 12:26:07 +010068 size_t rows() const { return rows_; }
Henrik Kjellandere8d191f2015-06-20 20:10:57 +020069
Karl Wiberg79eb1d92017-11-08 12:26:07 +010070 size_t cols() const { return cols_; }
Henrik Kjellandere8d191f2015-06-20 20:10:57 +020071
andrew@webrtc.org325cff02014-10-01 17:42:18 +000072 private:
Peter Kasting69558702016-01-12 16:26:35 -080073 size_t rows_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070074 size_t cols_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000075 T** head_row_;
76};
77
78} // namespace webrtc
79
Karl Wiberg29e7bee2018-03-22 14:11:52 +010080#endif // RTC_BASE_MEMORY_ALIGNED_ARRAY_H_