Apollo  6.0
Open source self driving car software
i_alloc.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 #include <iostream>
19 
21 
22 //
23 // Alignment power->alignment offset in bytes:
24 // 0->1
25 // 1->2
26 // 2->4
27 // 3->8
28 // 4->16
29 // 5->32
30 //
31 
32 // #define I_DEFAULT_ALIGNMENT_POWER 4 by default use 16-byte memory
33 // alignment option
34 namespace apollo {
35 namespace perception {
36 namespace common {
37 
38 template <typename T>
39 inline T *IAlloc(int memory_size) {
40  T *mem = nullptr;
41  if (!memory_size) {
42  return mem;
43  }
44  mem = new (std::nothrow) T[memory_size];
45  if (mem == nullptr) {
46  return nullptr;
47  }
48  return mem;
49 }
50 
51 template <typename T>
52 inline void IFree(T **mem) {
53  if (mem != nullptr && *mem != nullptr) {
54  delete[] * mem;
55  *mem = nullptr;
56  }
57 }
58 
59 // Memory allocated with this function must be released with IFree2
60 template <typename T>
61 inline T **IAlloc2(int m, int n) {
62  T *mem = nullptr;
63  T **head = nullptr;
64  mem = new (std::nothrow) T[m * n];
65  if (mem == nullptr) {
66  return nullptr;
67  }
68  head = new (std::nothrow) T *[m];
69  if (head == nullptr) {
70  delete[] mem;
71  return nullptr;
72  }
73  IMakeReference<T>(mem, head, m, n);
74  return head;
75 }
76 
77 // Free memory allocated with function IAlloc2
78 template <typename T>
79 inline void IFree2(T ***A) {
80  if (A != nullptr && *A != nullptr) {
81  delete[](*A)[0];
82  delete[] * A;
83  *A = nullptr;
84  }
85 }
86 
87 // Allocate an (l x m x n) T tensor
88 template <class T>
89 inline T ***IAlloc3(int l, int m, int n) {
90  T *mem = nullptr;
91  T ***head = nullptr;
92  int i, j;
93  mem = new (std::nothrow) T[l * m * n];
94  if (mem == nullptr) {
95  return nullptr;
96  }
97  head = new (std::nothrow) T **[l];
98  if (head == nullptr) {
99  delete[] mem;
100  return nullptr;
101  }
102  for (i = 0; i < l; i++) {
103  head[i] = new (std::nothrow) T *[m];
104  if (head[i] == nullptr) {
105  for (j = 0; j < i; j++) {
106  delete[] head[j];
107  }
108  delete[] head;
109  delete[] mem;
110  return nullptr;
111  }
112  }
113  IMakeReference(mem, head, l, m, n);
114  return head;
115 }
116 
117 // Memory allocated with this function must be released with IFreeAligned
118 template <typename T>
119 inline T *IAllocAligned(int memory_size, int alignment_power = 4) {
120  if (memory_size <= 0) {
121  return (reinterpret_cast<T *>(NULL));
122  }
123  int actual_alignment_power = (alignment_power >= 0) ? alignment_power : 0;
124  std::size_t memory_size_in_byte =
125  static_cast<size_t>(memory_size) * sizeof(T);
126  std::size_t mask = static_cast<size_t>((1 << actual_alignment_power) - 1);
127  std::size_t pointer_size_in_byte = sizeof(char *);
128  char *mem_begin = new (
129  std::nothrow) char[memory_size_in_byte + mask + pointer_size_in_byte];
130  if (!mem_begin) {
131  return (reinterpret_cast<T *>(NULL));
132  }
133  char *mem_actual = reinterpret_cast<char *>(
134  (reinterpret_cast<size_t>(mem_begin) + mask + pointer_size_in_byte) &
135  (~mask));
136  (reinterpret_cast<char **>(mem_actual))[-1] = mem_begin;
137  return (reinterpret_cast<T *>(mem_actual));
138 }
139 
140 // Free memory allocated with function IAllocAligned
141 template <typename T>
142 inline void IFreeAligned(T **mem) {
143  if (mem != nullptr && *mem != nullptr) {
144  delete[](reinterpret_cast<char **>(*mem))[-1];
145  *mem = nullptr;
146  }
147 }
148 
149 template <typename T>
150 inline int IVerifyAlignment(const T *mem, int alignment_power = 4) {
151  std::size_t mask = static_cast<size_t>((1 << alignment_power) - 1);
152  if (((size_t)mem) & mask) {
153  return 0;
154  } else {
155  return 1;
156  }
157 }
158 
159 } // namespace common
160 } // namespace perception
161 } // namespace apollo
int IVerifyAlignment(const T *mem, int alignment_power=4)
Definition: i_alloc.h:150
void IMakeReference(T *a, T **p, int m, int n)
Definition: i_basic.h:357
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
T * IAllocAligned(int memory_size, int alignment_power=4)
Definition: i_alloc.h:119
const size_t A
Definition: util.h:160
T * IAlloc(int memory_size)
Definition: i_alloc.h:39
T *** IAlloc3(int l, int m, int n)
Definition: i_alloc.h:89
T ** IAlloc2(int m, int n)
Definition: i_alloc.h:61
void IFree2(T ***A)
Definition: i_alloc.h:79
void IFree(T **mem)
Definition: i_alloc.h:52
void IFreeAligned(T **mem)
Definition: i_alloc.h:142