Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1
35#ifndef _UTILS_MACROS_H_
36#define _UTILS_MACROS_H_
37
38#ifdef __cplusplus
39#include <vector>
40#include <map>
41#include <string>
42#include <utility>
43
44#ifndef ARDUINO
45using std::map;
46#endif
47using std::vector;
48using std::string;
49using std::pair;
50
51#endif
52
53#include <stdlib.h> // for abort
54
55#if defined(__EMSCRIPTEN__)
56#if defined(EXPECT_DEATH)
57#undef EXPECT_DEATH
58#endif
59#define EXPECT_DEATH(x...)
60#endif
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65extern int g_death_lineno;
66extern const char* g_death_file;
67#ifdef __cplusplus
68}
69#endif
70
71#if defined(__FreeRTOS__)
72
73#ifdef RECORD_DEATH_FILE
74#define RECORD_DEATH() do { g_death_file = __FILE__ ; g_death_lineno = __LINE__; } while(0)
75#else
76#define RECORD_DEATH() do { g_death_lineno = __LINE__; } while(0)
77#endif
78
87#define HASSERT(x) do { if (!(x)) { RECORD_DEATH(); abort(); } } while(0)
88
89#define DIE(MSG) abort()
90
91#elif defined(ESP32) || defined(ESP_PLATFORM)
92
93#include <stdio.h>
94#include <assert.h>
95
96// Locate the relevant version of ets_sys.h based on the IDF_TARGET
97#if defined(CONFIG_IDF_TARGET_ESP32)
98#include <esp32/rom/ets_sys.h>
99#elif defined(CONFIG_IDF_TARGET_ESP32S2)
100#include <esp32s2/rom/ets_sys.h>
101#elif defined(CONFIG_IDF_TARGET_ESP32S3)
102#include <esp32s3/rom/ets_sys.h>
103#elif defined(CONFIG_IDF_TARGET_ESP32C3)
104#include <esp32c3/rom/ets_sys.h>
105#else
106#error Unknown/Unsupported ESP32 variant.
107#endif // CONFIG_IDF_TARGET_ESP32
108
109// For the ESP32 we are using ets_printf() instead of printf() to avoid the
110// internal locking within the newlib implementation. This locking can cause
111// difficult to parse backtraces when an ISR is on the same core as the code
112// that crashes due to these two macros.
113
114#define HASSERT(x) do { if (!(x)) { ets_printf("Assertion failed in file " __FILE__ " line %d: assert(%s)\n", __LINE__, #x); g_death_file = __FILE__; g_death_lineno = __LINE__; assert(0); abort();} } while(0)
115
116#define DIE(MSG) do { ets_printf("Crashed in file " __FILE__ " line %d: " MSG "\n", __LINE__); assert(0); abort(); } while(0)
117
118#elif defined(ESP_NONOS) || defined(ARDUINO)
119
120#include <stdio.h>
121#include <assert.h>
122
123#define HASSERT(x) do { if (!(x)) { printf("Assertion failed in file " __FILE__ " line %d: assert(%s)\n", __LINE__, #x); g_death_file = __FILE__; g_death_lineno = __LINE__; assert(0); abort();} } while(0)
124
125#define DIE(MSG) do { printf("Crashed in file " __FILE__ " line %d: " MSG "\n", __LINE__); assert(0); abort(); } while(0)
126
127#else
128
129#include <assert.h>
130#include <stdio.h>
131
132#ifdef NDEBUG
133#define HASSERT(x) do { if (!(x)) { fprintf(stderr, "Assertion failed in file " __FILE__ " line %d: assert(" #x ")\n", __LINE__); g_death_file = __FILE__; g_death_lineno = __LINE__; abort();} } while(0)
134#else
138#define HASSERT(x) do { assert(x); } while(0)
139#endif
140
143#define DIE(MSG) do { fprintf(stderr, "Crashed in file " __FILE__ " line %d: " MSG "\n", __LINE__); g_death_file = __FILE__; g_death_lineno = __LINE__; abort(); } while(0)
144
145#endif
146
147#ifdef NDEBUG
148
152#define DASSERT(x)
153
154#else
155
159#define DASSERT(x) HASSERT(x)
160
161#endif
162
163
171#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
172 \
173 TypeName(const TypeName &); \
174 \
175 void operator=(const TypeName &)
176
180#define OVERRIDE override
181
184#ifndef ARRAYSIZE
185#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
186#endif
187
193#define INHERIT_CONSTRUCTOR(CURRENT_CLASS, BASE_CLASS) \
194 template <typename... Args> \
195 explicit CURRENT_CLASS(Args... args) \
196 : BASE_CLASS(args...) \
197 { \
198 }
199
205#define INHERIT_CONSTEXPR_CONSTRUCTOR(CURRENT_CLASS, BASE_CLASS) \
206 template <typename... Args> \
207 explicit constexpr CURRENT_CLASS(Args... args) \
208 : BASE_CLASS(args...) \
209 { \
210 }
211
214#define C_STATIC_ASSERT(expr, name) \
215 typedef unsigned char __attribute__((unused)) __static_assert_##name[expr ? 0 : -1]
216
217#if defined(ARDUINO_ARCH_ESP32)
218#include <esp8266-compat.h>
219#elif !defined(ESP_NONOS)
222#define ICACHE_FLASH_ATTR
225#define ICACHE_RAM_ATTR
226#endif
227
239#define GET_PARENT_PTR(ParentClass, variable) \
240 reinterpret_cast<ParentClass *>( \
241 reinterpret_cast<char *>(this) - offsetof(ParentClass, variable));
242
243
245#define FAKELLP(x) static_cast<unsigned>((x) >> 32), static_cast<unsigned>((x) & 0xffffffffu)
246
248#define MUST_USE_RESULT __attribute__((__warn_unused_result__))
249
250#ifdef ESP32
252#include <endian.h>
253#ifndef __bswap64
254#define __bswap64(x) __bswap_64(x)
255#endif
256#endif
257
258
259#endif // _UTILS_MACROS_H_
int g_death_lineno
Captures point of death (line).
Definition os.c:93
const char * g_death_file
Captures point of death (file).
Definition os.c:95