xref: /aosp_15_r20/system/chre/util/include/chre/util/fragmentation_manager.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_UTIL_FRAGMENTATION_MANAGER
18 #define CHRE_UTIL_FRAGMENTATION_MANAGER
19 #include <stddef.h>
20 #include <type_traits>
21 #include "chre/util/optional.h"
22 namespace chre {
23 
24 /**
25  * Structure representing a fragment of continuous data.
26  *
27  * @tparam ObjectType the type of the data.
28  *
29  * @param data pointer to the start of the data.
30  * @param size number of count of the data.
31  */
32 template <typename ObjectType>
33 struct Fragment {
34   ObjectType *data;
35   size_t size;
FragmentFragment36   Fragment(ObjectType *data_, size_t size_) : data(data_), size(size_) {}
37 };
38 
39 /**
40  * A data structure designed to partition continuous sequences of data into
41  * manageable fragments.
42  *
43  * This class is particularly useful when dealing with large datasets, allowing
44  * for efficient processing. Each fragment represents a contiguous subset of the
45  * original data, with the size of each fragment determined by the
46  * @c fragmentSize parameter besides the last fragment. The last fragment might
47  * have less data then @c fragmentSize if there is no enough data left.
48  * It is also the creator's responsibility to make sure that the data is alive
49  * during the usage of FragmentationManager since FragmentationManager does not
50  * keep a copy of the data.
51  *
52  * @tparam ObjectType The specific type of data element (object) to be stored
53  * within this structure.
54  * @tparam fragmentSize The number of @c ObjectType elements that constitute a
55  * single fragment.
56  */
57 template <typename ObjectType, size_t fragmentSize>
58 class FragmentationManager {
59  public:
60   /**
61    * Initializes the fragmentation manager, partitioning a continuous block of
62    * data into fragments.
63    *
64    * @param dataSource A raw pointer to pointing to the beginning of the
65    * continuous data block to be fragmented.
66    * @param dataSize The total number of bytes in the dataSource.
67    *
68    * @return false if dataSource is initialized with a nullptr; otherwise true.
69    */
70   bool init(ObjectType *dataSource, size_t dataSize);
71 
72   /**
73    * Deinitializes the fragmentation manager.
74    */
75   void deinit();
76 
77   /**
78    * Retrieves the next available data fragment.
79    *
80    * @return a @c Fragment with @c data pointing to the address of the
81    * fragment's data; @c size with the size of the fragment. If there is no more
82    * fragments, return an empty optional.
83    */
84   Optional<Fragment<ObjectType>> getNextFragment();
85 
86   /**
87    * @return the number of fragments that have been emitted so far.
88    */
getEmittedFragmentedCount()89   size_t getEmittedFragmentedCount() {
90     return mEmittedFragment;
91   }
92   /**
93    * @return True if all fragments have been emitted.
94 
95    */
hasNoMoreFragment()96   bool hasNoMoreFragment() {
97     return mEmittedFragment * fragmentSize >= mDataSize;
98   }
99 
100  private:
101   // A pointer to the beginning of the continuous block of data being
102   // fragmented.
103   ObjectType *mData;
104   // The number of bytes in the 'mData' block.
105   size_t mDataSize;
106   // The number of fragments that have been emitted.
107   size_t mEmittedFragment;
108 };
109 }  // namespace chre
110 
111 #include "chre/util/fragmentation_manager_impl.h"  // IWYU pragma: export
112 
113 #endif  // CHRE_UTIL_FRAGMENTATION_MANAGER
114