Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / CHIPMem-Malloc.cpp
1 /*
2  *
3  *    Copyright (c) 2020-2021 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *      This file implements heap memory allocation APIs for CHIP. These functions are platform
22  *      specific and might be C Standard Library heap functions re-direction in most of cases.
23  *
24  */
25
26 #include <core/CHIPConfig.h>
27 #include <support/CHIPMem.h>
28
29 #include <stdlib.h>
30
31 #ifndef NDEBUG
32 #include <atomic>
33 #include <cstdio>
34 #endif
35
36 #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
37 #include <dmalloc.h>
38 #include <support/SafeInt.h>
39 #endif // CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
40
41 #if CHIP_CONFIG_MEMORY_MGMT_MALLOC
42
43 namespace chip {
44 namespace Platform {
45
46 #ifdef NDEBUG
47
48 #define VERIFY_INITIALIZED()
49 #define VERIFY_POINTER(p)
50
51 #else
52
53 #define VERIFY_INITIALIZED() VerifyInitialized(__func__)
54
55 static std::atomic_int memoryInitialized{ 0 };
56
57 static void VerifyInitialized(const char * func)
58 {
59     if (!memoryInitialized)
60     {
61         fprintf(stderr, "ABORT: chip::Platform::%s() called before chip::Platform::MemoryInit()\n", func);
62         abort();
63     }
64 }
65
66 #define VERIFY_POINTER(p)                                                                                                          \
67     do                                                                                                                             \
68         if (((p) != nullptr) && (MemoryDebugCheckPointer((p)) == false))                                                           \
69         {                                                                                                                          \
70             fprintf(stderr, "ABORT: chip::Platform::%s() found corruption on %p\n", __func__, (p));                                \
71             abort();                                                                                                               \
72         }                                                                                                                          \
73     while (0)
74
75 #endif
76
77 CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize)
78 {
79 #ifndef NDEBUG
80     if (memoryInitialized++ > 0)
81     {
82         fprintf(stderr, "ABORT: chip::Platform::MemoryInit() called twice.\n");
83         abort();
84     }
85 #endif
86     return CHIP_NO_ERROR;
87 }
88
89 void MemoryAllocatorShutdown()
90 {
91 #ifndef NDEBUG
92     if (--memoryInitialized < 0)
93     {
94         fprintf(stderr, "ABORT: chip::Platform::MemoryShutdown() called twice.\n");
95         abort();
96     }
97 #endif
98 #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
99     dmalloc_shutdown();
100 #endif // CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
101 }
102
103 void * MemoryAlloc(size_t size)
104 {
105     VERIFY_INITIALIZED();
106     return malloc(size);
107 }
108
109 void * MemoryCalloc(size_t num, size_t size)
110 {
111     VERIFY_INITIALIZED();
112     return calloc(num, size);
113 }
114
115 void * MemoryRealloc(void * p, size_t size)
116 {
117     VERIFY_INITIALIZED();
118     VERIFY_POINTER(p);
119     return realloc(p, size);
120 }
121
122 void MemoryFree(void * p)
123 {
124     VERIFY_INITIALIZED();
125     VERIFY_POINTER(p);
126     free(p);
127 }
128
129 bool MemoryInternalCheckPointer(const void * p, size_t min_size)
130 {
131 #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
132     return CanCastTo<int>(min_size) && (p != nullptr) &&
133         (dmalloc_verify_pnt(__FILE__, __LINE__, __func__, p, 1, static_cast<int>(min_size)) == MALLOC_VERIFY_NOERROR);
134 #else  // CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
135     return (p != nullptr);
136 #endif // CHIP_CONFIG_MEMORY_DEBUG_DMALLOC
137 }
138
139 } // namespace Platform
140 } // namespace chip
141
142 #endif // CHIP_CONFIG_MEMORY_MGMT_MALLOC