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 
22 namespace chre {
23 
24 /**
25  * Destroys count objects starting at first. This function is similar to
26  * std::destroy_n.
27  *
28  * @param first Starting address of count objects to destroy
29  * @param count The number of objects to destroy
30  */
31 template<typename ElementType>
32 void destroy(ElementType *first, size_t count);
33 
34 /**
35  * Performs move assignment (dest = std::move(source)) if supported by
36  * ElementType, otherwise copy assignment (dest = source).
37  */
38 template<typename ElementType>
39 void moveOrCopyAssign(ElementType& dest, ElementType& source);
40 
41 /**
42  * Initializes a new block of memory by transferring objects from another block,
43  * using memcpy if valid for the underlying type, or the move constructor if
44  * available, or the copy constructor. This function is similar to
45  * std::uninitialized_move_n.
46  *
47  * @param source The beginning of the data to transfer
48  * @param count The number of elements to transfer
49  * @param dest An uninitialized buffer to be populated with count elements
50  */
51 template<typename ElementType>
52 void uninitializedMoveOrCopy(ElementType *source, size_t count,
53                              ElementType *dest);
54 
55 /**
56  * Allocates memory for an object of size T and constructs the object in the
57  * newly allocated object by forwarding the provided parameters.
58  */
59 template<typename T, typename... Args>
60 T *memoryAlloc(Args&&... args);
61 
62 }  // namespace chre
63 
64 #include "chre/util/memory_impl.h"
65 
66 #endif  // CHRE_UTIL_MEMORY_H
67