-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmacros.h
More file actions
64 lines (51 loc) · 1.51 KB
/
Copy pathmacros.h
File metadata and controls
64 lines (51 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <pthread.h> // PThreadGuard below uses pthread_mutex_*
static inline void* _malloc_psram(size_t sz) {
if (!sz) return NULL;
void* p = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
return p;
}
static inline void* _calloc_psram(size_t n, size_t sz) {
if (!n || !sz) return NULL;
void* p = heap_caps_calloc(n, sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
return p;
}
static inline void* _realloc_psram(void* ptr, size_t sz) {
void* p = heap_caps_realloc(ptr, sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
return p;
}
#ifdef CONFIG_SPIRAM
#define MALLOC(s) _malloc_psram((s))
#define CALLOC(n, s) _calloc_psram((n), (s))
#define REALLOC(p, s) _realloc_psram((p), (s))
#else
#define MALLOC(s) malloc((s))
#define CALLOC(n, s) calloc((n), (s))
#define REALLOC(p, s) realloc((p), (s))
#endif
// DMA capable
#define MALLOC_DMA(s) heap_caps_malloc((s), MALLOC_CAP_DMA | MALLOC_CAP_8BIT)
#define CALLOC_DMA(n, s) heap_caps_calloc((n), (s), MALLOC_CAP_DMA | MALLOC_CAP_8BIT)
#define FREE(p) \
do { if (p) { heap_caps_free((p)); (p) = NULL; } } while (0)
#ifdef __cplusplus
template <typename T>
static void safe_free(T *&ptr)
{
if (ptr) {
free(ptr);
ptr = nullptr;
}
}
class PThreadGuard {
public:
explicit PThreadGuard(pthread_mutex_t &m) : m_mutex(m) {
pthread_mutex_lock(&m_mutex);
}
~PThreadGuard() {
pthread_mutex_unlock(&m_mutex);
}
private:
pthread_mutex_t &m_mutex;
};
#endif