xref: /aosp_15_r20/system/chre/util/include/chre/util/memory.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2017 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_MEMORY_H_
18 #define CHRE_UTIL_MEMORY_H_
19 
20 #include <cstddef>
21 #include <type_traits>
22 
23 namespace chre {
24 
25 /**
26  * Destroys count objects starting at first. This function is similar to
27  * std::destroy_n.
28  *
29  * @param first Starting address of count objects to destroy
30  * @param count The number of objects to destroy
31  */
32 template <typename ElementType>
33 void destroy(ElementType *first, size_t count);
34 
35 /**
36  * Performs move assignment (dest = std::move(source)) if supported by
37  * ElementType, otherwise copy assignment (dest = source).
38  */
39 template <typename ElementType>
40 void moveOrCopyAssign(ElementType &dest, ElementType &source);
41 
42 /**
43  * Initializes a new block of memory by transferring objects from another block,
44  * using memcpy if valid for the underlying type, or the move constructor if
45  * available, or the copy constructor. This function is similar to
46  * std::uninitialized_move_n.
47  *
48  * @param source The beginning of the data to transfer
49  * @param count The number of elements to transfer
50  * @param dest An uninitialized buffer to be populated with count elements
51  */
52 template <typename ElementType>
53 void uninitializedMoveOrCopy(ElementType *source, size_t count,
54                              ElementType *dest);
55 
56 /**
57  * Allocates memory for an object of size T and constructs the object in the
58  * newly allocated object by forwarding the provided parameters.
59  */
60 template <typename T, typename... Args>
61 T *memoryAlloc(Args &&... args);
62 
63 /**
64  * Allocates memory for an array of objects, default-initializing them (i.e.
65  * may be indeterminate/uninitialized). This is only supported for unbounded
66  * array types, e.g. char[].
67  */
68 template <typename T>
69 typename std::remove_extent<T>::type *memoryAllocArray(size_t count);
70 
71 /**
72  * Destroys an element and deallocate its memory.
73  *
74  * @param element the element to be destroy. Needs to be from memoryAlloc.
75  */
76 template <typename T>
77 void memoryFreeAndDestroy(T *element);
78 
79 }  // namespace chre
80 
81 #include "chre/util/memory_impl.h"  // IWYU pragma: export
82 
83 #endif  // CHRE_UTIL_MEMORY_H
84