|
26 | 26 | #include <lib/support/CHIPMem.h>
|
27 | 27 | #include <platform/CHIPDeviceLayer.h>
|
28 | 28 |
|
29 |
| -#include <atomic> |
30 |
| -#include <cstdio> |
31 |
| -#include <cstring> |
32 | 29 | #include <esp_heap_caps.h>
|
33 | 30 | #include <stdlib.h>
|
34 | 31 |
|
35 | 32 | namespace chip {
|
36 | 33 | namespace Platform {
|
37 | 34 |
|
38 |
| -#define VERIFY_INITIALIZED() VerifyInitialized(__func__) |
39 |
| - |
40 |
| -static std::atomic_int memoryInitialized{ 0 }; |
41 |
| - |
42 |
| -static void VerifyInitialized(const char * func) |
43 |
| -{ |
44 |
| - if (!memoryInitialized) |
45 |
| - { |
46 |
| - ChipLogError(DeviceLayer, "ABORT: chip::Platform::%s() called before chip::Platform::MemoryInit()", func); |
47 |
| - abort(); |
48 |
| - } |
49 |
| -} |
50 |
| - |
51 | 35 | CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize)
|
52 | 36 | {
|
53 |
| - if (memoryInitialized++ > 0) |
54 |
| - { |
55 |
| - ChipLogError(DeviceLayer, "ABORT: chip::Platform::MemoryInit() called twice."); |
56 |
| - abort(); |
57 |
| - } |
58 |
| - |
59 | 37 | return CHIP_NO_ERROR;
|
60 | 38 | }
|
61 | 39 |
|
62 |
| -void MemoryAllocatorShutdown() |
63 |
| -{ |
64 |
| - if (--memoryInitialized < 0) |
65 |
| - { |
66 |
| - ChipLogError(DeviceLayer, "ABORT: chip::Platform::MemoryShutdown() called twice."); |
67 |
| - abort(); |
68 |
| - } |
69 |
| -} |
| 40 | +void MemoryAllocatorShutdown() {} |
70 | 41 |
|
71 | 42 | void * MemoryAlloc(size_t size)
|
72 | 43 | {
|
73 |
| - void * ptr; |
74 |
| - VERIFY_INITIALIZED(); |
75 | 44 | #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL
|
76 |
| - ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
| 45 | + return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
77 | 46 | #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL)
|
78 |
| - ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 47 | + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
79 | 48 | #else
|
80 |
| - ptr = malloc(size); |
| 49 | + return malloc(size); |
81 | 50 | #endif
|
82 |
| - return ptr; |
83 | 51 | }
|
84 | 52 |
|
85 | 53 | void * MemoryAlloc(size_t size, bool isLongTermAlloc)
|
86 | 54 | {
|
87 |
| - void * ptr; |
88 |
| - VERIFY_INITIALIZED(); |
89 | 55 | #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL
|
90 |
| - ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
| 56 | + return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
91 | 57 | #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL)
|
92 |
| - ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 58 | + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
93 | 59 | #else
|
94 |
| - ptr = malloc(size); |
| 60 | + return malloc(size); |
95 | 61 | #endif
|
96 |
| - return ptr; |
97 | 62 | }
|
98 | 63 |
|
99 | 64 | void * MemoryCalloc(size_t num, size_t size)
|
100 | 65 | {
|
101 |
| - void * ptr; |
102 |
| - VERIFY_INITIALIZED(); |
103 | 66 | #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL
|
104 |
| - ptr = heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
| 67 | + return heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
105 | 68 | #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL)
|
106 |
| - ptr = heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 69 | + return heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
107 | 70 | #else
|
108 |
| - ptr = calloc(num, size); |
| 71 | + return calloc(num, size); |
109 | 72 | #endif
|
110 |
| - return ptr; |
111 | 73 | }
|
112 | 74 |
|
113 | 75 | void * MemoryRealloc(void * p, size_t size)
|
114 | 76 | {
|
115 |
| - VERIFY_INITIALIZED(); |
116 | 77 | #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL
|
117 | 78 | p = heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
118 | 79 | #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL)
|
119 |
| - p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 80 | + p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
120 | 81 | #else
|
121 |
| - p = realloc(p, size); |
| 82 | + p = realloc(p, size); |
122 | 83 | #endif
|
123 | 84 | return p;
|
124 | 85 | }
|
125 | 86 |
|
126 | 87 | void MemoryFree(void * p)
|
127 | 88 | {
|
128 |
| - VERIFY_INITIALIZED(); |
129 | 89 | #ifdef CONFIG_MATTER_MEM_ALLOC_MODE_DEFAULT
|
130 | 90 | free(p);
|
131 | 91 | #else
|
|
0 commit comments