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