1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 #include "oic_malloc.h"
27 #include "iotivity_config.h"
33 // Enable extra debug logging for malloc. Comment out to disable
34 #ifdef ENABLE_MALLOC_DEBUG
36 #define TAG "OIC_MALLOC"
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 // Internal API function
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
56 // Private internal function prototypes
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 #ifdef ENABLE_MALLOC_DEBUG
63 static uint32_t count;
66 void *OICMalloc(size_t size)
73 #ifdef ENABLE_MALLOC_DEBUG
74 void *ptr = malloc(size);
79 OIC_LOG_V(INFO, TAG, "malloc: ptr=%p, size=%u, count=%u", ptr, size, count);
86 void *OICCalloc(size_t num, size_t size)
88 if (0 == size || 0 == num)
93 #ifdef ENABLE_MALLOC_DEBUG
94 void *ptr = calloc(num, size);
99 OIC_LOG_V(INFO, TAG, "calloc: ptr=%p, num=%u, size=%u, count=%u", ptr, num, size, count);
102 return calloc(num, size);
106 void *OICRealloc(void* ptr, size_t size)
108 // Override realloc() behavior for NULL pointer which normally would
109 // work as per malloc(), however we suppress the behavior of possibly
110 // returning a non-null unique pointer.
113 return OICMalloc(size);
116 // Otherwise leave the behavior up to realloc() itself:
118 #ifdef ENABLE_MALLOC_DEBUG
119 void* newptr = realloc(ptr, size);
120 OIC_LOG_V(INFO, TAG, "realloc: ptr=%p, newptr=%p, size=%u", ptr, newptr, size);
121 // Very important to return the correct pointer here, as it only *somtimes*
122 // differs and thus can be hard to notice/test:
125 return realloc(ptr, size);
129 void OICFree(void *ptr)
131 #ifdef ENABLE_MALLOC_DEBUG
132 // Since OICMalloc() did not increment count if it returned NULL,
133 // guard the decrement:
138 OIC_LOG_V(INFO, TAG, "free: ptr=%p, count=%u", ptr, count);
144 void OICClearMemory(void *buf, size_t n)
147 #ifdef HAVE_WINDOWS_H
148 SecureZeroMemory(buf, n);
150 volatile unsigned char* p = (volatile unsigned char*)buf;