xref: /aosp_15_r20/external/libgav1/src/utils/array_2d.h (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop /*
2*09537850SAkhilesh Sanikop  * Copyright 2019 The libgav1 Authors
3*09537850SAkhilesh Sanikop  *
4*09537850SAkhilesh Sanikop  * Licensed under the Apache License, Version 2.0 (the "License");
5*09537850SAkhilesh Sanikop  * you may not use this file except in compliance with the License.
6*09537850SAkhilesh Sanikop  * You may obtain a copy of the License at
7*09537850SAkhilesh Sanikop  *
8*09537850SAkhilesh Sanikop  *      http://www.apache.org/licenses/LICENSE-2.0
9*09537850SAkhilesh Sanikop  *
10*09537850SAkhilesh Sanikop  * Unless required by applicable law or agreed to in writing, software
11*09537850SAkhilesh Sanikop  * distributed under the License is distributed on an "AS IS" BASIS,
12*09537850SAkhilesh Sanikop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*09537850SAkhilesh Sanikop  * See the License for the specific language governing permissions and
14*09537850SAkhilesh Sanikop  * limitations under the License.
15*09537850SAkhilesh Sanikop  */
16*09537850SAkhilesh Sanikop 
17*09537850SAkhilesh Sanikop #ifndef LIBGAV1_SRC_UTILS_ARRAY_2D_H_
18*09537850SAkhilesh Sanikop #define LIBGAV1_SRC_UTILS_ARRAY_2D_H_
19*09537850SAkhilesh Sanikop 
20*09537850SAkhilesh Sanikop #include <cassert>
21*09537850SAkhilesh Sanikop #include <cstddef>
22*09537850SAkhilesh Sanikop #include <cstring>
23*09537850SAkhilesh Sanikop #include <memory>
24*09537850SAkhilesh Sanikop #include <new>
25*09537850SAkhilesh Sanikop #include <type_traits>
26*09537850SAkhilesh Sanikop 
27*09537850SAkhilesh Sanikop #include "src/utils/compiler_attributes.h"
28*09537850SAkhilesh Sanikop 
29*09537850SAkhilesh Sanikop namespace libgav1 {
30*09537850SAkhilesh Sanikop 
31*09537850SAkhilesh Sanikop // Exposes a 1D allocated memory buffer as a 2D array.
32*09537850SAkhilesh Sanikop template <typename T>
33*09537850SAkhilesh Sanikop class Array2DView {
34*09537850SAkhilesh Sanikop  public:
35*09537850SAkhilesh Sanikop   Array2DView() = default;
Array2DView(int rows,int columns,T * const data)36*09537850SAkhilesh Sanikop   Array2DView(int rows, int columns, T* const data) {
37*09537850SAkhilesh Sanikop     Reset(rows, columns, data);
38*09537850SAkhilesh Sanikop   }
39*09537850SAkhilesh Sanikop 
40*09537850SAkhilesh Sanikop   // Copyable and Movable.
41*09537850SAkhilesh Sanikop   Array2DView(const Array2DView& rhs) = default;
42*09537850SAkhilesh Sanikop   Array2DView& operator=(const Array2DView& rhs) = default;
43*09537850SAkhilesh Sanikop 
Reset(int rows,int columns,T * const data)44*09537850SAkhilesh Sanikop   void Reset(int rows, int columns, T* const data) {
45*09537850SAkhilesh Sanikop     rows_ = rows;
46*09537850SAkhilesh Sanikop     columns_ = columns;
47*09537850SAkhilesh Sanikop     data_ = data;
48*09537850SAkhilesh Sanikop   }
49*09537850SAkhilesh Sanikop 
rows()50*09537850SAkhilesh Sanikop   int rows() const { return rows_; }
columns()51*09537850SAkhilesh Sanikop   int columns() const { return columns_; }
52*09537850SAkhilesh Sanikop 
53*09537850SAkhilesh Sanikop   T* operator[](int row) { return const_cast<T*>(GetRow(row)); }
54*09537850SAkhilesh Sanikop 
55*09537850SAkhilesh Sanikop   const T* operator[](int row) const { return GetRow(row); }
56*09537850SAkhilesh Sanikop 
57*09537850SAkhilesh Sanikop  private:
GetRow(int row)58*09537850SAkhilesh Sanikop   const T* GetRow(int row) const {
59*09537850SAkhilesh Sanikop     assert(row < rows_);
60*09537850SAkhilesh Sanikop     const ptrdiff_t offset = static_cast<ptrdiff_t>(row) * columns_;
61*09537850SAkhilesh Sanikop     return data_ + offset;
62*09537850SAkhilesh Sanikop   }
63*09537850SAkhilesh Sanikop 
64*09537850SAkhilesh Sanikop   int rows_ = 0;
65*09537850SAkhilesh Sanikop   int columns_ = 0;
66*09537850SAkhilesh Sanikop   T* data_ = nullptr;
67*09537850SAkhilesh Sanikop };
68*09537850SAkhilesh Sanikop 
69*09537850SAkhilesh Sanikop // Allocates and owns the contiguous memory and exposes an Array2DView of
70*09537850SAkhilesh Sanikop // dimension |rows| x |columns|.
71*09537850SAkhilesh Sanikop template <typename T>
72*09537850SAkhilesh Sanikop class Array2D {
73*09537850SAkhilesh Sanikop  public:
74*09537850SAkhilesh Sanikop   Array2D() = default;
75*09537850SAkhilesh Sanikop 
76*09537850SAkhilesh Sanikop   // Copyable and Movable.
77*09537850SAkhilesh Sanikop   Array2D(const Array2D& rhs) = default;
78*09537850SAkhilesh Sanikop   Array2D& operator=(const Array2D& rhs) = default;
79*09537850SAkhilesh Sanikop 
80*09537850SAkhilesh Sanikop   LIBGAV1_MUST_USE_RESULT bool Reset(int rows, int columns,
81*09537850SAkhilesh Sanikop                                      bool zero_initialize = true) {
82*09537850SAkhilesh Sanikop     size_ = rows * columns;
83*09537850SAkhilesh Sanikop     // If T is not a trivial type, we should always reallocate the data_
84*09537850SAkhilesh Sanikop     // buffer, so that the destructors of any existing objects are invoked.
85*09537850SAkhilesh Sanikop     if (!std::is_trivial<T>::value || allocated_size_ < size_) {
86*09537850SAkhilesh Sanikop       // Note: This invokes the global operator new if T is a non-class type,
87*09537850SAkhilesh Sanikop       // such as integer or enum types, or a class type that is not derived
88*09537850SAkhilesh Sanikop       // from libgav1::Allocable, such as std::unique_ptr. If we enforce a
89*09537850SAkhilesh Sanikop       // maximum allocation size or keep track of our own heap memory
90*09537850SAkhilesh Sanikop       // consumption, we will need to handle the allocations here that use the
91*09537850SAkhilesh Sanikop       // global operator new.
92*09537850SAkhilesh Sanikop       if (zero_initialize) {
93*09537850SAkhilesh Sanikop         data_.reset(new (std::nothrow) T[size_]());
94*09537850SAkhilesh Sanikop       } else {
95*09537850SAkhilesh Sanikop         data_.reset(new (std::nothrow) T[size_]);
96*09537850SAkhilesh Sanikop       }
97*09537850SAkhilesh Sanikop       if (data_ == nullptr) {
98*09537850SAkhilesh Sanikop         allocated_size_ = 0;
99*09537850SAkhilesh Sanikop         return false;
100*09537850SAkhilesh Sanikop       }
101*09537850SAkhilesh Sanikop       allocated_size_ = size_;
102*09537850SAkhilesh Sanikop     } else if (zero_initialize) {
103*09537850SAkhilesh Sanikop       // Cast the data_ pointer to void* to avoid the GCC -Wclass-memaccess
104*09537850SAkhilesh Sanikop       // warning. The memset is safe because T is a trivial type.
105*09537850SAkhilesh Sanikop       void* dest = data_.get();
106*09537850SAkhilesh Sanikop       memset(dest, 0, sizeof(T) * size_);
107*09537850SAkhilesh Sanikop     }
108*09537850SAkhilesh Sanikop     data_view_.Reset(rows, columns, data_.get());
109*09537850SAkhilesh Sanikop     return true;
110*09537850SAkhilesh Sanikop   }
111*09537850SAkhilesh Sanikop 
rows()112*09537850SAkhilesh Sanikop   int rows() const { return data_view_.rows(); }
columns()113*09537850SAkhilesh Sanikop   int columns() const { return data_view_.columns(); }
size()114*09537850SAkhilesh Sanikop   size_t size() const { return size_; }
data()115*09537850SAkhilesh Sanikop   T* data() { return data_.get(); }
data()116*09537850SAkhilesh Sanikop   const T* data() const { return data_.get(); }
117*09537850SAkhilesh Sanikop 
118*09537850SAkhilesh Sanikop   T* operator[](int row) { return data_view_[row]; }
119*09537850SAkhilesh Sanikop 
120*09537850SAkhilesh Sanikop   const T* operator[](int row) const { return data_view_[row]; }
121*09537850SAkhilesh Sanikop 
122*09537850SAkhilesh Sanikop  private:
123*09537850SAkhilesh Sanikop   std::unique_ptr<T[]> data_;
124*09537850SAkhilesh Sanikop   size_t allocated_size_ = 0;
125*09537850SAkhilesh Sanikop   size_t size_ = 0;
126*09537850SAkhilesh Sanikop   Array2DView<T> data_view_;
127*09537850SAkhilesh Sanikop };
128*09537850SAkhilesh Sanikop 
129*09537850SAkhilesh Sanikop }  // namespace libgav1
130*09537850SAkhilesh Sanikop 
131*09537850SAkhilesh Sanikop #endif  // LIBGAV1_SRC_UTILS_ARRAY_2D_H_
132