container_support.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_CONTAINER_SUPPORT_H_
18#define CHRE_UTIL_CONTAINER_SUPPORT_H_
19
20/**
21 * @file Provides replacements for macros and functions that are normally
22 * provided by the CHRE framework implementation. These portable implementations
23 * are implemented using the CHRE API rather than private system APIs.
24 */
25
26#ifdef CHRE_IS_NANOAPP_BUILD
27
28#include <chre.h>
29
30#include "chre/util/nanoapp/log.h"
31
32/**
33 * Provides the CHRE_ASSERT macro that uses chreAbort to abort the nanoapp upon
34 * failure.
35 *
36 * @param the condition to check for non-zero.
37 */
38#define CHRE_ASSERT(condition) do { \
39  if (!(condition)) {               \
40    chreAbort(UINT32_MAX);          \
41  }                                 \
42} while (0)
43
44
45/**
46 * Provides the CHRE_ASSERT_LOG macro that logs the assertion failure followed
47 * by CHRE_ASSERT if the condition is non-zero.
48 *
49 * @param condition the condition to check for non-zero.
50 * @param fmt the format string to log.
51 * @param ... arguments to format into the log message.
52 */
53#define CHRE_ASSERT_LOG(condition, fmt, ...) do { \
54  if (!(condition)) {                             \
55    LOGE("Assert: " fmt, ##__VA_ARGS__);          \
56    CHRE_ASSERT(condition);                       \
57  }                                               \
58} while (0)
59
60namespace chre {
61
62/**
63 * Provides the memoryAlloc function that is normally provided by the CHRE
64 * runtime. It maps into chreHeapAlloc.
65 *
66 * @param size the size of the allocation to make.
67 * @return a pointer to allocated memory or nullptr if allocation failed.
68 */
69inline void *memoryAlloc(size_t size) {
70  return chreHeapAlloc(static_cast<uint32_t>(size));
71}
72
73/**
74 * Provides the memoryFree function that is normally provided by the CHRE
75 * runtime. It maps into chreHeapFree.
76 *
77 * @param pointer the allocation to release.
78 */
79inline void memoryFree(void *pointer) {
80  chreHeapFree(pointer);
81}
82
83}  // namespace chre
84
85#else
86#include "chre/platform/assert.h"
87#include "chre/platform/memory.h"
88#endif
89
90#endif  // CHRE_UTIL_CONTAINER_SUPPORT_H_
91